Daniel Dunbar | c17a4d3 | 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 | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 10 | // This provides Objective-C code generation targeting the Apple runtime. |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CGObjCRuntime.h" |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 15 | #include "CGBlocks.h" |
John McCall | 36f893c | 2011-01-28 11:13:47 +0000 | [diff] [blame] | 16 | #include "CGCleanup.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 17 | #include "CGRecordLayout.h" |
| 18 | #include "CodeGenFunction.h" |
| 19 | #include "CodeGenModule.h" |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 21 | #include "clang/AST/Decl.h" |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 23 | #include "clang/AST/RecordLayout.h" |
Chris Lattner | 16f0049 | 2009-04-26 01:32:48 +0000 | [diff] [blame] | 24 | #include "clang/AST/StmtObjC.h" |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 25 | #include "clang/Basic/LangOptions.h" |
Chandler Carruth | 06057ce | 2010-06-15 23:19:56 +0000 | [diff] [blame] | 26 | #include "clang/Frontend/CodeGenOptions.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/DenseSet.h" |
| 28 | #include "llvm/ADT/SetVector.h" |
| 29 | #include "llvm/ADT/SmallPtrSet.h" |
| 30 | #include "llvm/ADT/SmallString.h" |
Chandler Carruth | 3b844ba | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 31 | #include "llvm/IR/DataLayout.h" |
| 32 | #include "llvm/IR/InlineAsm.h" |
| 33 | #include "llvm/IR/IntrinsicInst.h" |
| 34 | #include "llvm/IR/LLVMContext.h" |
| 35 | #include "llvm/IR/Module.h" |
John McCall | 8e3f861 | 2010-07-13 22:12:14 +0000 | [diff] [blame] | 36 | #include "llvm/Support/CallSite.h" |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 37 | #include "llvm/Support/raw_ostream.h" |
Torok Edwin | f42e4a6 | 2009-08-24 13:25:12 +0000 | [diff] [blame] | 38 | #include <cstdio> |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 39 | |
| 40 | using namespace clang; |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 41 | using namespace CodeGen; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 42 | |
| 43 | namespace { |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 44 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 45 | // FIXME: We should find a nicer way to make the labels for metadata, string |
| 46 | // concatenation is lame. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 47 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 48 | class ObjCCommonTypesHelper { |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 49 | protected: |
| 50 | llvm::LLVMContext &VMContext; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 51 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 52 | private: |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 53 | // The types of these functions don't really matter because we |
| 54 | // should always bitcast before calling them. |
| 55 | |
| 56 | /// id objc_msgSend (id, SEL, ...) |
| 57 | /// |
| 58 | /// The default messenger, used for sends whose ABI is unchanged from |
| 59 | /// the all-integer/pointer case. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 60 | llvm::Constant *getMessageSendFn() const { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 61 | // Add the non-lazy-bind attribute, since objc_msgSend is likely to |
| 62 | // be called a lot. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 63 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
Bill Wendling | c4c62fd | 2013-01-31 00:30:05 +0000 | [diff] [blame^] | 64 | return |
| 65 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 66 | params, true), |
| 67 | "objc_msgSend", |
| 68 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 69 | llvm::AttributeSet::FunctionIndex, |
| 70 | llvm::Attribute::NonLazyBind)); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 71 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 72 | |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 73 | /// void objc_msgSend_stret (id, SEL, ...) |
| 74 | /// |
| 75 | /// The messenger used when the return value is an aggregate returned |
| 76 | /// by indirect reference in the first argument, and therefore the |
| 77 | /// self and selector parameters are shifted over by one. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 78 | llvm::Constant *getMessageSendStretFn() const { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 79 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 80 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, |
| 81 | params, true), |
| 82 | "objc_msgSend_stret"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 83 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 84 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 85 | |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 86 | /// [double | long double] objc_msgSend_fpret(id self, SEL op, ...) |
| 87 | /// |
| 88 | /// The messenger used when the return value is returned on the x87 |
| 89 | /// floating-point stack; without a special entrypoint, the nil case |
| 90 | /// would be unbalanced. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 91 | llvm::Constant *getMessageSendFpretFn() const { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 92 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 93 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.DoubleTy, |
| 94 | params, true), |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 95 | "objc_msgSend_fpret"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 96 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 97 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 98 | |
Anders Carlsson | eea6480 | 2011-10-31 16:27:11 +0000 | [diff] [blame] | 99 | /// _Complex long double objc_msgSend_fp2ret(id self, SEL op, ...) |
| 100 | /// |
| 101 | /// The messenger used when the return value is returned in two values on the |
| 102 | /// x87 floating point stack; without a special entrypoint, the nil case |
| 103 | /// would be unbalanced. Only used on 64-bit X86. |
| 104 | llvm::Constant *getMessageSendFp2retFn() const { |
| 105 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
| 106 | llvm::Type *longDoubleType = llvm::Type::getX86_FP80Ty(VMContext); |
| 107 | llvm::Type *resultType = |
| 108 | llvm::StructType::get(longDoubleType, longDoubleType, NULL); |
| 109 | |
| 110 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(resultType, |
| 111 | params, true), |
| 112 | "objc_msgSend_fp2ret"); |
| 113 | } |
| 114 | |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 115 | /// id objc_msgSendSuper(struct objc_super *super, SEL op, ...) |
| 116 | /// |
| 117 | /// The messenger used for super calls, which have different dispatch |
| 118 | /// semantics. The class passed is the superclass of the current |
| 119 | /// class. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 120 | llvm::Constant *getMessageSendSuperFn() const { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 121 | llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 122 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 123 | params, true), |
| 124 | "objc_msgSendSuper"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 125 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 126 | |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 127 | /// id objc_msgSendSuper2(struct objc_super *super, SEL op, ...) |
| 128 | /// |
| 129 | /// A slightly different messenger used for super calls. The class |
| 130 | /// passed is the current class. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 131 | llvm::Constant *getMessageSendSuperFn2() const { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 132 | llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 133 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 134 | params, true), |
| 135 | "objc_msgSendSuper2"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 136 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 137 | |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 138 | /// void objc_msgSendSuper_stret(void *stretAddr, struct objc_super *super, |
| 139 | /// SEL op, ...) |
| 140 | /// |
| 141 | /// The messenger used for super calls which return an aggregate indirectly. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 142 | llvm::Constant *getMessageSendSuperStretFn() const { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 143 | llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 144 | return CGM.CreateRuntimeFunction( |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 145 | llvm::FunctionType::get(CGM.VoidTy, params, true), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 146 | "objc_msgSendSuper_stret"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 147 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 148 | |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 149 | /// void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super, |
| 150 | /// SEL op, ...) |
| 151 | /// |
| 152 | /// objc_msgSendSuper_stret with the super2 semantics. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 153 | llvm::Constant *getMessageSendSuperStretFn2() const { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 154 | llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 155 | return CGM.CreateRuntimeFunction( |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 156 | llvm::FunctionType::get(CGM.VoidTy, params, true), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 157 | "objc_msgSendSuper2_stret"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 158 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 159 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 160 | llvm::Constant *getMessageSendSuperFpretFn() const { |
| 161 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 162 | return getMessageSendSuperFn(); |
| 163 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 164 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 165 | llvm::Constant *getMessageSendSuperFpretFn2() const { |
| 166 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 167 | return getMessageSendSuperFn2(); |
| 168 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 169 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 170 | protected: |
| 171 | CodeGen::CodeGenModule &CGM; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 172 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 173 | public: |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 174 | llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy; |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 175 | llvm::Type *Int8PtrTy, *Int8PtrPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 176 | |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 177 | /// ObjectPtrTy - LLVM type for object handles (typeof(id)) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 178 | llvm::Type *ObjectPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 179 | |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 180 | /// PtrObjectPtrTy - LLVM type for id * |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 181 | llvm::Type *PtrObjectPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 182 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 183 | /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL)) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 184 | llvm::Type *SelectorPtrTy; |
Douglas Gregor | 4c86fdb | 2012-01-17 18:36:30 +0000 | [diff] [blame] | 185 | |
| 186 | private: |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 187 | /// ProtocolPtrTy - LLVM type for external protocol handles |
| 188 | /// (typeof(Protocol)) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 189 | llvm::Type *ExternalProtocolPtrTy; |
Douglas Gregor | 4c86fdb | 2012-01-17 18:36:30 +0000 | [diff] [blame] | 190 | |
| 191 | public: |
| 192 | llvm::Type *getExternalProtocolPtrTy() { |
| 193 | if (!ExternalProtocolPtrTy) { |
| 194 | // FIXME: It would be nice to unify this with the opaque type, so that the |
| 195 | // IR comes out a bit cleaner. |
| 196 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 197 | ASTContext &Ctx = CGM.getContext(); |
| 198 | llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); |
| 199 | ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T); |
| 200 | } |
| 201 | |
| 202 | return ExternalProtocolPtrTy; |
| 203 | } |
| 204 | |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 205 | // SuperCTy - clang type for struct objc_super. |
| 206 | QualType SuperCTy; |
| 207 | // SuperPtrCTy - clang type for struct objc_super *. |
| 208 | QualType SuperPtrCTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 209 | |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 210 | /// SuperTy - LLVM type for struct objc_super. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 211 | llvm::StructType *SuperTy; |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 212 | /// SuperPtrTy - LLVM type for struct objc_super *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 213 | llvm::Type *SuperPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 214 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 215 | /// PropertyTy - LLVM type for struct objc_property (struct _prop_t |
| 216 | /// in GCC parlance). |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 217 | llvm::StructType *PropertyTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 218 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 219 | /// PropertyListTy - LLVM type for struct objc_property_list |
| 220 | /// (_prop_list_t in GCC parlance). |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 221 | llvm::StructType *PropertyListTy; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 222 | /// PropertyListPtrTy - LLVM type for struct objc_property_list*. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 223 | llvm::Type *PropertyListPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 224 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 225 | // MethodTy - LLVM type for struct objc_method. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 226 | llvm::StructType *MethodTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 227 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 228 | /// CacheTy - LLVM type for struct objc_cache. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 229 | llvm::Type *CacheTy; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 230 | /// CachePtrTy - LLVM type for struct objc_cache *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 231 | llvm::Type *CachePtrTy; |
Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 232 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 233 | llvm::Constant *getGetPropertyFn() { |
| 234 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 235 | ASTContext &Ctx = CGM.getContext(); |
| 236 | // id objc_getProperty (id, SEL, ptrdiff_t, bool) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 237 | SmallVector<CanQualType,4> Params; |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 238 | CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType()); |
| 239 | CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType()); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 240 | Params.push_back(IdType); |
| 241 | Params.push_back(SelType); |
David Chisnall | ab5824e | 2011-03-22 20:03:13 +0000 | [diff] [blame] | 242 | Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified()); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 243 | Params.push_back(Ctx.BoolTy); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 244 | llvm::FunctionType *FTy = |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 245 | Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(IdType, Params, |
| 246 | FunctionType::ExtInfo(), |
| 247 | RequiredArgs::All)); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 248 | return CGM.CreateRuntimeFunction(FTy, "objc_getProperty"); |
| 249 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 250 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 251 | llvm::Constant *getSetPropertyFn() { |
| 252 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 253 | ASTContext &Ctx = CGM.getContext(); |
| 254 | // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 255 | SmallVector<CanQualType,6> Params; |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 256 | CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType()); |
| 257 | CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType()); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 258 | Params.push_back(IdType); |
| 259 | Params.push_back(SelType); |
David Chisnall | ab5824e | 2011-03-22 20:03:13 +0000 | [diff] [blame] | 260 | Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified()); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 261 | Params.push_back(IdType); |
| 262 | Params.push_back(Ctx.BoolTy); |
| 263 | Params.push_back(Ctx.BoolTy); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 264 | llvm::FunctionType *FTy = |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 265 | Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params, |
| 266 | FunctionType::ExtInfo(), |
| 267 | RequiredArgs::All)); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 268 | return CGM.CreateRuntimeFunction(FTy, "objc_setProperty"); |
| 269 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 270 | |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 271 | llvm::Constant *getOptimizedSetPropertyFn(bool atomic, bool copy) { |
| 272 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 273 | ASTContext &Ctx = CGM.getContext(); |
| 274 | // void objc_setProperty_atomic(id self, SEL _cmd, |
| 275 | // id newValue, ptrdiff_t offset); |
| 276 | // void objc_setProperty_nonatomic(id self, SEL _cmd, |
| 277 | // id newValue, ptrdiff_t offset); |
| 278 | // void objc_setProperty_atomic_copy(id self, SEL _cmd, |
| 279 | // id newValue, ptrdiff_t offset); |
| 280 | // void objc_setProperty_nonatomic_copy(id self, SEL _cmd, |
| 281 | // id newValue, ptrdiff_t offset); |
| 282 | |
| 283 | SmallVector<CanQualType,4> Params; |
| 284 | CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType()); |
| 285 | CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType()); |
| 286 | Params.push_back(IdType); |
| 287 | Params.push_back(SelType); |
| 288 | Params.push_back(IdType); |
| 289 | Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified()); |
| 290 | llvm::FunctionType *FTy = |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 291 | Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params, |
| 292 | FunctionType::ExtInfo(), |
| 293 | RequiredArgs::All)); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 294 | const char *name; |
| 295 | if (atomic && copy) |
| 296 | name = "objc_setProperty_atomic_copy"; |
| 297 | else if (atomic && !copy) |
| 298 | name = "objc_setProperty_atomic"; |
| 299 | else if (!atomic && copy) |
| 300 | name = "objc_setProperty_nonatomic_copy"; |
| 301 | else |
| 302 | name = "objc_setProperty_nonatomic"; |
| 303 | |
| 304 | return CGM.CreateRuntimeFunction(FTy, name); |
| 305 | } |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 306 | |
| 307 | llvm::Constant *getCopyStructFn() { |
| 308 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 309 | ASTContext &Ctx = CGM.getContext(); |
| 310 | // void objc_copyStruct (void *, const void *, size_t, bool, bool) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 311 | SmallVector<CanQualType,5> Params; |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 312 | Params.push_back(Ctx.VoidPtrTy); |
| 313 | Params.push_back(Ctx.VoidPtrTy); |
| 314 | Params.push_back(Ctx.LongTy); |
| 315 | Params.push_back(Ctx.BoolTy); |
| 316 | Params.push_back(Ctx.BoolTy); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 317 | llvm::FunctionType *FTy = |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 318 | Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params, |
| 319 | FunctionType::ExtInfo(), |
| 320 | RequiredArgs::All)); |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 321 | return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct"); |
| 322 | } |
| 323 | |
Fariborz Jahanian | e317302 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 324 | /// This routine declares and returns address of: |
| 325 | /// void objc_copyCppObjectAtomic( |
| 326 | /// void *dest, const void *src, |
| 327 | /// void (*copyHelper) (void *dest, const void *source)); |
| 328 | llvm::Constant *getCppAtomicObjectFunction() { |
| 329 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 330 | ASTContext &Ctx = CGM.getContext(); |
| 331 | /// void objc_copyCppObjectAtomic(void *dest, const void *src, void *helper); |
| 332 | SmallVector<CanQualType,3> Params; |
| 333 | Params.push_back(Ctx.VoidPtrTy); |
| 334 | Params.push_back(Ctx.VoidPtrTy); |
| 335 | Params.push_back(Ctx.VoidPtrTy); |
| 336 | llvm::FunctionType *FTy = |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 337 | Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params, |
| 338 | FunctionType::ExtInfo(), |
| 339 | RequiredArgs::All)); |
Fariborz Jahanian | e317302 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 340 | return CGM.CreateRuntimeFunction(FTy, "objc_copyCppObjectAtomic"); |
| 341 | } |
| 342 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 343 | llvm::Constant *getEnumerationMutationFn() { |
Daniel Dunbar | c1ab900 | 2009-07-11 20:32:50 +0000 | [diff] [blame] | 344 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 345 | ASTContext &Ctx = CGM.getContext(); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 346 | // void objc_enumerationMutation (id) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 347 | SmallVector<CanQualType,1> Params; |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 348 | Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType())); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 349 | llvm::FunctionType *FTy = |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 350 | Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params, |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 351 | FunctionType::ExtInfo(), |
| 352 | RequiredArgs::All)); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 353 | return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation"); |
| 354 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 355 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 356 | /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function. |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 357 | llvm::Constant *getGcReadWeakFn() { |
| 358 | // id objc_read_weak (id *) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 359 | llvm::Type *args[] = { ObjectPtrTy->getPointerTo() }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 360 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 361 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 362 | return CGM.CreateRuntimeFunction(FTy, "objc_read_weak"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 365 | /// GcAssignWeakFn -- LLVM objc_assign_weak function. |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 366 | llvm::Constant *getGcAssignWeakFn() { |
| 367 | // id objc_assign_weak (id, id *) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 368 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() }; |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 369 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 370 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 371 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak"); |
| 372 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 373 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 374 | /// GcAssignGlobalFn -- LLVM objc_assign_global function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 375 | llvm::Constant *getGcAssignGlobalFn() { |
| 376 | // id objc_assign_global(id, id *) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 377 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 378 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 379 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 380 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_global"); |
| 381 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 382 | |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 383 | /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function. |
| 384 | llvm::Constant *getGcAssignThreadLocalFn() { |
| 385 | // id objc_assign_threadlocal(id src, id * dest) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 386 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() }; |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 387 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 388 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 389 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal"); |
| 390 | } |
| 391 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 392 | /// GcAssignIvarFn -- LLVM objc_assign_ivar function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 393 | llvm::Constant *getGcAssignIvarFn() { |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 394 | // id objc_assign_ivar(id, id *, ptrdiff_t) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 395 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo(), |
| 396 | CGM.PtrDiffTy }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 397 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 398 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 399 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar"); |
| 400 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 401 | |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 402 | /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function. |
| 403 | llvm::Constant *GcMemmoveCollectableFn() { |
| 404 | // void *objc_memmove_collectable(void *dst, const void *src, size_t size) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 405 | llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, LongTy }; |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 406 | llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, args, false); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 407 | return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable"); |
| 408 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 409 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 410 | /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 411 | llvm::Constant *getGcAssignStrongCastFn() { |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 412 | // id objc_assign_strongCast(id, id *) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 413 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 414 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 415 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 416 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast"); |
| 417 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 418 | |
| 419 | /// ExceptionThrowFn - LLVM objc_exception_throw function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 420 | llvm::Constant *getExceptionThrowFn() { |
| 421 | // void objc_exception_throw(id) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 422 | llvm::Type *args[] = { ObjectPtrTy }; |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 423 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 424 | llvm::FunctionType::get(CGM.VoidTy, args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 425 | return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw"); |
| 426 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 427 | |
Fariborz Jahanian | 69677ea | 2010-05-28 17:34:43 +0000 | [diff] [blame] | 428 | /// ExceptionRethrowFn - LLVM objc_exception_rethrow function. |
| 429 | llvm::Constant *getExceptionRethrowFn() { |
| 430 | // void objc_exception_rethrow(void) |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 431 | llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, false); |
Fariborz Jahanian | 69677ea | 2010-05-28 17:34:43 +0000 | [diff] [blame] | 432 | return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow"); |
| 433 | } |
| 434 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 435 | /// SyncEnterFn - LLVM object_sync_enter function. |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 436 | llvm::Constant *getSyncEnterFn() { |
Aaron Ballman | 2d234d73 | 2012-09-06 16:44:16 +0000 | [diff] [blame] | 437 | // int objc_sync_enter (id) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 438 | llvm::Type *args[] = { ObjectPtrTy }; |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 439 | llvm::FunctionType *FTy = |
Aaron Ballman | 2d234d73 | 2012-09-06 16:44:16 +0000 | [diff] [blame] | 440 | llvm::FunctionType::get(CGM.IntTy, args, false); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 441 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter"); |
| 442 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 443 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 444 | /// SyncExitFn - LLVM object_sync_exit function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 445 | llvm::Constant *getSyncExitFn() { |
Aaron Ballman | 2d234d73 | 2012-09-06 16:44:16 +0000 | [diff] [blame] | 446 | // int objc_sync_exit (id) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 447 | llvm::Type *args[] = { ObjectPtrTy }; |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 448 | llvm::FunctionType *FTy = |
Aaron Ballman | 2d234d73 | 2012-09-06 16:44:16 +0000 | [diff] [blame] | 449 | llvm::FunctionType::get(CGM.IntTy, args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 450 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit"); |
| 451 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 452 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 453 | llvm::Constant *getSendFn(bool IsSuper) const { |
| 454 | return IsSuper ? getMessageSendSuperFn() : getMessageSendFn(); |
| 455 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 456 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 457 | llvm::Constant *getSendFn2(bool IsSuper) const { |
| 458 | return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn(); |
| 459 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 460 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 461 | llvm::Constant *getSendStretFn(bool IsSuper) const { |
| 462 | return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn(); |
| 463 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 464 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 465 | llvm::Constant *getSendStretFn2(bool IsSuper) const { |
| 466 | return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn(); |
| 467 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 468 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 469 | llvm::Constant *getSendFpretFn(bool IsSuper) const { |
| 470 | return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn(); |
| 471 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 472 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 473 | llvm::Constant *getSendFpretFn2(bool IsSuper) const { |
| 474 | return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn(); |
| 475 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 476 | |
Anders Carlsson | eea6480 | 2011-10-31 16:27:11 +0000 | [diff] [blame] | 477 | llvm::Constant *getSendFp2retFn(bool IsSuper) const { |
| 478 | return IsSuper ? getMessageSendSuperFn() : getMessageSendFp2retFn(); |
| 479 | } |
| 480 | |
| 481 | llvm::Constant *getSendFp2RetFn2(bool IsSuper) const { |
| 482 | return IsSuper ? getMessageSendSuperFn2() : getMessageSendFp2retFn(); |
| 483 | } |
| 484 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 485 | ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm); |
| 486 | ~ObjCCommonTypesHelper(){} |
| 487 | }; |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 488 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 489 | /// ObjCTypesHelper - Helper class that encapsulates lazy |
| 490 | /// construction of varies types used during ObjC generation. |
| 491 | class ObjCTypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 492 | public: |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 493 | /// SymtabTy - LLVM type for struct objc_symtab. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 494 | llvm::StructType *SymtabTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 495 | /// SymtabPtrTy - LLVM type for struct objc_symtab *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 496 | llvm::Type *SymtabPtrTy; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 497 | /// ModuleTy - LLVM type for struct objc_module. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 498 | llvm::StructType *ModuleTy; |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 499 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 500 | /// ProtocolTy - LLVM type for struct objc_protocol. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 501 | llvm::StructType *ProtocolTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 502 | /// ProtocolPtrTy - LLVM type for struct objc_protocol *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 503 | llvm::Type *ProtocolPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 504 | /// ProtocolExtensionTy - LLVM type for struct |
| 505 | /// objc_protocol_extension. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 506 | llvm::StructType *ProtocolExtensionTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 507 | /// ProtocolExtensionTy - LLVM type for struct |
| 508 | /// objc_protocol_extension *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 509 | llvm::Type *ProtocolExtensionPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 510 | /// MethodDescriptionTy - LLVM type for struct |
| 511 | /// objc_method_description. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 512 | llvm::StructType *MethodDescriptionTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 513 | /// MethodDescriptionListTy - LLVM type for struct |
| 514 | /// objc_method_description_list. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 515 | llvm::StructType *MethodDescriptionListTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 516 | /// MethodDescriptionListPtrTy - LLVM type for struct |
| 517 | /// objc_method_description_list *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 518 | llvm::Type *MethodDescriptionListPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 519 | /// ProtocolListTy - LLVM type for struct objc_property_list. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 520 | llvm::StructType *ProtocolListTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 521 | /// ProtocolListPtrTy - LLVM type for struct objc_property_list*. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 522 | llvm::Type *ProtocolListPtrTy; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 523 | /// CategoryTy - LLVM type for struct objc_category. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 524 | llvm::StructType *CategoryTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 525 | /// ClassTy - LLVM type for struct objc_class. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 526 | llvm::StructType *ClassTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 527 | /// ClassPtrTy - LLVM type for struct objc_class *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 528 | llvm::Type *ClassPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 529 | /// ClassExtensionTy - LLVM type for struct objc_class_ext. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 530 | llvm::StructType *ClassExtensionTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 531 | /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 532 | llvm::Type *ClassExtensionPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 533 | // IvarTy - LLVM type for struct objc_ivar. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 534 | llvm::StructType *IvarTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 535 | /// IvarListTy - LLVM type for struct objc_ivar_list. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 536 | llvm::Type *IvarListTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 537 | /// IvarListPtrTy - LLVM type for struct objc_ivar_list *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 538 | llvm::Type *IvarListPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 539 | /// MethodListTy - LLVM type for struct objc_method_list. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 540 | llvm::Type *MethodListTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 541 | /// MethodListPtrTy - LLVM type for struct objc_method_list *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 542 | llvm::Type *MethodListPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 543 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 544 | /// ExceptionDataTy - LLVM type for struct _objc_exception_data. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 545 | llvm::Type *ExceptionDataTy; |
Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 546 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 547 | /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 548 | llvm::Constant *getExceptionTryEnterFn() { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 549 | llvm::Type *params[] = { ExceptionDataTy->getPointerTo() }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 550 | return CGM.CreateRuntimeFunction( |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 551 | llvm::FunctionType::get(CGM.VoidTy, params, false), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 552 | "objc_exception_try_enter"); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 553 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 554 | |
| 555 | /// ExceptionTryExitFn - LLVM objc_exception_try_exit function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 556 | llvm::Constant *getExceptionTryExitFn() { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 557 | llvm::Type *params[] = { ExceptionDataTy->getPointerTo() }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 558 | return CGM.CreateRuntimeFunction( |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 559 | llvm::FunctionType::get(CGM.VoidTy, params, false), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 560 | "objc_exception_try_exit"); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 561 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 562 | |
| 563 | /// ExceptionExtractFn - LLVM objc_exception_extract function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 564 | llvm::Constant *getExceptionExtractFn() { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 565 | llvm::Type *params[] = { ExceptionDataTy->getPointerTo() }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 566 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 567 | params, false), |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 568 | "objc_exception_extract"); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 569 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 570 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 571 | /// ExceptionMatchFn - LLVM objc_exception_match function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 572 | llvm::Constant *getExceptionMatchFn() { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 573 | llvm::Type *params[] = { ClassPtrTy, ObjectPtrTy }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 574 | return CGM.CreateRuntimeFunction( |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 575 | llvm::FunctionType::get(CGM.Int32Ty, params, false), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 576 | "objc_exception_match"); |
| 577 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 578 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 579 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 580 | /// SetJmpFn - LLVM _setjmp function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 581 | llvm::Constant *getSetJmpFn() { |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 582 | // This is specifically the prototype for x86. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 583 | llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() }; |
Bill Wendling | c4c62fd | 2013-01-31 00:30:05 +0000 | [diff] [blame^] | 584 | return |
| 585 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty, |
| 586 | params, false), |
| 587 | "_setjmp", |
| 588 | llvm::AttributeSet::get(CGM.getLLVMContext(), |
| 589 | llvm::AttributeSet::FunctionIndex, |
| 590 | llvm::Attribute::NonLazyBind)); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 591 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 592 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 593 | public: |
| 594 | ObjCTypesHelper(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 595 | ~ObjCTypesHelper() {} |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 596 | }; |
| 597 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 598 | /// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 599 | /// modern abi |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 600 | class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 601 | public: |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 602 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 603 | // MethodListnfABITy - LLVM for struct _method_list_t |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 604 | llvm::StructType *MethodListnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 605 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 606 | // MethodListnfABIPtrTy - LLVM for struct _method_list_t* |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 607 | llvm::Type *MethodListnfABIPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 608 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 609 | // ProtocolnfABITy = LLVM for struct _protocol_t |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 610 | llvm::StructType *ProtocolnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 611 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 612 | // ProtocolnfABIPtrTy = LLVM for struct _protocol_t* |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 613 | llvm::Type *ProtocolnfABIPtrTy; |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 614 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 615 | // ProtocolListnfABITy - LLVM for struct _objc_protocol_list |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 616 | llvm::StructType *ProtocolListnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 617 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 618 | // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list* |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 619 | llvm::Type *ProtocolListnfABIPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 620 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 621 | // ClassnfABITy - LLVM for struct _class_t |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 622 | llvm::StructType *ClassnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 623 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 624 | // ClassnfABIPtrTy - LLVM for struct _class_t* |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 625 | llvm::Type *ClassnfABIPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 626 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 627 | // IvarnfABITy - LLVM for struct _ivar_t |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 628 | llvm::StructType *IvarnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 629 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 630 | // IvarListnfABITy - LLVM for struct _ivar_list_t |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 631 | llvm::StructType *IvarListnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 632 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 633 | // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t* |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 634 | llvm::Type *IvarListnfABIPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 635 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 636 | // ClassRonfABITy - LLVM for struct _class_ro_t |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 637 | llvm::StructType *ClassRonfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 638 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 639 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 640 | llvm::Type *ImpnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 641 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 642 | // CategorynfABITy - LLVM for struct _category_t |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 643 | llvm::StructType *CategorynfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 644 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 645 | // New types for nonfragile abi messaging. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 646 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 647 | // MessageRefTy - LLVM for: |
| 648 | // struct _message_ref_t { |
| 649 | // IMP messenger; |
| 650 | // SEL name; |
| 651 | // }; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 652 | llvm::StructType *MessageRefTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 653 | // MessageRefCTy - clang type for struct _message_ref_t |
| 654 | QualType MessageRefCTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 655 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 656 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 657 | llvm::Type *MessageRefPtrTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 658 | // MessageRefCPtrTy - clang type for struct _message_ref_t* |
| 659 | QualType MessageRefCPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 660 | |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 661 | // MessengerTy - Type of the messenger (shown as IMP above) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 662 | llvm::FunctionType *MessengerTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 663 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 664 | // SuperMessageRefTy - LLVM for: |
| 665 | // struct _super_message_ref_t { |
| 666 | // SUPER_IMP messenger; |
| 667 | // SEL name; |
| 668 | // }; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 669 | llvm::StructType *SuperMessageRefTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 670 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 671 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 672 | llvm::Type *SuperMessageRefPtrTy; |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 673 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 674 | llvm::Constant *getMessageSendFixupFn() { |
| 675 | // id objc_msgSend_fixup(id, struct message_ref_t*, ...) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 676 | llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 677 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 678 | params, true), |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 679 | "objc_msgSend_fixup"); |
| 680 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 681 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 682 | llvm::Constant *getMessageSendFpretFixupFn() { |
| 683 | // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 684 | llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 685 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 686 | params, true), |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 687 | "objc_msgSend_fpret_fixup"); |
| 688 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 689 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 690 | llvm::Constant *getMessageSendStretFixupFn() { |
| 691 | // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 692 | llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 693 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 694 | params, true), |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 695 | "objc_msgSend_stret_fixup"); |
| 696 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 697 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 698 | llvm::Constant *getMessageSendSuper2FixupFn() { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 699 | // id objc_msgSendSuper2_fixup (struct objc_super *, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 700 | // struct _super_message_ref_t*, ...) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 701 | llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 702 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 703 | params, true), |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 704 | "objc_msgSendSuper2_fixup"); |
| 705 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 706 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 707 | llvm::Constant *getMessageSendSuper2StretFixupFn() { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 708 | // id objc_msgSendSuper2_stret_fixup(struct objc_super *, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 709 | // struct _super_message_ref_t*, ...) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 710 | llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 711 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 712 | params, true), |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 713 | "objc_msgSendSuper2_stret_fixup"); |
| 714 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 715 | |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 716 | llvm::Constant *getObjCEndCatchFn() { |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 717 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false), |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 718 | "objc_end_catch"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 719 | |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 720 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 721 | |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 722 | llvm::Constant *getObjCBeginCatchFn() { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 723 | llvm::Type *params[] = { Int8PtrTy }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 724 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 725 | params, false), |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 726 | "objc_begin_catch"); |
| 727 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 728 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 729 | llvm::StructType *EHTypeTy; |
| 730 | llvm::Type *EHTypePtrTy; |
Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 731 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 732 | ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm); |
| 733 | ~ObjCNonFragileABITypesHelper(){} |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 734 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 735 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 736 | class CGObjCCommonMac : public CodeGen::CGObjCRuntime { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 737 | public: |
| 738 | // FIXME - accessibility |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 739 | class GC_IVAR { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 740 | public: |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 741 | unsigned ivar_bytepos; |
| 742 | unsigned ivar_size; |
| 743 | GC_IVAR(unsigned bytepos = 0, unsigned size = 0) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 744 | : ivar_bytepos(bytepos), ivar_size(size) {} |
Daniel Dunbar | 0941b49 | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 745 | |
| 746 | // Allow sorting based on byte pos. |
| 747 | bool operator<(const GC_IVAR &b) const { |
| 748 | return ivar_bytepos < b.ivar_bytepos; |
| 749 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 750 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 751 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 752 | class SKIP_SCAN { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 753 | public: |
| 754 | unsigned skip; |
| 755 | unsigned scan; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 756 | SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0) |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 757 | : skip(_skip), scan(_scan) {} |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 758 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 759 | |
Fariborz Jahanian | e11dba8 | 2012-10-25 21:15:04 +0000 | [diff] [blame] | 760 | /// opcode for captured block variables layout 'instructions'. |
| 761 | /// In the following descriptions, 'I' is the value of the immediate field. |
| 762 | /// (field following the opcode). |
| 763 | /// |
| 764 | enum BLOCK_LAYOUT_OPCODE { |
| 765 | /// An operator which affects how the following layout should be |
| 766 | /// interpreted. |
| 767 | /// I == 0: Halt interpretation and treat everything else as |
| 768 | /// a non-pointer. Note that this instruction is equal |
| 769 | /// to '\0'. |
| 770 | /// I != 0: Currently unused. |
| 771 | BLOCK_LAYOUT_OPERATOR = 0, |
| 772 | |
| 773 | /// The next I+1 bytes do not contain a value of object pointer type. |
| 774 | /// Note that this can leave the stream unaligned, meaning that |
| 775 | /// subsequent word-size instructions do not begin at a multiple of |
| 776 | /// the pointer size. |
| 777 | BLOCK_LAYOUT_NON_OBJECT_BYTES = 1, |
| 778 | |
| 779 | /// The next I+1 words do not contain a value of object pointer type. |
| 780 | /// This is simply an optimized version of BLOCK_LAYOUT_BYTES for |
| 781 | /// when the required skip quantity is a multiple of the pointer size. |
| 782 | BLOCK_LAYOUT_NON_OBJECT_WORDS = 2, |
| 783 | |
| 784 | /// The next I+1 words are __strong pointers to Objective-C |
| 785 | /// objects or blocks. |
| 786 | BLOCK_LAYOUT_STRONG = 3, |
| 787 | |
| 788 | /// The next I+1 words are pointers to __block variables. |
| 789 | BLOCK_LAYOUT_BYREF = 4, |
| 790 | |
| 791 | /// The next I+1 words are __weak pointers to Objective-C |
| 792 | /// objects or blocks. |
| 793 | BLOCK_LAYOUT_WEAK = 5, |
| 794 | |
| 795 | /// The next I+1 words are __unsafe_unretained pointers to |
| 796 | /// Objective-C objects or blocks. |
| 797 | BLOCK_LAYOUT_UNRETAINED = 6 |
| 798 | |
| 799 | /// The next I+1 words are block or object pointers with some |
| 800 | /// as-yet-unspecified ownership semantics. If we add more |
| 801 | /// flavors of ownership semantics, values will be taken from |
| 802 | /// this range. |
| 803 | /// |
| 804 | /// This is included so that older tools can at least continue |
| 805 | /// processing the layout past such things. |
| 806 | //BLOCK_LAYOUT_OWNERSHIP_UNKNOWN = 7..10, |
| 807 | |
| 808 | /// All other opcodes are reserved. Halt interpretation and |
| 809 | /// treat everything else as opaque. |
| 810 | }; |
| 811 | |
| 812 | class RUN_SKIP { |
| 813 | public: |
| 814 | enum BLOCK_LAYOUT_OPCODE opcode; |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 815 | CharUnits block_var_bytepos; |
| 816 | CharUnits block_var_size; |
Fariborz Jahanian | e11dba8 | 2012-10-25 21:15:04 +0000 | [diff] [blame] | 817 | RUN_SKIP(enum BLOCK_LAYOUT_OPCODE Opcode = BLOCK_LAYOUT_OPERATOR, |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 818 | CharUnits BytePos = CharUnits::Zero(), |
| 819 | CharUnits Size = CharUnits::Zero()) |
Fariborz Jahanian | c46b435 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 820 | : opcode(Opcode), block_var_bytepos(BytePos), block_var_size(Size) {} |
Fariborz Jahanian | e11dba8 | 2012-10-25 21:15:04 +0000 | [diff] [blame] | 821 | |
| 822 | // Allow sorting based on byte pos. |
| 823 | bool operator<(const RUN_SKIP &b) const { |
| 824 | return block_var_bytepos < b.block_var_bytepos; |
| 825 | } |
| 826 | }; |
| 827 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 828 | protected: |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 829 | llvm::LLVMContext &VMContext; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 830 | // FIXME! May not be needing this after all. |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 831 | unsigned ObjCABI; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 832 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 833 | // gc ivar layout bitmap calculation helper caches. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 834 | SmallVector<GC_IVAR, 16> SkipIvars; |
| 835 | SmallVector<GC_IVAR, 16> IvarsInfo; |
Fariborz Jahanian | e11dba8 | 2012-10-25 21:15:04 +0000 | [diff] [blame] | 836 | |
| 837 | // arc/mrr layout of captured block literal variables. |
| 838 | SmallVector<RUN_SKIP, 16> RunSkipBlockVars; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 839 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 840 | /// LazySymbols - Symbols to generate a lazy reference for. See |
| 841 | /// DefinedSymbols and FinishModule(). |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 842 | llvm::SetVector<IdentifierInfo*> LazySymbols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 843 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 844 | /// DefinedSymbols - External symbols which are defined by this |
| 845 | /// module. The symbols in this list and LazySymbols are used to add |
| 846 | /// special linker symbols which ensure that Objective-C modules are |
| 847 | /// linked properly. |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 848 | llvm::SetVector<IdentifierInfo*> DefinedSymbols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 849 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 850 | /// ClassNames - uniqued class names. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 851 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 852 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 853 | /// MethodVarNames - uniqued method variable names. |
| 854 | llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 855 | |
Fariborz Jahanian | b9c5b3d | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 856 | /// DefinedCategoryNames - list of category names in form Class_Category. |
| 857 | llvm::SetVector<std::string> DefinedCategoryNames; |
| 858 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 859 | /// MethodVarTypes - uniqued method type signatures. We have to use |
| 860 | /// a StringMap here because have no other unique reference. |
| 861 | llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 862 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 863 | /// MethodDefinitions - map of methods which have been defined in |
| 864 | /// this translation unit. |
| 865 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 866 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 867 | /// PropertyNames - uniqued method variable names. |
| 868 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 869 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 870 | /// ClassReferences - uniqued class references. |
| 871 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 872 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 873 | /// SelectorReferences - uniqued selector references. |
| 874 | llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 875 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 876 | /// Protocols - Protocols for which an objc_protocol structure has |
| 877 | /// been emitted. Forward declarations are handled by creating an |
| 878 | /// empty structure whose initializer is filled in when/if defined. |
| 879 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 880 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 881 | /// DefinedProtocols - Protocols which have actually been |
| 882 | /// defined. We should not need this, see FIXME in GenerateProtocol. |
| 883 | llvm::DenseSet<IdentifierInfo*> DefinedProtocols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 884 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 885 | /// DefinedClasses - List of defined classes. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 886 | SmallVector<llvm::GlobalValue*, 16> DefinedClasses; |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 887 | |
| 888 | /// DefinedNonLazyClasses - List of defined "non-lazy" classes. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 889 | SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyClasses; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 890 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 891 | /// DefinedCategories - List of defined categories. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 892 | SmallVector<llvm::GlobalValue*, 16> DefinedCategories; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 893 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 894 | /// DefinedNonLazyCategories - List of defined "non-lazy" categories. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 895 | SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyCategories; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 896 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 897 | /// GetNameForMethod - Return a name for the given method. |
| 898 | /// \param[out] NameOut - The return value. |
| 899 | void GetNameForMethod(const ObjCMethodDecl *OMD, |
| 900 | const ObjCContainerDecl *CD, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 901 | SmallVectorImpl<char> &NameOut); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 902 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 903 | /// GetMethodVarName - Return a unique constant for the given |
| 904 | /// selector's name. The return value has type char *. |
| 905 | llvm::Constant *GetMethodVarName(Selector Sel); |
| 906 | llvm::Constant *GetMethodVarName(IdentifierInfo *Ident); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 907 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 908 | /// GetMethodVarType - Return a unique constant for the given |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 909 | /// method's type encoding string. The return value has type char *. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 910 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 911 | // FIXME: This is a horrible name. |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 912 | llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D, |
| 913 | bool Extended = false); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 914 | llvm::Constant *GetMethodVarType(const FieldDecl *D); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 915 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 916 | /// GetPropertyName - Return a unique constant for the given |
| 917 | /// name. The return value has type char *. |
| 918 | llvm::Constant *GetPropertyName(IdentifierInfo *Ident); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 919 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 920 | // FIXME: This can be dropped once string functions are unified. |
| 921 | llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 922 | const Decl *Container); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 923 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 924 | /// GetClassName - Return a unique constant for the given selector's |
| 925 | /// name. The return value has type char *. |
| 926 | llvm::Constant *GetClassName(IdentifierInfo *Ident); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 927 | |
Argyrios Kyrtzidis | 9d50c06 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 928 | llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD); |
| 929 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 930 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 931 | /// implementation for the __strong or __weak case. |
| 932 | /// |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 933 | llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI, |
| 934 | bool ForStrongLayout); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 935 | |
Fariborz Jahanian | b8fd2eb | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 936 | llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 937 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 938 | void BuildAggrIvarRecordLayout(const RecordType *RT, |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 939 | unsigned int BytePos, bool ForStrongLayout, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 940 | bool &HasUnion); |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 941 | void BuildAggrIvarLayout(const ObjCImplementationDecl *OI, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 942 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 943 | const RecordDecl *RD, |
Bill Wendling | 795b100 | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 944 | ArrayRef<const FieldDecl*> RecFields, |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 945 | unsigned int BytePos, bool ForStrongLayout, |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 946 | bool &HasUnion); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 947 | |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 948 | Qualifiers::ObjCLifetime getBlockCaptureLifetime(QualType QT, bool ByrefLayout); |
Fariborz Jahanian | 44fcff9 | 2012-11-02 22:51:18 +0000 | [diff] [blame] | 949 | |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 950 | void UpdateRunSkipBlockVars(bool IsByref, |
| 951 | Qualifiers::ObjCLifetime LifeTime, |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 952 | CharUnits FieldOffset, |
| 953 | CharUnits FieldSize); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 954 | |
| 955 | void BuildRCBlockVarRecordLayout(const RecordType *RT, |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 956 | CharUnits BytePos, bool &HasUnion, |
| 957 | bool ByrefLayout=false); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 958 | |
| 959 | void BuildRCRecordLayout(const llvm::StructLayout *RecLayout, |
| 960 | const RecordDecl *RD, |
| 961 | ArrayRef<const FieldDecl*> RecFields, |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 962 | CharUnits BytePos, bool &HasUnion, |
| 963 | bool ByrefLayout); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 964 | |
Fariborz Jahanian | f22ae65 | 2012-11-01 18:32:55 +0000 | [diff] [blame] | 965 | uint64_t InlineLayoutInstruction(SmallVectorImpl<unsigned char> &Layout); |
| 966 | |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 967 | llvm::Constant *getBitmapBlockLayout(bool ComputeByrefLayout); |
| 968 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 969 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 970 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 971 | /// ivar layout bitmap. |
| 972 | llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident, |
| 973 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 974 | |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 975 | /// EmitPropertyList - Emit the given property list. The return |
| 976 | /// value has type PropertyListPtrTy. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 977 | llvm::Constant *EmitPropertyList(Twine Name, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 978 | const Decl *Container, |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 979 | const ObjCContainerDecl *OCD, |
| 980 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 981 | |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 982 | /// EmitProtocolMethodTypes - Generate the array of extended method type |
| 983 | /// strings. The return value has type Int8PtrPtrTy. |
| 984 | llvm::Constant *EmitProtocolMethodTypes(Twine Name, |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 985 | ArrayRef<llvm::Constant*> MethodTypes, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 986 | const ObjCCommonTypesHelper &ObjCTypes); |
| 987 | |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 988 | /// PushProtocolProperties - Push protocol's property on the input stack. |
Bill Wendling | 3964e62 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 989 | void PushProtocolProperties( |
| 990 | llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 991 | SmallVectorImpl<llvm::Constant*> &Properties, |
Bill Wendling | 3964e62 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 992 | const Decl *Container, |
| 993 | const ObjCProtocolDecl *PROTO, |
| 994 | const ObjCCommonTypesHelper &ObjCTypes); |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 995 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 996 | /// GetProtocolRef - Return a reference to the internal protocol |
| 997 | /// description, creating an empty one if it has not been |
| 998 | /// defined. The return value has type ProtocolPtrTy. |
| 999 | llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD); |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 1000 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 1001 | /// CreateMetadataVar - Create a global variable with internal |
| 1002 | /// linkage for use by the Objective-C runtime. |
| 1003 | /// |
| 1004 | /// This is a convenience wrapper which not only creates the |
| 1005 | /// variable, but also sets the section and alignment and adds the |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 1006 | /// global to the "llvm.used" list. |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 1007 | /// |
| 1008 | /// \param Name - The variable name. |
| 1009 | /// \param Init - The variable initializer; this is also used to |
| 1010 | /// define the type of the variable. |
| 1011 | /// \param Section - The section the variable should go into, or 0. |
| 1012 | /// \param Align - The alignment for the variable, or 0. |
| 1013 | /// \param AddToUsed - Whether the variable should be added to |
Daniel Dunbar | c158306 | 2009-04-14 17:42:51 +0000 | [diff] [blame] | 1014 | /// "llvm.used". |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1015 | llvm::GlobalVariable *CreateMetadataVar(Twine Name, |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 1016 | llvm::Constant *Init, |
| 1017 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 1018 | unsigned Align, |
| 1019 | bool AddToUsed); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 1020 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1021 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1022 | ReturnValueSlot Return, |
| 1023 | QualType ResultType, |
| 1024 | llvm::Value *Sel, |
| 1025 | llvm::Value *Arg0, |
| 1026 | QualType Arg0Ty, |
| 1027 | bool IsSuper, |
| 1028 | const CallArgList &CallArgs, |
| 1029 | const ObjCMethodDecl *OMD, |
| 1030 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 1031 | |
Daniel Dunbar | fce176b | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 1032 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 1033 | /// level information. |
| 1034 | void EmitImageInfo(); |
| 1035 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1036 | public: |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 1037 | CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1038 | CGObjCRuntime(cgm), VMContext(cgm.getLLVMContext()) { } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1039 | |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 1040 | virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1041 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1042 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
| 1043 | const ObjCContainerDecl *CD=0); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1044 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1045 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1046 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1047 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1048 | /// declaration, emitting it if necessary. The return value has type |
| 1049 | /// ProtocolPtrTy. |
| 1050 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1051 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1052 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1053 | /// object for the given declaration, emitting it if needed. These |
| 1054 | /// forward references will be filled in with empty bodies if no |
| 1055 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 1056 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0; |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1057 | virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM, |
| 1058 | const CGBlockInfo &blockInfo); |
Fariborz Jahanian | c46b435 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 1059 | virtual llvm::Constant *BuildRCBlockLayout(CodeGen::CodeGenModule &CGM, |
| 1060 | const CGBlockInfo &blockInfo); |
Fariborz Jahanian | 89ecd41 | 2010-08-04 16:57:49 +0000 | [diff] [blame] | 1061 | |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 1062 | virtual llvm::Constant *BuildByrefLayout(CodeGen::CodeGenModule &CGM, |
| 1063 | QualType T); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1064 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1065 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1066 | class CGObjCMac : public CGObjCCommonMac { |
| 1067 | private: |
| 1068 | ObjCTypesHelper ObjCTypes; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1069 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1070 | /// EmitModuleInfo - Another marker encoding module level |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1071 | /// information. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1072 | void EmitModuleInfo(); |
| 1073 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1074 | /// EmitModuleSymols - Emit module symbols, the list of defined |
| 1075 | /// classes and categories. The result has type SymtabPtrTy. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1076 | llvm::Constant *EmitModuleSymbols(); |
| 1077 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1078 | /// FinishModule - Write out global data structures at the end of |
| 1079 | /// processing a translation unit. |
| 1080 | void FinishModule(); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1081 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1082 | /// EmitClassExtension - Generate the class extension structure used |
| 1083 | /// to store the weak ivar layout and properties. The return value |
| 1084 | /// has type ClassExtensionPtrTy. |
| 1085 | llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID); |
| 1086 | |
| 1087 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 1088 | /// for the given class. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1089 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1090 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 1091 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1092 | llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder, |
| 1093 | IdentifierInfo *II); |
| 1094 | |
| 1095 | llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder); |
| 1096 | |
Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 1097 | /// EmitSuperClassRef - Emits reference to class's main metadata class. |
| 1098 | llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1099 | |
| 1100 | /// EmitIvarList - Emit the ivar list for the given |
| 1101 | /// implementation. If ForClass is true the list of class ivars |
| 1102 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 1103 | /// interface ivars will be emitted. The return value has type |
| 1104 | /// IvarListPtrTy. |
| 1105 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1106 | bool ForClass); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1107 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1108 | /// EmitMetaClass - Emit a forward reference to the class structure |
| 1109 | /// for the metaclass of the given interface. The return value has |
| 1110 | /// type ClassPtrTy. |
| 1111 | llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID); |
| 1112 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1113 | /// EmitMetaClass - Emit a class structure for the metaclass of the |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1114 | /// given implementation. The return value has type ClassPtrTy. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1115 | llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID, |
| 1116 | llvm::Constant *Protocols, |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 1117 | ArrayRef<llvm::Constant*> Methods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1118 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1119 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1120 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1121 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1122 | |
| 1123 | /// EmitMethodList - Emit the method list for the given |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1124 | /// implementation. The return value has type MethodListPtrTy. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1125 | llvm::Constant *EmitMethodList(Twine Name, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1126 | const char *Section, |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 1127 | ArrayRef<llvm::Constant*> Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1128 | |
| 1129 | /// EmitMethodDescList - Emit a method description list for a list of |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1130 | /// method declarations. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1131 | /// - TypeName: The name for the type containing the methods. |
Sylvestre Ledru | f3477c1 | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 1132 | /// - IsProtocol: True iff these methods are for a protocol. |
| 1133 | /// - ClassMethds: True iff these are class methods. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1134 | /// - Required: When true, only "required" methods are |
| 1135 | /// listed. Similarly, when false only "optional" methods are |
| 1136 | /// listed. For classes this should always be true. |
| 1137 | /// - begin, end: The method list to output. |
| 1138 | /// |
| 1139 | /// The return value has type MethodDescriptionListPtrTy. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1140 | llvm::Constant *EmitMethodDescList(Twine Name, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1141 | const char *Section, |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 1142 | ArrayRef<llvm::Constant*> Methods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1143 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1144 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1145 | /// declaration, emitting it if necessary. The return value has type |
| 1146 | /// ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1147 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1148 | |
| 1149 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1150 | /// object for the given declaration, emitting it if needed. These |
| 1151 | /// forward references will be filled in with empty bodies if no |
| 1152 | /// definition is seen. The return value has type ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1153 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1154 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1155 | /// EmitProtocolExtension - Generate the protocol extension |
| 1156 | /// structure used to store optional instance and class methods, and |
| 1157 | /// protocol properties. The return value has type |
| 1158 | /// ProtocolExtensionPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1159 | llvm::Constant * |
| 1160 | EmitProtocolExtension(const ObjCProtocolDecl *PD, |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 1161 | ArrayRef<llvm::Constant*> OptInstanceMethods, |
| 1162 | ArrayRef<llvm::Constant*> OptClassMethods, |
| 1163 | ArrayRef<llvm::Constant*> MethodTypesExt); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1164 | |
| 1165 | /// EmitProtocolList - Generate the list of referenced |
| 1166 | /// protocols. The return value has type ProtocolListPtrTy. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1167 | llvm::Constant *EmitProtocolList(Twine Name, |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1168 | ObjCProtocolDecl::protocol_iterator begin, |
| 1169 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1170 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1171 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1172 | /// for the given selector. |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1173 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel, |
| 1174 | bool lval=false); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1175 | |
| 1176 | public: |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1177 | CGObjCMac(CodeGen::CodeGenModule &cgm); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1178 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1179 | virtual llvm::Function *ModuleInitFunction(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1180 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1181 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1182 | ReturnValueSlot Return, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1183 | QualType ResultType, |
| 1184 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1185 | llvm::Value *Receiver, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1186 | const CallArgList &CallArgs, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1187 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1188 | const ObjCMethodDecl *Method); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1189 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1190 | virtual CodeGen::RValue |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1191 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1192 | ReturnValueSlot Return, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1193 | QualType ResultType, |
| 1194 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1195 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1196 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1197 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1198 | bool IsClassMessage, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1199 | const CallArgList &CallArgs, |
| 1200 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1201 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1202 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1203 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1204 | |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1205 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel, |
| 1206 | bool lval = false); |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1207 | |
| 1208 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an |
| 1209 | /// untyped one. |
| 1210 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, |
| 1211 | const ObjCMethodDecl *Method); |
| 1212 | |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1213 | virtual llvm::Constant *GetEHType(QualType T); |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1214 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1215 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1216 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1217 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1218 | |
Daniel Dunbar | 85fdea0 | 2012-02-28 15:36:15 +0000 | [diff] [blame] | 1219 | virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {} |
David Chisnall | 29254f4 | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 1220 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1221 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1222 | const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1223 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1224 | virtual llvm::Constant *GetPropertyGetFunction(); |
| 1225 | virtual llvm::Constant *GetPropertySetFunction(); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1226 | virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic, |
| 1227 | bool copy); |
David Chisnall | 8fac25d | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 1228 | virtual llvm::Constant *GetGetStructFunction(); |
| 1229 | virtual llvm::Constant *GetSetStructFunction(); |
David Chisnall | d397cfe | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 1230 | virtual llvm::Constant *GetCppAtomicObjectGetFunction(); |
| 1231 | virtual llvm::Constant *GetCppAtomicObjectSetFunction(); |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1232 | virtual llvm::Constant *EnumerationMutationFunction(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1233 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1234 | virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
| 1235 | const ObjCAtTryStmt &S); |
| 1236 | virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1237 | const ObjCAtSynchronizedStmt &S); |
| 1238 | void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1239 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6a3c70e | 2013-01-10 19:02:56 +0000 | [diff] [blame] | 1240 | const ObjCAtThrowStmt &S, |
| 1241 | bool ClearInsertionPoint=true); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1242 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1243 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1244 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1245 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1246 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 1247 | llvm::Value *src, llvm::Value *dest, |
| 1248 | bool threadlocal = false); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 1249 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1250 | llvm::Value *src, llvm::Value *dest, |
| 1251 | llvm::Value *ivarOffset); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1252 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 1253 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 1254 | virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
| 1255 | llvm::Value *dest, llvm::Value *src, |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 1256 | llvm::Value *size); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1257 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1258 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1259 | QualType ObjectTy, |
| 1260 | llvm::Value *BaseValue, |
| 1261 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1262 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1263 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1264 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1265 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | 6f40e22 | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1266 | |
| 1267 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 1268 | /// class of the given name. |
| 1269 | virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1270 | llvm_unreachable("CGObjCMac::GetClassGlobal"); |
Fariborz Jahanian | 6f40e22 | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1271 | } |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1272 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1273 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1274 | class CGObjCNonFragileABIMac : public CGObjCCommonMac { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1275 | private: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1276 | ObjCNonFragileABITypesHelper ObjCTypes; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1277 | llvm::GlobalVariable* ObjCEmptyCacheVar; |
| 1278 | llvm::GlobalVariable* ObjCEmptyVtableVar; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1279 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1280 | /// SuperClassReferences - uniqued super class references. |
| 1281 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1282 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1283 | /// MetaClassReferences - uniqued meta class references. |
| 1284 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1285 | |
| 1286 | /// EHTypeReferences - uniqued class ehtype references. |
| 1287 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1288 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1289 | /// VTableDispatchMethods - List of methods for which we generate |
| 1290 | /// vtable-based message dispatch. |
| 1291 | llvm::DenseSet<Selector> VTableDispatchMethods; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1292 | |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 1293 | /// DefinedMetaClasses - List of defined meta-classes. |
| 1294 | std::vector<llvm::GlobalValue*> DefinedMetaClasses; |
| 1295 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1296 | /// isVTableDispatchedSelector - Returns true if SEL is a |
| 1297 | /// vtable-based selector. |
| 1298 | bool isVTableDispatchedSelector(Selector Sel); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1299 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1300 | /// FinishNonFragileABIModule - Write out global data structures at the end of |
| 1301 | /// processing a translation unit. |
| 1302 | void FinishNonFragileABIModule(); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1303 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 1304 | /// AddModuleClassList - Add the given list of class pointers to the |
| 1305 | /// module with the provided symbol and section names. |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 1306 | void AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 1307 | const char *SymbolName, |
| 1308 | const char *SectionName); |
| 1309 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1310 | llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags, |
| 1311 | unsigned InstanceStart, |
| 1312 | unsigned InstanceSize, |
| 1313 | const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1314 | llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1315 | llvm::Constant *IsAGV, |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1316 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 1317 | llvm::Constant *ClassRoGV, |
| 1318 | bool HiddenVisibility); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1319 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1320 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1321 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 1322 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1323 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1324 | /// EmitMethodList - Emit the method list for the given |
| 1325 | /// implementation. The return value has type MethodListnfABITy. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1326 | llvm::Constant *EmitMethodList(Twine Name, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1327 | const char *Section, |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 1328 | ArrayRef<llvm::Constant*> Methods); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 1329 | /// EmitIvarList - Emit the ivar list for the given |
| 1330 | /// implementation. If ForClass is true the list of class ivars |
| 1331 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 1332 | /// interface ivars will be emitted. The return value has type |
| 1333 | /// IvarListnfABIPtrTy. |
| 1334 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1335 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1336 | llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 1337 | const ObjCIvarDecl *Ivar, |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 1338 | unsigned long int offset); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1339 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1340 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1341 | /// declaration, emitting it if necessary. The return value has type |
| 1342 | /// ProtocolPtrTy. |
| 1343 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1344 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1345 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1346 | /// object for the given declaration, emitting it if needed. These |
| 1347 | /// forward references will be filled in with empty bodies if no |
| 1348 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 1349 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1350 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1351 | /// EmitProtocolList - Generate the list of referenced |
| 1352 | /// protocols. The return value has type ProtocolListPtrTy. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1353 | llvm::Constant *EmitProtocolList(Twine Name, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1354 | ObjCProtocolDecl::protocol_iterator begin, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1355 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1356 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1357 | CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1358 | ReturnValueSlot Return, |
| 1359 | QualType ResultType, |
| 1360 | Selector Sel, |
| 1361 | llvm::Value *Receiver, |
| 1362 | QualType Arg0Ty, |
| 1363 | bool IsSuper, |
| 1364 | const CallArgList &CallArgs, |
| 1365 | const ObjCMethodDecl *Method); |
Fariborz Jahanian | 6f40e22 | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1366 | |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 1367 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 1368 | /// class of the given name. |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 1369 | llvm::GlobalVariable *GetClassGlobal(const std::string &Name); |
Fariborz Jahanian | 6f40e22 | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1370 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1371 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1372 | /// for the given class reference. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1373 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1374 | const ObjCInterfaceDecl *ID); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1375 | |
| 1376 | llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder, |
| 1377 | IdentifierInfo *II); |
| 1378 | |
| 1379 | llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1380 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1381 | /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 1382 | /// for the given super class reference. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1383 | llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder, |
| 1384 | const ObjCInterfaceDecl *ID); |
| 1385 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1386 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 1387 | /// meta-data |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1388 | llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1389 | const ObjCInterfaceDecl *ID); |
| 1390 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1391 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 1392 | /// the given ivar. |
| 1393 | /// |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 1394 | llvm::GlobalVariable * ObjCIvarOffsetVariable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1395 | const ObjCInterfaceDecl *ID, |
| 1396 | const ObjCIvarDecl *Ivar); |
| 1397 | |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 1398 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1399 | /// for the given selector. |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1400 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel, |
| 1401 | bool lval=false); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1402 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1403 | /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1404 | /// interface. The return value has type EHTypePtrTy. |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1405 | llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1406 | bool ForDefinition); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1407 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1408 | const char *getMetaclassSymbolPrefix() const { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1409 | return "OBJC_METACLASS_$_"; |
| 1410 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1411 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1412 | const char *getClassSymbolPrefix() const { |
| 1413 | return "OBJC_CLASS_$_"; |
| 1414 | } |
| 1415 | |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 1416 | void GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1417 | uint32_t &InstanceStart, |
| 1418 | uint32_t &InstanceSize); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1419 | |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1420 | // Shamelessly stolen from Analysis/CFRefCount.cpp |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1421 | Selector GetNullarySelector(const char* name) const { |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1422 | IdentifierInfo* II = &CGM.getContext().Idents.get(name); |
| 1423 | return CGM.getContext().Selectors.getSelector(0, &II); |
| 1424 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1425 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1426 | Selector GetUnarySelector(const char* name) const { |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1427 | IdentifierInfo* II = &CGM.getContext().Idents.get(name); |
| 1428 | return CGM.getContext().Selectors.getSelector(1, &II); |
| 1429 | } |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1430 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1431 | /// ImplementationIsNonLazy - Check whether the given category or |
| 1432 | /// class implementation is "non-lazy". |
Fariborz Jahanian | ecfbdcb | 2009-05-21 01:03:45 +0000 | [diff] [blame] | 1433 | bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const; |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1434 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1435 | public: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1436 | CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1437 | // FIXME. All stubs for now! |
| 1438 | virtual llvm::Function *ModuleInitFunction(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1439 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1440 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1441 | ReturnValueSlot Return, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1442 | QualType ResultType, |
| 1443 | Selector Sel, |
| 1444 | llvm::Value *Receiver, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1445 | const CallArgList &CallArgs, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1446 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1447 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1448 | |
| 1449 | virtual CodeGen::RValue |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1450 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1451 | ReturnValueSlot Return, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1452 | QualType ResultType, |
| 1453 | Selector Sel, |
| 1454 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1455 | bool isCategoryImpl, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1456 | llvm::Value *Receiver, |
| 1457 | bool IsClassMessage, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1458 | const CallArgList &CallArgs, |
| 1459 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1460 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1461 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1462 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1463 | |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1464 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel, |
| 1465 | bool lvalue = false) |
| 1466 | { return EmitSelector(Builder, Sel, lvalue); } |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1467 | |
| 1468 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an |
| 1469 | /// untyped one. |
| 1470 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, |
| 1471 | const ObjCMethodDecl *Method) |
| 1472 | { return EmitSelector(Builder, Method->getSelector()); } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1473 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 1474 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1475 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1476 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
David Chisnall | 29254f4 | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 1477 | |
Daniel Dunbar | 85fdea0 | 2012-02-28 15:36:15 +0000 | [diff] [blame] | 1478 | virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {} |
David Chisnall | 29254f4 | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 1479 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1480 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 1481 | const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1482 | |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1483 | virtual llvm::Constant *GetEHType(QualType T); |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1484 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1485 | virtual llvm::Constant *GetPropertyGetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1486 | return ObjCTypes.getGetPropertyFn(); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1487 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1488 | virtual llvm::Constant *GetPropertySetFunction() { |
| 1489 | return ObjCTypes.getSetPropertyFn(); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1490 | } |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 1491 | |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1492 | virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic, |
| 1493 | bool copy) { |
| 1494 | return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy); |
| 1495 | } |
| 1496 | |
David Chisnall | 8fac25d | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 1497 | virtual llvm::Constant *GetSetStructFunction() { |
| 1498 | return ObjCTypes.getCopyStructFn(); |
| 1499 | } |
| 1500 | virtual llvm::Constant *GetGetStructFunction() { |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 1501 | return ObjCTypes.getCopyStructFn(); |
| 1502 | } |
David Chisnall | d397cfe | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 1503 | virtual llvm::Constant *GetCppAtomicObjectSetFunction() { |
| 1504 | return ObjCTypes.getCppAtomicObjectFunction(); |
| 1505 | } |
| 1506 | virtual llvm::Constant *GetCppAtomicObjectGetFunction() { |
Fariborz Jahanian | e317302 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 1507 | return ObjCTypes.getCppAtomicObjectFunction(); |
| 1508 | } |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 1509 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1510 | virtual llvm::Constant *EnumerationMutationFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1511 | return ObjCTypes.getEnumerationMutationFn(); |
Daniel Dunbar | 28ed084 | 2009-02-16 18:48:45 +0000 | [diff] [blame] | 1512 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1513 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1514 | virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
| 1515 | const ObjCAtTryStmt &S); |
| 1516 | virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1517 | const ObjCAtSynchronizedStmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1518 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6a3c70e | 2013-01-10 19:02:56 +0000 | [diff] [blame] | 1519 | const ObjCAtThrowStmt &S, |
| 1520 | bool ClearInsertionPoint=true); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1521 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1522 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1523 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1524 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1525 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 1526 | llvm::Value *src, llvm::Value *dest, |
| 1527 | bool threadlocal = false); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1528 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1529 | llvm::Value *src, llvm::Value *dest, |
| 1530 | llvm::Value *ivarOffset); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1531 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1532 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 1533 | virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
| 1534 | llvm::Value *dest, llvm::Value *src, |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 1535 | llvm::Value *size); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1536 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1537 | QualType ObjectTy, |
| 1538 | llvm::Value *BaseValue, |
| 1539 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1540 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1541 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1542 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1543 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1544 | }; |
Fariborz Jahanian | 4e1524b | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1545 | |
| 1546 | /// A helper class for performing the null-initialization of a return |
| 1547 | /// value. |
| 1548 | struct NullReturnState { |
| 1549 | llvm::BasicBlock *NullBB; |
| 1550 | llvm::BasicBlock *callBB; |
| 1551 | NullReturnState() : NullBB(0), callBB(0) {} |
| 1552 | |
| 1553 | void init(CodeGenFunction &CGF, llvm::Value *receiver) { |
| 1554 | // Make blocks for the null-init and call edges. |
| 1555 | NullBB = CGF.createBasicBlock("msgSend.nullinit"); |
| 1556 | callBB = CGF.createBasicBlock("msgSend.call"); |
| 1557 | |
| 1558 | // Check for a null receiver and, if there is one, jump to the |
| 1559 | // null-init test. |
| 1560 | llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver); |
| 1561 | CGF.Builder.CreateCondBr(isNull, NullBB, callBB); |
| 1562 | |
| 1563 | // Otherwise, start performing the call. |
| 1564 | CGF.EmitBlock(callBB); |
| 1565 | } |
| 1566 | |
Fariborz Jahanian | 3c267e6 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1567 | RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType, |
| 1568 | const CallArgList &CallArgs, |
| 1569 | const ObjCMethodDecl *Method) { |
Fariborz Jahanian | 4e1524b | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1570 | if (!NullBB) return result; |
Fariborz Jahanian | 3c267e6 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1571 | |
| 1572 | llvm::Value *NullInitPtr = 0; |
| 1573 | if (result.isScalar() && !resultType->isVoidType()) { |
| 1574 | NullInitPtr = CGF.CreateTempAlloca(result.getScalarVal()->getType()); |
| 1575 | CGF.Builder.CreateStore(result.getScalarVal(), NullInitPtr); |
| 1576 | } |
Fariborz Jahanian | 4e1524b | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1577 | |
| 1578 | // Finish the call path. |
| 1579 | llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont"); |
| 1580 | if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB); |
| 1581 | |
| 1582 | // Emit the null-init block and perform the null-initialization there. |
| 1583 | CGF.EmitBlock(NullBB); |
Fariborz Jahanian | 3c267e6 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1584 | |
| 1585 | // Release consumed arguments along the null-receiver path. |
| 1586 | if (Method) { |
| 1587 | CallArgList::const_iterator I = CallArgs.begin(); |
| 1588 | for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(), |
| 1589 | e = Method->param_end(); i != e; ++i, ++I) { |
| 1590 | const ParmVarDecl *ParamDecl = (*i); |
| 1591 | if (ParamDecl->hasAttr<NSConsumedAttr>()) { |
| 1592 | RValue RV = I->RV; |
| 1593 | assert(RV.isScalar() && |
| 1594 | "NullReturnState::complete - arg not on object"); |
| 1595 | CGF.EmitARCRelease(RV.getScalarVal(), true); |
| 1596 | } |
| 1597 | } |
| 1598 | } |
| 1599 | |
| 1600 | if (result.isScalar()) { |
| 1601 | if (NullInitPtr) |
| 1602 | CGF.EmitNullInitialization(NullInitPtr, resultType); |
| 1603 | // Jump to the continuation block. |
| 1604 | CGF.EmitBlock(contBB); |
| 1605 | return NullInitPtr ? RValue::get(CGF.Builder.CreateLoad(NullInitPtr)) |
| 1606 | : result; |
| 1607 | } |
| 1608 | |
Fariborz Jahanian | 4e1524b | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1609 | if (!resultType->isAnyComplexType()) { |
| 1610 | assert(result.isAggregate() && "null init of non-aggregate result?"); |
| 1611 | CGF.EmitNullInitialization(result.getAggregateAddr(), resultType); |
| 1612 | // Jump to the continuation block. |
| 1613 | CGF.EmitBlock(contBB); |
| 1614 | return result; |
| 1615 | } |
| 1616 | |
| 1617 | // _Complex type |
| 1618 | // FIXME. Now easy to handle any other scalar type whose result is returned |
| 1619 | // in memory due to ABI limitations. |
| 1620 | CGF.EmitBlock(contBB); |
| 1621 | CodeGenFunction::ComplexPairTy CallCV = result.getComplexVal(); |
| 1622 | llvm::Type *MemberType = CallCV.first->getType(); |
| 1623 | llvm::Constant *ZeroCV = llvm::Constant::getNullValue(MemberType); |
| 1624 | // Create phi instruction for scalar complex value. |
| 1625 | llvm::PHINode *PHIReal = CGF.Builder.CreatePHI(MemberType, 2); |
| 1626 | PHIReal->addIncoming(ZeroCV, NullBB); |
| 1627 | PHIReal->addIncoming(CallCV.first, callBB); |
| 1628 | llvm::PHINode *PHIImag = CGF.Builder.CreatePHI(MemberType, 2); |
| 1629 | PHIImag->addIncoming(ZeroCV, NullBB); |
| 1630 | PHIImag->addIncoming(CallCV.second, callBB); |
| 1631 | return RValue::getComplex(PHIReal, PHIImag); |
| 1632 | } |
| 1633 | }; |
| 1634 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1635 | } // end anonymous namespace |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1636 | |
| 1637 | /* *** Helper Functions *** */ |
| 1638 | |
| 1639 | /// getConstantGEP() - Help routine to construct simple GEPs. |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1640 | static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1641 | llvm::Constant *C, |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1642 | unsigned idx0, |
| 1643 | unsigned idx1) { |
| 1644 | llvm::Value *Idxs[] = { |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1645 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0), |
| 1646 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1647 | }; |
Jay Foad | a5c0434 | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 1648 | return llvm::ConstantExpr::getGetElementPtr(C, Idxs); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1651 | /// hasObjCExceptionAttribute - Return true if this class or any super |
| 1652 | /// class has the __objc_exception__ attribute. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1653 | static bool hasObjCExceptionAttribute(ASTContext &Context, |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 1654 | const ObjCInterfaceDecl *OID) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1655 | if (OID->hasAttr<ObjCExceptionAttr>()) |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1656 | return true; |
| 1657 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 1658 | return hasObjCExceptionAttribute(Context, Super); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1659 | return false; |
| 1660 | } |
| 1661 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1662 | /* *** CGObjCMac Public Interface *** */ |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1663 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1664 | CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1665 | ObjCTypes(cgm) { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1666 | ObjCABI = 1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1667 | EmitImageInfo(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 1670 | /// GetClass - Return a reference to the class for the given interface |
| 1671 | /// decl. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1672 | llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1673 | const ObjCInterfaceDecl *ID) { |
| 1674 | return EmitClassRef(Builder, ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
| 1677 | /// GetSelector - Return the pointer to the unique'd string for this selector. |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1678 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel, |
| 1679 | bool lval) { |
| 1680 | return EmitSelector(Builder, Sel, lval); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1681 | } |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1682 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1683 | *Method) { |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1684 | return EmitSelector(Builder, Method->getSelector()); |
| 1685 | } |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1686 | |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1687 | llvm::Constant *CGObjCMac::GetEHType(QualType T) { |
Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 1688 | if (T->isObjCIdType() || |
| 1689 | T->isObjCQualifiedIdType()) { |
| 1690 | return CGM.GetAddrOfRTTIDescriptor( |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 1691 | CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true); |
Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 1692 | } |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1693 | if (T->isObjCClassType() || |
| 1694 | T->isObjCQualifiedClassType()) { |
| 1695 | return CGM.GetAddrOfRTTIDescriptor( |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 1696 | CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true); |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1697 | } |
| 1698 | if (T->isObjCObjectPointerType()) |
| 1699 | return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true); |
| 1700 | |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1701 | llvm_unreachable("asking for catch type for ObjC type in fragile runtime"); |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1704 | /// Generate a constant CFString object. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1705 | /* |
| 1706 | struct __builtin_CFString { |
| 1707 | const int *isa; // point to __CFConstantStringClassReference |
| 1708 | int flags; |
| 1709 | const char *str; |
| 1710 | long length; |
| 1711 | }; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1712 | */ |
| 1713 | |
Fariborz Jahanian | 33e982b | 2010-04-22 20:26:39 +0000 | [diff] [blame] | 1714 | /// or Generate a constant NSString object. |
| 1715 | /* |
| 1716 | struct __builtin_NSString { |
| 1717 | const int *isa; // point to __NSConstantStringClassReference |
| 1718 | const char *str; |
| 1719 | unsigned int length; |
| 1720 | }; |
| 1721 | */ |
| 1722 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1723 | llvm::Constant *CGObjCCommonMac::GenerateConstantString( |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 1724 | const StringLiteral *SL) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1725 | return (CGM.getLangOpts().NoConstantCFStrings == 0 ? |
Fariborz Jahanian | 33e982b | 2010-04-22 20:26:39 +0000 | [diff] [blame] | 1726 | CGM.GetAddrOfConstantCFString(SL) : |
Fariborz Jahanian | 4c73307 | 2010-10-19 17:19:29 +0000 | [diff] [blame] | 1727 | CGM.GetAddrOfConstantString(SL)); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1728 | } |
| 1729 | |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1730 | enum { |
| 1731 | kCFTaggedObjectID_Integer = (1 << 1) + 1 |
| 1732 | }; |
| 1733 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1734 | /// Generates a message send where the super is the receiver. This is |
| 1735 | /// a message send to self with special delivery semantics indicating |
| 1736 | /// which class's method should be called. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1737 | CodeGen::RValue |
| 1738 | CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1739 | ReturnValueSlot Return, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1740 | QualType ResultType, |
| 1741 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1742 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1743 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1744 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1745 | bool IsClassMessage, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1746 | const CodeGen::CallArgList &CallArgs, |
| 1747 | const ObjCMethodDecl *Method) { |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1748 | // Create and init a super structure; this is a (receiver, class) |
| 1749 | // pair we will pass to objc_msgSendSuper. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1750 | llvm::Value *ObjCSuper = |
John McCall | 604da29 | 2011-03-04 08:00:29 +0000 | [diff] [blame] | 1751 | CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1752 | llvm::Value *ReceiverAsObject = |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1753 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1754 | CGF.Builder.CreateStore(ReceiverAsObject, |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1755 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1756 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1757 | // If this is a class message the metaclass is passed as the target. |
| 1758 | llvm::Value *Target; |
| 1759 | if (IsClassMessage) { |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1760 | if (isCategoryImpl) { |
| 1761 | // Message sent to 'super' in a class method defined in a category |
| 1762 | // implementation requires an odd treatment. |
| 1763 | // If we are in a class method, we must retrieve the |
| 1764 | // _metaclass_ for the current class, pointed at by |
| 1765 | // the class's "isa" pointer. The following assumes that |
| 1766 | // isa" is the first ivar in a class (which it must be). |
| 1767 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1768 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 1769 | Target = CGF.Builder.CreateLoad(Target); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 1770 | } else { |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1771 | llvm::Value *MetaClassPtr = EmitMetaClassRef(Class); |
| 1772 | llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1); |
| 1773 | llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr); |
| 1774 | Target = Super; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1775 | } |
Fariborz Jahanian | 182f268 | 2009-11-14 02:18:31 +0000 | [diff] [blame] | 1776 | } |
| 1777 | else if (isCategoryImpl) |
| 1778 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1779 | else { |
Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 1780 | llvm::Value *ClassPtr = EmitSuperClassRef(Class); |
| 1781 | ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1); |
| 1782 | Target = CGF.Builder.CreateLoad(ClassPtr); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1783 | } |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1784 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext and |
| 1785 | // ObjCTypes types. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1786 | llvm::Type *ClassTy = |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1787 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1788 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1789 | CGF.Builder.CreateStore(Target, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1790 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1791 | return EmitMessageSend(CGF, Return, ResultType, |
| 1792 | EmitSelector(CGF.Builder, Sel), |
| 1793 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 1794 | true, CallArgs, Method, ObjCTypes); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1795 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1796 | |
| 1797 | /// Generate code for a message send expression. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1798 | CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1799 | ReturnValueSlot Return, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1800 | QualType ResultType, |
| 1801 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1802 | llvm::Value *Receiver, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1803 | const CallArgList &CallArgs, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1804 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1805 | const ObjCMethodDecl *Method) { |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1806 | return EmitMessageSend(CGF, Return, ResultType, |
| 1807 | EmitSelector(CGF.Builder, Sel), |
| 1808 | Receiver, CGF.getContext().getObjCIdType(), |
| 1809 | false, CallArgs, Method, ObjCTypes); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1810 | } |
| 1811 | |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1812 | CodeGen::RValue |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1813 | CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1814 | ReturnValueSlot Return, |
| 1815 | QualType ResultType, |
| 1816 | llvm::Value *Sel, |
| 1817 | llvm::Value *Arg0, |
| 1818 | QualType Arg0Ty, |
| 1819 | bool IsSuper, |
| 1820 | const CallArgList &CallArgs, |
| 1821 | const ObjCMethodDecl *Method, |
| 1822 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1823 | CallArgList ActualArgs; |
Fariborz Jahanian | d019d96 | 2009-04-24 21:07:43 +0000 | [diff] [blame] | 1824 | if (!IsSuper) |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 1825 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy); |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1826 | ActualArgs.add(RValue::get(Arg0), Arg0Ty); |
| 1827 | ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1828 | ActualArgs.addFrom(CallArgs); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1829 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1830 | // If we're calling a method, use the formal signature. |
| 1831 | MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1832 | |
Anders Carlsson | 7e70fb2 | 2010-06-21 20:59:55 +0000 | [diff] [blame] | 1833 | if (Method) |
| 1834 | assert(CGM.getContext().getCanonicalType(Method->getResultType()) == |
| 1835 | CGM.getContext().getCanonicalType(ResultType) && |
| 1836 | "Result type mismatch!"); |
| 1837 | |
John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 1838 | NullReturnState nullReturn; |
| 1839 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1840 | llvm::Constant *Fn = NULL; |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1841 | if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) { |
Fariborz Jahanian | 4e1524b | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1842 | if (!IsSuper) nullReturn.init(CGF, Arg0); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1843 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1844 | : ObjCTypes.getSendStretFn(IsSuper); |
Daniel Dunbar | dacf9dd | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 1845 | } else if (CGM.ReturnTypeUsesFPRet(ResultType)) { |
| 1846 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper) |
| 1847 | : ObjCTypes.getSendFpretFn(IsSuper); |
Anders Carlsson | eea6480 | 2011-10-31 16:27:11 +0000 | [diff] [blame] | 1848 | } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) { |
| 1849 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper) |
| 1850 | : ObjCTypes.getSendFp2retFn(IsSuper); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1851 | } else { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1852 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1853 | : ObjCTypes.getSendFn(IsSuper); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1854 | } |
Fariborz Jahanian | 3c267e6 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1855 | |
| 1856 | bool requiresnullCheck = false; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1857 | if (CGM.getLangOpts().ObjCAutoRefCount && Method) |
Fariborz Jahanian | 3c267e6 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1858 | for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(), |
| 1859 | e = Method->param_end(); i != e; ++i) { |
| 1860 | const ParmVarDecl *ParamDecl = (*i); |
| 1861 | if (ParamDecl->hasAttr<NSConsumedAttr>()) { |
| 1862 | if (!nullReturn.NullBB) |
| 1863 | nullReturn.init(CGF, Arg0); |
| 1864 | requiresnullCheck = true; |
| 1865 | break; |
| 1866 | } |
| 1867 | } |
| 1868 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1869 | Fn = llvm::ConstantExpr::getBitCast(Fn, MSI.MessengerType); |
| 1870 | RValue rvalue = CGF.EmitCall(MSI.CallInfo, Fn, Return, ActualArgs); |
Fariborz Jahanian | 3c267e6 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1871 | return nullReturn.complete(CGF, rvalue, ResultType, CallArgs, |
| 1872 | requiresnullCheck ? Method : 0); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1873 | } |
| 1874 | |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1875 | static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) { |
| 1876 | if (FQT.isObjCGCStrong()) |
| 1877 | return Qualifiers::Strong; |
| 1878 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1879 | if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak) |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1880 | return Qualifiers::Weak; |
| 1881 | |
Fariborz Jahanian | ba83c95 | 2012-02-16 00:15:02 +0000 | [diff] [blame] | 1882 | // check for __unsafe_unretained |
| 1883 | if (FQT.getObjCLifetime() == Qualifiers::OCL_ExplicitNone) |
| 1884 | return Qualifiers::GCNone; |
| 1885 | |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1886 | if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType()) |
| 1887 | return Qualifiers::Strong; |
| 1888 | |
| 1889 | if (const PointerType *PT = FQT->getAs<PointerType>()) |
| 1890 | return GetGCAttrTypeForType(Ctx, PT->getPointeeType()); |
| 1891 | |
| 1892 | return Qualifiers::GCNone; |
| 1893 | } |
| 1894 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1895 | llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM, |
| 1896 | const CGBlockInfo &blockInfo) { |
Fariborz Jahanian | c46b435 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 1897 | |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1898 | llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1899 | if (CGM.getLangOpts().getGC() == LangOptions::NonGC && |
| 1900 | !CGM.getLangOpts().ObjCAutoRefCount) |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1901 | return nullPtr; |
| 1902 | |
Fariborz Jahanian | a1f024c | 2010-08-06 16:28:55 +0000 | [diff] [blame] | 1903 | bool hasUnion = false; |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1904 | SkipIvars.clear(); |
| 1905 | IvarsInfo.clear(); |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 1906 | unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0); |
| 1907 | unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth(); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1908 | |
Fariborz Jahanian | 8197982 | 2010-09-09 00:21:45 +0000 | [diff] [blame] | 1909 | // __isa is the first field in block descriptor and must assume by runtime's |
| 1910 | // convention that it is GC'able. |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 1911 | IvarsInfo.push_back(GC_IVAR(0, 1)); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1912 | |
| 1913 | const BlockDecl *blockDecl = blockInfo.getBlockDecl(); |
| 1914 | |
| 1915 | // Calculate the basic layout of the block structure. |
| 1916 | const llvm::StructLayout *layout = |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 1917 | CGM.getDataLayout().getStructLayout(blockInfo.StructureType); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1918 | |
| 1919 | // Ignore the optional 'this' capture: C++ objects are not assumed |
| 1920 | // to be GC'ed. |
| 1921 | |
| 1922 | // Walk the captured variables. |
| 1923 | for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(), |
| 1924 | ce = blockDecl->capture_end(); ci != ce; ++ci) { |
| 1925 | const VarDecl *variable = ci->getVariable(); |
| 1926 | QualType type = variable->getType(); |
| 1927 | |
| 1928 | const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); |
| 1929 | |
| 1930 | // Ignore constant captures. |
| 1931 | if (capture.isConstant()) continue; |
| 1932 | |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 1933 | uint64_t fieldOffset = layout->getElementOffset(capture.getIndex()); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1934 | |
| 1935 | // __block variables are passed by their descriptor address. |
| 1936 | if (ci->isByRef()) { |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 1937 | IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1)); |
Fariborz Jahanian | c5904b4 | 2010-09-11 01:27:29 +0000 | [diff] [blame] | 1938 | continue; |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1939 | } |
| 1940 | |
| 1941 | assert(!type->isArrayType() && "array variable should not be caught"); |
| 1942 | if (const RecordType *record = type->getAs<RecordType>()) { |
| 1943 | BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion); |
Fariborz Jahanian | e1a4898 | 2010-08-05 21:00:25 +0000 | [diff] [blame] | 1944 | continue; |
| 1945 | } |
Fariborz Jahanian | a1f024c | 2010-08-06 16:28:55 +0000 | [diff] [blame] | 1946 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1947 | Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type); |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 1948 | unsigned fieldSize = CGM.getContext().getTypeSize(type); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1949 | |
| 1950 | if (GCAttr == Qualifiers::Strong) |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 1951 | IvarsInfo.push_back(GC_IVAR(fieldOffset, |
| 1952 | fieldSize / WordSizeInBits)); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1953 | else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak) |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 1954 | SkipIvars.push_back(GC_IVAR(fieldOffset, |
| 1955 | fieldSize / ByteSizeInBits)); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1956 | } |
| 1957 | |
| 1958 | if (IvarsInfo.empty()) |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1959 | return nullPtr; |
| 1960 | |
| 1961 | // Sort on byte position; captures might not be allocated in order, |
| 1962 | // and unions can do funny things. |
| 1963 | llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end()); |
| 1964 | llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end()); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1965 | |
| 1966 | std::string BitMap; |
Fariborz Jahanian | b8fd2eb | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 1967 | llvm::Constant *C = BuildIvarLayoutBitmap(BitMap); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1968 | if (CGM.getLangOpts().ObjCGCBitmapPrint) { |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1969 | printf("\n block variable layout for block: "); |
Roman Divacky | 31ba613 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 1970 | const unsigned char *s = (const unsigned char*)BitMap.c_str(); |
Bill Wendling | 13562a1 | 2012-02-07 09:06:01 +0000 | [diff] [blame] | 1971 | for (unsigned i = 0, e = BitMap.size(); i < e; i++) |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1972 | if (!(s[i] & 0xf0)) |
| 1973 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 1974 | else |
| 1975 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 1976 | printf("\n"); |
| 1977 | } |
| 1978 | |
| 1979 | return C; |
Fariborz Jahanian | 89ecd41 | 2010-08-04 16:57:49 +0000 | [diff] [blame] | 1980 | } |
| 1981 | |
Fariborz Jahanian | c441cd3 | 2012-11-04 18:19:40 +0000 | [diff] [blame] | 1982 | /// getBlockCaptureLifetime - This routine returns life time of the captured |
| 1983 | /// block variable for the purpose of block layout meta-data generation. FQT is |
| 1984 | /// the type of the variable captured in the block. |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 1985 | Qualifiers::ObjCLifetime CGObjCCommonMac::getBlockCaptureLifetime(QualType FQT, |
| 1986 | bool ByrefLayout) { |
Fariborz Jahanian | 44fcff9 | 2012-11-02 22:51:18 +0000 | [diff] [blame] | 1987 | if (CGM.getLangOpts().ObjCAutoRefCount) |
| 1988 | return FQT.getObjCLifetime(); |
| 1989 | |
Fariborz Jahanian | c441cd3 | 2012-11-04 18:19:40 +0000 | [diff] [blame] | 1990 | // MRR. |
Fariborz Jahanian | 44fcff9 | 2012-11-02 22:51:18 +0000 | [diff] [blame] | 1991 | if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType()) |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 1992 | return ByrefLayout ? Qualifiers::OCL_ExplicitNone : Qualifiers::OCL_Strong; |
Fariborz Jahanian | 44fcff9 | 2012-11-02 22:51:18 +0000 | [diff] [blame] | 1993 | |
| 1994 | return Qualifiers::OCL_None; |
| 1995 | } |
| 1996 | |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 1997 | void CGObjCCommonMac::UpdateRunSkipBlockVars(bool IsByref, |
| 1998 | Qualifiers::ObjCLifetime LifeTime, |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 1999 | CharUnits FieldOffset, |
| 2000 | CharUnits FieldSize) { |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2001 | // __block variables are passed by their descriptor address. |
| 2002 | if (IsByref) |
| 2003 | RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_BYREF, FieldOffset, |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2004 | FieldSize)); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2005 | else if (LifeTime == Qualifiers::OCL_Strong) |
| 2006 | RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_STRONG, FieldOffset, |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2007 | FieldSize)); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2008 | else if (LifeTime == Qualifiers::OCL_Weak) |
| 2009 | RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_WEAK, FieldOffset, |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2010 | FieldSize)); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2011 | else if (LifeTime == Qualifiers::OCL_ExplicitNone) |
| 2012 | RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_UNRETAINED, FieldOffset, |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2013 | FieldSize)); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2014 | else |
| 2015 | RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_NON_OBJECT_BYTES, |
| 2016 | FieldOffset, |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2017 | FieldSize)); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2018 | } |
| 2019 | |
| 2020 | void CGObjCCommonMac::BuildRCRecordLayout(const llvm::StructLayout *RecLayout, |
| 2021 | const RecordDecl *RD, |
| 2022 | ArrayRef<const FieldDecl*> RecFields, |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2023 | CharUnits BytePos, bool &HasUnion, |
| 2024 | bool ByrefLayout) { |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2025 | bool IsUnion = (RD && RD->isUnion()); |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2026 | CharUnits MaxUnionSize = CharUnits::Zero(); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2027 | const FieldDecl *MaxField = 0; |
| 2028 | const FieldDecl *LastFieldBitfieldOrUnnamed = 0; |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2029 | CharUnits MaxFieldOffset = CharUnits::Zero(); |
| 2030 | CharUnits LastBitfieldOrUnnamedOffset = CharUnits::Zero(); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2031 | |
| 2032 | if (RecFields.empty()) |
| 2033 | return; |
| 2034 | unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth(); |
| 2035 | |
| 2036 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
| 2037 | const FieldDecl *Field = RecFields[i]; |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2038 | // Note that 'i' here is actually the field index inside RD of Field, |
| 2039 | // although this dependency is hidden. |
| 2040 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2041 | CharUnits FieldOffset = |
| 2042 | CGM.getContext().toCharUnitsFromBits(RL.getFieldOffset(i)); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2043 | |
| 2044 | // Skip over unnamed or bitfields |
| 2045 | if (!Field->getIdentifier() || Field->isBitField()) { |
| 2046 | LastFieldBitfieldOrUnnamed = Field; |
| 2047 | LastBitfieldOrUnnamedOffset = FieldOffset; |
| 2048 | continue; |
| 2049 | } |
| 2050 | |
| 2051 | LastFieldBitfieldOrUnnamed = 0; |
| 2052 | QualType FQT = Field->getType(); |
| 2053 | if (FQT->isRecordType() || FQT->isUnionType()) { |
| 2054 | if (FQT->isUnionType()) |
| 2055 | HasUnion = true; |
| 2056 | |
| 2057 | BuildRCBlockVarRecordLayout(FQT->getAs<RecordType>(), |
| 2058 | BytePos + FieldOffset, HasUnion); |
| 2059 | continue; |
| 2060 | } |
| 2061 | |
| 2062 | if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 2063 | const ConstantArrayType *CArray = |
| 2064 | dyn_cast_or_null<ConstantArrayType>(Array); |
| 2065 | uint64_t ElCount = CArray->getSize().getZExtValue(); |
| 2066 | assert(CArray && "only array with known element size is supported"); |
| 2067 | FQT = CArray->getElementType(); |
| 2068 | while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 2069 | const ConstantArrayType *CArray = |
| 2070 | dyn_cast_or_null<ConstantArrayType>(Array); |
| 2071 | ElCount *= CArray->getSize().getZExtValue(); |
| 2072 | FQT = CArray->getElementType(); |
| 2073 | } |
| 2074 | |
| 2075 | assert(!FQT->isUnionType() && |
| 2076 | "layout for array of unions not supported"); |
| 2077 | if (FQT->isRecordType() && ElCount) { |
| 2078 | int OldIndex = RunSkipBlockVars.size() - 1; |
| 2079 | const RecordType *RT = FQT->getAs<RecordType>(); |
| 2080 | BuildRCBlockVarRecordLayout(RT, BytePos + FieldOffset, |
| 2081 | HasUnion); |
| 2082 | |
| 2083 | // Replicate layout information for each array element. Note that |
| 2084 | // one element is already done. |
| 2085 | uint64_t ElIx = 1; |
| 2086 | for (int FirstIndex = RunSkipBlockVars.size() - 1 ;ElIx < ElCount; ElIx++) { |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2087 | CharUnits Size = CGM.getContext().getTypeSizeInChars(RT); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2088 | for (int i = OldIndex+1; i <= FirstIndex; ++i) |
| 2089 | RunSkipBlockVars.push_back( |
| 2090 | RUN_SKIP(RunSkipBlockVars[i].opcode, |
| 2091 | RunSkipBlockVars[i].block_var_bytepos + Size*ElIx, |
| 2092 | RunSkipBlockVars[i].block_var_size)); |
| 2093 | } |
| 2094 | continue; |
| 2095 | } |
| 2096 | } |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2097 | CharUnits FieldSize = CGM.getContext().getTypeSizeInChars(Field->getType()); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2098 | if (IsUnion) { |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2099 | CharUnits UnionIvarSize = FieldSize; |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2100 | if (UnionIvarSize > MaxUnionSize) { |
| 2101 | MaxUnionSize = UnionIvarSize; |
| 2102 | MaxField = Field; |
| 2103 | MaxFieldOffset = FieldOffset; |
| 2104 | } |
| 2105 | } else { |
| 2106 | UpdateRunSkipBlockVars(false, |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2107 | getBlockCaptureLifetime(FQT, ByrefLayout), |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2108 | BytePos + FieldOffset, |
| 2109 | FieldSize); |
| 2110 | } |
| 2111 | } |
| 2112 | |
| 2113 | if (LastFieldBitfieldOrUnnamed) { |
| 2114 | if (LastFieldBitfieldOrUnnamed->isBitField()) { |
| 2115 | // Last field was a bitfield. Must update the info. |
| 2116 | uint64_t BitFieldSize |
| 2117 | = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext()); |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2118 | unsigned UnsSize = (BitFieldSize / ByteSizeInBits) + |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 2119 | ((BitFieldSize % ByteSizeInBits) != 0); |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2120 | CharUnits Size = CharUnits::fromQuantity(UnsSize); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2121 | Size += LastBitfieldOrUnnamedOffset; |
| 2122 | UpdateRunSkipBlockVars(false, |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2123 | getBlockCaptureLifetime(LastFieldBitfieldOrUnnamed->getType(), |
| 2124 | ByrefLayout), |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2125 | BytePos + LastBitfieldOrUnnamedOffset, |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2126 | Size); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2127 | } else { |
| 2128 | assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed"); |
| 2129 | // Last field was unnamed. Must update skip info. |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2130 | CharUnits FieldSize |
| 2131 | = CGM.getContext().getTypeSizeInChars(LastFieldBitfieldOrUnnamed->getType()); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2132 | UpdateRunSkipBlockVars(false, |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2133 | getBlockCaptureLifetime(LastFieldBitfieldOrUnnamed->getType(), |
| 2134 | ByrefLayout), |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2135 | BytePos + LastBitfieldOrUnnamedOffset, |
| 2136 | FieldSize); |
| 2137 | } |
| 2138 | } |
| 2139 | |
| 2140 | if (MaxField) |
| 2141 | UpdateRunSkipBlockVars(false, |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2142 | getBlockCaptureLifetime(MaxField->getType(), ByrefLayout), |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2143 | BytePos + MaxFieldOffset, |
| 2144 | MaxUnionSize); |
| 2145 | } |
| 2146 | |
| 2147 | void CGObjCCommonMac::BuildRCBlockVarRecordLayout(const RecordType *RT, |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2148 | CharUnits BytePos, |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2149 | bool &HasUnion, |
| 2150 | bool ByrefLayout) { |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2151 | const RecordDecl *RD = RT->getDecl(); |
| 2152 | SmallVector<const FieldDecl*, 16> Fields; |
| 2153 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 2154 | e = RD->field_end(); i != e; ++i) |
| 2155 | Fields.push_back(*i); |
| 2156 | llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0)); |
| 2157 | const llvm::StructLayout *RecLayout = |
| 2158 | CGM.getDataLayout().getStructLayout(cast<llvm::StructType>(Ty)); |
| 2159 | |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2160 | BuildRCRecordLayout(RecLayout, RD, Fields, BytePos, HasUnion, ByrefLayout); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2161 | } |
| 2162 | |
Fariborz Jahanian | f22ae65 | 2012-11-01 18:32:55 +0000 | [diff] [blame] | 2163 | /// InlineLayoutInstruction - This routine produce an inline instruction for the |
| 2164 | /// block variable layout if it can. If not, it returns 0. Rules are as follow: |
| 2165 | /// If ((uintptr_t) layout) < (1 << 12), the layout is inline. In the 64bit world, |
| 2166 | /// an inline layout of value 0x0000000000000xyz is interpreted as follows: |
| 2167 | /// x captured object pointers of BLOCK_LAYOUT_STRONG. Followed by |
| 2168 | /// y captured object of BLOCK_LAYOUT_BYREF. Followed by |
| 2169 | /// z captured object of BLOCK_LAYOUT_WEAK. If any of the above is missing, zero |
| 2170 | /// replaces it. For example, 0x00000x00 means x BLOCK_LAYOUT_STRONG and no |
| 2171 | /// BLOCK_LAYOUT_BYREF and no BLOCK_LAYOUT_WEAK objects are captured. |
| 2172 | uint64_t CGObjCCommonMac::InlineLayoutInstruction( |
| 2173 | SmallVectorImpl<unsigned char> &Layout) { |
| 2174 | uint64_t Result = 0; |
| 2175 | if (Layout.size() <= 3) { |
| 2176 | unsigned size = Layout.size(); |
| 2177 | unsigned strong_word_count = 0, byref_word_count=0, weak_word_count=0; |
| 2178 | unsigned char inst; |
| 2179 | enum BLOCK_LAYOUT_OPCODE opcode ; |
| 2180 | switch (size) { |
| 2181 | case 3: |
| 2182 | inst = Layout[0]; |
| 2183 | opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2184 | if (opcode == BLOCK_LAYOUT_STRONG) |
| 2185 | strong_word_count = (inst & 0xF)+1; |
| 2186 | else |
| 2187 | return 0; |
| 2188 | inst = Layout[1]; |
| 2189 | opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2190 | if (opcode == BLOCK_LAYOUT_BYREF) |
| 2191 | byref_word_count = (inst & 0xF)+1; |
| 2192 | else |
| 2193 | return 0; |
| 2194 | inst = Layout[2]; |
| 2195 | opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2196 | if (opcode == BLOCK_LAYOUT_WEAK) |
| 2197 | weak_word_count = (inst & 0xF)+1; |
| 2198 | else |
| 2199 | return 0; |
| 2200 | break; |
| 2201 | |
| 2202 | case 2: |
| 2203 | inst = Layout[0]; |
| 2204 | opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2205 | if (opcode == BLOCK_LAYOUT_STRONG) { |
| 2206 | strong_word_count = (inst & 0xF)+1; |
| 2207 | inst = Layout[1]; |
| 2208 | opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2209 | if (opcode == BLOCK_LAYOUT_BYREF) |
| 2210 | byref_word_count = (inst & 0xF)+1; |
| 2211 | else if (opcode == BLOCK_LAYOUT_WEAK) |
| 2212 | weak_word_count = (inst & 0xF)+1; |
| 2213 | else |
| 2214 | return 0; |
| 2215 | } |
| 2216 | else if (opcode == BLOCK_LAYOUT_BYREF) { |
| 2217 | byref_word_count = (inst & 0xF)+1; |
| 2218 | inst = Layout[1]; |
| 2219 | opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2220 | if (opcode == BLOCK_LAYOUT_WEAK) |
| 2221 | weak_word_count = (inst & 0xF)+1; |
| 2222 | else |
| 2223 | return 0; |
| 2224 | } |
| 2225 | else |
| 2226 | return 0; |
| 2227 | break; |
| 2228 | |
| 2229 | case 1: |
| 2230 | inst = Layout[0]; |
| 2231 | opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2232 | if (opcode == BLOCK_LAYOUT_STRONG) |
| 2233 | strong_word_count = (inst & 0xF)+1; |
| 2234 | else if (opcode == BLOCK_LAYOUT_BYREF) |
| 2235 | byref_word_count = (inst & 0xF)+1; |
| 2236 | else if (opcode == BLOCK_LAYOUT_WEAK) |
| 2237 | weak_word_count = (inst & 0xF)+1; |
| 2238 | else |
| 2239 | return 0; |
| 2240 | break; |
| 2241 | |
| 2242 | default: |
| 2243 | return 0; |
| 2244 | } |
| 2245 | |
| 2246 | // Cannot inline when any of the word counts is 15. Because this is one less |
| 2247 | // than the actual work count (so 15 means 16 actual word counts), |
| 2248 | // and we can only display 0 thru 15 word counts. |
| 2249 | if (strong_word_count == 16 || byref_word_count == 16 || weak_word_count == 16) |
| 2250 | return 0; |
| 2251 | |
| 2252 | unsigned count = |
| 2253 | (strong_word_count != 0) + (byref_word_count != 0) + (weak_word_count != 0); |
| 2254 | |
| 2255 | if (size == count) { |
| 2256 | if (strong_word_count) |
| 2257 | Result = strong_word_count; |
| 2258 | Result <<= 4; |
| 2259 | if (byref_word_count) |
| 2260 | Result += byref_word_count; |
| 2261 | Result <<= 4; |
| 2262 | if (weak_word_count) |
| 2263 | Result += weak_word_count; |
| 2264 | } |
| 2265 | } |
| 2266 | return Result; |
| 2267 | } |
| 2268 | |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2269 | llvm::Constant *CGObjCCommonMac::getBitmapBlockLayout(bool ComputeByrefLayout) { |
| 2270 | llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy); |
| 2271 | if (RunSkipBlockVars.empty()) |
| 2272 | return nullPtr; |
| 2273 | unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0); |
| 2274 | unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth(); |
| 2275 | unsigned WordSizeInBytes = WordSizeInBits/ByteSizeInBits; |
| 2276 | |
| 2277 | // Sort on byte position; captures might not be allocated in order, |
| 2278 | // and unions can do funny things. |
| 2279 | llvm::array_pod_sort(RunSkipBlockVars.begin(), RunSkipBlockVars.end()); |
| 2280 | SmallVector<unsigned char, 16> Layout; |
| 2281 | |
| 2282 | unsigned size = RunSkipBlockVars.size(); |
| 2283 | for (unsigned i = 0; i < size; i++) { |
| 2284 | enum BLOCK_LAYOUT_OPCODE opcode = RunSkipBlockVars[i].opcode; |
| 2285 | CharUnits start_byte_pos = RunSkipBlockVars[i].block_var_bytepos; |
| 2286 | CharUnits end_byte_pos = start_byte_pos; |
| 2287 | unsigned j = i+1; |
| 2288 | while (j < size) { |
| 2289 | if (opcode == RunSkipBlockVars[j].opcode) { |
| 2290 | end_byte_pos = RunSkipBlockVars[j++].block_var_bytepos; |
| 2291 | i++; |
| 2292 | } |
| 2293 | else |
| 2294 | break; |
| 2295 | } |
| 2296 | CharUnits size_in_bytes = |
| 2297 | end_byte_pos - start_byte_pos + RunSkipBlockVars[j-1].block_var_size; |
| 2298 | if (j < size) { |
| 2299 | CharUnits gap = |
| 2300 | RunSkipBlockVars[j].block_var_bytepos - |
| 2301 | RunSkipBlockVars[j-1].block_var_bytepos - RunSkipBlockVars[j-1].block_var_size; |
| 2302 | size_in_bytes += gap; |
| 2303 | } |
| 2304 | CharUnits residue_in_bytes = CharUnits::Zero(); |
| 2305 | if (opcode == BLOCK_LAYOUT_NON_OBJECT_BYTES) { |
| 2306 | residue_in_bytes = size_in_bytes % WordSizeInBytes; |
| 2307 | size_in_bytes -= residue_in_bytes; |
| 2308 | opcode = BLOCK_LAYOUT_NON_OBJECT_WORDS; |
| 2309 | } |
| 2310 | |
| 2311 | unsigned size_in_words = size_in_bytes.getQuantity() / WordSizeInBytes; |
| 2312 | while (size_in_words >= 16) { |
| 2313 | // Note that value in imm. is one less that the actual |
| 2314 | // value. So, 0xf means 16 words follow! |
| 2315 | unsigned char inst = (opcode << 4) | 0xf; |
| 2316 | Layout.push_back(inst); |
| 2317 | size_in_words -= 16; |
| 2318 | } |
| 2319 | if (size_in_words > 0) { |
| 2320 | // Note that value in imm. is one less that the actual |
| 2321 | // value. So, we subtract 1 away! |
| 2322 | unsigned char inst = (opcode << 4) | (size_in_words-1); |
| 2323 | Layout.push_back(inst); |
| 2324 | } |
| 2325 | if (residue_in_bytes > CharUnits::Zero()) { |
| 2326 | unsigned char inst = |
| 2327 | (BLOCK_LAYOUT_NON_OBJECT_BYTES << 4) | (residue_in_bytes.getQuantity()-1); |
| 2328 | Layout.push_back(inst); |
| 2329 | } |
| 2330 | } |
| 2331 | |
| 2332 | int e = Layout.size()-1; |
| 2333 | while (e >= 0) { |
| 2334 | unsigned char inst = Layout[e--]; |
| 2335 | enum BLOCK_LAYOUT_OPCODE opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2336 | if (opcode == BLOCK_LAYOUT_NON_OBJECT_BYTES || opcode == BLOCK_LAYOUT_NON_OBJECT_WORDS) |
| 2337 | Layout.pop_back(); |
| 2338 | else |
| 2339 | break; |
| 2340 | } |
| 2341 | |
| 2342 | uint64_t Result = InlineLayoutInstruction(Layout); |
| 2343 | if (Result != 0) { |
| 2344 | // Block variable layout instruction has been inlined. |
| 2345 | if (CGM.getLangOpts().ObjCGCBitmapPrint) { |
| 2346 | if (ComputeByrefLayout) |
| 2347 | printf("\n Inline instruction for BYREF variable layout: "); |
| 2348 | else |
| 2349 | printf("\n Inline instruction for block variable layout: "); |
| 2350 | printf("0x0%llx\n", (unsigned long long)Result); |
| 2351 | } |
| 2352 | if (WordSizeInBytes == 8) { |
| 2353 | const llvm::APInt Instruction(64, Result); |
| 2354 | return llvm::Constant::getIntegerValue(CGM.Int64Ty, Instruction); |
| 2355 | } |
| 2356 | else { |
| 2357 | const llvm::APInt Instruction(32, Result); |
| 2358 | return llvm::Constant::getIntegerValue(CGM.Int32Ty, Instruction); |
| 2359 | } |
| 2360 | } |
| 2361 | |
| 2362 | unsigned char inst = (BLOCK_LAYOUT_OPERATOR << 4) | 0; |
| 2363 | Layout.push_back(inst); |
| 2364 | std::string BitMap; |
| 2365 | for (unsigned i = 0, e = Layout.size(); i != e; i++) |
| 2366 | BitMap += Layout[i]; |
| 2367 | |
| 2368 | if (CGM.getLangOpts().ObjCGCBitmapPrint) { |
| 2369 | if (ComputeByrefLayout) |
| 2370 | printf("\n BYREF variable layout: "); |
| 2371 | else |
| 2372 | printf("\n block variable layout: "); |
| 2373 | for (unsigned i = 0, e = BitMap.size(); i != e; i++) { |
| 2374 | unsigned char inst = BitMap[i]; |
| 2375 | enum BLOCK_LAYOUT_OPCODE opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2376 | unsigned delta = 1; |
| 2377 | switch (opcode) { |
| 2378 | case BLOCK_LAYOUT_OPERATOR: |
| 2379 | printf("BL_OPERATOR:"); |
| 2380 | delta = 0; |
| 2381 | break; |
| 2382 | case BLOCK_LAYOUT_NON_OBJECT_BYTES: |
| 2383 | printf("BL_NON_OBJECT_BYTES:"); |
| 2384 | break; |
| 2385 | case BLOCK_LAYOUT_NON_OBJECT_WORDS: |
| 2386 | printf("BL_NON_OBJECT_WORD:"); |
| 2387 | break; |
| 2388 | case BLOCK_LAYOUT_STRONG: |
| 2389 | printf("BL_STRONG:"); |
| 2390 | break; |
| 2391 | case BLOCK_LAYOUT_BYREF: |
| 2392 | printf("BL_BYREF:"); |
| 2393 | break; |
| 2394 | case BLOCK_LAYOUT_WEAK: |
| 2395 | printf("BL_WEAK:"); |
| 2396 | break; |
| 2397 | case BLOCK_LAYOUT_UNRETAINED: |
| 2398 | printf("BL_UNRETAINED:"); |
| 2399 | break; |
| 2400 | } |
| 2401 | // Actual value of word count is one more that what is in the imm. |
| 2402 | // field of the instruction |
| 2403 | printf("%d", (inst & 0xf) + delta); |
| 2404 | if (i < e-1) |
| 2405 | printf(", "); |
| 2406 | else |
| 2407 | printf("\n"); |
| 2408 | } |
| 2409 | } |
| 2410 | |
| 2411 | llvm::GlobalVariable * Entry = |
| 2412 | CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
| 2413 | llvm::ConstantDataArray::getString(VMContext, BitMap,false), |
| 2414 | "__TEXT,__objc_classname,cstring_literals", 1, true); |
| 2415 | return getConstantGEP(VMContext, Entry, 0, 0); |
| 2416 | } |
| 2417 | |
Fariborz Jahanian | c46b435 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2418 | llvm::Constant *CGObjCCommonMac::BuildRCBlockLayout(CodeGenModule &CGM, |
| 2419 | const CGBlockInfo &blockInfo) { |
Fariborz Jahanian | c46b435 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2420 | assert(CGM.getLangOpts().getGC() == LangOptions::NonGC); |
| 2421 | |
Fariborz Jahanian | c46b435 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2422 | RunSkipBlockVars.clear(); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2423 | bool hasUnion = false; |
| 2424 | |
Fariborz Jahanian | c46b435 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2425 | unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0); |
| 2426 | unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth(); |
| 2427 | unsigned WordSizeInBytes = WordSizeInBits/ByteSizeInBits; |
| 2428 | |
| 2429 | const BlockDecl *blockDecl = blockInfo.getBlockDecl(); |
| 2430 | |
| 2431 | // Calculate the basic layout of the block structure. |
| 2432 | const llvm::StructLayout *layout = |
| 2433 | CGM.getDataLayout().getStructLayout(blockInfo.StructureType); |
| 2434 | |
| 2435 | // Ignore the optional 'this' capture: C++ objects are not assumed |
| 2436 | // to be GC'ed. |
Fariborz Jahanian | ff685c5 | 2012-12-04 17:20:57 +0000 | [diff] [blame] | 2437 | if (blockInfo.BlockHeaderForcedGapSize != CharUnits::Zero()) |
| 2438 | UpdateRunSkipBlockVars(false, Qualifiers::OCL_None, |
| 2439 | blockInfo.BlockHeaderForcedGapOffset, |
| 2440 | blockInfo.BlockHeaderForcedGapSize); |
Fariborz Jahanian | c46b435 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2441 | // Walk the captured variables. |
| 2442 | for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(), |
| 2443 | ce = blockDecl->capture_end(); ci != ce; ++ci) { |
| 2444 | const VarDecl *variable = ci->getVariable(); |
| 2445 | QualType type = variable->getType(); |
| 2446 | |
| 2447 | const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); |
| 2448 | |
| 2449 | // Ignore constant captures. |
| 2450 | if (capture.isConstant()) continue; |
| 2451 | |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2452 | CharUnits fieldOffset = |
| 2453 | CharUnits::fromQuantity(layout->getElementOffset(capture.getIndex())); |
Fariborz Jahanian | c46b435 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2454 | |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2455 | assert(!type->isArrayType() && "array variable should not be caught"); |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2456 | if (!ci->isByRef()) |
| 2457 | if (const RecordType *record = type->getAs<RecordType>()) { |
| 2458 | BuildRCBlockVarRecordLayout(record, fieldOffset, hasUnion); |
| 2459 | continue; |
| 2460 | } |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2461 | CharUnits fieldSize; |
| 2462 | if (ci->isByRef()) |
| 2463 | fieldSize = CharUnits::fromQuantity(WordSizeInBytes); |
| 2464 | else |
| 2465 | fieldSize = CGM.getContext().getTypeSizeInChars(type); |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2466 | UpdateRunSkipBlockVars(ci->isByRef(), getBlockCaptureLifetime(type, false), |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2467 | fieldOffset, fieldSize); |
Fariborz Jahanian | c46b435 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2468 | } |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2469 | return getBitmapBlockLayout(false); |
| 2470 | } |
Fariborz Jahanian | c46b435 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2471 | |
Fariborz Jahanian | c46b435 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2472 | |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2473 | llvm::Constant *CGObjCCommonMac::BuildByrefLayout(CodeGen::CodeGenModule &CGM, |
| 2474 | QualType T) { |
| 2475 | assert(CGM.getLangOpts().getGC() == LangOptions::NonGC); |
| 2476 | assert(!T->isArrayType() && "__block array variable should not be caught"); |
| 2477 | CharUnits fieldOffset; |
| 2478 | RunSkipBlockVars.clear(); |
| 2479 | bool hasUnion = false; |
| 2480 | if (const RecordType *record = T->getAs<RecordType>()) { |
| 2481 | BuildRCBlockVarRecordLayout(record, fieldOffset, hasUnion, true /*ByrefLayout */); |
| 2482 | llvm::Constant *Result = getBitmapBlockLayout(true); |
| 2483 | return Result; |
Fariborz Jahanian | c46b435 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2484 | } |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2485 | llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy); |
| 2486 | return nullPtr; |
Fariborz Jahanian | c46b435 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2487 | } |
| 2488 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2489 | llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 2490 | const ObjCProtocolDecl *PD) { |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 2491 | // FIXME: I don't understand why gcc generates this, or where it is |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2492 | // resolved. Investigate. Its also wasteful to look this up over and over. |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 2493 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 2494 | |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2495 | return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), |
Douglas Gregor | 4c86fdb | 2012-01-17 18:36:30 +0000 | [diff] [blame] | 2496 | ObjCTypes.getExternalProtocolPtrTy()); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2497 | } |
| 2498 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 2499 | void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2500 | // FIXME: We shouldn't need this, the protocol decl should contain enough |
| 2501 | // information to tell us whether this was a declaration or a definition. |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2502 | DefinedProtocols.insert(PD->getIdentifier()); |
| 2503 | |
| 2504 | // If we have generated a forward reference to this protocol, emit |
| 2505 | // it now. Otherwise do nothing, the protocol objects are lazily |
| 2506 | // emitted. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2507 | if (Protocols.count(PD->getIdentifier())) |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2508 | GetOrEmitProtocol(PD); |
| 2509 | } |
| 2510 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 2511 | llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2512 | if (DefinedProtocols.count(PD->getIdentifier())) |
| 2513 | return GetOrEmitProtocol(PD); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 2514 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2515 | return GetOrEmitProtocolRef(PD); |
| 2516 | } |
| 2517 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2518 | /* |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2519 | // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions |
| 2520 | struct _objc_protocol { |
| 2521 | struct _objc_protocol_extension *isa; |
| 2522 | char *protocol_name; |
| 2523 | struct _objc_protocol_list *protocol_list; |
| 2524 | struct _objc__method_prototype_list *instance_methods; |
| 2525 | struct _objc__method_prototype_list *class_methods |
| 2526 | }; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2527 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2528 | See EmitProtocolExtension(). |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2529 | */ |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2530 | llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) { |
John McCall | 50651b9 | 2012-03-30 21:29:05 +0000 | [diff] [blame] | 2531 | llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()]; |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2532 | |
| 2533 | // Early exit if a defining object has already been generated. |
| 2534 | if (Entry && Entry->hasInitializer()) |
| 2535 | return Entry; |
| 2536 | |
Douglas Gregor | 1d784b2 | 2012-01-01 19:51:50 +0000 | [diff] [blame] | 2537 | // Use the protocol definition, if there is one. |
| 2538 | if (const ObjCProtocolDecl *Def = PD->getDefinition()) |
| 2539 | PD = Def; |
| 2540 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2541 | // FIXME: I don't understand why gcc generates this, or where it is |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2542 | // resolved. Investigate. Its also wasteful to look this up over and over. |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2543 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 2544 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2545 | // Construct method lists. |
| 2546 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 2547 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2548 | std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2549 | for (ObjCProtocolDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2550 | i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2551 | ObjCMethodDecl *MD = *i; |
| 2552 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 2553 | if (!C) |
| 2554 | return GetOrEmitProtocolRef(PD); |
| 2555 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2556 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 2557 | OptInstanceMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2558 | OptMethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2559 | } else { |
| 2560 | InstanceMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2561 | MethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2562 | } |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2563 | } |
| 2564 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2565 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2566 | i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2567 | ObjCMethodDecl *MD = *i; |
| 2568 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 2569 | if (!C) |
| 2570 | return GetOrEmitProtocolRef(PD); |
| 2571 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2572 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 2573 | OptClassMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2574 | OptMethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2575 | } else { |
| 2576 | ClassMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2577 | MethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2578 | } |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2579 | } |
| 2580 | |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2581 | MethodTypesExt.insert(MethodTypesExt.end(), |
| 2582 | OptMethodTypesExt.begin(), OptMethodTypesExt.end()); |
| 2583 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2584 | llvm::Constant *Values[] = { |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2585 | EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods, |
| 2586 | MethodTypesExt), |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2587 | GetClassName(PD->getIdentifier()), |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2588 | EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 2589 | PD->protocol_begin(), |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2590 | PD->protocol_end()), |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2591 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2592 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2593 | InstanceMethods), |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2594 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2595 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2596 | ClassMethods) |
| 2597 | }; |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2598 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2599 | Values); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2600 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2601 | if (Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2602 | // Already created, fix the linkage and update the initializer. |
| 2603 | Entry->setLinkage(llvm::GlobalValue::InternalLinkage); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2604 | Entry->setInitializer(Init); |
| 2605 | } else { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2606 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2607 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2608 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2609 | Init, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2610 | "\01L_OBJC_PROTOCOL_" + PD->getName()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2611 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2612 | // FIXME: Is this necessary? Why only for protocol? |
| 2613 | Entry->setAlignment(4); |
John McCall | 50651b9 | 2012-03-30 21:29:05 +0000 | [diff] [blame] | 2614 | |
| 2615 | Protocols[PD->getIdentifier()] = Entry; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2616 | } |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2617 | CGM.AddUsedGlobal(Entry); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2618 | |
| 2619 | return Entry; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2620 | } |
| 2621 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2622 | llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2623 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 2624 | |
| 2625 | if (!Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2626 | // We use the initializer as a marker of whether this is a forward |
| 2627 | // reference or not. At module finalization we add the empty |
| 2628 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2629 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2630 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2631 | llvm::GlobalValue::ExternalLinkage, |
| 2632 | 0, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2633 | "\01L_OBJC_PROTOCOL_" + PD->getName()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2634 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2635 | // FIXME: Is this necessary? Why only for protocol? |
| 2636 | Entry->setAlignment(4); |
| 2637 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2638 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2639 | return Entry; |
| 2640 | } |
| 2641 | |
| 2642 | /* |
| 2643 | struct _objc_protocol_extension { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2644 | uint32_t size; |
| 2645 | struct objc_method_description_list *optional_instance_methods; |
| 2646 | struct objc_method_description_list *optional_class_methods; |
| 2647 | struct objc_property_list *instance_properties; |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2648 | const char ** extendedMethodTypes; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2649 | }; |
| 2650 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2651 | llvm::Constant * |
| 2652 | CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD, |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2653 | ArrayRef<llvm::Constant*> OptInstanceMethods, |
| 2654 | ArrayRef<llvm::Constant*> OptClassMethods, |
| 2655 | ArrayRef<llvm::Constant*> MethodTypesExt) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2656 | uint64_t Size = |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2657 | CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy); |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2658 | llvm::Constant *Values[] = { |
| 2659 | llvm::ConstantInt::get(ObjCTypes.IntTy, Size), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2660 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2661 | + PD->getName(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2662 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2663 | OptInstanceMethods), |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2664 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2665 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2666 | OptClassMethods), |
| 2667 | EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2668 | ObjCTypes), |
| 2669 | EmitProtocolMethodTypes("\01L_OBJC_PROTOCOL_METHOD_TYPES_" + PD->getName(), |
| 2670 | MethodTypesExt, ObjCTypes) |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2671 | }; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2672 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2673 | // Return null if no extension bits are used. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2674 | if (Values[1]->isNullValue() && Values[2]->isNullValue() && |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2675 | Values[3]->isNullValue() && Values[4]->isNullValue()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2676 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2677 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2678 | llvm::Constant *Init = |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2679 | llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2680 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2681 | // No special section, but goes in llvm.used |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2682 | return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2683 | Init, |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2684 | 0, 0, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2685 | } |
| 2686 | |
| 2687 | /* |
| 2688 | struct objc_protocol_list { |
Bill Wendling | 3964e62 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2689 | struct objc_protocol_list *next; |
| 2690 | long count; |
| 2691 | Protocol *list[]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2692 | }; |
| 2693 | */ |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 2694 | llvm::Constant * |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2695 | CGObjCMac::EmitProtocolList(Twine Name, |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 2696 | ObjCProtocolDecl::protocol_iterator begin, |
| 2697 | ObjCProtocolDecl::protocol_iterator end) { |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2698 | SmallVector<llvm::Constant *, 16> ProtocolRefs; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2699 | |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 2700 | for (; begin != end; ++begin) |
| 2701 | ProtocolRefs.push_back(GetProtocolRef(*begin)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2702 | |
| 2703 | // Just return null for empty protocol lists |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2704 | if (ProtocolRefs.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2705 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2706 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2707 | // This list is null terminated. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2708 | ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2709 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2710 | llvm::Constant *Values[3]; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2711 | // This field is only used by the runtime. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2712 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2713 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2714 | ProtocolRefs.size() - 1); |
| 2715 | Values[2] = |
| 2716 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy, |
| 2717 | ProtocolRefs.size()), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2718 | ProtocolRefs); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2719 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2720 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2721 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2722 | CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2723 | 4, false); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2724 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2725 | } |
| 2726 | |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2727 | void CGObjCCommonMac:: |
| 2728 | PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*,16> &PropertySet, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2729 | SmallVectorImpl<llvm::Constant *> &Properties, |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2730 | const Decl *Container, |
| 2731 | const ObjCProtocolDecl *PROTO, |
| 2732 | const ObjCCommonTypesHelper &ObjCTypes) { |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2733 | for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(), |
| 2734 | E = PROTO->protocol_end(); P != E; ++P) |
| 2735 | PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes); |
| 2736 | for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(), |
| 2737 | E = PROTO->prop_end(); I != E; ++I) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2738 | const ObjCPropertyDecl *PD = *I; |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2739 | if (!PropertySet.insert(PD->getIdentifier())) |
| 2740 | continue; |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2741 | llvm::Constant *Prop[] = { |
| 2742 | GetPropertyName(PD->getIdentifier()), |
| 2743 | GetPropertyTypeString(PD, Container) |
| 2744 | }; |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2745 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop)); |
| 2746 | } |
| 2747 | } |
| 2748 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2749 | /* |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2750 | struct _objc_property { |
Bill Wendling | 3964e62 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2751 | const char * const name; |
| 2752 | const char * const attributes; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2753 | }; |
| 2754 | |
| 2755 | struct _objc_property_list { |
Bill Wendling | 3964e62 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2756 | uint32_t entsize; // sizeof (struct _objc_property) |
| 2757 | uint32_t prop_count; |
| 2758 | struct _objc_property[prop_count]; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2759 | }; |
| 2760 | */ |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2761 | llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2762 | const Decl *Container, |
| 2763 | const ObjCContainerDecl *OCD, |
| 2764 | const ObjCCommonTypesHelper &ObjCTypes) { |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2765 | SmallVector<llvm::Constant *, 16> Properties; |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2766 | llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2767 | for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(), |
| 2768 | E = OCD->prop_end(); I != E; ++I) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2769 | const ObjCPropertyDecl *PD = *I; |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2770 | PropertySet.insert(PD->getIdentifier()); |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2771 | llvm::Constant *Prop[] = { |
| 2772 | GetPropertyName(PD->getIdentifier()), |
| 2773 | GetPropertyTypeString(PD, Container) |
| 2774 | }; |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2775 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2776 | Prop)); |
| 2777 | } |
Fariborz Jahanian | 6afbdf5 | 2010-06-22 16:33:55 +0000 | [diff] [blame] | 2778 | if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 2779 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 2780 | P = OID->all_referenced_protocol_begin(), |
| 2781 | E = OID->all_referenced_protocol_end(); P != E; ++P) |
Fariborz Jahanian | 6afbdf5 | 2010-06-22 16:33:55 +0000 | [diff] [blame] | 2782 | PushProtocolProperties(PropertySet, Properties, Container, (*P), |
| 2783 | ObjCTypes); |
| 2784 | } |
| 2785 | else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) { |
| 2786 | for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(), |
| 2787 | E = CD->protocol_end(); P != E; ++P) |
| 2788 | PushProtocolProperties(PropertySet, Properties, Container, (*P), |
| 2789 | ObjCTypes); |
| 2790 | } |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2791 | |
| 2792 | // Return null for empty list. |
| 2793 | if (Properties.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2794 | return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2795 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2796 | unsigned PropertySize = |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2797 | CGM.getDataLayout().getTypeAllocSize(ObjCTypes.PropertyTy); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2798 | llvm::Constant *Values[3]; |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2799 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize); |
| 2800 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2801 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy, |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2802 | Properties.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2803 | Values[2] = llvm::ConstantArray::get(AT, Properties); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2804 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2805 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2806 | llvm::GlobalVariable *GV = |
| 2807 | CreateMetadataVar(Name, Init, |
| 2808 | (ObjCABI == 2) ? "__DATA, __objc_const" : |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2809 | "__OBJC,__property,regular,no_dead_strip", |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2810 | (ObjCABI == 2) ? 8 : 4, |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2811 | true); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2812 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2813 | } |
| 2814 | |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2815 | llvm::Constant * |
| 2816 | CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name, |
| 2817 | ArrayRef<llvm::Constant*> MethodTypes, |
| 2818 | const ObjCCommonTypesHelper &ObjCTypes) { |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2819 | // Return null for empty list. |
| 2820 | if (MethodTypes.empty()) |
| 2821 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy); |
| 2822 | |
| 2823 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 2824 | MethodTypes.size()); |
| 2825 | llvm::Constant *Init = llvm::ConstantArray::get(AT, MethodTypes); |
| 2826 | |
| 2827 | llvm::GlobalVariable *GV = |
| 2828 | CreateMetadataVar(Name, Init, |
| 2829 | (ObjCABI == 2) ? "__DATA, __objc_const" : 0, |
| 2830 | (ObjCABI == 2) ? 8 : 4, |
| 2831 | true); |
| 2832 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.Int8PtrPtrTy); |
| 2833 | } |
| 2834 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2835 | /* |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2836 | struct objc_method_description_list { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2837 | int count; |
| 2838 | struct objc_method_description list[]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2839 | }; |
| 2840 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2841 | llvm::Constant * |
| 2842 | CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2843 | llvm::Constant *Desc[] = { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2844 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2845 | ObjCTypes.SelectorPtrTy), |
| 2846 | GetMethodVarType(MD) |
| 2847 | }; |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 2848 | if (!Desc[1]) |
| 2849 | return 0; |
| 2850 | |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2851 | return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2852 | Desc); |
| 2853 | } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2854 | |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2855 | llvm::Constant * |
| 2856 | CGObjCMac::EmitMethodDescList(Twine Name, const char *Section, |
| 2857 | ArrayRef<llvm::Constant*> Methods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2858 | // Return null for empty list. |
| 2859 | if (Methods.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2860 | return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2861 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2862 | llvm::Constant *Values[2]; |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2863 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2864 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2865 | Methods.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2866 | Values[1] = llvm::ConstantArray::get(AT, Methods); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2867 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2868 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2869 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2870 | return llvm::ConstantExpr::getBitCast(GV, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2871 | ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2872 | } |
| 2873 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2874 | /* |
| 2875 | struct _objc_category { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2876 | char *category_name; |
| 2877 | char *class_name; |
| 2878 | struct _objc_method_list *instance_methods; |
| 2879 | struct _objc_method_list *class_methods; |
| 2880 | struct _objc_protocol_list *protocols; |
| 2881 | uint32_t size; // <rdar://4585769> |
| 2882 | struct _objc_property_list *instance_properties; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2883 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2884 | */ |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2885 | void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2886 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.CategoryTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2887 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2888 | // FIXME: This is poor design, the OCD should have a pointer to the category |
| 2889 | // decl. Additionally, note that Category can be null for the @implementation |
| 2890 | // w/o an @interface case. Sema should just create one for us as it does for |
| 2891 | // @implementation so everyone else can live life under a clear blue sky. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2892 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2893 | const ObjCCategoryDecl *Category = |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2894 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2895 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2896 | SmallString<256> ExtName; |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2897 | llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_' |
| 2898 | << OCD->getName(); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2899 | |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2900 | SmallVector<llvm::Constant *, 16> InstanceMethods, ClassMethods; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2901 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2902 | i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2903 | // Instance methods should always be defined. |
| 2904 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 2905 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2906 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2907 | i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2908 | // Class methods should always be defined. |
| 2909 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 2910 | } |
| 2911 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2912 | llvm::Constant *Values[7]; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2913 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 2914 | Values[1] = GetClassName(Interface->getIdentifier()); |
Fariborz Jahanian | 679cd7f | 2009-04-29 20:40:05 +0000 | [diff] [blame] | 2915 | LazySymbols.insert(Interface->getIdentifier()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2916 | Values[2] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2917 | EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(), |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2918 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2919 | InstanceMethods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2920 | Values[3] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2921 | EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2922 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2923 | ClassMethods); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2924 | if (Category) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2925 | Values[4] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2926 | EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2927 | Category->protocol_begin(), |
| 2928 | Category->protocol_end()); |
| 2929 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2930 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2931 | } |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2932 | Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2933 | |
| 2934 | // If there is no category @interface then there can be no properties. |
| 2935 | if (Category) { |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2936 | Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 2937 | OCD, Category, ObjCTypes); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2938 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2939 | Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2940 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2941 | |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2942 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2943 | Values); |
| 2944 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2945 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2946 | CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init, |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2947 | "__OBJC,__category,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2948 | 4, true); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2949 | DefinedCategories.push_back(GV); |
Fariborz Jahanian | b9c5b3d | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 2950 | DefinedCategoryNames.insert(ExtName.str()); |
Fariborz Jahanian | 64089ce | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 2951 | // method definition entries must be clear for next implementation. |
| 2952 | MethodDefinitions.clear(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2953 | } |
| 2954 | |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 2955 | enum FragileClassFlags { |
| 2956 | FragileABI_Class_Factory = 0x00001, |
| 2957 | FragileABI_Class_Meta = 0x00002, |
| 2958 | FragileABI_Class_HasCXXStructors = 0x02000, |
| 2959 | FragileABI_Class_Hidden = 0x20000 |
| 2960 | }; |
| 2961 | |
| 2962 | enum NonFragileClassFlags { |
| 2963 | /// Is a meta-class. |
| 2964 | NonFragileABI_Class_Meta = 0x00001, |
| 2965 | |
| 2966 | /// Is a root class. |
| 2967 | NonFragileABI_Class_Root = 0x00002, |
| 2968 | |
| 2969 | /// Has a C++ constructor and destructor. |
| 2970 | NonFragileABI_Class_HasCXXStructors = 0x00004, |
| 2971 | |
| 2972 | /// Has hidden visibility. |
| 2973 | NonFragileABI_Class_Hidden = 0x00010, |
| 2974 | |
| 2975 | /// Has the exception attribute. |
| 2976 | NonFragileABI_Class_Exception = 0x00020, |
| 2977 | |
| 2978 | /// (Obsolete) ARC-specific: this class has a .release_ivars method |
| 2979 | NonFragileABI_Class_HasIvarReleaser = 0x00040, |
| 2980 | |
| 2981 | /// Class implementation was compiled under ARC. |
John McCall | b03527a | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 2982 | NonFragileABI_Class_CompiledByARC = 0x00080, |
| 2983 | |
| 2984 | /// Class has non-trivial destructors, but zero-initialization is okay. |
| 2985 | NonFragileABI_Class_HasCXXDestructorOnly = 0x00100 |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2986 | }; |
| 2987 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2988 | /* |
| 2989 | struct _objc_class { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2990 | Class isa; |
| 2991 | Class super_class; |
| 2992 | const char *name; |
| 2993 | long version; |
| 2994 | long info; |
| 2995 | long instance_size; |
| 2996 | struct _objc_ivar_list *ivars; |
| 2997 | struct _objc_method_list *methods; |
| 2998 | struct _objc_cache *cache; |
| 2999 | struct _objc_protocol_list *protocols; |
| 3000 | // Objective-C 1.0 extensions (<rdr://4585769>) |
| 3001 | const char *ivar_layout; |
| 3002 | struct _objc_class_ext *ext; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3003 | }; |
| 3004 | |
| 3005 | See EmitClassExtension(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3006 | */ |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3007 | void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3008 | DefinedSymbols.insert(ID->getIdentifier()); |
| 3009 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3010 | std::string ClassName = ID->getNameAsString(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3011 | // FIXME: Gross |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3012 | ObjCInterfaceDecl *Interface = |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3013 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3014 | llvm::Constant *Protocols = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 3015 | EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(), |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 3016 | Interface->all_referenced_protocol_begin(), |
| 3017 | Interface->all_referenced_protocol_end()); |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 3018 | unsigned Flags = FragileABI_Class_Factory; |
John McCall | b03527a | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 3019 | if (ID->hasNonZeroConstructors() || ID->hasDestructors()) |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 3020 | Flags |= FragileABI_Class_HasCXXStructors; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3021 | unsigned Size = |
Ken Dyck | 5f022d8 | 2011-02-09 01:59:34 +0000 | [diff] [blame] | 3022 | CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3023 | |
| 3024 | // FIXME: Set CXX-structors flag. |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 3025 | if (ID->getClassInterface()->getVisibility() == HiddenVisibility) |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 3026 | Flags |= FragileABI_Class_Hidden; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3027 | |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3028 | SmallVector<llvm::Constant *, 16> InstanceMethods, ClassMethods; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3029 | for (ObjCImplementationDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3030 | i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3031 | // Instance methods should always be defined. |
| 3032 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 3033 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3034 | for (ObjCImplementationDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3035 | i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3036 | // Class methods should always be defined. |
| 3037 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 3038 | } |
| 3039 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3040 | for (ObjCImplementationDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3041 | i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3042 | ObjCPropertyImplDecl *PID = *i; |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3043 | |
| 3044 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 3045 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 3046 | |
| 3047 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 3048 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 3049 | InstanceMethods.push_back(C); |
| 3050 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 3051 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 3052 | InstanceMethods.push_back(C); |
| 3053 | } |
| 3054 | } |
| 3055 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3056 | llvm::Constant *Values[12]; |
Daniel Dunbar | 5384b09 | 2009-05-03 08:56:52 +0000 | [diff] [blame] | 3057 | Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3058 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3059 | // Record a reference to the super class. |
| 3060 | LazySymbols.insert(Super->getIdentifier()); |
| 3061 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3062 | Values[ 1] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3063 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3064 | ObjCTypes.ClassPtrTy); |
| 3065 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3066 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3067 | } |
| 3068 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 3069 | // Version is always 0. |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3070 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 3071 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 3072 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 3073 | Values[ 6] = EmitIvarList(ID, false); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3074 | Values[ 7] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 3075 | EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(), |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3076 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3077 | InstanceMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3078 | // cache is always NULL. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3079 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3080 | Values[ 9] = Protocols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3081 | Values[10] = BuildIvarLayout(ID, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3082 | Values[11] = EmitClassExtension(ID); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 3083 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3084 | Values); |
Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 3085 | std::string Name("\01L_OBJC_CLASS_"); |
| 3086 | Name += ClassName; |
| 3087 | const char *Section = "__OBJC,__class,regular,no_dead_strip"; |
| 3088 | // Check for a forward reference. |
| 3089 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 3090 | if (GV) { |
| 3091 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 3092 | "Forward metaclass reference has incorrect type."); |
| 3093 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 3094 | GV->setInitializer(Init); |
| 3095 | GV->setSection(Section); |
| 3096 | GV->setAlignment(4); |
| 3097 | CGM.AddUsedGlobal(GV); |
| 3098 | } |
| 3099 | else |
| 3100 | GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3101 | DefinedClasses.push_back(GV); |
Fariborz Jahanian | 64089ce | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 3102 | // method definition entries must be clear for next implementation. |
| 3103 | MethodDefinitions.clear(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3104 | } |
| 3105 | |
| 3106 | llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID, |
| 3107 | llvm::Constant *Protocols, |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 3108 | ArrayRef<llvm::Constant*> Methods) { |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 3109 | unsigned Flags = FragileABI_Class_Meta; |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3110 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3111 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 3112 | if (ID->getClassInterface()->getVisibility() == HiddenVisibility) |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 3113 | Flags |= FragileABI_Class_Hidden; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3114 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3115 | llvm::Constant *Values[12]; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3116 | // The isa for the metaclass is the root of the hierarchy. |
| 3117 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 3118 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 3119 | Root = Super; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3120 | Values[ 0] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3121 | llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3122 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3123 | // The super class for the metaclass is emitted as the name of the |
| 3124 | // super class. The runtime fixes this up to point to the |
| 3125 | // *metaclass* for the super class. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3126 | if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3127 | Values[ 1] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3128 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3129 | ObjCTypes.ClassPtrTy); |
| 3130 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3131 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3132 | } |
| 3133 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 3134 | // Version is always 0. |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3135 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 3136 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 3137 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 3138 | Values[ 6] = EmitIvarList(ID, true); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3139 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3140 | EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 3141 | "__OBJC,__cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3142 | Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3143 | // cache is always NULL. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3144 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3145 | Values[ 9] = Protocols; |
| 3146 | // ivar_layout for metaclass is always NULL. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3147 | Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3148 | // The class extension is always unused for metaclasses. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3149 | Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 3150 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3151 | Values); |
| 3152 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 3153 | std::string Name("\01L_OBJC_METACLASS_"); |
Benjamin Kramer | 94be8ea | 2012-07-31 11:45:39 +0000 | [diff] [blame] | 3154 | Name += ID->getName(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 3155 | |
| 3156 | // Check for a forward reference. |
| 3157 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 3158 | if (GV) { |
| 3159 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 3160 | "Forward metaclass reference has incorrect type."); |
| 3161 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 3162 | GV->setInitializer(Init); |
| 3163 | } else { |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 3164 | GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 3165 | llvm::GlobalValue::InternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 3166 | Init, Name); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 3167 | } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3168 | GV->setSection("__OBJC,__meta_class,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 3169 | GV->setAlignment(4); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 3170 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3171 | |
| 3172 | return GV; |
| 3173 | } |
| 3174 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3175 | llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3176 | std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 3177 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 3178 | // FIXME: Should we look these up somewhere other than the module. Its a bit |
| 3179 | // silly since we only generate these while processing an implementation, so |
| 3180 | // exactly one pointer would work if know when we entered/exitted an |
| 3181 | // implementation block. |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 3182 | |
| 3183 | // Check for an existing forward reference. |
Fariborz Jahanian | b0d2794 | 2009-01-07 20:11:22 +0000 | [diff] [blame] | 3184 | // Previously, metaclass with internal linkage may have been defined. |
| 3185 | // pass 'true' as 2nd argument so it is returned. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3186 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, |
| 3187 | true)) { |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 3188 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 3189 | "Forward metaclass reference has incorrect type."); |
| 3190 | return GV; |
| 3191 | } else { |
| 3192 | // Generate as an external reference to keep a consistent |
| 3193 | // module. This will be patched up when we emit the metaclass. |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 3194 | return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 3195 | llvm::GlobalValue::ExternalLinkage, |
| 3196 | 0, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 3197 | Name); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 3198 | } |
| 3199 | } |
| 3200 | |
Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 3201 | llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) { |
| 3202 | std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString(); |
| 3203 | |
| 3204 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, |
| 3205 | true)) { |
| 3206 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 3207 | "Forward class metadata reference has incorrect type."); |
| 3208 | return GV; |
| 3209 | } else { |
| 3210 | return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
| 3211 | llvm::GlobalValue::ExternalLinkage, |
| 3212 | 0, |
| 3213 | Name); |
| 3214 | } |
| 3215 | } |
| 3216 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3217 | /* |
| 3218 | struct objc_class_ext { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3219 | uint32_t size; |
| 3220 | const char *weak_ivar_layout; |
| 3221 | struct _objc_property_list *properties; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3222 | }; |
| 3223 | */ |
| 3224 | llvm::Constant * |
| 3225 | CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3226 | uint64_t Size = |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3227 | CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassExtensionTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3228 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3229 | llvm::Constant *Values[3]; |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3230 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 3231 | Values[1] = BuildIvarLayout(ID, false); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 3232 | Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 3233 | ID, ID->getClassInterface(), ObjCTypes); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3234 | |
| 3235 | // Return null if no extension bits are used. |
| 3236 | if (Values[1]->isNullValue() && Values[2]->isNullValue()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3237 | return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3238 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3239 | llvm::Constant *Init = |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 3240 | llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 3241 | return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3242 | Init, "__OBJC,__class_ext,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 3243 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3244 | } |
| 3245 | |
| 3246 | /* |
| 3247 | struct objc_ivar { |
Bill Wendling | 795b100 | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 3248 | char *ivar_name; |
| 3249 | char *ivar_type; |
| 3250 | int ivar_offset; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3251 | }; |
| 3252 | |
| 3253 | struct objc_ivar_list { |
Bill Wendling | 795b100 | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 3254 | int ivar_count; |
| 3255 | struct objc_ivar list[count]; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3256 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3257 | */ |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3258 | llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 3259 | bool ForClass) { |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 3260 | std::vector<llvm::Constant*> Ivars; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3261 | |
| 3262 | // When emitting the root class GCC emits ivar entries for the |
| 3263 | // actual class structure. It is not clear if we need to follow this |
| 3264 | // behavior; for now lets try and get away with not doing it. If so, |
| 3265 | // the cleanest solution would be to make up an ObjCInterfaceDecl |
| 3266 | // for the class. |
| 3267 | if (ForClass) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3268 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3269 | |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3270 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3271 | |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3272 | for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin(); |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 3273 | IVD; IVD = IVD->getNextIvar()) { |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 3274 | // Ignore unnamed bit-fields. |
| 3275 | if (!IVD->getDeclName()) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3276 | continue; |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 3277 | llvm::Constant *Ivar[] = { |
| 3278 | GetMethodVarName(IVD->getIdentifier()), |
| 3279 | GetMethodVarType(IVD), |
| 3280 | llvm::ConstantInt::get(ObjCTypes.IntTy, |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 3281 | ComputeIvarBaseOffset(CGM, OID, IVD)) |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 3282 | }; |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 3283 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3284 | } |
| 3285 | |
| 3286 | // Return null for empty list. |
| 3287 | if (Ivars.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3288 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3289 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3290 | llvm::Constant *Values[2]; |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3291 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3292 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3293 | Ivars.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 3294 | Values[1] = llvm::ConstantArray::get(AT, Ivars); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3295 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3296 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3297 | llvm::GlobalVariable *GV; |
| 3298 | if (ForClass) |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 3299 | GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3300 | Init, "__OBJC,__class_vars,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 3301 | 4, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3302 | else |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 3303 | GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3304 | Init, "__OBJC,__instance_vars,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 3305 | 4, true); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3306 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3307 | } |
| 3308 | |
| 3309 | /* |
| 3310 | struct objc_method { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3311 | SEL method_name; |
| 3312 | char *method_types; |
| 3313 | void *method; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3314 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3315 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3316 | struct objc_method_list { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3317 | struct objc_method_list *obsolete; |
| 3318 | int count; |
| 3319 | struct objc_method methods_list[count]; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3320 | }; |
| 3321 | */ |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3322 | |
| 3323 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 3324 | /// given method if it has been defined. The result is null if the |
| 3325 | /// method has not been defined. The return value has type MethodPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 3326 | llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) { |
Argyrios Kyrtzidis | 9d50c06 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 3327 | llvm::Function *Fn = GetMethodDefinition(MD); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3328 | if (!Fn) |
| 3329 | return 0; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3330 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 3331 | llvm::Constant *Method[] = { |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3332 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 3333 | ObjCTypes.SelectorPtrTy), |
| 3334 | GetMethodVarType(MD), |
| 3335 | llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy) |
| 3336 | }; |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 3337 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3338 | } |
| 3339 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3340 | llvm::Constant *CGObjCMac::EmitMethodList(Twine Name, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3341 | const char *Section, |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 3342 | ArrayRef<llvm::Constant*> Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3343 | // Return null for empty list. |
| 3344 | if (Methods.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3345 | return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3346 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3347 | llvm::Constant *Values[3]; |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3348 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3349 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3350 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3351 | Methods.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 3352 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3353 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3354 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 3355 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3356 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3357 | } |
| 3358 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 3359 | llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3360 | const ObjCContainerDecl *CD) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 3361 | SmallString<256> Name; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 3362 | GetNameForMethod(OMD, CD, Name); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3363 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 3364 | CodeGenTypes &Types = CGM.getTypes(); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3365 | llvm::FunctionType *MethodTy = |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 3366 | Types.GetFunctionType(Types.arrangeObjCMethodDeclaration(OMD)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3367 | llvm::Function *Method = |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 3368 | llvm::Function::Create(MethodTy, |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3369 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 3370 | Name.str(), |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3371 | &CGM.getModule()); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3372 | MethodDefinitions.insert(std::make_pair(OMD, Method)); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3373 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3374 | return Method; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 3375 | } |
| 3376 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 3377 | llvm::GlobalVariable * |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3378 | CGObjCCommonMac::CreateMetadataVar(Twine Name, |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 3379 | llvm::Constant *Init, |
| 3380 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 3381 | unsigned Align, |
| 3382 | bool AddToUsed) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3383 | llvm::Type *Ty = Init->getType(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3384 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 3385 | new llvm::GlobalVariable(CGM.getModule(), Ty, false, |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 3386 | llvm::GlobalValue::InternalLinkage, Init, Name); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 3387 | if (Section) |
| 3388 | GV->setSection(Section); |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 3389 | if (Align) |
| 3390 | GV->setAlignment(Align); |
| 3391 | if (AddToUsed) |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 3392 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 3393 | return GV; |
| 3394 | } |
| 3395 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3396 | llvm::Function *CGObjCMac::ModuleInitFunction() { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3397 | // Abuse this interface function as a place to finalize. |
| 3398 | FinishModule(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 3399 | return NULL; |
| 3400 | } |
| 3401 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 3402 | llvm::Constant *CGObjCMac::GetPropertyGetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 3403 | return ObjCTypes.getGetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 3404 | } |
| 3405 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 3406 | llvm::Constant *CGObjCMac::GetPropertySetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 3407 | return ObjCTypes.getSetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 3408 | } |
| 3409 | |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 3410 | llvm::Constant *CGObjCMac::GetOptimizedPropertySetFunction(bool atomic, |
| 3411 | bool copy) { |
| 3412 | return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy); |
| 3413 | } |
| 3414 | |
David Chisnall | 8fac25d | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 3415 | llvm::Constant *CGObjCMac::GetGetStructFunction() { |
| 3416 | return ObjCTypes.getCopyStructFn(); |
| 3417 | } |
| 3418 | llvm::Constant *CGObjCMac::GetSetStructFunction() { |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 3419 | return ObjCTypes.getCopyStructFn(); |
| 3420 | } |
| 3421 | |
David Chisnall | d397cfe | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 3422 | llvm::Constant *CGObjCMac::GetCppAtomicObjectGetFunction() { |
| 3423 | return ObjCTypes.getCppAtomicObjectFunction(); |
| 3424 | } |
| 3425 | llvm::Constant *CGObjCMac::GetCppAtomicObjectSetFunction() { |
Fariborz Jahanian | e317302 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 3426 | return ObjCTypes.getCppAtomicObjectFunction(); |
| 3427 | } |
| 3428 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 3429 | llvm::Constant *CGObjCMac::EnumerationMutationFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 3430 | return ObjCTypes.getEnumerationMutationFn(); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 3431 | } |
| 3432 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3433 | void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) { |
| 3434 | return EmitTryOrSynchronizedStmt(CGF, S); |
| 3435 | } |
| 3436 | |
| 3437 | void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF, |
| 3438 | const ObjCAtSynchronizedStmt &S) { |
| 3439 | return EmitTryOrSynchronizedStmt(CGF, S); |
| 3440 | } |
| 3441 | |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3442 | namespace { |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 3443 | struct PerformFragileFinally : EHScopeStack::Cleanup { |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3444 | const Stmt &S; |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3445 | llvm::Value *SyncArgSlot; |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3446 | llvm::Value *CallTryExitVar; |
| 3447 | llvm::Value *ExceptionData; |
| 3448 | ObjCTypesHelper &ObjCTypes; |
| 3449 | PerformFragileFinally(const Stmt *S, |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3450 | llvm::Value *SyncArgSlot, |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3451 | llvm::Value *CallTryExitVar, |
| 3452 | llvm::Value *ExceptionData, |
| 3453 | ObjCTypesHelper *ObjCTypes) |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3454 | : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar), |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3455 | ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {} |
| 3456 | |
John McCall | ad346f4 | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 3457 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3458 | // Check whether we need to call objc_exception_try_exit. |
| 3459 | // In optimized code, this branch will always be folded. |
| 3460 | llvm::BasicBlock *FinallyCallExit = |
| 3461 | CGF.createBasicBlock("finally.call_exit"); |
| 3462 | llvm::BasicBlock *FinallyNoCallExit = |
| 3463 | CGF.createBasicBlock("finally.no_call_exit"); |
| 3464 | CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar), |
| 3465 | FinallyCallExit, FinallyNoCallExit); |
| 3466 | |
| 3467 | CGF.EmitBlock(FinallyCallExit); |
| 3468 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData) |
| 3469 | ->setDoesNotThrow(); |
| 3470 | |
| 3471 | CGF.EmitBlock(FinallyNoCallExit); |
| 3472 | |
| 3473 | if (isa<ObjCAtTryStmt>(S)) { |
| 3474 | if (const ObjCAtFinallyStmt* FinallyStmt = |
John McCall | d96a8e7 | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 3475 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) { |
| 3476 | // Save the current cleanup destination in case there's |
| 3477 | // control flow inside the finally statement. |
| 3478 | llvm::Value *CurCleanupDest = |
| 3479 | CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot()); |
| 3480 | |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3481 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
| 3482 | |
John McCall | d96a8e7 | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 3483 | if (CGF.HaveInsertPoint()) { |
| 3484 | CGF.Builder.CreateStore(CurCleanupDest, |
| 3485 | CGF.getNormalCleanupDestSlot()); |
| 3486 | } else { |
| 3487 | // Currently, the end of the cleanup must always exist. |
| 3488 | CGF.EnsureInsertPoint(); |
| 3489 | } |
| 3490 | } |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3491 | } else { |
| 3492 | // Emit objc_sync_exit(expr); as finally's sole statement for |
| 3493 | // @synchronized. |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3494 | llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot); |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3495 | CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg) |
| 3496 | ->setDoesNotThrow(); |
| 3497 | } |
| 3498 | } |
| 3499 | }; |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3500 | |
| 3501 | class FragileHazards { |
| 3502 | CodeGenFunction &CGF; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3503 | SmallVector<llvm::Value*, 20> Locals; |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3504 | llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry; |
| 3505 | |
| 3506 | llvm::InlineAsm *ReadHazard; |
| 3507 | llvm::InlineAsm *WriteHazard; |
| 3508 | |
| 3509 | llvm::FunctionType *GetAsmFnType(); |
| 3510 | |
| 3511 | void collectLocals(); |
| 3512 | void emitReadHazard(CGBuilderTy &Builder); |
| 3513 | |
| 3514 | public: |
| 3515 | FragileHazards(CodeGenFunction &CGF); |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3516 | |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3517 | void emitWriteHazard(); |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3518 | void emitHazardsInNewBlocks(); |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3519 | }; |
| 3520 | } |
| 3521 | |
| 3522 | /// Create the fragile-ABI read and write hazards based on the current |
| 3523 | /// state of the function, which is presumed to be immediately prior |
| 3524 | /// to a @try block. These hazards are used to maintain correct |
| 3525 | /// semantics in the face of optimization and the fragile ABI's |
| 3526 | /// cavalier use of setjmp/longjmp. |
| 3527 | FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) { |
| 3528 | collectLocals(); |
| 3529 | |
| 3530 | if (Locals.empty()) return; |
| 3531 | |
| 3532 | // Collect all the blocks in the function. |
| 3533 | for (llvm::Function::iterator |
| 3534 | I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I) |
| 3535 | BlocksBeforeTry.insert(&*I); |
| 3536 | |
| 3537 | llvm::FunctionType *AsmFnTy = GetAsmFnType(); |
| 3538 | |
| 3539 | // Create a read hazard for the allocas. This inhibits dead-store |
| 3540 | // optimizations and forces the values to memory. This hazard is |
| 3541 | // inserted before any 'throwing' calls in the protected scope to |
| 3542 | // reflect the possibility that the variables might be read from the |
| 3543 | // catch block if the call throws. |
| 3544 | { |
| 3545 | std::string Constraint; |
| 3546 | for (unsigned I = 0, E = Locals.size(); I != E; ++I) { |
| 3547 | if (I) Constraint += ','; |
| 3548 | Constraint += "*m"; |
| 3549 | } |
| 3550 | |
| 3551 | ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false); |
| 3552 | } |
| 3553 | |
| 3554 | // Create a write hazard for the allocas. This inhibits folding |
| 3555 | // loads across the hazard. This hazard is inserted at the |
| 3556 | // beginning of the catch path to reflect the possibility that the |
| 3557 | // variables might have been written within the protected scope. |
| 3558 | { |
| 3559 | std::string Constraint; |
| 3560 | for (unsigned I = 0, E = Locals.size(); I != E; ++I) { |
| 3561 | if (I) Constraint += ','; |
| 3562 | Constraint += "=*m"; |
| 3563 | } |
| 3564 | |
| 3565 | WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false); |
| 3566 | } |
| 3567 | } |
| 3568 | |
| 3569 | /// Emit a write hazard at the current location. |
| 3570 | void FragileHazards::emitWriteHazard() { |
| 3571 | if (Locals.empty()) return; |
| 3572 | |
Jay Foad | 4c7d9f1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 3573 | CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow(); |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3574 | } |
| 3575 | |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3576 | void FragileHazards::emitReadHazard(CGBuilderTy &Builder) { |
| 3577 | assert(!Locals.empty()); |
Jay Foad | 4c7d9f1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 3578 | Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow(); |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3579 | } |
| 3580 | |
| 3581 | /// Emit read hazards in all the protected blocks, i.e. all the blocks |
| 3582 | /// which have been inserted since the beginning of the try. |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3583 | void FragileHazards::emitHazardsInNewBlocks() { |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3584 | if (Locals.empty()) return; |
| 3585 | |
| 3586 | CGBuilderTy Builder(CGF.getLLVMContext()); |
| 3587 | |
| 3588 | // Iterate through all blocks, skipping those prior to the try. |
| 3589 | for (llvm::Function::iterator |
| 3590 | FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) { |
| 3591 | llvm::BasicBlock &BB = *FI; |
| 3592 | if (BlocksBeforeTry.count(&BB)) continue; |
| 3593 | |
| 3594 | // Walk through all the calls in the block. |
| 3595 | for (llvm::BasicBlock::iterator |
| 3596 | BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) { |
| 3597 | llvm::Instruction &I = *BI; |
| 3598 | |
| 3599 | // Ignore instructions that aren't non-intrinsic calls. |
| 3600 | // These are the only calls that can possibly call longjmp. |
| 3601 | if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue; |
| 3602 | if (isa<llvm::IntrinsicInst>(I)) |
| 3603 | continue; |
| 3604 | |
| 3605 | // Ignore call sites marked nounwind. This may be questionable, |
| 3606 | // since 'nounwind' doesn't necessarily mean 'does not call longjmp'. |
| 3607 | llvm::CallSite CS(&I); |
| 3608 | if (CS.doesNotThrow()) continue; |
| 3609 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3610 | // Insert a read hazard before the call. This will ensure that |
| 3611 | // any writes to the locals are performed before making the |
| 3612 | // call. If the call throws, then this is sufficient to |
| 3613 | // guarantee correctness as long as it doesn't also write to any |
| 3614 | // locals. |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3615 | Builder.SetInsertPoint(&BB, BI); |
| 3616 | emitReadHazard(Builder); |
| 3617 | } |
| 3618 | } |
| 3619 | } |
| 3620 | |
| 3621 | static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) { |
| 3622 | if (V) S.insert(V); |
| 3623 | } |
| 3624 | |
| 3625 | void FragileHazards::collectLocals() { |
| 3626 | // Compute a set of allocas to ignore. |
| 3627 | llvm::DenseSet<llvm::Value*> AllocasToIgnore; |
| 3628 | addIfPresent(AllocasToIgnore, CGF.ReturnValue); |
| 3629 | addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest); |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3630 | |
| 3631 | // Collect all the allocas currently in the function. This is |
| 3632 | // probably way too aggressive. |
| 3633 | llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock(); |
| 3634 | for (llvm::BasicBlock::iterator |
| 3635 | I = Entry.begin(), E = Entry.end(); I != E; ++I) |
| 3636 | if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I)) |
| 3637 | Locals.push_back(&*I); |
| 3638 | } |
| 3639 | |
| 3640 | llvm::FunctionType *FragileHazards::GetAsmFnType() { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3641 | SmallVector<llvm::Type *, 16> tys(Locals.size()); |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 3642 | for (unsigned i = 0, e = Locals.size(); i != e; ++i) |
| 3643 | tys[i] = Locals[i]->getType(); |
| 3644 | return llvm::FunctionType::get(CGF.VoidTy, tys, false); |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3645 | } |
| 3646 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3647 | /* |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3648 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3649 | Objective-C setjmp-longjmp (sjlj) Exception Handling |
| 3650 | -- |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3651 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3652 | A catch buffer is a setjmp buffer plus: |
| 3653 | - a pointer to the exception that was caught |
| 3654 | - a pointer to the previous exception data buffer |
| 3655 | - two pointers of reserved storage |
| 3656 | Therefore catch buffers form a stack, with a pointer to the top |
| 3657 | of the stack kept in thread-local storage. |
| 3658 | |
| 3659 | objc_exception_try_enter pushes a catch buffer onto the EH stack. |
| 3660 | objc_exception_try_exit pops the given catch buffer, which is |
| 3661 | required to be the top of the EH stack. |
| 3662 | objc_exception_throw pops the top of the EH stack, writes the |
| 3663 | thrown exception into the appropriate field, and longjmps |
| 3664 | to the setjmp buffer. It crashes the process (with a printf |
| 3665 | and an abort()) if there are no catch buffers on the stack. |
| 3666 | objc_exception_extract just reads the exception pointer out of the |
| 3667 | catch buffer. |
| 3668 | |
| 3669 | There's no reason an implementation couldn't use a light-weight |
| 3670 | setjmp here --- something like __builtin_setjmp, but API-compatible |
| 3671 | with the heavyweight setjmp. This will be more important if we ever |
| 3672 | want to implement correct ObjC/C++ exception interactions for the |
| 3673 | fragile ABI. |
| 3674 | |
| 3675 | Note that for this use of setjmp/longjmp to be correct, we may need |
| 3676 | to mark some local variables volatile: if a non-volatile local |
| 3677 | variable is modified between the setjmp and the longjmp, it has |
| 3678 | indeterminate value. For the purposes of LLVM IR, it may be |
| 3679 | sufficient to make loads and stores within the @try (to variables |
| 3680 | declared outside the @try) volatile. This is necessary for |
| 3681 | optimized correctness, but is not currently being done; this is |
| 3682 | being tracked as rdar://problem/8160285 |
| 3683 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3684 | The basic framework for a @try-catch-finally is as follows: |
| 3685 | { |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3686 | objc_exception_data d; |
| 3687 | id _rethrow = null; |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 3688 | bool _call_try_exit = true; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3689 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3690 | objc_exception_try_enter(&d); |
| 3691 | if (!setjmp(d.jmp_buf)) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3692 | ... try body ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3693 | } else { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3694 | // exception path |
| 3695 | id _caught = objc_exception_extract(&d); |
| 3696 | |
| 3697 | // enter new try scope for handlers |
| 3698 | if (!setjmp(d.jmp_buf)) { |
| 3699 | ... match exception and execute catch blocks ... |
| 3700 | |
| 3701 | // fell off end, rethrow. |
| 3702 | _rethrow = _caught; |
| 3703 | ... jump-through-finally to finally_rethrow ... |
| 3704 | } else { |
| 3705 | // exception in catch block |
| 3706 | _rethrow = objc_exception_extract(&d); |
| 3707 | _call_try_exit = false; |
| 3708 | ... jump-through-finally to finally_rethrow ... |
| 3709 | } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3710 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3711 | ... jump-through-finally to finally_end ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3712 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3713 | finally: |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 3714 | if (_call_try_exit) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3715 | objc_exception_try_exit(&d); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 3716 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3717 | ... finally block .... |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3718 | ... dispatch to finally destination ... |
| 3719 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3720 | finally_rethrow: |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3721 | objc_exception_throw(_rethrow); |
| 3722 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3723 | finally_end: |
| 3724 | } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3725 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3726 | This framework differs slightly from the one gcc uses, in that gcc |
| 3727 | uses _rethrow to determine if objc_exception_try_exit should be called |
| 3728 | and if the object should be rethrown. This breaks in the face of |
| 3729 | throwing nil and introduces unnecessary branches. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3730 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3731 | We specialize this framework for a few particular circumstances: |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3732 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3733 | - If there are no catch blocks, then we avoid emitting the second |
| 3734 | exception handling context. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3735 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3736 | - If there is a catch-all catch block (i.e. @catch(...) or @catch(id |
| 3737 | e)) we avoid emitting the code to rethrow an uncaught exception. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3738 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3739 | - FIXME: If there is no @finally block we can do a few more |
| 3740 | simplifications. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3741 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3742 | Rethrows and Jumps-Through-Finally |
| 3743 | -- |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3744 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3745 | '@throw;' is supported by pushing the currently-caught exception |
| 3746 | onto ObjCEHStack while the @catch blocks are emitted. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3747 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3748 | Branches through the @finally block are handled with an ordinary |
| 3749 | normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC |
| 3750 | exceptions are not compatible with C++ exceptions, and this is |
| 3751 | hardly the only place where this will go wrong. |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3752 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3753 | @synchronized(expr) { stmt; } is emitted as if it were: |
| 3754 | id synch_value = expr; |
| 3755 | objc_sync_enter(synch_value); |
| 3756 | @try { stmt; } @finally { objc_sync_exit(synch_value); } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3757 | */ |
| 3758 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 3759 | void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 3760 | const Stmt &S) { |
| 3761 | bool isTry = isa<ObjCAtTryStmt>(S); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3762 | |
| 3763 | // A destination for the fall-through edges of the catch handlers to |
| 3764 | // jump to. |
| 3765 | CodeGenFunction::JumpDest FinallyEnd = |
| 3766 | CGF.getJumpDestInCurrentScope("finally.end"); |
| 3767 | |
| 3768 | // A destination for the rethrow edge of the catch handlers to jump |
| 3769 | // to. |
| 3770 | CodeGenFunction::JumpDest FinallyRethrow = |
| 3771 | CGF.getJumpDestInCurrentScope("finally.rethrow"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3772 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3773 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 3774 | // evaluation of the expression must occur before we enter the |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3775 | // @synchronized. We can't avoid a temp here because we need the |
| 3776 | // value to be preserved. If the backend ever does liveness |
| 3777 | // correctly after setjmp, this will be unnecessary. |
| 3778 | llvm::Value *SyncArgSlot = 0; |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3779 | if (!isTry) { |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3780 | llvm::Value *SyncArg = |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3781 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 3782 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3783 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg) |
| 3784 | ->setDoesNotThrow(); |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3785 | |
| 3786 | SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg"); |
| 3787 | CGF.Builder.CreateStore(SyncArg, SyncArgSlot); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3788 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3789 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3790 | // Allocate memory for the setjmp buffer. This needs to be kept |
| 3791 | // live throughout the try and catch blocks. |
| 3792 | llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy, |
| 3793 | "exceptiondata.ptr"); |
| 3794 | |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3795 | // Create the fragile hazards. Note that this will not capture any |
| 3796 | // of the allocas required for exception processing, but will |
| 3797 | // capture the current basic block (which extends all the way to the |
| 3798 | // setjmp call) as "before the @try". |
| 3799 | FragileHazards Hazards(CGF); |
| 3800 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3801 | // Create a flag indicating whether the cleanup needs to call |
| 3802 | // objc_exception_try_exit. This is true except when |
| 3803 | // - no catches match and we're branching through the cleanup |
| 3804 | // just to rethrow the exception, or |
| 3805 | // - a catch matched and we're falling out of the catch handler. |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3806 | // The setjmp-safety rule here is that we should always store to this |
| 3807 | // variable in a place that dominates the branch through the cleanup |
| 3808 | // without passing through any setjmps. |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3809 | llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(), |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 3810 | "_call_try_exit"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3811 | |
John McCall | 9e2213d | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3812 | // A slot containing the exception to rethrow. Only needed when we |
| 3813 | // have both a @catch and a @finally. |
| 3814 | llvm::Value *PropagatingExnVar = 0; |
| 3815 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3816 | // Push a normal cleanup to leave the try scope. |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 3817 | CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S, |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3818 | SyncArgSlot, |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 3819 | CallTryExitVar, |
| 3820 | ExceptionData, |
| 3821 | &ObjCTypes); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3822 | |
| 3823 | // Enter a try block: |
| 3824 | // - Call objc_exception_try_enter to push ExceptionData on top of |
| 3825 | // the EH stack. |
| 3826 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData) |
| 3827 | ->setDoesNotThrow(); |
| 3828 | |
| 3829 | // - Call setjmp on the exception data buffer. |
| 3830 | llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0); |
| 3831 | llvm::Value *GEPIndexes[] = { Zero, Zero, Zero }; |
| 3832 | llvm::Value *SetJmpBuffer = |
Jay Foad | 0f6ac7c | 2011-07-22 08:16:57 +0000 | [diff] [blame] | 3833 | CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer"); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3834 | llvm::CallInst *SetJmpResult = |
| 3835 | CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result"); |
| 3836 | SetJmpResult->setDoesNotThrow(); |
Bill Wendling | 6446c3e | 2011-12-19 23:53:28 +0000 | [diff] [blame] | 3837 | SetJmpResult->setCanReturnTwice(); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3838 | |
| 3839 | // If setjmp returned 0, enter the protected block; otherwise, |
| 3840 | // branch to the handler. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 3841 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 3842 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3843 | llvm::Value *DidCatch = |
John McCall | d96a8e7 | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 3844 | CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception"); |
| 3845 | CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3846 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3847 | // Emit the protected block. |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3848 | CGF.EmitBlock(TryBlock); |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3849 | CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3850 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3851 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3852 | |
| 3853 | CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3854 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3855 | // Emit the exception handler block. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 3856 | CGF.EmitBlock(TryHandler); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3857 | |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3858 | // Don't optimize loads of the in-scope locals across this point. |
| 3859 | Hazards.emitWriteHazard(); |
| 3860 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3861 | // For a @synchronized (or a @try with no catches), just branch |
| 3862 | // through the cleanup to the rethrow block. |
| 3863 | if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) { |
| 3864 | // Tell the cleanup not to re-pop the exit. |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3865 | CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3866 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3867 | |
| 3868 | // Otherwise, we have to match against the caught exceptions. |
| 3869 | } else { |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3870 | // Retrieve the exception object. We may emit multiple blocks but |
| 3871 | // nothing can cross this so the value is already in SSA form. |
| 3872 | llvm::CallInst *Caught = |
| 3873 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 3874 | ExceptionData, "caught"); |
| 3875 | Caught->setDoesNotThrow(); |
| 3876 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3877 | // Push the exception to rethrow onto the EH value stack for the |
| 3878 | // benefit of any @throws in the handlers. |
| 3879 | CGF.ObjCEHValueStack.push_back(Caught); |
| 3880 | |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 3881 | const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3882 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3883 | bool HasFinally = (AtTryStmt->getFinallyStmt() != 0); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3884 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3885 | llvm::BasicBlock *CatchBlock = 0; |
| 3886 | llvm::BasicBlock *CatchHandler = 0; |
| 3887 | if (HasFinally) { |
John McCall | 9e2213d | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3888 | // Save the currently-propagating exception before |
| 3889 | // objc_exception_try_enter clears the exception slot. |
| 3890 | PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(), |
| 3891 | "propagating_exception"); |
| 3892 | CGF.Builder.CreateStore(Caught, PropagatingExnVar); |
| 3893 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3894 | // Enter a new exception try block (in case a @catch block |
| 3895 | // throws an exception). |
| 3896 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData) |
| 3897 | ->setDoesNotThrow(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3898 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3899 | llvm::CallInst *SetJmpResult = |
| 3900 | CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, |
| 3901 | "setjmp.result"); |
| 3902 | SetJmpResult->setDoesNotThrow(); |
Bill Wendling | 6446c3e | 2011-12-19 23:53:28 +0000 | [diff] [blame] | 3903 | SetJmpResult->setCanReturnTwice(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3904 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3905 | llvm::Value *Threw = |
| 3906 | CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception"); |
| 3907 | |
| 3908 | CatchBlock = CGF.createBasicBlock("catch"); |
| 3909 | CatchHandler = CGF.createBasicBlock("catch_for_catch"); |
| 3910 | CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock); |
| 3911 | |
| 3912 | CGF.EmitBlock(CatchBlock); |
| 3913 | } |
| 3914 | |
| 3915 | CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3916 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3917 | // Handle catch list. As a special case we check if everything is |
| 3918 | // matched and avoid generating code for falling off the end if |
| 3919 | // so. |
| 3920 | bool AllMatched = false; |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 3921 | for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) { |
| 3922 | const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3923 | |
Douglas Gregor | c00d8e1 | 2010-04-26 16:46:50 +0000 | [diff] [blame] | 3924 | const VarDecl *CatchParam = CatchStmt->getCatchParamDecl(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3925 | const ObjCObjectPointerType *OPT = 0; |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 3926 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3927 | // catch(...) always matches. |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3928 | if (!CatchParam) { |
| 3929 | AllMatched = true; |
| 3930 | } else { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3931 | OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3932 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3933 | // catch(id e) always matches under this ABI, since only |
| 3934 | // ObjC exceptions end up here in the first place. |
Daniel Dunbar | 97f61d1 | 2008-09-27 22:21:14 +0000 | [diff] [blame] | 3935 | // FIXME: For the time being we also match id<X>; this should |
| 3936 | // be rejected by Sema instead. |
Eli Friedman | 818e96f | 2009-07-11 00:57:02 +0000 | [diff] [blame] | 3937 | if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType())) |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3938 | AllMatched = true; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3939 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3940 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3941 | // If this is a catch-all, we don't need to test anything. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3942 | if (AllMatched) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3943 | CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF); |
| 3944 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3945 | if (CatchParam) { |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 3946 | CGF.EmitAutoVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 3947 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3948 | |
| 3949 | // These types work out because ConvertType(id) == i8*. |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 3950 | CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3951 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3952 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3953 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3954 | |
| 3955 | // The scope of the catch variable ends right here. |
| 3956 | CatchVarCleanups.ForceCleanup(); |
| 3957 | |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3958 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3959 | break; |
| 3960 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3961 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3962 | assert(OPT && "Unexpected non-object pointer type in @catch"); |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 3963 | const ObjCObjectType *ObjTy = OPT->getObjectType(); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3964 | |
| 3965 | // FIXME: @catch (Class c) ? |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 3966 | ObjCInterfaceDecl *IDecl = ObjTy->getInterface(); |
| 3967 | assert(IDecl && "Catch parameter must have Objective-C type!"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3968 | |
| 3969 | // Check if the @catch block matches the exception object. |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 3970 | llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3971 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3972 | llvm::CallInst *Match = |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 3973 | CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(), |
| 3974 | Class, Caught, "match"); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3975 | Match->setDoesNotThrow(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3976 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3977 | llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match"); |
| 3978 | llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3979 | |
| 3980 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 3981 | MatchedBlock, NextCatchBlock); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3982 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3983 | // Emit the @catch block. |
| 3984 | CGF.EmitBlock(MatchedBlock); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3985 | |
| 3986 | // Collect any cleanups for the catch variable. The scope lasts until |
| 3987 | // the end of the catch body. |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3988 | CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3989 | |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 3990 | CGF.EmitAutoVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 3991 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3992 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3993 | // Initialize the catch variable. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3994 | llvm::Value *Tmp = |
| 3995 | CGF.Builder.CreateBitCast(Caught, |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 3996 | CGF.ConvertType(CatchParam->getType())); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 3997 | CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3998 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3999 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4000 | |
| 4001 | // We're done with the catch variable. |
| 4002 | CatchVarCleanups.ForceCleanup(); |
| 4003 | |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 4004 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4005 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 4006 | CGF.EmitBlock(NextCatchBlock); |
| 4007 | } |
| 4008 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4009 | CGF.ObjCEHValueStack.pop_back(); |
| 4010 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4011 | // If nothing wanted anything to do with the caught exception, |
| 4012 | // kill the extract call. |
| 4013 | if (Caught->use_empty()) |
| 4014 | Caught->eraseFromParent(); |
| 4015 | |
| 4016 | if (!AllMatched) |
| 4017 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 4018 | |
| 4019 | if (HasFinally) { |
| 4020 | // Emit the exception handler for the @catch blocks. |
| 4021 | CGF.EmitBlock(CatchHandler); |
| 4022 | |
| 4023 | // In theory we might now need a write hazard, but actually it's |
| 4024 | // unnecessary because there's no local-accessing code between |
| 4025 | // the try's write hazard and here. |
| 4026 | //Hazards.emitWriteHazard(); |
| 4027 | |
John McCall | 9e2213d | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 4028 | // Extract the new exception and save it to the |
| 4029 | // propagating-exception slot. |
| 4030 | assert(PropagatingExnVar); |
| 4031 | llvm::CallInst *NewCaught = |
| 4032 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 4033 | ExceptionData, "caught"); |
| 4034 | NewCaught->setDoesNotThrow(); |
| 4035 | CGF.Builder.CreateStore(NewCaught, PropagatingExnVar); |
| 4036 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4037 | // Don't pop the catch handler; the throw already did. |
| 4038 | CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 4039 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 4040 | } |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 4041 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4042 | |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 4043 | // Insert read hazards as required in the new blocks. |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4044 | Hazards.emitHazardsInNewBlocks(); |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 4045 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4046 | // Pop the cleanup. |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4047 | CGF.Builder.restoreIP(TryFallthroughIP); |
| 4048 | if (CGF.HaveInsertPoint()) |
| 4049 | CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4050 | CGF.PopCleanupBlock(); |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4051 | CGF.EmitBlock(FinallyEnd.getBlock(), true); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 4052 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4053 | // Emit the rethrow block. |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 4054 | CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP(); |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 4055 | CGF.EmitBlock(FinallyRethrow.getBlock(), true); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4056 | if (CGF.HaveInsertPoint()) { |
John McCall | 9e2213d | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 4057 | // If we have a propagating-exception variable, check it. |
| 4058 | llvm::Value *PropagatingExn; |
| 4059 | if (PropagatingExnVar) { |
| 4060 | PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar); |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4061 | |
John McCall | 9e2213d | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 4062 | // Otherwise, just look in the buffer for the exception to throw. |
| 4063 | } else { |
| 4064 | llvm::CallInst *Caught = |
| 4065 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 4066 | ExceptionData); |
| 4067 | Caught->setDoesNotThrow(); |
| 4068 | PropagatingExn = Caught; |
| 4069 | } |
| 4070 | |
| 4071 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn) |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4072 | ->setDoesNotThrow(); |
| 4073 | CGF.Builder.CreateUnreachable(); |
Fariborz Jahanian | f2878e5 | 2008-11-21 19:21:53 +0000 | [diff] [blame] | 4074 | } |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 4075 | |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 4076 | CGF.Builder.restoreIP(SavedIP); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 4077 | } |
| 4078 | |
| 4079 | void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6a3c70e | 2013-01-10 19:02:56 +0000 | [diff] [blame] | 4080 | const ObjCAtThrowStmt &S, |
| 4081 | bool ClearInsertionPoint) { |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 4082 | llvm::Value *ExceptionAsObject; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4083 | |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 4084 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
John McCall | 2b014d6 | 2011-10-01 10:32:24 +0000 | [diff] [blame] | 4085 | llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4086 | ExceptionAsObject = |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 4087 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy); |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 4088 | } else { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4089 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 4090 | "Unexpected rethrow outside @catch block."); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 4091 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 4092 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4093 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4094 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject) |
| 4095 | ->setDoesNotReturn(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 4096 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 4097 | |
| 4098 | // Clear the insertion point to indicate we are in unreachable code. |
Fariborz Jahanian | 6a3c70e | 2013-01-10 19:02:56 +0000 | [diff] [blame] | 4099 | if (ClearInsertionPoint) |
| 4100 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 4101 | } |
| 4102 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 4103 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 4104 | /// object: objc_read_weak (id *src) |
| 4105 | /// |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 4106 | llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4107 | llvm::Value *AddrWeakObj) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4108 | llvm::Type* DestTy = |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4109 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
| 4110 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, |
| 4111 | ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 4112 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 4113 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 4114 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 4115 | return read_weak; |
| 4116 | } |
| 4117 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 4118 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 4119 | /// objc_assign_weak (id src, id *dst) |
| 4120 | /// |
| 4121 | void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4122 | llvm::Value *src, llvm::Value *dst) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4123 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4124 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 4125 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4126 | assert(Size <= 8 && "does not support size > 8"); |
| 4127 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4128 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 4129 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 4130 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 4131 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 4132 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 4133 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 4134 | src, dst, "weakassign"); |
| 4135 | return; |
| 4136 | } |
| 4137 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 4138 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 4139 | /// objc_assign_global (id src, id *dst) |
| 4140 | /// |
| 4141 | void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 4142 | llvm::Value *src, llvm::Value *dst, |
| 4143 | bool threadlocal) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4144 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4145 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 4146 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4147 | assert(Size <= 8 && "does not support size > 8"); |
| 4148 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4149 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 4150 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 4151 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 4152 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 4153 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 4154 | if (!threadlocal) |
| 4155 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
| 4156 | src, dst, "globalassign"); |
| 4157 | else |
| 4158 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(), |
| 4159 | src, dst, "threadlocalassign"); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 4160 | return; |
| 4161 | } |
| 4162 | |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 4163 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 4164 | /// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset) |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 4165 | /// |
| 4166 | void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 4167 | llvm::Value *src, llvm::Value *dst, |
| 4168 | llvm::Value *ivarOffset) { |
| 4169 | assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL"); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4170 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4171 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 4172 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4173 | assert(Size <= 8 && "does not support size > 8"); |
| 4174 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4175 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 4176 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 4177 | } |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 4178 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 4179 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 4180 | CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(), |
| 4181 | src, dst, ivarOffset); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 4182 | return; |
| 4183 | } |
| 4184 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 4185 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 4186 | /// objc_assign_strongCast (id src, id *dst) |
| 4187 | /// |
| 4188 | void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4189 | llvm::Value *src, llvm::Value *dst) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4190 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4191 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 4192 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4193 | assert(Size <= 8 && "does not support size > 8"); |
| 4194 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4195 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 4196 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 4197 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 4198 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 4199 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 4200 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 4201 | src, dst, "weakassign"); |
| 4202 | return; |
| 4203 | } |
| 4204 | |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 4205 | void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4206 | llvm::Value *DestPtr, |
| 4207 | llvm::Value *SrcPtr, |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 4208 | llvm::Value *size) { |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 4209 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy); |
| 4210 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 4211 | CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(), |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 4212 | DestPtr, SrcPtr, size); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 4213 | return; |
| 4214 | } |
| 4215 | |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 4216 | /// EmitObjCValueForIvar - Code Gen for ivar reference. |
| 4217 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4218 | LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 4219 | QualType ObjectTy, |
| 4220 | llvm::Value *BaseValue, |
| 4221 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4222 | unsigned CVRQualifiers) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4223 | const ObjCInterfaceDecl *ID = |
| 4224 | ObjectTy->getAs<ObjCObjectType>()->getInterface(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 4225 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 4226 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 4227 | } |
| 4228 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4229 | llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 4230 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4231 | const ObjCIvarDecl *Ivar) { |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4232 | uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar); |
| 4233 | return llvm::ConstantInt::get( |
| 4234 | CGM.getTypes().ConvertType(CGM.getContext().LongTy), |
| 4235 | Offset); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4236 | } |
| 4237 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4238 | /* *** Private Interface *** */ |
| 4239 | |
| 4240 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 4241 | /// level information. |
| 4242 | /// |
| 4243 | /// See: <rdr://4810609&4810587&4810587> |
| 4244 | /// struct IMAGE_INFO { |
| 4245 | /// unsigned version; |
| 4246 | /// unsigned flags; |
| 4247 | /// }; |
| 4248 | enum ImageInfoFlags { |
Daniel Dunbar | fce176b | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 4249 | eImageInfo_FixAndContinue = (1 << 0), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4250 | eImageInfo_GarbageCollected = (1 << 1), |
| 4251 | eImageInfo_GCOnly = (1 << 2), |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 4252 | eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set. |
| 4253 | |
Daniel Dunbar | fce176b | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 4254 | // A flag indicating that the module has no instances of a @synthesize of a |
| 4255 | // superclass variable. <rdar://problem/6803242> |
Bill Wendling | 0982251 | 2012-04-24 11:04:57 +0000 | [diff] [blame] | 4256 | eImageInfo_CorrectedSynthesize = (1 << 4), |
| 4257 | eImageInfo_ImageIsSimulated = (1 << 5) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4258 | }; |
| 4259 | |
Daniel Dunbar | fce176b | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 4260 | void CGObjCCommonMac::EmitImageInfo() { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4261 | unsigned version = 0; // Version is unused? |
Bill Wendling | 3973acc | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 4262 | const char *Section = (ObjCABI == 1) ? |
| 4263 | "__OBJC, __image_info,regular" : |
| 4264 | "__DATA, __objc_imageinfo, regular, no_dead_strip"; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4265 | |
Bill Wendling | 3973acc | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 4266 | // Generate module-level named metadata to convey this information to the |
| 4267 | // linker and code-gen. |
| 4268 | llvm::Module &Mod = CGM.getModule(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4269 | |
Bill Wendling | 3973acc | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 4270 | // Add the ObjC ABI version to the module flags. |
| 4271 | Mod.addModuleFlag(llvm::Module::Error, "Objective-C Version", ObjCABI); |
| 4272 | Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Version", |
| 4273 | version); |
| 4274 | Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Section", |
| 4275 | llvm::MDString::get(VMContext,Section)); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4276 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4277 | if (CGM.getLangOpts().getGC() == LangOptions::NonGC) { |
Bill Wendling | 3973acc | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 4278 | // Non-GC overrides those files which specify GC. |
| 4279 | Mod.addModuleFlag(llvm::Module::Override, |
| 4280 | "Objective-C Garbage Collection", (uint32_t)0); |
| 4281 | } else { |
| 4282 | // Add the ObjC garbage collection value. |
| 4283 | Mod.addModuleFlag(llvm::Module::Error, |
| 4284 | "Objective-C Garbage Collection", |
| 4285 | eImageInfo_GarbageCollected); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4286 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4287 | if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) { |
Bill Wendling | 3973acc | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 4288 | // Add the ObjC GC Only value. |
| 4289 | Mod.addModuleFlag(llvm::Module::Error, "Objective-C GC Only", |
| 4290 | eImageInfo_GCOnly); |
| 4291 | |
| 4292 | // Require that GC be specified and set to eImageInfo_GarbageCollected. |
| 4293 | llvm::Value *Ops[2] = { |
| 4294 | llvm::MDString::get(VMContext, "Objective-C Garbage Collection"), |
| 4295 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), |
| 4296 | eImageInfo_GarbageCollected) |
| 4297 | }; |
| 4298 | Mod.addModuleFlag(llvm::Module::Require, "Objective-C GC Only", |
| 4299 | llvm::MDNode::get(VMContext, Ops)); |
| 4300 | } |
| 4301 | } |
Bill Wendling | 0982251 | 2012-04-24 11:04:57 +0000 | [diff] [blame] | 4302 | |
| 4303 | // Indicate whether we're compiling this to run on a simulator. |
| 4304 | const llvm::Triple &Triple = CGM.getTarget().getTriple(); |
| 4305 | if (Triple.getOS() == llvm::Triple::IOS && |
| 4306 | (Triple.getArch() == llvm::Triple::x86 || |
| 4307 | Triple.getArch() == llvm::Triple::x86_64)) |
| 4308 | Mod.addModuleFlag(llvm::Module::Error, "Objective-C Is Simulated", |
| 4309 | eImageInfo_ImageIsSimulated); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4310 | } |
| 4311 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4312 | // struct objc_module { |
| 4313 | // unsigned long version; |
| 4314 | // unsigned long size; |
| 4315 | // const char *name; |
| 4316 | // Symtab symtab; |
| 4317 | // }; |
| 4318 | |
| 4319 | // FIXME: Get from somewhere |
| 4320 | static const int ModuleVersion = 7; |
| 4321 | |
| 4322 | void CGObjCMac::EmitModuleInfo() { |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 4323 | uint64_t Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ModuleTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4324 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 4325 | llvm::Constant *Values[] = { |
| 4326 | llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion), |
| 4327 | llvm::ConstantInt::get(ObjCTypes.LongTy, Size), |
| 4328 | // This used to be the filename, now it is unused. <rdr://4327263> |
| 4329 | GetClassName(&CGM.getContext().Idents.get("")), |
| 4330 | EmitModuleSymbols() |
| 4331 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4332 | CreateMetadataVar("\01L_OBJC_MODULES", |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 4333 | llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4334 | "__OBJC,__module_info,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 4335 | 4, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4336 | } |
| 4337 | |
| 4338 | llvm::Constant *CGObjCMac::EmitModuleSymbols() { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4339 | unsigned NumClasses = DefinedClasses.size(); |
| 4340 | unsigned NumCategories = DefinedCategories.size(); |
| 4341 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4342 | // Return null if no symbols were defined. |
| 4343 | if (!NumClasses && !NumCategories) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4344 | return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4345 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 4346 | llvm::Constant *Values[5]; |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4347 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4348 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4349 | Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses); |
| 4350 | Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4351 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 4352 | // The runtime expects exactly the list of defined classes followed |
| 4353 | // by the list of defined categories, in a single array. |
Chris Lattner | 0b23971 | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 4354 | SmallVector<llvm::Constant*, 8> Symbols(NumClasses + NumCategories); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 4355 | for (unsigned i=0; i<NumClasses; i++) |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4356 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 4357 | ObjCTypes.Int8PtrTy); |
| 4358 | for (unsigned i=0; i<NumCategories; i++) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4359 | Symbols[NumClasses + i] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4360 | llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 4361 | ObjCTypes.Int8PtrTy); |
| 4362 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4363 | Values[4] = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4364 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Chris Lattner | 0b23971 | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 4365 | Symbols.size()), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4366 | Symbols); |
| 4367 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 4368 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4369 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4370 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4371 | CreateMetadataVar("\01L_OBJC_SYMBOLS", Init, |
| 4372 | "__OBJC,__symbols,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 4373 | 4, true); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4374 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4375 | } |
| 4376 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4377 | llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder, |
| 4378 | IdentifierInfo *II) { |
| 4379 | LazySymbols.insert(II); |
| 4380 | |
| 4381 | llvm::GlobalVariable *&Entry = ClassReferences[II]; |
| 4382 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4383 | if (!Entry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4384 | llvm::Constant *Casted = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4385 | llvm::ConstantExpr::getBitCast(GetClassName(II), |
| 4386 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4387 | Entry = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4388 | CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted, |
| 4389 | "__OBJC,__cls_refs,literal_pointers,no_dead_strip", |
| 4390 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4391 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4392 | |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 4393 | return Builder.CreateLoad(Entry); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4394 | } |
| 4395 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4396 | llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder, |
| 4397 | const ObjCInterfaceDecl *ID) { |
| 4398 | return EmitClassRefFromId(Builder, ID->getIdentifier()); |
| 4399 | } |
| 4400 | |
| 4401 | llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) { |
| 4402 | IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool"); |
| 4403 | return EmitClassRefFromId(Builder, II); |
| 4404 | } |
| 4405 | |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 4406 | llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel, |
| 4407 | bool lvalue) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4408 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4409 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4410 | if (!Entry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4411 | llvm::Constant *Casted = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4412 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4413 | ObjCTypes.SelectorPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4414 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4415 | CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted, |
| 4416 | "__OBJC,__message_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 4417 | 4, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4418 | } |
| 4419 | |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 4420 | if (lvalue) |
| 4421 | return Entry; |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 4422 | return Builder.CreateLoad(Entry); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4423 | } |
| 4424 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4425 | llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4426 | llvm::GlobalVariable *&Entry = ClassNames[Ident]; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4427 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4428 | if (!Entry) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4429 | Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
Chris Lattner | 9401069 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4430 | llvm::ConstantDataArray::getString(VMContext, |
| 4431 | Ident->getNameStart()), |
Daniel Dunbar | af19ac4 | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4432 | ((ObjCABI == 2) ? |
| 4433 | "__TEXT,__objc_classname,cstring_literals" : |
| 4434 | "__TEXT,__cstring,cstring_literals"), |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4435 | 1, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4436 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4437 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4438 | } |
| 4439 | |
Argyrios Kyrtzidis | 9d50c06 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 4440 | llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) { |
| 4441 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator |
| 4442 | I = MethodDefinitions.find(MD); |
| 4443 | if (I != MethodDefinitions.end()) |
| 4444 | return I->second; |
| 4445 | |
Argyrios Kyrtzidis | 9d50c06 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 4446 | return NULL; |
| 4447 | } |
| 4448 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 4449 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 4450 | /// ivar layout bitmap. |
| 4451 | llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4452 | const ObjCCommonTypesHelper &ObjCTypes) { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4453 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 4454 | } |
| 4455 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 4456 | void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT, |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4457 | unsigned int BytePos, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 4458 | bool ForStrongLayout, |
| 4459 | bool &HasUnion) { |
| 4460 | const RecordDecl *RD = RT->getDecl(); |
| 4461 | // FIXME - Use iterator. |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4462 | SmallVector<const FieldDecl*, 16> Fields; |
| 4463 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 4464 | e = RD->field_end(); i != e; ++i) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4465 | Fields.push_back(*i); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4466 | llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4467 | const llvm::StructLayout *RecLayout = |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 4468 | CGM.getDataLayout().getStructLayout(cast<llvm::StructType>(Ty)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4469 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 4470 | BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos, |
| 4471 | ForStrongLayout, HasUnion); |
| 4472 | } |
| 4473 | |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 4474 | void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4475 | const llvm::StructLayout *Layout, |
| 4476 | const RecordDecl *RD, |
Bill Wendling | 795b100 | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 4477 | ArrayRef<const FieldDecl*> RecFields, |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4478 | unsigned int BytePos, bool ForStrongLayout, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4479 | bool &HasUnion) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4480 | bool IsUnion = (RD && RD->isUnion()); |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4481 | uint64_t MaxUnionIvarSize = 0; |
| 4482 | uint64_t MaxSkippedUnionIvarSize = 0; |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4483 | const FieldDecl *MaxField = 0; |
| 4484 | const FieldDecl *MaxSkippedField = 0; |
| 4485 | const FieldDecl *LastFieldBitfieldOrUnnamed = 0; |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4486 | uint64_t MaxFieldOffset = 0; |
| 4487 | uint64_t MaxSkippedFieldOffset = 0; |
| 4488 | uint64_t LastBitfieldOrUnnamedOffset = 0; |
| 4489 | uint64_t FirstFieldDelta = 0; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4490 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 4491 | if (RecFields.empty()) |
| 4492 | return; |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4493 | unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4494 | unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4495 | if (!RD && CGM.getLangOpts().ObjCAutoRefCount) { |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4496 | const FieldDecl *FirstField = RecFields[0]; |
| 4497 | FirstFieldDelta = |
| 4498 | ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4499 | } |
| 4500 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 4501 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4502 | const FieldDecl *Field = RecFields[i]; |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4503 | uint64_t FieldOffset; |
Anders Carlsson | fc514ab | 2009-07-24 17:23:54 +0000 | [diff] [blame] | 4504 | if (RD) { |
Daniel Dunbar | f8f8eba | 2010-04-14 17:02:21 +0000 | [diff] [blame] | 4505 | // Note that 'i' here is actually the field index inside RD of Field, |
| 4506 | // although this dependency is hidden. |
| 4507 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4508 | FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta; |
Anders Carlsson | fc514ab | 2009-07-24 17:23:54 +0000 | [diff] [blame] | 4509 | } else |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4510 | FieldOffset = |
| 4511 | ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta; |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 4512 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 4513 | // Skip over unnamed or bitfields |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 4514 | if (!Field->getIdentifier() || Field->isBitField()) { |
Argyrios Kyrtzidis | 0dc7509 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 4515 | LastFieldBitfieldOrUnnamed = Field; |
| 4516 | LastBitfieldOrUnnamedOffset = FieldOffset; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 4517 | continue; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 4518 | } |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 4519 | |
Argyrios Kyrtzidis | 0dc7509 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 4520 | LastFieldBitfieldOrUnnamed = 0; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 4521 | QualType FQT = Field->getType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 4522 | if (FQT->isRecordType() || FQT->isUnionType()) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 4523 | if (FQT->isUnionType()) |
| 4524 | HasUnion = true; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4525 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4526 | BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(), |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 4527 | BytePos + FieldOffset, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 4528 | ForStrongLayout, HasUnion); |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 4529 | continue; |
| 4530 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4531 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 4532 | if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4533 | const ConstantArrayType *CArray = |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 4534 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 4535 | uint64_t ElCount = CArray->getSize().getZExtValue(); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 4536 | assert(CArray && "only array with known element size is supported"); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4537 | FQT = CArray->getElementType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 4538 | while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 4539 | const ConstantArrayType *CArray = |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 4540 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 4541 | ElCount *= CArray->getSize().getZExtValue(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 4542 | FQT = CArray->getElementType(); |
| 4543 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4544 | |
| 4545 | assert(!FQT->isUnionType() && |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4546 | "layout for array of unions not supported"); |
Fariborz Jahanian | f96bdf4 | 2011-01-03 19:23:18 +0000 | [diff] [blame] | 4547 | if (FQT->isRecordType() && ElCount) { |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4548 | int OldIndex = IvarsInfo.size() - 1; |
| 4549 | int OldSkIndex = SkipIvars.size() -1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4550 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4551 | const RecordType *RT = FQT->getAs<RecordType>(); |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 4552 | BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 4553 | ForStrongLayout, HasUnion); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4554 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4555 | // Replicate layout information for each array element. Note that |
| 4556 | // one element is already done. |
| 4557 | uint64_t ElIx = 1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4558 | for (int FirstIndex = IvarsInfo.size() - 1, |
| 4559 | FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) { |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4560 | uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits; |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 4561 | for (int i = OldIndex+1; i <= FirstIndex; ++i) |
| 4562 | IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx, |
| 4563 | IvarsInfo[i].ivar_size)); |
| 4564 | for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i) |
| 4565 | SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx, |
| 4566 | SkipIvars[i].ivar_size)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4567 | } |
| 4568 | continue; |
| 4569 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 4570 | } |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4571 | // At this point, we are done with Record/Union and array there of. |
| 4572 | // For other arrays we are down to its element type. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4573 | Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 4574 | |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4575 | unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4576 | if ((ForStrongLayout && GCAttr == Qualifiers::Strong) |
| 4577 | || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) { |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 4578 | if (IsUnion) { |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4579 | uint64_t UnionIvarSize = FieldSize / WordSizeInBits; |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 4580 | if (UnionIvarSize > MaxUnionIvarSize) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4581 | MaxUnionIvarSize = UnionIvarSize; |
| 4582 | MaxField = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 4583 | MaxFieldOffset = FieldOffset; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4584 | } |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 4585 | } else { |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4586 | IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset, |
| 4587 | FieldSize / WordSizeInBits)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4588 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4589 | } else if ((ForStrongLayout && |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4590 | (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)) |
| 4591 | || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) { |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 4592 | if (IsUnion) { |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4593 | // FIXME: Why the asymmetry? We divide by word size in bits on other |
| 4594 | // side. |
| 4595 | uint64_t UnionIvarSize = FieldSize / ByteSizeInBits; |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 4596 | if (UnionIvarSize > MaxSkippedUnionIvarSize) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4597 | MaxSkippedUnionIvarSize = UnionIvarSize; |
| 4598 | MaxSkippedField = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 4599 | MaxSkippedFieldOffset = FieldOffset; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4600 | } |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 4601 | } else { |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4602 | // FIXME: Why the asymmetry, we divide by byte size in bits here? |
| 4603 | SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset, |
| 4604 | FieldSize / ByteSizeInBits)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4605 | } |
| 4606 | } |
| 4607 | } |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 4608 | |
Argyrios Kyrtzidis | 0dc7509 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 4609 | if (LastFieldBitfieldOrUnnamed) { |
| 4610 | if (LastFieldBitfieldOrUnnamed->isBitField()) { |
| 4611 | // Last field was a bitfield. Must update skip info. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4612 | uint64_t BitFieldSize |
| 4613 | = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext()); |
Argyrios Kyrtzidis | 0dc7509 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 4614 | GC_IVAR skivar; |
| 4615 | skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset; |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4616 | skivar.ivar_size = (BitFieldSize / ByteSizeInBits) |
| 4617 | + ((BitFieldSize % ByteSizeInBits) != 0); |
Argyrios Kyrtzidis | 0dc7509 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 4618 | SkipIvars.push_back(skivar); |
| 4619 | } else { |
| 4620 | assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed"); |
| 4621 | // Last field was unnamed. Must update skip info. |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4622 | unsigned FieldSize |
| 4623 | = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType()); |
Argyrios Kyrtzidis | 0dc7509 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 4624 | SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset, |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4625 | FieldSize / ByteSizeInBits)); |
Argyrios Kyrtzidis | 0dc7509 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 4626 | } |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 4627 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4628 | |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 4629 | if (MaxField) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4630 | IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 4631 | MaxUnionIvarSize)); |
| 4632 | if (MaxSkippedField) |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 4633 | SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 4634 | MaxSkippedUnionIvarSize)); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 4635 | } |
| 4636 | |
Fariborz Jahanian | b8fd2eb | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 4637 | /// BuildIvarLayoutBitmap - This routine is the horsework for doing all |
| 4638 | /// the computations and returning the layout bitmap (for ivar or blocks) in |
| 4639 | /// the given argument BitMap string container. Routine reads |
| 4640 | /// two containers, IvarsInfo and SkipIvars which are assumed to be |
| 4641 | /// filled already by the caller. |
Chris Lattner | 9401069 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4642 | llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string &BitMap) { |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4643 | unsigned int WordsToScan, WordsToSkip; |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4644 | llvm::Type *PtrTy = CGM.Int8PtrTy; |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4645 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4646 | // Build the string of skip/scan nibbles |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4647 | SmallVector<SKIP_SCAN, 32> SkipScanIvars; |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4648 | unsigned int WordSize = |
| 4649 | CGM.getTypes().getDataLayout().getTypeAllocSize(PtrTy); |
| 4650 | if (IvarsInfo[0].ivar_bytepos == 0) { |
| 4651 | WordsToSkip = 0; |
| 4652 | WordsToScan = IvarsInfo[0].ivar_size; |
| 4653 | } else { |
| 4654 | WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize; |
| 4655 | WordsToScan = IvarsInfo[0].ivar_size; |
| 4656 | } |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4657 | for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) { |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4658 | unsigned int TailPrevGCObjC = |
| 4659 | IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4660 | if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4661 | // consecutive 'scanned' object pointers. |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4662 | WordsToScan += IvarsInfo[i].ivar_size; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4663 | } else { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4664 | // Skip over 'gc'able object pointer which lay over each other. |
| 4665 | if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos) |
| 4666 | continue; |
| 4667 | // Must skip over 1 or more words. We save current skip/scan values |
| 4668 | // and start a new pair. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 4669 | SKIP_SCAN SkScan; |
| 4670 | SkScan.skip = WordsToSkip; |
| 4671 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4672 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4673 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4674 | // Skip the hole. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 4675 | SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize; |
| 4676 | SkScan.scan = 0; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4677 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4678 | WordsToSkip = 0; |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4679 | WordsToScan = IvarsInfo[i].ivar_size; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4680 | } |
| 4681 | } |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4682 | if (WordsToScan > 0) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 4683 | SKIP_SCAN SkScan; |
| 4684 | SkScan.skip = WordsToSkip; |
| 4685 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4686 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4687 | } |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4688 | |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4689 | if (!SkipIvars.empty()) { |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4690 | unsigned int LastIndex = SkipIvars.size()-1; |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4691 | int LastByteSkipped = |
| 4692 | SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4693 | LastIndex = IvarsInfo.size()-1; |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4694 | int LastByteScanned = |
| 4695 | IvarsInfo[LastIndex].ivar_bytepos + |
| 4696 | IvarsInfo[LastIndex].ivar_size * WordSize; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4697 | // Compute number of bytes to skip at the tail end of the last ivar scanned. |
Benjamin Kramer | 54d76db | 2009-12-25 15:43:36 +0000 | [diff] [blame] | 4698 | if (LastByteSkipped > LastByteScanned) { |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4699 | unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 4700 | SKIP_SCAN SkScan; |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4701 | SkScan.skip = TotalWords - (LastByteScanned/WordSize); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 4702 | SkScan.scan = 0; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4703 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4704 | } |
| 4705 | } |
| 4706 | // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced |
| 4707 | // as 0xMN. |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4708 | int SkipScan = SkipScanIvars.size()-1; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4709 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4710 | if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0 |
| 4711 | && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) { |
| 4712 | // 0xM0 followed by 0x0N detected. |
| 4713 | SkipScanIvars[i].scan = SkipScanIvars[i+1].scan; |
| 4714 | for (int j = i+1; j < SkipScan; j++) |
| 4715 | SkipScanIvars[j] = SkipScanIvars[j+1]; |
| 4716 | --SkipScan; |
| 4717 | } |
| 4718 | } |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4719 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4720 | // Generate the string. |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4721 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4722 | unsigned char byte; |
| 4723 | unsigned int skip_small = SkipScanIvars[i].skip % 0xf; |
| 4724 | unsigned int scan_small = SkipScanIvars[i].scan % 0xf; |
| 4725 | unsigned int skip_big = SkipScanIvars[i].skip / 0xf; |
| 4726 | unsigned int scan_big = SkipScanIvars[i].scan / 0xf; |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4727 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4728 | // first skip big. |
| 4729 | for (unsigned int ix = 0; ix < skip_big; ix++) |
| 4730 | BitMap += (unsigned char)(0xf0); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4731 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4732 | // next (skip small, scan) |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4733 | if (skip_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4734 | byte = skip_small << 4; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4735 | if (scan_big > 0) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4736 | byte |= 0xf; |
| 4737 | --scan_big; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4738 | } else if (scan_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4739 | byte |= scan_small; |
| 4740 | scan_small = 0; |
| 4741 | } |
| 4742 | BitMap += byte; |
| 4743 | } |
| 4744 | // next scan big |
| 4745 | for (unsigned int ix = 0; ix < scan_big; ix++) |
| 4746 | BitMap += (unsigned char)(0x0f); |
| 4747 | // last scan small |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4748 | if (scan_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4749 | byte = scan_small; |
| 4750 | BitMap += byte; |
| 4751 | } |
| 4752 | } |
| 4753 | // null terminate string. |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 4754 | unsigned char zero = 0; |
| 4755 | BitMap += zero; |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4756 | |
| 4757 | llvm::GlobalVariable * Entry = |
| 4758 | CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
Chris Lattner | 9401069 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4759 | llvm::ConstantDataArray::getString(VMContext, BitMap,false), |
Daniel Dunbar | af19ac4 | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4760 | ((ObjCABI == 2) ? |
| 4761 | "__TEXT,__objc_classname,cstring_literals" : |
| 4762 | "__TEXT,__cstring,cstring_literals"), |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4763 | 1, true); |
| 4764 | return getConstantGEP(VMContext, Entry, 0, 0); |
| 4765 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4766 | |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4767 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 4768 | /// implementation for the __strong or __weak case. |
| 4769 | /// The layout map displays which words in ivar list must be skipped |
| 4770 | /// and which must be scanned by GC (see below). String is built of bytes. |
| 4771 | /// Each byte is divided up in two nibbles (4-bit each). Left nibble is count |
| 4772 | /// of words to skip and right nibble is count of words to scan. So, each |
| 4773 | /// nibble represents up to 15 workds to skip or scan. Skipping the rest is |
| 4774 | /// represented by a 0x00 byte which also ends the string. |
| 4775 | /// 1. when ForStrongLayout is true, following ivars are scanned: |
| 4776 | /// - id, Class |
| 4777 | /// - object * |
| 4778 | /// - __strong anything |
| 4779 | /// |
| 4780 | /// 2. When ForStrongLayout is false, following ivars are scanned: |
| 4781 | /// - __weak anything |
| 4782 | /// |
| 4783 | llvm::Constant *CGObjCCommonMac::BuildIvarLayout( |
| 4784 | const ObjCImplementationDecl *OMD, |
| 4785 | bool ForStrongLayout) { |
| 4786 | bool hasUnion = false; |
| 4787 | |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4788 | llvm::Type *PtrTy = CGM.Int8PtrTy; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4789 | if (CGM.getLangOpts().getGC() == LangOptions::NonGC && |
| 4790 | !CGM.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4791 | return llvm::Constant::getNullValue(PtrTy); |
| 4792 | |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4793 | const ObjCInterfaceDecl *OI = OMD->getClassInterface(); |
| 4794 | SmallVector<const FieldDecl*, 32> RecFields; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4795 | if (CGM.getLangOpts().ObjCAutoRefCount) { |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4796 | for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin(); |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 4797 | IVD; IVD = IVD->getNextIvar()) |
| 4798 | RecFields.push_back(cast<FieldDecl>(IVD)); |
| 4799 | } |
| 4800 | else { |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4801 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4802 | CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4803 | |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4804 | // FIXME: This is not ideal; we shouldn't have to do this copy. |
| 4805 | RecFields.append(Ivars.begin(), Ivars.end()); |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 4806 | } |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4807 | |
| 4808 | if (RecFields.empty()) |
| 4809 | return llvm::Constant::getNullValue(PtrTy); |
| 4810 | |
| 4811 | SkipIvars.clear(); |
| 4812 | IvarsInfo.clear(); |
| 4813 | |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4814 | BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4815 | if (IvarsInfo.empty()) |
| 4816 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | b8fd2eb | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 4817 | // Sort on byte position in case we encounterred a union nested in |
| 4818 | // the ivar list. |
| 4819 | if (hasUnion && !IvarsInfo.empty()) |
| 4820 | std::sort(IvarsInfo.begin(), IvarsInfo.end()); |
| 4821 | if (hasUnion && !SkipIvars.empty()) |
| 4822 | std::sort(SkipIvars.begin(), SkipIvars.end()); |
| 4823 | |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4824 | std::string BitMap; |
Fariborz Jahanian | b8fd2eb | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 4825 | llvm::Constant *C = BuildIvarLayoutBitmap(BitMap); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4826 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4827 | if (CGM.getLangOpts().ObjCGCBitmapPrint) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4828 | printf("\n%s ivar layout for class '%s': ", |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 4829 | ForStrongLayout ? "strong" : "weak", |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4830 | OMD->getClassInterface()->getName().data()); |
Roman Divacky | 31ba613 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 4831 | const unsigned char *s = (const unsigned char*)BitMap.c_str(); |
Bill Wendling | 13562a1 | 2012-02-07 09:06:01 +0000 | [diff] [blame] | 4832 | for (unsigned i = 0, e = BitMap.size(); i < e; i++) |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 4833 | if (!(s[i] & 0xf0)) |
| 4834 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 4835 | else |
| 4836 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 4837 | printf("\n"); |
| 4838 | } |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4839 | return C; |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 4840 | } |
| 4841 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4842 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4843 | llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; |
| 4844 | |
Chris Lattner | 0b23971 | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 4845 | // FIXME: Avoid std::string in "Sel.getAsString()" |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4846 | if (!Entry) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4847 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_", |
Chris Lattner | 9401069 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4848 | llvm::ConstantDataArray::getString(VMContext, Sel.getAsString()), |
Daniel Dunbar | af19ac4 | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4849 | ((ObjCABI == 2) ? |
| 4850 | "__TEXT,__objc_methname,cstring_literals" : |
| 4851 | "__TEXT,__cstring,cstring_literals"), |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4852 | 1, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4853 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4854 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4855 | } |
| 4856 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4857 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4858 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4859 | return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); |
| 4860 | } |
| 4861 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4862 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) { |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4863 | std::string TypeStr; |
| 4864 | CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); |
| 4865 | |
| 4866 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4867 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4868 | if (!Entry) |
| 4869 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
Chris Lattner | 9401069 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4870 | llvm::ConstantDataArray::getString(VMContext, TypeStr), |
Daniel Dunbar | af19ac4 | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4871 | ((ObjCABI == 2) ? |
| 4872 | "__TEXT,__objc_methtype,cstring_literals" : |
| 4873 | "__TEXT,__cstring,cstring_literals"), |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4874 | 1, true); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4875 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4876 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4877 | } |
| 4878 | |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4879 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D, |
| 4880 | bool Extended) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4881 | std::string TypeStr; |
Bill Wendling | a4dc693 | 2012-02-09 22:45:21 +0000 | [diff] [blame] | 4882 | if (CGM.getContext().getObjCEncodingForMethodDecl(D, TypeStr, Extended)) |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4883 | return 0; |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4884 | |
| 4885 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
| 4886 | |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4887 | if (!Entry) |
| 4888 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
Chris Lattner | 9401069 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4889 | llvm::ConstantDataArray::getString(VMContext, TypeStr), |
Daniel Dunbar | af19ac4 | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4890 | ((ObjCABI == 2) ? |
| 4891 | "__TEXT,__objc_methtype,cstring_literals" : |
| 4892 | "__TEXT,__cstring,cstring_literals"), |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4893 | 1, true); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4894 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4895 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4896 | } |
| 4897 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4898 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4899 | llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4900 | llvm::GlobalVariable *&Entry = PropertyNames[Ident]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4901 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4902 | if (!Entry) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4903 | Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_", |
Chris Lattner | 9401069 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4904 | llvm::ConstantDataArray::getString(VMContext, |
| 4905 | Ident->getNameStart()), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4906 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4907 | 1, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4908 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4909 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4910 | } |
| 4911 | |
| 4912 | // FIXME: Merge into a single cstring creation function. |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4913 | // FIXME: This Decl should be more precise. |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4914 | llvm::Constant * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4915 | CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 4916 | const Decl *Container) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4917 | std::string TypeStr; |
| 4918 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4919 | return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); |
| 4920 | } |
| 4921 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4922 | void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D, |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4923 | const ObjCContainerDecl *CD, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4924 | SmallVectorImpl<char> &Name) { |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4925 | llvm::raw_svector_ostream OS(Name); |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 4926 | assert (CD && "Missing container decl in GetNameForMethod"); |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4927 | OS << '\01' << (D->isInstanceMethod() ? '-' : '+') |
| 4928 | << '[' << CD->getName(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4929 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4930 | dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) |
Benjamin Kramer | f978059 | 2012-02-07 11:57:45 +0000 | [diff] [blame] | 4931 | OS << '(' << *CID << ')'; |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4932 | OS << ' ' << D->getSelector().getAsString() << ']'; |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 4933 | } |
| 4934 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4935 | void CGObjCMac::FinishModule() { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4936 | EmitModuleInfo(); |
| 4937 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4938 | // Emit the dummy bodies for any protocols which were referenced but |
| 4939 | // never defined. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4940 | for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4941 | I = Protocols.begin(), e = Protocols.end(); I != e; ++I) { |
| 4942 | if (I->second->hasInitializer()) |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4943 | continue; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4944 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 4945 | llvm::Constant *Values[5]; |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4946 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4947 | Values[1] = GetClassName(I->first); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4948 | Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4949 | Values[3] = Values[4] = |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4950 | llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4951 | I->second->setLinkage(llvm::GlobalValue::InternalLinkage); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 4952 | I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4953 | Values)); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4954 | CGM.AddUsedGlobal(I->second); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4955 | } |
| 4956 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4957 | // Add assembler directives to add lazy undefined symbol references |
| 4958 | // for classes which are referenced but not defined. This is |
| 4959 | // important for correct linker interaction. |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4960 | // |
| 4961 | // FIXME: It would be nice if we had an LLVM construct for this. |
| 4962 | if (!LazySymbols.empty() || !DefinedSymbols.empty()) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 4963 | SmallString<256> Asm; |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4964 | Asm += CGM.getModule().getModuleInlineAsm(); |
| 4965 | if (!Asm.empty() && Asm.back() != '\n') |
| 4966 | Asm += '\n'; |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4967 | |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4968 | llvm::raw_svector_ostream OS(Asm); |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4969 | for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(), |
| 4970 | e = DefinedSymbols.end(); I != e; ++I) |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 4971 | OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n" |
| 4972 | << "\t.globl .objc_class_name_" << (*I)->getName() << "\n"; |
Chris Lattner | afd5eda | 2010-04-17 18:26:20 +0000 | [diff] [blame] | 4973 | for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(), |
Fariborz Jahanian | b9c5b3d | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 4974 | e = LazySymbols.end(); I != e; ++I) { |
Chris Lattner | afd5eda | 2010-04-17 18:26:20 +0000 | [diff] [blame] | 4975 | OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n"; |
Fariborz Jahanian | b9c5b3d | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 4976 | } |
| 4977 | |
Bill Wendling | 13562a1 | 2012-02-07 09:06:01 +0000 | [diff] [blame] | 4978 | for (size_t i = 0, e = DefinedCategoryNames.size(); i < e; ++i) { |
Fariborz Jahanian | b9c5b3d | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 4979 | OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n" |
| 4980 | << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n"; |
| 4981 | } |
Chris Lattner | afd5eda | 2010-04-17 18:26:20 +0000 | [diff] [blame] | 4982 | |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4983 | CGM.getModule().setModuleInlineAsm(OS.str()); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4984 | } |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4985 | } |
| 4986 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4987 | CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4988 | : CGObjCCommonMac(cgm), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4989 | ObjCTypes(cgm) { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4990 | ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4991 | ObjCABI = 2; |
| 4992 | } |
| 4993 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4994 | /* *** */ |
| 4995 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4996 | ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm) |
Douglas Gregor | 65a1e67 | 2012-01-17 23:38:32 +0000 | [diff] [blame] | 4997 | : VMContext(cgm.getLLVMContext()), CGM(cgm), ExternalProtocolPtrTy(0) |
| 4998 | { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4999 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 5000 | ASTContext &Ctx = CGM.getContext(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5001 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5002 | ShortTy = Types.ConvertType(Ctx.ShortTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5003 | IntTy = Types.ConvertType(Ctx.IntTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 5004 | LongTy = Types.ConvertType(Ctx.LongTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5005 | LongLongTy = Types.ConvertType(Ctx.LongLongTy); |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5006 | Int8PtrTy = CGM.Int8PtrTy; |
| 5007 | Int8PtrPtrTy = CGM.Int8PtrPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5008 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 5009 | ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5010 | PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 5011 | SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5012 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5013 | // I'm not sure I like this. The implicit coordination is a bit |
| 5014 | // gross. We should solve this in a reasonable fashion because this |
| 5015 | // is a pretty common task (match some runtime data structure with |
| 5016 | // an LLVM data structure). |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5017 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5018 | // FIXME: This is leaked. |
| 5019 | // FIXME: Merge with rewriter code? |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5020 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5021 | // struct _objc_super { |
| 5022 | // id self; |
| 5023 | // Class cls; |
| 5024 | // } |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5025 | RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct, |
Daniel Dunbar | daa3ac5 | 2010-04-29 16:29:11 +0000 | [diff] [blame] | 5026 | Ctx.getTranslationUnitDecl(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5027 | SourceLocation(), SourceLocation(), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5028 | &Ctx.Idents.get("_objc_super")); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5029 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 5030 | Ctx.getObjCIdType(), 0, 0, false, ICIS_NoInit)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5031 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 5032 | Ctx.getObjCClassType(), 0, 0, false, |
| 5033 | ICIS_NoInit)); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 5034 | RD->completeDefinition(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5035 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5036 | SuperCTy = Ctx.getTagDeclType(RD); |
| 5037 | SuperPtrCTy = Ctx.getPointerType(SuperCTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5038 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5039 | SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5040 | SuperPtrTy = llvm::PointerType::getUnqual(SuperTy); |
| 5041 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5042 | // struct _prop_t { |
| 5043 | // char *name; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5044 | // char *attributes; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5045 | // } |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5046 | PropertyTy = llvm::StructType::create("struct._prop_t", |
| 5047 | Int8PtrTy, Int8PtrTy, NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5048 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5049 | // struct _prop_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5050 | // uint32_t entsize; // sizeof(struct _prop_t) |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5051 | // uint32_t count_of_properties; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5052 | // struct _prop_t prop_list[count_of_properties]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5053 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5054 | PropertyListTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5055 | llvm::StructType::create("struct._prop_list_t", IntTy, IntTy, |
| 5056 | llvm::ArrayType::get(PropertyTy, 0), NULL); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5057 | // struct _prop_list_t * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5058 | PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5059 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5060 | // struct _objc_method { |
| 5061 | // SEL _cmd; |
| 5062 | // char *method_type; |
| 5063 | // char *_imp; |
| 5064 | // } |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5065 | MethodTy = llvm::StructType::create("struct._objc_method", |
| 5066 | SelectorPtrTy, Int8PtrTy, Int8PtrTy, |
| 5067 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5068 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5069 | // struct _objc_cache * |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5070 | CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache"); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5071 | CachePtrTy = llvm::PointerType::getUnqual(CacheTy); |
Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 5072 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5073 | } |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 5074 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5075 | ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5076 | : ObjCCommonTypesHelper(cgm) { |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5077 | // struct _objc_method_description { |
| 5078 | // SEL name; |
| 5079 | // char *types; |
| 5080 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5081 | MethodDescriptionTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5082 | llvm::StructType::create("struct._objc_method_description", |
| 5083 | SelectorPtrTy, Int8PtrTy, NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5084 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5085 | // struct _objc_method_description_list { |
| 5086 | // int count; |
| 5087 | // struct _objc_method_description[1]; |
| 5088 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5089 | MethodDescriptionListTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5090 | llvm::StructType::create("struct._objc_method_description_list", |
| 5091 | IntTy, |
| 5092 | llvm::ArrayType::get(MethodDescriptionTy, 0),NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5093 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5094 | // struct _objc_method_description_list * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5095 | MethodDescriptionListPtrTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5096 | llvm::PointerType::getUnqual(MethodDescriptionListTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5097 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5098 | // Protocol description structures |
| 5099 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5100 | // struct _objc_protocol_extension { |
| 5101 | // uint32_t size; // sizeof(struct _objc_protocol_extension) |
| 5102 | // struct _objc_method_description_list *optional_instance_methods; |
| 5103 | // struct _objc_method_description_list *optional_class_methods; |
| 5104 | // struct _objc_property_list *instance_properties; |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5105 | // const char ** extendedMethodTypes; |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5106 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5107 | ProtocolExtensionTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5108 | llvm::StructType::create("struct._objc_protocol_extension", |
| 5109 | IntTy, MethodDescriptionListPtrTy, |
| 5110 | MethodDescriptionListPtrTy, PropertyListPtrTy, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5111 | Int8PtrPtrTy, NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5112 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5113 | // struct _objc_protocol_extension * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5114 | ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5115 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 5116 | // Handle recursive construction of Protocol and ProtocolList types |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5117 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5118 | ProtocolTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5119 | llvm::StructType::create(VMContext, "struct._objc_protocol"); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5120 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5121 | ProtocolListTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5122 | llvm::StructType::create(VMContext, "struct._objc_protocol_list"); |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5123 | ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy), |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5124 | LongTy, |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5125 | llvm::ArrayType::get(ProtocolTy, 0), |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5126 | NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5127 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5128 | // struct _objc_protocol { |
| 5129 | // struct _objc_protocol_extension *isa; |
| 5130 | // char *protocol_name; |
| 5131 | // struct _objc_protocol **_objc_protocol_list; |
| 5132 | // struct _objc_method_description_list *instance_methods; |
| 5133 | // struct _objc_method_description_list *class_methods; |
| 5134 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5135 | ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy, |
| 5136 | llvm::PointerType::getUnqual(ProtocolListTy), |
| 5137 | MethodDescriptionListPtrTy, |
| 5138 | MethodDescriptionListPtrTy, |
| 5139 | NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5140 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5141 | // struct _objc_protocol_list * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5142 | ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5143 | |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5144 | ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5145 | |
| 5146 | // Class description structures |
| 5147 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5148 | // struct _objc_ivar { |
| 5149 | // char *ivar_name; |
| 5150 | // char *ivar_type; |
| 5151 | // int ivar_offset; |
| 5152 | // } |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5153 | IvarTy = llvm::StructType::create("struct._objc_ivar", |
| 5154 | Int8PtrTy, Int8PtrTy, IntTy, NULL); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5155 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5156 | // struct _objc_ivar_list * |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5157 | IvarListTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5158 | llvm::StructType::create(VMContext, "struct._objc_ivar_list"); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5159 | IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5160 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5161 | // struct _objc_method_list * |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5162 | MethodListTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5163 | llvm::StructType::create(VMContext, "struct._objc_method_list"); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5164 | MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5165 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5166 | // struct _objc_class_extension * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5167 | ClassExtensionTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5168 | llvm::StructType::create("struct._objc_class_extension", |
| 5169 | IntTy, Int8PtrTy, PropertyListPtrTy, NULL); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5170 | ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5171 | |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5172 | ClassTy = llvm::StructType::create(VMContext, "struct._objc_class"); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5173 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5174 | // struct _objc_class { |
| 5175 | // Class isa; |
| 5176 | // Class super_class; |
| 5177 | // char *name; |
| 5178 | // long version; |
| 5179 | // long info; |
| 5180 | // long instance_size; |
| 5181 | // struct _objc_ivar_list *ivars; |
| 5182 | // struct _objc_method_list *methods; |
| 5183 | // struct _objc_cache *cache; |
| 5184 | // struct _objc_protocol_list *protocols; |
| 5185 | // char *ivar_layout; |
| 5186 | // struct _objc_class_ext *ext; |
| 5187 | // }; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5188 | ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy), |
| 5189 | llvm::PointerType::getUnqual(ClassTy), |
| 5190 | Int8PtrTy, |
| 5191 | LongTy, |
| 5192 | LongTy, |
| 5193 | LongTy, |
| 5194 | IvarListPtrTy, |
| 5195 | MethodListPtrTy, |
| 5196 | CachePtrTy, |
| 5197 | ProtocolListPtrTy, |
| 5198 | Int8PtrTy, |
| 5199 | ClassExtensionPtrTy, |
| 5200 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5201 | |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5202 | ClassPtrTy = llvm::PointerType::getUnqual(ClassTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5203 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5204 | // struct _objc_category { |
| 5205 | // char *category_name; |
| 5206 | // char *class_name; |
| 5207 | // struct _objc_method_list *instance_method; |
| 5208 | // struct _objc_method_list *class_method; |
| 5209 | // uint32_t size; // sizeof(struct _objc_category) |
| 5210 | // struct _objc_property_list *instance_properties;// category's @property |
| 5211 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5212 | CategoryTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5213 | llvm::StructType::create("struct._objc_category", |
| 5214 | Int8PtrTy, Int8PtrTy, MethodListPtrTy, |
| 5215 | MethodListPtrTy, ProtocolListPtrTy, |
| 5216 | IntTy, PropertyListPtrTy, NULL); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 5217 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5218 | // Global metadata structures |
| 5219 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5220 | // struct _objc_symtab { |
| 5221 | // long sel_ref_cnt; |
| 5222 | // SEL *refs; |
| 5223 | // short cls_def_cnt; |
| 5224 | // short cat_def_cnt; |
| 5225 | // char *defs[cls_def_cnt + cat_def_cnt]; |
| 5226 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5227 | SymtabTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5228 | llvm::StructType::create("struct._objc_symtab", |
| 5229 | LongTy, SelectorPtrTy, ShortTy, ShortTy, |
| 5230 | llvm::ArrayType::get(Int8PtrTy, 0), NULL); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5231 | SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5232 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 5233 | // struct _objc_module { |
| 5234 | // long version; |
| 5235 | // long size; // sizeof(struct _objc_module) |
| 5236 | // char *name; |
| 5237 | // struct _objc_symtab* symtab; |
| 5238 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5239 | ModuleTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5240 | llvm::StructType::create("struct._objc_module", |
| 5241 | LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 5242 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5243 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 5244 | // FIXME: This is the size of the setjmp buffer and should be target |
| 5245 | // specific. 18 is what's used on 32-bit X86. |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 5246 | uint64_t SetJmpBufferSize = 18; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5247 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 5248 | // Exceptions |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5249 | llvm::Type *StackPtrTy = llvm::ArrayType::get(CGM.Int8PtrTy, 4); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5250 | |
| 5251 | ExceptionDataTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5252 | llvm::StructType::create("struct._objc_exception_data", |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5253 | llvm::ArrayType::get(CGM.Int32Ty,SetJmpBufferSize), |
| 5254 | StackPtrTy, NULL); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 5255 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 5256 | } |
| 5257 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5258 | ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5259 | : ObjCCommonTypesHelper(cgm) { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5260 | // struct _method_list_t { |
| 5261 | // uint32_t entsize; // sizeof(struct _objc_method) |
| 5262 | // uint32_t method_count; |
| 5263 | // struct _objc_method method_list[method_count]; |
| 5264 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5265 | MethodListnfABITy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5266 | llvm::StructType::create("struct.__method_list_t", IntTy, IntTy, |
| 5267 | llvm::ArrayType::get(MethodTy, 0), NULL); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5268 | // struct method_list_t * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5269 | MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5270 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5271 | // struct _protocol_t { |
| 5272 | // id isa; // NULL |
| 5273 | // const char * const protocol_name; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5274 | // const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5275 | // const struct method_list_t * const instance_methods; |
| 5276 | // const struct method_list_t * const class_methods; |
| 5277 | // const struct method_list_t *optionalInstanceMethods; |
| 5278 | // const struct method_list_t *optionalClassMethods; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5279 | // const struct _prop_list_t * properties; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5280 | // const uint32_t size; // sizeof(struct _protocol_t) |
| 5281 | // const uint32_t flags; // = 0 |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5282 | // const char ** extendedMethodTypes; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5283 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5284 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5285 | // Holder for struct _protocol_list_t * |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5286 | ProtocolListnfABITy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5287 | llvm::StructType::create(VMContext, "struct._objc_protocol_list"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5288 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5289 | ProtocolnfABITy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5290 | llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy, |
| 5291 | llvm::PointerType::getUnqual(ProtocolListnfABITy), |
| 5292 | MethodListnfABIPtrTy, MethodListnfABIPtrTy, |
| 5293 | MethodListnfABIPtrTy, MethodListnfABIPtrTy, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5294 | PropertyListPtrTy, IntTy, IntTy, Int8PtrPtrTy, |
| 5295 | NULL); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5296 | |
| 5297 | // struct _protocol_t* |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5298 | ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5299 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5300 | // struct _protocol_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5301 | // long protocol_count; // Note, this is 32/64 bit |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5302 | // struct _protocol_t *[protocol_count]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5303 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5304 | ProtocolListnfABITy->setBody(LongTy, |
| 5305 | llvm::ArrayType::get(ProtocolnfABIPtrTy, 0), |
| 5306 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5307 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5308 | // struct _objc_protocol_list* |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5309 | ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5310 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5311 | // struct _ivar_t { |
| 5312 | // unsigned long int *offset; // pointer to ivar offset location |
| 5313 | // char *name; |
| 5314 | // char *type; |
| 5315 | // uint32_t alignment; |
| 5316 | // uint32_t size; |
| 5317 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5318 | IvarnfABITy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5319 | llvm::StructType::create("struct._ivar_t", |
| 5320 | llvm::PointerType::getUnqual(LongTy), |
| 5321 | Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5322 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5323 | // struct _ivar_list_t { |
| 5324 | // uint32 entsize; // sizeof(struct _ivar_t) |
| 5325 | // uint32 count; |
| 5326 | // struct _iver_t list[count]; |
| 5327 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5328 | IvarListnfABITy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5329 | llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy, |
| 5330 | llvm::ArrayType::get(IvarnfABITy, 0), NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5331 | |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5332 | IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5333 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5334 | // struct _class_ro_t { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5335 | // uint32_t const flags; |
| 5336 | // uint32_t const instanceStart; |
| 5337 | // uint32_t const instanceSize; |
| 5338 | // uint32_t const reserved; // only when building for 64bit targets |
| 5339 | // const uint8_t * const ivarLayout; |
| 5340 | // const char *const name; |
| 5341 | // const struct _method_list_t * const baseMethods; |
| 5342 | // const struct _objc_protocol_list *const baseProtocols; |
| 5343 | // const struct _ivar_list_t *const ivars; |
| 5344 | // const uint8_t * const weakIvarLayout; |
| 5345 | // const struct _prop_list_t * const properties; |
| 5346 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5347 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5348 | // FIXME. Add 'reserved' field in 64bit abi mode! |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5349 | ClassRonfABITy = llvm::StructType::create("struct._class_ro_t", |
| 5350 | IntTy, IntTy, IntTy, Int8PtrTy, |
| 5351 | Int8PtrTy, MethodListnfABIPtrTy, |
| 5352 | ProtocolListnfABIPtrTy, |
| 5353 | IvarListnfABIPtrTy, |
| 5354 | Int8PtrTy, PropertyListPtrTy, NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5355 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5356 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5357 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 5358 | ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false) |
| 5359 | ->getPointerTo(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5360 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5361 | // struct _class_t { |
| 5362 | // struct _class_t *isa; |
| 5363 | // struct _class_t * const superclass; |
| 5364 | // void *cache; |
| 5365 | // IMP *vtable; |
| 5366 | // struct class_ro_t *ro; |
| 5367 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5368 | |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5369 | ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t"); |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5370 | ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy), |
| 5371 | llvm::PointerType::getUnqual(ClassnfABITy), |
| 5372 | CachePtrTy, |
| 5373 | llvm::PointerType::getUnqual(ImpnfABITy), |
| 5374 | llvm::PointerType::getUnqual(ClassRonfABITy), |
| 5375 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5376 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5377 | // LLVM for struct _class_t * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5378 | ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5379 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5380 | // struct _category_t { |
| 5381 | // const char * const name; |
| 5382 | // struct _class_t *const cls; |
| 5383 | // const struct _method_list_t * const instance_methods; |
| 5384 | // const struct _method_list_t * const class_methods; |
| 5385 | // const struct _protocol_list_t * const protocols; |
| 5386 | // const struct _prop_list_t * const properties; |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 5387 | // } |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5388 | CategorynfABITy = llvm::StructType::create("struct._category_t", |
| 5389 | Int8PtrTy, ClassnfABIPtrTy, |
| 5390 | MethodListnfABIPtrTy, |
| 5391 | MethodListnfABIPtrTy, |
| 5392 | ProtocolListnfABIPtrTy, |
| 5393 | PropertyListPtrTy, |
| 5394 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5395 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 5396 | // New types for nonfragile abi messaging. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5397 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 5398 | ASTContext &Ctx = CGM.getContext(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5399 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 5400 | // MessageRefTy - LLVM for: |
| 5401 | // struct _message_ref_t { |
| 5402 | // IMP messenger; |
| 5403 | // SEL name; |
| 5404 | // }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5405 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5406 | // First the clang type for struct _message_ref_t |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5407 | RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct, |
Daniel Dunbar | daa3ac5 | 2010-04-29 16:29:11 +0000 | [diff] [blame] | 5408 | Ctx.getTranslationUnitDecl(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5409 | SourceLocation(), SourceLocation(), |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5410 | &Ctx.Idents.get("_message_ref_t")); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5411 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 5412 | Ctx.VoidPtrTy, 0, 0, false, ICIS_NoInit)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5413 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 5414 | Ctx.getObjCSelType(), 0, 0, false, |
| 5415 | ICIS_NoInit)); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 5416 | RD->completeDefinition(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5417 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5418 | MessageRefCTy = Ctx.getTagDeclType(RD); |
| 5419 | MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy); |
| 5420 | MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5421 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 5422 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5423 | MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5424 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 5425 | // SuperMessageRefTy - LLVM for: |
| 5426 | // struct _super_message_ref_t { |
| 5427 | // SUPER_IMP messenger; |
| 5428 | // SEL name; |
| 5429 | // }; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5430 | SuperMessageRefTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5431 | llvm::StructType::create("struct._super_message_ref_t", |
| 5432 | ImpnfABITy, SelectorPtrTy, NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5433 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 5434 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5435 | SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy); |
Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 5436 | |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5437 | |
| 5438 | // struct objc_typeinfo { |
| 5439 | // const void** vtable; // objc_ehtype_vtable + 2 |
| 5440 | // const char* name; // c++ typeinfo string |
| 5441 | // Class cls; |
| 5442 | // }; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5443 | EHTypeTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5444 | llvm::StructType::create("struct._objc_typeinfo", |
| 5445 | llvm::PointerType::getUnqual(Int8PtrTy), |
| 5446 | Int8PtrTy, ClassnfABIPtrTy, NULL); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5447 | EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 5448 | } |
| 5449 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5450 | llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5451 | FinishNonFragileABIModule(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5452 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5453 | return NULL; |
| 5454 | } |
| 5455 | |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 5456 | void CGObjCNonFragileABIMac:: |
| 5457 | AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container, |
| 5458 | const char *SymbolName, |
| 5459 | const char *SectionName) { |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5460 | unsigned NumClasses = Container.size(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5461 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5462 | if (!NumClasses) |
| 5463 | return; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5464 | |
Chris Lattner | 0b23971 | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 5465 | SmallVector<llvm::Constant*, 8> Symbols(NumClasses); |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5466 | for (unsigned i=0; i<NumClasses; i++) |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 5467 | Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i], |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5468 | ObjCTypes.Int8PtrTy); |
Chris Lattner | 0b23971 | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 5469 | llvm::Constant *Init = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5470 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Chris Lattner | 0b23971 | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 5471 | Symbols.size()), |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5472 | Symbols); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5473 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5474 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5475 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5476 | llvm::GlobalValue::InternalLinkage, |
| 5477 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5478 | SymbolName); |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 5479 | GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType())); |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5480 | GV->setSection(SectionName); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5481 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5482 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5483 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5484 | void CGObjCNonFragileABIMac::FinishNonFragileABIModule() { |
| 5485 | // nonfragile abi has no module definition. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5486 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5487 | // Build list of all implemented class addresses in array |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 5488 | // L_OBJC_LABEL_CLASS_$. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5489 | AddModuleClassList(DefinedClasses, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5490 | "\01L_OBJC_LABEL_CLASS_$", |
| 5491 | "__DATA, __objc_classlist, regular, no_dead_strip"); |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 5492 | |
Bill Wendling | 13562a1 | 2012-02-07 09:06:01 +0000 | [diff] [blame] | 5493 | for (unsigned i = 0, e = DefinedClasses.size(); i < e; i++) { |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 5494 | llvm::GlobalValue *IMPLGV = DefinedClasses[i]; |
| 5495 | if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage) |
| 5496 | continue; |
| 5497 | IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 5498 | } |
| 5499 | |
Bill Wendling | 13562a1 | 2012-02-07 09:06:01 +0000 | [diff] [blame] | 5500 | for (unsigned i = 0, e = DefinedMetaClasses.size(); i < e; i++) { |
Fariborz Jahanian | 0e93d25 | 2009-12-01 18:25:24 +0000 | [diff] [blame] | 5501 | llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i]; |
| 5502 | if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage) |
| 5503 | continue; |
| 5504 | IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 5505 | } |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 5506 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5507 | AddModuleClassList(DefinedNonLazyClasses, |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5508 | "\01L_OBJC_LABEL_NONLAZY_CLASS_$", |
| 5509 | "__DATA, __objc_nlclslist, regular, no_dead_strip"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5510 | |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 5511 | // Build list of all implemented category addresses in array |
| 5512 | // L_OBJC_LABEL_CATEGORY_$. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5513 | AddModuleClassList(DefinedCategories, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5514 | "\01L_OBJC_LABEL_CATEGORY_$", |
| 5515 | "__DATA, __objc_catlist, regular, no_dead_strip"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5516 | AddModuleClassList(DefinedNonLazyCategories, |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5517 | "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$", |
| 5518 | "__DATA, __objc_nlcatlist, regular, no_dead_strip"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5519 | |
Daniel Dunbar | fce176b | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 5520 | EmitImageInfo(); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5521 | } |
| 5522 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5523 | /// isVTableDispatchedSelector - Returns true if SEL is not in the list of |
| 5524 | /// VTableDispatchMethods; false otherwise. What this means is that |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5525 | /// except for the 19 selectors in the list, we generate 32bit-style |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5526 | /// message dispatch call for all the rest. |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5527 | bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) { |
| 5528 | // At various points we've experimented with using vtable-based |
| 5529 | // dispatch for all methods. |
Daniel Dunbar | f643b9b | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 5530 | switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) { |
Daniel Dunbar | f643b9b | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 5531 | case CodeGenOptions::Legacy: |
Fariborz Jahanian | 776dbf9 | 2010-04-19 17:53:30 +0000 | [diff] [blame] | 5532 | return false; |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5533 | case CodeGenOptions::NonLegacy: |
| 5534 | return true; |
Daniel Dunbar | f643b9b | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 5535 | case CodeGenOptions::Mixed: |
| 5536 | break; |
| 5537 | } |
| 5538 | |
| 5539 | // If so, see whether this selector is in the white-list of things which must |
| 5540 | // use the new dispatch convention. We lazily build a dense set for this. |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5541 | if (VTableDispatchMethods.empty()) { |
| 5542 | VTableDispatchMethods.insert(GetNullarySelector("alloc")); |
| 5543 | VTableDispatchMethods.insert(GetNullarySelector("class")); |
| 5544 | VTableDispatchMethods.insert(GetNullarySelector("self")); |
| 5545 | VTableDispatchMethods.insert(GetNullarySelector("isFlipped")); |
| 5546 | VTableDispatchMethods.insert(GetNullarySelector("length")); |
| 5547 | VTableDispatchMethods.insert(GetNullarySelector("count")); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5548 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5549 | // These are vtable-based if GC is disabled. |
| 5550 | // Optimistically use vtable dispatch for hybrid compiles. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5551 | if (CGM.getLangOpts().getGC() != LangOptions::GCOnly) { |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5552 | VTableDispatchMethods.insert(GetNullarySelector("retain")); |
| 5553 | VTableDispatchMethods.insert(GetNullarySelector("release")); |
| 5554 | VTableDispatchMethods.insert(GetNullarySelector("autorelease")); |
| 5555 | } |
| 5556 | |
| 5557 | VTableDispatchMethods.insert(GetUnarySelector("allocWithZone")); |
| 5558 | VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass")); |
| 5559 | VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector")); |
| 5560 | VTableDispatchMethods.insert(GetUnarySelector("objectForKey")); |
| 5561 | VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex")); |
| 5562 | VTableDispatchMethods.insert(GetUnarySelector("isEqualToString")); |
| 5563 | VTableDispatchMethods.insert(GetUnarySelector("isEqual")); |
| 5564 | |
| 5565 | // These are vtable-based if GC is enabled. |
| 5566 | // Optimistically use vtable dispatch for hybrid compiles. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5567 | if (CGM.getLangOpts().getGC() != LangOptions::NonGC) { |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5568 | VTableDispatchMethods.insert(GetNullarySelector("hash")); |
| 5569 | VTableDispatchMethods.insert(GetUnarySelector("addObject")); |
| 5570 | |
| 5571 | // "countByEnumeratingWithState:objects:count" |
| 5572 | IdentifierInfo *KeyIdents[] = { |
| 5573 | &CGM.getContext().Idents.get("countByEnumeratingWithState"), |
| 5574 | &CGM.getContext().Idents.get("objects"), |
| 5575 | &CGM.getContext().Idents.get("count") |
| 5576 | }; |
| 5577 | VTableDispatchMethods.insert( |
| 5578 | CGM.getContext().Selectors.getSelector(3, KeyIdents)); |
| 5579 | } |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5580 | } |
Daniel Dunbar | f643b9b | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 5581 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5582 | return VTableDispatchMethods.count(Sel); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5583 | } |
| 5584 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5585 | /// BuildClassRoTInitializer - generate meta-data for: |
| 5586 | /// struct _class_ro_t { |
| 5587 | /// uint32_t const flags; |
| 5588 | /// uint32_t const instanceStart; |
| 5589 | /// uint32_t const instanceSize; |
| 5590 | /// uint32_t const reserved; // only when building for 64bit targets |
| 5591 | /// const uint8_t * const ivarLayout; |
| 5592 | /// const char *const name; |
| 5593 | /// const struct _method_list_t * const baseMethods; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5594 | /// const struct _protocol_list_t *const baseProtocols; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5595 | /// const struct _ivar_list_t *const ivars; |
| 5596 | /// const uint8_t * const weakIvarLayout; |
| 5597 | /// const struct _prop_list_t * const properties; |
| 5598 | /// } |
| 5599 | /// |
| 5600 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5601 | unsigned flags, |
| 5602 | unsigned InstanceStart, |
| 5603 | unsigned InstanceSize, |
| 5604 | const ObjCImplementationDecl *ID) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5605 | std::string ClassName = ID->getNameAsString(); |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5606 | llvm::Constant *Values[10]; // 11 for 64bit targets! |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5607 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5608 | if (CGM.getLangOpts().ObjCAutoRefCount) |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5609 | flags |= NonFragileABI_Class_CompiledByARC; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5610 | |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5611 | Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
| 5612 | Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart); |
| 5613 | Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5614 | // FIXME. For 64bit targets add 0 here. |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5615 | Values[ 3] = (flags & NonFragileABI_Class_Meta) |
| 5616 | ? GetIvarLayoutName(0, ObjCTypes) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5617 | : BuildIvarLayout(ID, true); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5618 | Values[ 4] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5619 | // const struct _method_list_t * const baseMethods; |
| 5620 | std::vector<llvm::Constant*> Methods; |
| 5621 | std::string MethodListName("\01l_OBJC_$_"); |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5622 | if (flags & NonFragileABI_Class_Meta) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5623 | MethodListName += "CLASS_METHODS_" + ID->getNameAsString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5624 | for (ObjCImplementationDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5625 | i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5626 | // Class methods should always be defined. |
| 5627 | Methods.push_back(GetMethodConstant(*i)); |
| 5628 | } |
| 5629 | } else { |
| 5630 | MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5631 | for (ObjCImplementationDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5632 | i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5633 | // Instance methods should always be defined. |
| 5634 | Methods.push_back(GetMethodConstant(*i)); |
| 5635 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5636 | for (ObjCImplementationDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5637 | i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5638 | ObjCPropertyImplDecl *PID = *i; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5639 | |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 5640 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){ |
| 5641 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5642 | |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 5643 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 5644 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 5645 | Methods.push_back(C); |
| 5646 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 5647 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 5648 | Methods.push_back(C); |
| 5649 | } |
| 5650 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5651 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5652 | Values[ 5] = EmitMethodList(MethodListName, |
| 5653 | "__DATA, __objc_const", Methods); |
| 5654 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5655 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 5656 | assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5657 | Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5658 | + OID->getName(), |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 5659 | OID->all_referenced_protocol_begin(), |
| 5660 | OID->all_referenced_protocol_end()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5661 | |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5662 | if (flags & NonFragileABI_Class_Meta) { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5663 | Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5664 | Values[ 8] = GetIvarLayoutName(0, ObjCTypes); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5665 | Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5666 | } else { |
| 5667 | Values[ 7] = EmitIvarList(ID); |
| 5668 | Values[ 8] = BuildIvarLayout(ID, false); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5669 | Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(), |
| 5670 | ID, ID->getClassInterface(), ObjCTypes); |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5671 | } |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5672 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5673 | Values); |
| 5674 | llvm::GlobalVariable *CLASS_RO_GV = |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5675 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false, |
| 5676 | llvm::GlobalValue::InternalLinkage, |
| 5677 | Init, |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5678 | (flags & NonFragileABI_Class_Meta) ? |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5679 | std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName : |
| 5680 | std::string("\01l_OBJC_CLASS_RO_$_")+ClassName); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5681 | CLASS_RO_GV->setAlignment( |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 5682 | CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassRonfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5683 | CLASS_RO_GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5684 | return CLASS_RO_GV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5685 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5686 | } |
| 5687 | |
| 5688 | /// BuildClassMetaData - This routine defines that to-level meta-data |
| 5689 | /// for the given ClassName for: |
| 5690 | /// struct _class_t { |
| 5691 | /// struct _class_t *isa; |
| 5692 | /// struct _class_t * const superclass; |
| 5693 | /// void *cache; |
| 5694 | /// IMP *vtable; |
| 5695 | /// struct class_ro_t *ro; |
| 5696 | /// } |
| 5697 | /// |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5698 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5699 | std::string &ClassName, |
| 5700 | llvm::Constant *IsAGV, |
| 5701 | llvm::Constant *SuperClassGV, |
| 5702 | llvm::Constant *ClassRoGV, |
| 5703 | bool HiddenVisibility) { |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5704 | llvm::Constant *Values[] = { |
| 5705 | IsAGV, |
| 5706 | SuperClassGV, |
| 5707 | ObjCEmptyCacheVar, // &ObjCEmptyCacheVar |
| 5708 | ObjCEmptyVtableVar, // &ObjCEmptyVtableVar |
| 5709 | ClassRoGV // &CLASS_RO_GV |
| 5710 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5711 | if (!Values[1]) |
| 5712 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5713 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5714 | Values); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5715 | llvm::GlobalVariable *GV = GetClassGlobal(ClassName); |
| 5716 | GV->setInitializer(Init); |
Fariborz Jahanian | dd0db2a | 2009-01-31 01:07:39 +0000 | [diff] [blame] | 5717 | GV->setSection("__DATA, __objc_data"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5718 | GV->setAlignment( |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 5719 | CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassnfABITy)); |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5720 | if (HiddenVisibility) |
| 5721 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5722 | return GV; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5723 | } |
| 5724 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5725 | bool |
Fariborz Jahanian | ecfbdcb | 2009-05-21 01:03:45 +0000 | [diff] [blame] | 5726 | CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5727 | return OD->getClassMethod(GetNullarySelector("load")) != 0; |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5728 | } |
| 5729 | |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 5730 | void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 5731 | uint32_t &InstanceStart, |
| 5732 | uint32_t &InstanceSize) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5733 | const ASTRecordLayout &RL = |
Daniel Dunbar | b4c79e0 | 2009-05-04 21:26:30 +0000 | [diff] [blame] | 5734 | CGM.getContext().getASTObjCImplementationLayout(OID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5735 | |
Daniel Dunbar | 6e8575b | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 5736 | // InstanceSize is really instance end. |
Ken Dyck | ec29903 | 2011-02-11 02:20:09 +0000 | [diff] [blame] | 5737 | InstanceSize = RL.getDataSize().getQuantity(); |
Daniel Dunbar | 6e8575b | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 5738 | |
| 5739 | // If there are no fields, the start is the same as the end. |
| 5740 | if (!RL.getFieldCount()) |
| 5741 | InstanceStart = InstanceSize; |
| 5742 | else |
Ken Dyck | fb67ccd | 2011-04-14 00:43:09 +0000 | [diff] [blame] | 5743 | InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth(); |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 5744 | } |
| 5745 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5746 | void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) { |
| 5747 | std::string ClassName = ID->getNameAsString(); |
| 5748 | if (!ObjCEmptyCacheVar) { |
| 5749 | ObjCEmptyCacheVar = new llvm::GlobalVariable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5750 | CGM.getModule(), |
| 5751 | ObjCTypes.CacheTy, |
| 5752 | false, |
| 5753 | llvm::GlobalValue::ExternalLinkage, |
| 5754 | 0, |
| 5755 | "_objc_empty_cache"); |
| 5756 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5757 | ObjCEmptyVtableVar = new llvm::GlobalVariable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5758 | CGM.getModule(), |
| 5759 | ObjCTypes.ImpnfABITy, |
| 5760 | false, |
| 5761 | llvm::GlobalValue::ExternalLinkage, |
| 5762 | 0, |
| 5763 | "_objc_empty_vtable"); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5764 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5765 | assert(ID->getClassInterface() && |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5766 | "CGObjCNonFragileABIMac::GenerateClass - class is 0"); |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 5767 | // FIXME: Is this correct (that meta class size is never computed)? |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5768 | uint32_t InstanceStart = |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 5769 | CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassnfABITy); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5770 | uint32_t InstanceSize = InstanceStart; |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5771 | uint32_t flags = NonFragileABI_Class_Meta; |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5772 | std::string ObjCMetaClassName(getMetaclassSymbolPrefix()); |
| 5773 | std::string ObjCClassName(getClassSymbolPrefix()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5774 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5775 | llvm::GlobalVariable *SuperClassGV, *IsAGV; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5776 | |
John McCall | b03527a | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 5777 | // Build the flags for the metaclass. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5778 | bool classIsHidden = |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 5779 | ID->getClassInterface()->getVisibility() == HiddenVisibility; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5780 | if (classIsHidden) |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5781 | flags |= NonFragileABI_Class_Hidden; |
John McCall | b03527a | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 5782 | |
| 5783 | // FIXME: why is this flag set on the metaclass? |
| 5784 | // ObjC metaclasses have no fields and don't really get constructed. |
| 5785 | if (ID->hasNonZeroConstructors() || ID->hasDestructors()) { |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5786 | flags |= NonFragileABI_Class_HasCXXStructors; |
John McCall | b03527a | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 5787 | if (!ID->hasNonZeroConstructors()) |
| 5788 | flags |= NonFragileABI_Class_HasCXXDestructorOnly; |
| 5789 | } |
| 5790 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5791 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5792 | // class is root |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5793 | flags |= NonFragileABI_Class_Root; |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5794 | SuperClassGV = GetClassGlobal(ObjCClassName + ClassName); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5795 | IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5796 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5797 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 5798 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 5799 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 5800 | Root = Super; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5801 | IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString()); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5802 | if (Root->isWeakImported()) |
Fariborz Jahanian | 0e93d25 | 2009-12-01 18:25:24 +0000 | [diff] [blame] | 5803 | IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 5804 | // work on super class metadata symbol. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5805 | std::string SuperClassName = |
Fariborz Jahanian | 0e93d25 | 2009-12-01 18:25:24 +0000 | [diff] [blame] | 5806 | ObjCMetaClassName + |
| 5807 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5808 | SuperClassGV = GetClassGlobal(SuperClassName); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5809 | if (ID->getClassInterface()->getSuperClass()->isWeakImported()) |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 5810 | SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5811 | } |
| 5812 | llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags, |
| 5813 | InstanceStart, |
| 5814 | InstanceSize,ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5815 | std::string TClassName = ObjCMetaClassName + ClassName; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5816 | llvm::GlobalVariable *MetaTClass = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5817 | BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV, |
| 5818 | classIsHidden); |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 5819 | DefinedMetaClasses.push_back(MetaTClass); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5820 | |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5821 | // Metadata for the class |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5822 | flags = 0; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5823 | if (classIsHidden) |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5824 | flags |= NonFragileABI_Class_Hidden; |
John McCall | b03527a | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 5825 | |
| 5826 | if (ID->hasNonZeroConstructors() || ID->hasDestructors()) { |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5827 | flags |= NonFragileABI_Class_HasCXXStructors; |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5828 | |
John McCall | b03527a | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 5829 | // Set a flag to enable a runtime optimization when a class has |
| 5830 | // fields that require destruction but which don't require |
| 5831 | // anything except zero-initialization during construction. This |
| 5832 | // is most notably true of __strong and __weak types, but you can |
| 5833 | // also imagine there being C++ types with non-trivial default |
| 5834 | // constructors that merely set all fields to null. |
| 5835 | if (!ID->hasNonZeroConstructors()) |
| 5836 | flags |= NonFragileABI_Class_HasCXXDestructorOnly; |
| 5837 | } |
| 5838 | |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 5839 | if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface())) |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5840 | flags |= NonFragileABI_Class_Exception; |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5841 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5842 | if (!ID->getClassInterface()->getSuperClass()) { |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5843 | flags |= NonFragileABI_Class_Root; |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5844 | SuperClassGV = 0; |
Chris Lattner | b7b58b1 | 2009-04-19 06:02:28 +0000 | [diff] [blame] | 5845 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5846 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 5847 | std::string RootClassName = |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5848 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5849 | SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5850 | if (ID->getClassInterface()->getSuperClass()->isWeakImported()) |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 5851 | SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5852 | } |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 5853 | GetClassSizeInfo(ID, InstanceStart, InstanceSize); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5854 | CLASS_RO_GV = BuildClassRoTInitializer(flags, |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 5855 | InstanceStart, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5856 | InstanceSize, |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 5857 | ID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5858 | |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5859 | TClassName = ObjCClassName + ClassName; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5860 | llvm::GlobalVariable *ClassMD = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5861 | BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV, |
| 5862 | classIsHidden); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 5863 | DefinedClasses.push_back(ClassMD); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5864 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5865 | // Determine if this class is also "non-lazy". |
| 5866 | if (ImplementationIsNonLazy(ID)) |
| 5867 | DefinedNonLazyClasses.push_back(ClassMD); |
| 5868 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5869 | // Force the definition of the EHType if necessary. |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5870 | if (flags & NonFragileABI_Class_Exception) |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5871 | GetInterfaceEHType(ID->getClassInterface(), true); |
Fariborz Jahanian | 64089ce | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 5872 | // Make sure method definition entries are all clear for next implementation. |
| 5873 | MethodDefinitions.clear(); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5874 | } |
| 5875 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5876 | /// GenerateProtocolRef - This routine is called to generate code for |
| 5877 | /// a protocol reference expression; as in: |
| 5878 | /// @code |
| 5879 | /// @protocol(Proto1); |
| 5880 | /// @endcode |
| 5881 | /// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1 |
| 5882 | /// which will hold address of the protocol meta-data. |
| 5883 | /// |
| 5884 | llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5885 | const ObjCProtocolDecl *PD) { |
| 5886 | |
Fariborz Jahanian | 960cd06 | 2009-04-10 18:47:34 +0000 | [diff] [blame] | 5887 | // This routine is called for @protocol only. So, we must build definition |
| 5888 | // of protocol's meta-data (not a reference to it!) |
| 5889 | // |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5890 | llvm::Constant *Init = |
| 5891 | llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD), |
Douglas Gregor | 4c86fdb | 2012-01-17 18:36:30 +0000 | [diff] [blame] | 5892 | ObjCTypes.getExternalProtocolPtrTy()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5893 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5894 | std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_"); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5895 | ProtocolName += PD->getName(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5896 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5897 | llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName); |
| 5898 | if (PTGV) |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 5899 | return Builder.CreateLoad(PTGV); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5900 | PTGV = new llvm::GlobalVariable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5901 | CGM.getModule(), |
| 5902 | Init->getType(), false, |
| 5903 | llvm::GlobalValue::WeakAnyLinkage, |
| 5904 | Init, |
| 5905 | ProtocolName); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5906 | PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip"); |
| 5907 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5908 | CGM.AddUsedGlobal(PTGV); |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 5909 | return Builder.CreateLoad(PTGV); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5910 | } |
| 5911 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5912 | /// GenerateCategory - Build metadata for a category implementation. |
| 5913 | /// struct _category_t { |
| 5914 | /// const char * const name; |
| 5915 | /// struct _class_t *const cls; |
| 5916 | /// const struct _method_list_t * const instance_methods; |
| 5917 | /// const struct _method_list_t * const class_methods; |
| 5918 | /// const struct _protocol_list_t * const protocols; |
| 5919 | /// const struct _prop_list_t * const properties; |
| 5920 | /// } |
| 5921 | /// |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5922 | void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5923 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5924 | const char *Prefix = "\01l_OBJC_$_CATEGORY_"; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5925 | std::string ExtCatName(Prefix + Interface->getNameAsString()+ |
| 5926 | "_$_" + OCD->getNameAsString()); |
| 5927 | std::string ExtClassName(getClassSymbolPrefix() + |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5928 | Interface->getNameAsString()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5929 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5930 | llvm::Constant *Values[6]; |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5931 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 5932 | // meta-class entry symbol |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5933 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5934 | if (Interface->isWeakImported()) |
Fariborz Jahanian | 2cdcc4c | 2009-11-17 22:02:21 +0000 | [diff] [blame] | 5935 | ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
| 5936 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5937 | Values[1] = ClassGV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5938 | std::vector<llvm::Constant*> Methods; |
| 5939 | std::string MethodListName(Prefix); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5940 | MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() + |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5941 | "_$_" + OCD->getNameAsString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5942 | |
| 5943 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5944 | i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5945 | // Instance methods should always be defined. |
| 5946 | Methods.push_back(GetMethodConstant(*i)); |
| 5947 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5948 | |
| 5949 | Values[2] = EmitMethodList(MethodListName, |
| 5950 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5951 | Methods); |
| 5952 | |
| 5953 | MethodListName = Prefix; |
| 5954 | MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" + |
| 5955 | OCD->getNameAsString(); |
| 5956 | Methods.clear(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5957 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5958 | i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5959 | // Class methods should always be defined. |
| 5960 | Methods.push_back(GetMethodConstant(*i)); |
| 5961 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5962 | |
| 5963 | Values[3] = EmitMethodList(MethodListName, |
| 5964 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5965 | Methods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5966 | const ObjCCategoryDecl *Category = |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 5967 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5968 | if (Category) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 5969 | SmallString<256> ExtName; |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5970 | llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_" |
| 5971 | << OCD->getName(); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5972 | Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5973 | + Interface->getName() + "_$_" |
| 5974 | + Category->getName(), |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5975 | Category->protocol_begin(), |
| 5976 | Category->protocol_end()); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5977 | Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(), |
| 5978 | OCD, Category, ObjCTypes); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5979 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5980 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 5981 | Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5982 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5983 | |
| 5984 | llvm::Constant *Init = |
| 5985 | llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy, |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5986 | Values); |
| 5987 | llvm::GlobalVariable *GCATV |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5988 | = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy, |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5989 | false, |
| 5990 | llvm::GlobalValue::InternalLinkage, |
| 5991 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5992 | ExtCatName); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5993 | GCATV->setAlignment( |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 5994 | CGM.getDataLayout().getABITypeAlignment(ObjCTypes.CategorynfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5995 | GCATV->setSection("__DATA, __objc_const"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5996 | CGM.AddUsedGlobal(GCATV); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5997 | DefinedCategories.push_back(GCATV); |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5998 | |
| 5999 | // Determine if this category is also "non-lazy". |
| 6000 | if (ImplementationIsNonLazy(OCD)) |
| 6001 | DefinedNonLazyCategories.push_back(GCATV); |
Fariborz Jahanian | 64089ce | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 6002 | // method definition entries must be clear for next implementation. |
| 6003 | MethodDefinitions.clear(); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 6004 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 6005 | |
| 6006 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 6007 | /// given method if it has been defined. The result is null if the |
| 6008 | /// method has not been defined. The return value has type MethodPtrTy. |
| 6009 | llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6010 | const ObjCMethodDecl *MD) { |
Argyrios Kyrtzidis | 9d50c06 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 6011 | llvm::Function *Fn = GetMethodDefinition(MD); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 6012 | if (!Fn) |
| 6013 | return 0; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6014 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 6015 | llvm::Constant *Method[] = { |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 6016 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 6017 | ObjCTypes.SelectorPtrTy), |
| 6018 | GetMethodVarType(MD), |
| 6019 | llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy) |
| 6020 | }; |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 6021 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 6022 | } |
| 6023 | |
| 6024 | /// EmitMethodList - Build meta-data for method declarations |
| 6025 | /// struct _method_list_t { |
| 6026 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 6027 | /// uint32_t method_count; |
| 6028 | /// struct _objc_method method_list[method_count]; |
| 6029 | /// } |
| 6030 | /// |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 6031 | llvm::Constant * |
| 6032 | CGObjCNonFragileABIMac::EmitMethodList(Twine Name, |
| 6033 | const char *Section, |
| 6034 | ArrayRef<llvm::Constant*> Methods) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 6035 | // Return null for empty list. |
| 6036 | if (Methods.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 6037 | return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6038 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 6039 | llvm::Constant *Values[3]; |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 6040 | // sizeof(struct _objc_method) |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6041 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.MethodTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 6042 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 6043 | // method_count |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 6044 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 6045 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 6046 | Methods.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 6047 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 6048 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6049 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 6050 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6051 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 6052 | llvm::GlobalValue::InternalLinkage, Init, Name); |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6053 | GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType())); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 6054 | GV->setSection(Section); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 6055 | CGM.AddUsedGlobal(GV); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 6056 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 6057 | } |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6058 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 6059 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 6060 | /// the given ivar. |
Daniel Dunbar | e83be12 | 2010-04-02 21:14:02 +0000 | [diff] [blame] | 6061 | llvm::GlobalVariable * |
| 6062 | CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID, |
| 6063 | const ObjCIvarDecl *Ivar) { |
| 6064 | const ObjCInterfaceDecl *Container = Ivar->getContainingInterface(); |
Daniel Dunbar | a81419d | 2009-05-05 00:36:57 +0000 | [diff] [blame] | 6065 | std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() + |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 6066 | '.' + Ivar->getNameAsString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6067 | llvm::GlobalVariable *IvarOffsetGV = |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 6068 | CGM.getModule().getGlobalVariable(Name); |
| 6069 | if (!IvarOffsetGV) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6070 | IvarOffsetGV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6071 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 6072 | false, |
| 6073 | llvm::GlobalValue::ExternalLinkage, |
| 6074 | 0, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6075 | Name); |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 6076 | return IvarOffsetGV; |
| 6077 | } |
| 6078 | |
Daniel Dunbar | e83be12 | 2010-04-02 21:14:02 +0000 | [diff] [blame] | 6079 | llvm::Constant * |
| 6080 | CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
| 6081 | const ObjCIvarDecl *Ivar, |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 6082 | unsigned long int Offset) { |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 6083 | llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6084 | IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy, |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 6085 | Offset)); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 6086 | IvarOffsetGV->setAlignment( |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6087 | CGM.getDataLayout().getABITypeAlignment(ObjCTypes.LongTy)); |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 6088 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 6089 | // FIXME: This matches gcc, but shouldn't the visibility be set on the use as |
| 6090 | // well (i.e., in ObjCIvarOffsetVariable). |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 6091 | if (Ivar->getAccessControl() == ObjCIvarDecl::Private || |
| 6092 | Ivar->getAccessControl() == ObjCIvarDecl::Package || |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 6093 | ID->getVisibility() == HiddenVisibility) |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 6094 | IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 6095 | else |
Fariborz Jahanian | 77c9fd2 | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 6096 | IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Bill Wendling | 1f38251 | 2011-05-04 21:37:25 +0000 | [diff] [blame] | 6097 | IvarOffsetGV->setSection("__DATA, __objc_ivar"); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 6098 | return IvarOffsetGV; |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 6099 | } |
| 6100 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6101 | /// EmitIvarList - Emit the ivar list for the given |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6102 | /// implementation. The return value has type |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6103 | /// IvarListnfABIPtrTy. |
| 6104 | /// struct _ivar_t { |
| 6105 | /// unsigned long int *offset; // pointer to ivar offset location |
| 6106 | /// char *name; |
| 6107 | /// char *type; |
| 6108 | /// uint32_t alignment; |
| 6109 | /// uint32_t size; |
| 6110 | /// } |
| 6111 | /// struct _ivar_list_t { |
| 6112 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 6113 | /// uint32 count; |
| 6114 | /// struct _iver_t list[count]; |
| 6115 | /// } |
| 6116 | /// |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 6117 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6118 | llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6119 | const ObjCImplementationDecl *ID) { |
| 6120 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 6121 | std::vector<llvm::Constant*> Ivars; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6122 | |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 6123 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6124 | assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6125 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 6126 | // FIXME. Consolidate this with similar code in GenerateClass. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6127 | |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 6128 | for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin(); |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 6129 | IVD; IVD = IVD->getNextIvar()) { |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 6130 | // Ignore unnamed bit-fields. |
| 6131 | if (!IVD->getDeclName()) |
| 6132 | continue; |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 6133 | llvm::Constant *Ivar[5]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6134 | Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD, |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 6135 | ComputeIvarBaseOffset(CGM, ID, IVD)); |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 6136 | Ivar[1] = GetMethodVarName(IVD->getIdentifier()); |
| 6137 | Ivar[2] = GetMethodVarType(IVD); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6138 | llvm::Type *FieldTy = |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 6139 | CGM.getTypes().ConvertTypeForMem(IVD->getType()); |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6140 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(FieldTy); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6141 | unsigned Align = CGM.getContext().getPreferredTypeAlign( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6142 | IVD->getType().getTypePtr()) >> 3; |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6143 | Align = llvm::Log2_32(Align); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 6144 | Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align); |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 6145 | // NOTE. Size of a bitfield does not match gcc's, because of the |
| 6146 | // way bitfields are treated special in each. But I am told that |
| 6147 | // 'size' for bitfield ivars is ignored by the runtime so it does |
| 6148 | // not matter. If it matters, there is enough info to get the |
| 6149 | // bitfield right! |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 6150 | Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 6151 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar)); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6152 | } |
| 6153 | // Return null for empty list. |
| 6154 | if (Ivars.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 6155 | return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 6156 | |
| 6157 | llvm::Constant *Values[3]; |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6158 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.IvarnfABITy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 6159 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 6160 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 6161 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6162 | Ivars.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 6163 | Values[2] = llvm::ConstantArray::get(AT, Ivars); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 6164 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6165 | const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_"; |
| 6166 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6167 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6168 | llvm::GlobalValue::InternalLinkage, |
| 6169 | Init, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6170 | Prefix + OID->getName()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 6171 | GV->setAlignment( |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6172 | CGM.getDataLayout().getABITypeAlignment(Init->getType())); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 6173 | GV->setSection("__DATA, __objc_const"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6174 | |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 6175 | CGM.AddUsedGlobal(GV); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 6176 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6177 | } |
| 6178 | |
| 6179 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6180 | const ObjCProtocolDecl *PD) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6181 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6182 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6183 | if (!Entry) { |
| 6184 | // We use the initializer as a marker of whether this is a forward |
| 6185 | // reference or not. At module finalization we add the empty |
| 6186 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6187 | Entry = |
| 6188 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false, |
| 6189 | llvm::GlobalValue::ExternalLinkage, |
| 6190 | 0, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6191 | "\01l_OBJC_PROTOCOL_$_" + PD->getName()); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6192 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6193 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6194 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6195 | return Entry; |
| 6196 | } |
| 6197 | |
| 6198 | /// GetOrEmitProtocol - Generate the protocol meta-data: |
| 6199 | /// @code |
| 6200 | /// struct _protocol_t { |
| 6201 | /// id isa; // NULL |
| 6202 | /// const char * const protocol_name; |
| 6203 | /// const struct _protocol_list_t * protocol_list; // super protocols |
| 6204 | /// const struct method_list_t * const instance_methods; |
| 6205 | /// const struct method_list_t * const class_methods; |
| 6206 | /// const struct method_list_t *optionalInstanceMethods; |
| 6207 | /// const struct method_list_t *optionalClassMethods; |
| 6208 | /// const struct _prop_list_t * properties; |
| 6209 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 6210 | /// const uint32_t flags; // = 0 |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6211 | /// const char ** extendedMethodTypes; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6212 | /// } |
| 6213 | /// @endcode |
| 6214 | /// |
| 6215 | |
| 6216 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6217 | const ObjCProtocolDecl *PD) { |
John McCall | 50651b9 | 2012-03-30 21:29:05 +0000 | [diff] [blame] | 6218 | llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6219 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6220 | // Early exit if a defining object has already been generated. |
| 6221 | if (Entry && Entry->hasInitializer()) |
| 6222 | return Entry; |
| 6223 | |
Douglas Gregor | 1d784b2 | 2012-01-01 19:51:50 +0000 | [diff] [blame] | 6224 | // Use the protocol definition, if there is one. |
| 6225 | if (const ObjCProtocolDecl *Def = PD->getDefinition()) |
| 6226 | PD = Def; |
| 6227 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6228 | // Construct method lists. |
| 6229 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 6230 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6231 | std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6232 | for (ObjCProtocolDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 6233 | i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6234 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 6235 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 6236 | if (!C) |
| 6237 | return GetOrEmitProtocolRef(PD); |
| 6238 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6239 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 6240 | OptInstanceMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6241 | OptMethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6242 | } else { |
| 6243 | InstanceMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6244 | MethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6245 | } |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6246 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6247 | |
| 6248 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 6249 | i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6250 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 6251 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 6252 | if (!C) |
| 6253 | return GetOrEmitProtocolRef(PD); |
| 6254 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6255 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 6256 | OptClassMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6257 | OptMethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6258 | } else { |
| 6259 | ClassMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6260 | MethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6261 | } |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6262 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6263 | |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6264 | MethodTypesExt.insert(MethodTypesExt.end(), |
| 6265 | OptMethodTypesExt.begin(), OptMethodTypesExt.end()); |
| 6266 | |
| 6267 | llvm::Constant *Values[11]; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6268 | // isa is NULL |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 6269 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6270 | Values[1] = GetClassName(PD->getIdentifier()); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6271 | Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(), |
| 6272 | PD->protocol_begin(), |
| 6273 | PD->protocol_end()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6274 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 6275 | Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6276 | + PD->getName(), |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6277 | "__DATA, __objc_const", |
| 6278 | InstanceMethods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6279 | Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6280 | + PD->getName(), |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6281 | "__DATA, __objc_const", |
| 6282 | ClassMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 6283 | Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6284 | + PD->getName(), |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6285 | "__DATA, __objc_const", |
| 6286 | OptInstanceMethods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6287 | Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6288 | + PD->getName(), |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6289 | "__DATA, __objc_const", |
| 6290 | OptClassMethods); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6291 | Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(), |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6292 | 0, PD, ObjCTypes); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6293 | uint32_t Size = |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6294 | CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolnfABITy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 6295 | Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 6296 | Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6297 | Values[10] = EmitProtocolMethodTypes("\01l_OBJC_$_PROTOCOL_METHOD_TYPES_" |
| 6298 | + PD->getName(), |
| 6299 | MethodTypesExt, ObjCTypes); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 6300 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6301 | Values); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6302 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6303 | if (Entry) { |
| 6304 | // Already created, fix the linkage and update the initializer. |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 6305 | Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6306 | Entry->setInitializer(Init); |
| 6307 | } else { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6308 | Entry = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6309 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, |
| 6310 | false, llvm::GlobalValue::WeakAnyLinkage, Init, |
| 6311 | "\01l_OBJC_PROTOCOL_$_" + PD->getName()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 6312 | Entry->setAlignment( |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6313 | CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABITy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6314 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
John McCall | 50651b9 | 2012-03-30 21:29:05 +0000 | [diff] [blame] | 6315 | |
| 6316 | Protocols[PD->getIdentifier()] = Entry; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6317 | } |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 6318 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 6319 | CGM.AddUsedGlobal(Entry); |
| 6320 | |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 6321 | // Use this protocol meta-data to build protocol list table in section |
| 6322 | // __DATA, __objc_protolist |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6323 | llvm::GlobalVariable *PTGV = |
| 6324 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy, |
| 6325 | false, llvm::GlobalValue::WeakAnyLinkage, Entry, |
| 6326 | "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 6327 | PTGV->setAlignment( |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6328 | CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy)); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 6329 | PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip"); |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 6330 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 6331 | CGM.AddUsedGlobal(PTGV); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6332 | return Entry; |
| 6333 | } |
| 6334 | |
| 6335 | /// EmitProtocolList - Generate protocol list meta-data: |
| 6336 | /// @code |
| 6337 | /// struct _protocol_list_t { |
| 6338 | /// long protocol_count; // Note, this is 32/64 bit |
| 6339 | /// struct _protocol_t[protocol_count]; |
| 6340 | /// } |
| 6341 | /// @endcode |
| 6342 | /// |
| 6343 | llvm::Constant * |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6344 | CGObjCNonFragileABIMac::EmitProtocolList(Twine Name, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6345 | ObjCProtocolDecl::protocol_iterator begin, |
| 6346 | ObjCProtocolDecl::protocol_iterator end) { |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 6347 | SmallVector<llvm::Constant *, 16> ProtocolRefs; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6348 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6349 | // Just return null for empty protocol lists |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6350 | if (begin == end) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 6351 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6352 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 6353 | // FIXME: We shouldn't need to do this lookup here, should we? |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 6354 | SmallString<256> TmpName; |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6355 | Name.toVector(TmpName); |
| 6356 | llvm::GlobalVariable *GV = |
| 6357 | CGM.getModule().getGlobalVariable(TmpName.str(), true); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6358 | if (GV) |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6359 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6360 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 6361 | for (; begin != end; ++begin) |
| 6362 | ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented??? |
| 6363 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6364 | // This list is null terminated. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 6365 | ProtocolRefs.push_back(llvm::Constant::getNullValue( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6366 | ObjCTypes.ProtocolnfABIPtrTy)); |
| 6367 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 6368 | llvm::Constant *Values[2]; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 6369 | Values[0] = |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 6370 | llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6371 | Values[1] = |
Bill Wendling | 3964e62 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 6372 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy, |
| 6373 | ProtocolRefs.size()), |
| 6374 | ProtocolRefs); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6375 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 6376 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6377 | GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6378 | llvm::GlobalValue::InternalLinkage, |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 6379 | Init, Name); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6380 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 6381 | GV->setAlignment( |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6382 | CGM.getDataLayout().getABITypeAlignment(Init->getType())); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 6383 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6384 | return llvm::ConstantExpr::getBitCast(GV, |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 6385 | ObjCTypes.ProtocolListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6386 | } |
| 6387 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 6388 | /// GetMethodDescriptionConstant - This routine build following meta-data: |
| 6389 | /// struct _objc_method { |
| 6390 | /// SEL _cmd; |
| 6391 | /// char *method_type; |
| 6392 | /// char *_imp; |
| 6393 | /// } |
| 6394 | |
| 6395 | llvm::Constant * |
| 6396 | CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 6397 | llvm::Constant *Desc[3]; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 6398 | Desc[0] = |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6399 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 6400 | ObjCTypes.SelectorPtrTy); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 6401 | Desc[1] = GetMethodVarType(MD); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 6402 | if (!Desc[1]) |
| 6403 | return 0; |
| 6404 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 6405 | // Protocol methods have no implementation. So, this entry is always NULL. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 6406 | Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 6407 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 6408 | } |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 6409 | |
| 6410 | /// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference. |
| 6411 | /// This code gen. amounts to generating code for: |
| 6412 | /// @code |
| 6413 | /// (type *)((char *)base + _OBJC_IVAR_$_.ivar; |
| 6414 | /// @encode |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6415 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 6416 | LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar( |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 6417 | CodeGen::CodeGenFunction &CGF, |
| 6418 | QualType ObjectTy, |
| 6419 | llvm::Value *BaseValue, |
| 6420 | const ObjCIvarDecl *Ivar, |
| 6421 | unsigned CVRQualifiers) { |
| 6422 | ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface(); |
Fariborz Jahanian | cd285d0 | 2012-02-20 22:42:22 +0000 | [diff] [blame] | 6423 | llvm::Value *Offset = EmitIvarOffset(CGF, ID, Ivar); |
| 6424 | if (llvm::LoadInst *LI = dyn_cast<llvm::LoadInst>(Offset)) |
| 6425 | LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"), |
| 6426 | llvm::MDNode::get(VMContext, |
| 6427 | ArrayRef<llvm::Value*>())); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 6428 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
Fariborz Jahanian | cd285d0 | 2012-02-20 22:42:22 +0000 | [diff] [blame] | 6429 | Offset); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 6430 | } |
| 6431 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 6432 | llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6433 | CodeGen::CodeGenFunction &CGF, |
| 6434 | const ObjCInterfaceDecl *Interface, |
| 6435 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 2da84ff | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 6436 | return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar"); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 6437 | } |
| 6438 | |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6439 | static void appendSelectorForMessageRefTable(std::string &buffer, |
| 6440 | Selector selector) { |
| 6441 | if (selector.isUnarySelector()) { |
| 6442 | buffer += selector.getNameForSlot(0); |
| 6443 | return; |
| 6444 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6445 | |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6446 | for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) { |
| 6447 | buffer += selector.getNameForSlot(i); |
| 6448 | buffer += '_'; |
| 6449 | } |
| 6450 | } |
| 6451 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6452 | /// Emit a "v-table" message send. We emit a weak hidden-visibility |
| 6453 | /// struct, initially containing the selector pointer and a pointer to |
| 6454 | /// a "fixup" variant of the appropriate objc_msgSend. To call, we |
| 6455 | /// load and call the function pointer, passing the address of the |
| 6456 | /// struct as the second parameter. The runtime determines whether |
| 6457 | /// the selector is currently emitted using vtable dispatch; if so, it |
| 6458 | /// substitutes a stub function which simply tail-calls through the |
| 6459 | /// appropriate vtable slot, and if not, it substitues a stub function |
| 6460 | /// which tail-calls objc_msgSend. Both stubs adjust the selector |
| 6461 | /// argument to correctly point to the selector. |
| 6462 | RValue |
| 6463 | CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF, |
| 6464 | ReturnValueSlot returnSlot, |
| 6465 | QualType resultType, |
| 6466 | Selector selector, |
| 6467 | llvm::Value *arg0, |
| 6468 | QualType arg0Type, |
| 6469 | bool isSuper, |
| 6470 | const CallArgList &formalArgs, |
| 6471 | const ObjCMethodDecl *method) { |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6472 | // Compute the actual arguments. |
| 6473 | CallArgList args; |
| 6474 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6475 | // First argument: the receiver / super-call structure. |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6476 | if (!isSuper) |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6477 | arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy); |
| 6478 | args.add(RValue::get(arg0), arg0Type); |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6479 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6480 | // Second argument: a pointer to the message ref structure. Leave |
| 6481 | // the actual argument value blank for now. |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6482 | args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy); |
| 6483 | |
| 6484 | args.insert(args.end(), formalArgs.begin(), formalArgs.end()); |
| 6485 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 6486 | MessageSendInfo MSI = getMessageSendInfo(method, resultType, args); |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6487 | |
John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 6488 | NullReturnState nullReturn; |
| 6489 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6490 | // Find the function to call and the mangled name for the message |
| 6491 | // ref structure. Using a different mangled name wouldn't actually |
| 6492 | // be a problem; it would just be a waste. |
| 6493 | // |
| 6494 | // The runtime currently never uses vtable dispatch for anything |
| 6495 | // except normal, non-super message-sends. |
| 6496 | // FIXME: don't use this for that. |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6497 | llvm::Constant *fn = 0; |
| 6498 | std::string messageRefName("\01l_"); |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 6499 | if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) { |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6500 | if (isSuper) { |
| 6501 | fn = ObjCTypes.getMessageSendSuper2StretFixupFn(); |
| 6502 | messageRefName += "objc_msgSendSuper2_stret_fixup"; |
Chris Lattner | 9b7d670 | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 6503 | } else { |
John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 6504 | nullReturn.init(CGF, arg0); |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6505 | fn = ObjCTypes.getMessageSendStretFixupFn(); |
| 6506 | messageRefName += "objc_msgSend_stret_fixup"; |
Chris Lattner | 9b7d670 | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 6507 | } |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6508 | } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) { |
| 6509 | fn = ObjCTypes.getMessageSendFpretFixupFn(); |
| 6510 | messageRefName += "objc_msgSend_fpret_fixup"; |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 6511 | } else { |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6512 | if (isSuper) { |
| 6513 | fn = ObjCTypes.getMessageSendSuper2FixupFn(); |
| 6514 | messageRefName += "objc_msgSendSuper2_fixup"; |
Chris Lattner | 9b7d670 | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 6515 | } else { |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6516 | fn = ObjCTypes.getMessageSendFixupFn(); |
| 6517 | messageRefName += "objc_msgSend_fixup"; |
Chris Lattner | 9b7d670 | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 6518 | } |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 6519 | } |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6520 | assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend"); |
| 6521 | messageRefName += '_'; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6522 | |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6523 | // Append the selector name, except use underscores anywhere we |
| 6524 | // would have used colons. |
| 6525 | appendSelectorForMessageRefTable(messageRefName, selector); |
| 6526 | |
| 6527 | llvm::GlobalVariable *messageRef |
| 6528 | = CGM.getModule().getGlobalVariable(messageRefName); |
| 6529 | if (!messageRef) { |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6530 | // Build the message ref structure. |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6531 | llvm::Constant *values[] = { fn, GetMethodVarName(selector) }; |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 6532 | llvm::Constant *init = llvm::ConstantStruct::getAnon(values); |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6533 | messageRef = new llvm::GlobalVariable(CGM.getModule(), |
| 6534 | init->getType(), |
| 6535 | /*constant*/ false, |
| 6536 | llvm::GlobalValue::WeakAnyLinkage, |
| 6537 | init, |
| 6538 | messageRefName); |
| 6539 | messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 6540 | messageRef->setAlignment(16); |
| 6541 | messageRef->setSection("__DATA, __objc_msgrefs, coalesced"); |
| 6542 | } |
Fariborz Jahanian | 3c52d36 | 2012-01-30 23:39:30 +0000 | [diff] [blame] | 6543 | |
| 6544 | bool requiresnullCheck = false; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6545 | if (CGM.getLangOpts().ObjCAutoRefCount && method) |
Fariborz Jahanian | 3c52d36 | 2012-01-30 23:39:30 +0000 | [diff] [blame] | 6546 | for (ObjCMethodDecl::param_const_iterator i = method->param_begin(), |
| 6547 | e = method->param_end(); i != e; ++i) { |
| 6548 | const ParmVarDecl *ParamDecl = (*i); |
| 6549 | if (ParamDecl->hasAttr<NSConsumedAttr>()) { |
| 6550 | if (!nullReturn.NullBB) |
| 6551 | nullReturn.init(CGF, arg0); |
| 6552 | requiresnullCheck = true; |
| 6553 | break; |
| 6554 | } |
| 6555 | } |
| 6556 | |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6557 | llvm::Value *mref = |
| 6558 | CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy); |
| 6559 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6560 | // Update the message ref argument. |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6561 | args[1].RV = RValue::get(mref); |
| 6562 | |
| 6563 | // Load the function to call from the message ref table. |
| 6564 | llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0); |
| 6565 | callee = CGF.Builder.CreateLoad(callee, "msgSend_fn"); |
| 6566 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 6567 | callee = CGF.Builder.CreateBitCast(callee, MSI.MessengerType); |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6568 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 6569 | RValue result = CGF.EmitCall(MSI.CallInfo, callee, returnSlot, args); |
Fariborz Jahanian | 3c52d36 | 2012-01-30 23:39:30 +0000 | [diff] [blame] | 6570 | return nullReturn.complete(CGF, result, resultType, formalArgs, |
| 6571 | requiresnullCheck ? method : 0); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 6572 | } |
| 6573 | |
| 6574 | /// Generate code for a message send expression in the nonfragile abi. |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 6575 | CodeGen::RValue |
| 6576 | CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 6577 | ReturnValueSlot Return, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 6578 | QualType ResultType, |
| 6579 | Selector Sel, |
| 6580 | llvm::Value *Receiver, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 6581 | const CallArgList &CallArgs, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 6582 | const ObjCInterfaceDecl *Class, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 6583 | const ObjCMethodDecl *Method) { |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6584 | return isVTableDispatchedSelector(Sel) |
| 6585 | ? EmitVTableMessageSend(CGF, Return, ResultType, Sel, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6586 | Receiver, CGF.getContext().getObjCIdType(), |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6587 | false, CallArgs, Method) |
| 6588 | : EmitMessageSend(CGF, Return, ResultType, |
| 6589 | EmitSelector(CGF.Builder, Sel), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6590 | Receiver, CGF.getContext().getObjCIdType(), |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6591 | false, CallArgs, Method, ObjCTypes); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 6592 | } |
| 6593 | |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 6594 | llvm::GlobalVariable * |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 6595 | CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 6596 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 6597 | |
Daniel Dunbar | dfff230 | 2009-03-02 05:18:14 +0000 | [diff] [blame] | 6598 | if (!GV) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6599 | GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6600 | false, llvm::GlobalValue::ExternalLinkage, |
| 6601 | 0, Name); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 6602 | } |
| 6603 | |
| 6604 | return GV; |
| 6605 | } |
| 6606 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6607 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder, |
| 6608 | IdentifierInfo *II) { |
| 6609 | llvm::GlobalVariable *&Entry = ClassReferences[II]; |
| 6610 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 6611 | if (!Entry) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6612 | std::string ClassName(getClassSymbolPrefix() + II->getName().str()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 6613 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6614 | Entry = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6615 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, |
| 6616 | false, llvm::GlobalValue::InternalLinkage, |
| 6617 | ClassGV, |
| 6618 | "\01L_OBJC_CLASSLIST_REFERENCES_$_"); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 6619 | Entry->setAlignment( |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6620 | CGM.getDataLayout().getABITypeAlignment( |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6621 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6622 | Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 6623 | CGM.AddUsedGlobal(Entry); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6624 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6625 | |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 6626 | return Builder.CreateLoad(Entry); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6627 | } |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 6628 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6629 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder, |
| 6630 | const ObjCInterfaceDecl *ID) { |
| 6631 | return EmitClassRefFromId(Builder, ID->getIdentifier()); |
| 6632 | } |
| 6633 | |
| 6634 | llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef( |
| 6635 | CGBuilderTy &Builder) { |
| 6636 | IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool"); |
| 6637 | return EmitClassRefFromId(Builder, II); |
| 6638 | } |
| 6639 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6640 | llvm::Value * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6641 | CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6642 | const ObjCInterfaceDecl *ID) { |
| 6643 | llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6644 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6645 | if (!Entry) { |
| 6646 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
| 6647 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6648 | Entry = |
| 6649 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6650 | false, llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6651 | ClassGV, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6652 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_"); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6653 | Entry->setAlignment( |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6654 | CGM.getDataLayout().getABITypeAlignment( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6655 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6656 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 6657 | CGM.AddUsedGlobal(Entry); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 6658 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6659 | |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 6660 | return Builder.CreateLoad(Entry); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 6661 | } |
| 6662 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6663 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 6664 | /// meta-data |
| 6665 | /// |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6666 | llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder, |
| 6667 | const ObjCInterfaceDecl *ID) { |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6668 | llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()]; |
| 6669 | if (Entry) |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 6670 | return Builder.CreateLoad(Entry); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6671 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 6672 | std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 6673 | llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6674 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6675 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6676 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6677 | MetaClassGV, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6678 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_"); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6679 | Entry->setAlignment( |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6680 | CGM.getDataLayout().getABITypeAlignment( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6681 | ObjCTypes.ClassnfABIPtrTy)); |
| 6682 | |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 6683 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 6684 | CGM.AddUsedGlobal(Entry); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6685 | |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 6686 | return Builder.CreateLoad(Entry); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6687 | } |
| 6688 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 6689 | /// GetClass - Return a reference to the class for the given interface |
| 6690 | /// decl. |
| 6691 | llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder, |
| 6692 | const ObjCInterfaceDecl *ID) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 6693 | if (ID->isWeakImported()) { |
Fariborz Jahanian | 269f8bc | 2009-11-17 22:42:00 +0000 | [diff] [blame] | 6694 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
| 6695 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
| 6696 | ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
| 6697 | } |
| 6698 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 6699 | return EmitClassRef(Builder, ID); |
| 6700 | } |
| 6701 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6702 | /// Generates a message send where the super is the receiver. This is |
| 6703 | /// a message send to self with special delivery semantics indicating |
| 6704 | /// which class's method should be called. |
| 6705 | CodeGen::RValue |
| 6706 | CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 6707 | ReturnValueSlot Return, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6708 | QualType ResultType, |
| 6709 | Selector Sel, |
| 6710 | const ObjCInterfaceDecl *Class, |
| 6711 | bool isCategoryImpl, |
| 6712 | llvm::Value *Receiver, |
| 6713 | bool IsClassMessage, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 6714 | const CodeGen::CallArgList &CallArgs, |
| 6715 | const ObjCMethodDecl *Method) { |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6716 | // ... |
| 6717 | // Create and init a super structure; this is a (receiver, class) |
| 6718 | // pair we will pass to objc_msgSendSuper. |
| 6719 | llvm::Value *ObjCSuper = |
John McCall | 604da29 | 2011-03-04 08:00:29 +0000 | [diff] [blame] | 6720 | CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6721 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6722 | llvm::Value *ReceiverAsObject = |
| 6723 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 6724 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 6725 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6726 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6727 | // If this is a class message the metaclass is passed as the target. |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 6728 | llvm::Value *Target; |
Fariborz Jahanian | b9966bd | 2012-10-10 23:11:18 +0000 | [diff] [blame] | 6729 | if (IsClassMessage) |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 6730 | Target = EmitMetaClassRef(CGF.Builder, Class); |
Fariborz Jahanian | b9966bd | 2012-10-10 23:11:18 +0000 | [diff] [blame] | 6731 | else |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6732 | Target = EmitSuperClassRef(CGF.Builder, Class); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6733 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 6734 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext and |
| 6735 | // ObjCTypes types. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6736 | llvm::Type *ClassTy = |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6737 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
| 6738 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
| 6739 | CGF.Builder.CreateStore(Target, |
| 6740 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6741 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6742 | return (isVTableDispatchedSelector(Sel)) |
| 6743 | ? EmitVTableMessageSend(CGF, Return, ResultType, Sel, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6744 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6745 | true, CallArgs, Method) |
| 6746 | : EmitMessageSend(CGF, Return, ResultType, |
| 6747 | EmitSelector(CGF.Builder, Sel), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6748 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6749 | true, CallArgs, Method, ObjCTypes); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6750 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6751 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6752 | llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder, |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 6753 | Selector Sel, bool lval) { |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6754 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6755 | |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6756 | if (!Entry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6757 | llvm::Constant *Casted = |
| 6758 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 6759 | ObjCTypes.SelectorPtrTy); |
| 6760 | Entry = |
| 6761 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false, |
| 6762 | llvm::GlobalValue::InternalLinkage, |
| 6763 | Casted, "\01L_OBJC_SELECTOR_REFERENCES_"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 6764 | Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 6765 | CGM.AddUsedGlobal(Entry); |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6766 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6767 | |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 6768 | if (lval) |
| 6769 | return Entry; |
Pete Cooper | b6d7114 | 2011-11-10 21:45:06 +0000 | [diff] [blame] | 6770 | llvm::LoadInst* LI = Builder.CreateLoad(Entry); |
| 6771 | |
| 6772 | LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"), |
| 6773 | llvm::MDNode::get(VMContext, |
| 6774 | ArrayRef<llvm::Value*>())); |
| 6775 | return LI; |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6776 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6777 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 6778 | /// objc_assign_ivar (id src, id *dst, ptrdiff_t) |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6779 | /// |
| 6780 | void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6781 | llvm::Value *src, |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 6782 | llvm::Value *dst, |
| 6783 | llvm::Value *ivarOffset) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6784 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6785 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6786 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6787 | assert(Size <= 8 && "does not support size > 8"); |
| 6788 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 6789 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 6790 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 6791 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6792 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 6793 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 6794 | CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(), |
| 6795 | src, dst, ivarOffset); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6796 | return; |
| 6797 | } |
| 6798 | |
| 6799 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 6800 | /// objc_assign_strongCast (id src, id *dst) |
| 6801 | /// |
| 6802 | void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6803 | CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6804 | llvm::Value *src, llvm::Value *dst) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6805 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6806 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6807 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6808 | assert(Size <= 8 && "does not support size > 8"); |
| 6809 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6810 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 6811 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 6812 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6813 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 6814 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 6815 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6816 | src, dst, "weakassign"); |
| 6817 | return; |
| 6818 | } |
| 6819 | |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 6820 | void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6821 | CodeGen::CodeGenFunction &CGF, |
| 6822 | llvm::Value *DestPtr, |
| 6823 | llvm::Value *SrcPtr, |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 6824 | llvm::Value *Size) { |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 6825 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy); |
| 6826 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 6827 | CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(), |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 6828 | DestPtr, SrcPtr, Size); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 6829 | return; |
| 6830 | } |
| 6831 | |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6832 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
| 6833 | /// object: objc_read_weak (id *src) |
| 6834 | /// |
| 6835 | llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6836 | CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6837 | llvm::Value *AddrWeakObj) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6838 | llvm::Type* DestTy = |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6839 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
| 6840 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 6841 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6842 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 6843 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6844 | return read_weak; |
| 6845 | } |
| 6846 | |
| 6847 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 6848 | /// objc_assign_weak (id src, id *dst) |
| 6849 | /// |
| 6850 | void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6851 | llvm::Value *src, llvm::Value *dst) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6852 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6853 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6854 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6855 | assert(Size <= 8 && "does not support size > 8"); |
| 6856 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 6857 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 6858 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 6859 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6860 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 6861 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 6862 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6863 | src, dst, "weakassign"); |
| 6864 | return; |
| 6865 | } |
| 6866 | |
| 6867 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 6868 | /// objc_assign_global (id src, id *dst) |
| 6869 | /// |
| 6870 | void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 6871 | llvm::Value *src, llvm::Value *dst, |
| 6872 | bool threadlocal) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6873 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6874 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6875 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6876 | assert(Size <= 8 && "does not support size > 8"); |
| 6877 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 6878 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 6879 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 6880 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6881 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 6882 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 6883 | if (!threadlocal) |
| 6884 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
| 6885 | src, dst, "globalassign"); |
| 6886 | else |
| 6887 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(), |
| 6888 | src, dst, "threadlocalassign"); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6889 | return; |
| 6890 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6891 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6892 | void |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6893 | CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 6894 | const ObjCAtSynchronizedStmt &S) { |
David Chisnall | 05dc91b | 2011-03-25 17:46:35 +0000 | [diff] [blame] | 6895 | EmitAtSynchronizedStmt(CGF, S, |
| 6896 | cast<llvm::Function>(ObjCTypes.getSyncEnterFn()), |
| 6897 | cast<llvm::Function>(ObjCTypes.getSyncExitFn())); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6898 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6899 | |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6900 | llvm::Constant * |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 6901 | CGObjCNonFragileABIMac::GetEHType(QualType T) { |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6902 | // There's a particular fixed type info for 'id'. |
| 6903 | if (T->isObjCIdType() || |
| 6904 | T->isObjCQualifiedIdType()) { |
| 6905 | llvm::Constant *IDEHType = |
| 6906 | CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id"); |
| 6907 | if (!IDEHType) |
| 6908 | IDEHType = |
| 6909 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, |
| 6910 | false, |
| 6911 | llvm::GlobalValue::ExternalLinkage, |
| 6912 | 0, "OBJC_EHTYPE_id"); |
| 6913 | return IDEHType; |
| 6914 | } |
| 6915 | |
| 6916 | // All other types should be Objective-C interface pointer types. |
| 6917 | const ObjCObjectPointerType *PT = |
| 6918 | T->getAs<ObjCObjectPointerType>(); |
| 6919 | assert(PT && "Invalid @catch type."); |
| 6920 | const ObjCInterfaceType *IT = PT->getInterfaceType(); |
| 6921 | assert(IT && "Invalid @catch type."); |
| 6922 | return GetInterfaceEHType(IT->getDecl(), false); |
| 6923 | } |
| 6924 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6925 | void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
| 6926 | const ObjCAtTryStmt &S) { |
David Chisnall | 05dc91b | 2011-03-25 17:46:35 +0000 | [diff] [blame] | 6927 | EmitTryCatchStmt(CGF, S, |
| 6928 | cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()), |
| 6929 | cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()), |
| 6930 | cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn())); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 6931 | } |
| 6932 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6933 | /// EmitThrowStmt - Generate code for a throw statement. |
| 6934 | void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6a3c70e | 2013-01-10 19:02:56 +0000 | [diff] [blame] | 6935 | const ObjCAtThrowStmt &S, |
| 6936 | bool ClearInsertionPoint) { |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6937 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
John McCall | 2b014d6 | 2011-10-01 10:32:24 +0000 | [diff] [blame] | 6938 | llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr); |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 6939 | Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy); |
Jay Foad | 4c7d9f1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 6940 | CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception) |
John McCall | 7ec404c | 2010-10-16 08:21:07 +0000 | [diff] [blame] | 6941 | .setDoesNotReturn(); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6942 | } else { |
Jay Foad | 4c7d9f1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 6943 | CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn()) |
John McCall | 7ec404c | 2010-10-16 08:21:07 +0000 | [diff] [blame] | 6944 | .setDoesNotReturn(); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6945 | } |
| 6946 | |
John McCall | 7ec404c | 2010-10-16 08:21:07 +0000 | [diff] [blame] | 6947 | CGF.Builder.CreateUnreachable(); |
Fariborz Jahanian | 6a3c70e | 2013-01-10 19:02:56 +0000 | [diff] [blame] | 6948 | if (ClearInsertionPoint) |
| 6949 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6950 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6951 | |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6952 | llvm::Constant * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6953 | CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6954 | bool ForDefinition) { |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6955 | llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()]; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6956 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6957 | // If we don't need a definition, return the entry if found or check |
| 6958 | // if we use an external reference. |
| 6959 | if (!ForDefinition) { |
| 6960 | if (Entry) |
| 6961 | return Entry; |
Daniel Dunbar | 7e075cb | 2009-04-07 06:43:45 +0000 | [diff] [blame] | 6962 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6963 | // If this type (or a super class) has the __objc_exception__ |
| 6964 | // attribute, emit an external reference. |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 6965 | if (hasObjCExceptionAttribute(CGM.getContext(), ID)) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6966 | return Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6967 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false, |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6968 | llvm::GlobalValue::ExternalLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6969 | 0, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6970 | ("OBJC_EHTYPE_$_" + |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 6971 | ID->getIdentifier()->getName())); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6972 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6973 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6974 | // Otherwise we need to either make a new entry or fill in the |
| 6975 | // initializer. |
| 6976 | assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition"); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 6977 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6978 | std::string VTableName = "objc_ehtype_vtable"; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6979 | llvm::GlobalVariable *VTableGV = |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6980 | CGM.getModule().getGlobalVariable(VTableName); |
| 6981 | if (!VTableGV) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6982 | VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy, |
| 6983 | false, |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6984 | llvm::GlobalValue::ExternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6985 | 0, VTableName); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6986 | |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6987 | llvm::Value *VTableIdx = llvm::ConstantInt::get(CGM.Int32Ty, 2); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6988 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 6989 | llvm::Constant *Values[] = { |
| 6990 | llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx), |
| 6991 | GetClassName(ID->getIdentifier()), |
| 6992 | GetClassGlobal(ClassName) |
| 6993 | }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 6994 | llvm::Constant *Init = |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 6995 | llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6996 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6997 | if (Entry) { |
| 6998 | Entry->setInitializer(Init); |
| 6999 | } else { |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 7000 | Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false, |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 7001 | llvm::GlobalValue::WeakAnyLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 7002 | Init, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 7003 | ("OBJC_EHTYPE_$_" + |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 7004 | ID->getIdentifier()->getName())); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 7005 | } |
| 7006 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7007 | if (CGM.getLangOpts().getVisibilityMode() == HiddenVisibility) |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 7008 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 7009 | Entry->setAlignment(CGM.getDataLayout().getABITypeAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 7010 | ObjCTypes.EHTypeTy)); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 7011 | |
| 7012 | if (ForDefinition) { |
| 7013 | Entry->setSection("__DATA,__objc_const"); |
| 7014 | Entry->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 7015 | } else { |
| 7016 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 7017 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 7018 | |
| 7019 | return Entry; |
| 7020 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 7021 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 7022 | /* *** */ |
| 7023 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 7024 | CodeGen::CGObjCRuntime * |
| 7025 | CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 7026 | switch (CGM.getLangOpts().ObjCRuntime.getKind()) { |
| 7027 | case ObjCRuntime::FragileMacOSX: |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 7028 | return new CGObjCMac(CGM); |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 7029 | |
| 7030 | case ObjCRuntime::MacOSX: |
| 7031 | case ObjCRuntime::iOS: |
| 7032 | return new CGObjCNonFragileABIMac(CGM); |
| 7033 | |
David Chisnall | 11d3f4c | 2012-07-03 20:49:52 +0000 | [diff] [blame] | 7034 | case ObjCRuntime::GNUstep: |
| 7035 | case ObjCRuntime::GCC: |
John McCall | f7226fb | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 7036 | case ObjCRuntime::ObjFW: |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 7037 | llvm_unreachable("these runtimes are not Mac runtimes"); |
| 7038 | } |
| 7039 | llvm_unreachable("bad runtime"); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 7040 | } |