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 }; |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 64 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 65 | params, true), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 66 | "objc_msgSend", |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 67 | llvm::Attribute::get(CGM.getLLVMContext(), |
| 68 | llvm::Attribute::NonLazyBind)); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 69 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 70 | |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 71 | /// void objc_msgSend_stret (id, SEL, ...) |
| 72 | /// |
| 73 | /// The messenger used when the return value is an aggregate returned |
| 74 | /// by indirect reference in the first argument, and therefore the |
| 75 | /// self and selector parameters are shifted over by one. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 76 | llvm::Constant *getMessageSendStretFn() const { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 77 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 78 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, |
| 79 | params, true), |
| 80 | "objc_msgSend_stret"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 81 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 82 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 83 | |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 84 | /// [double | long double] objc_msgSend_fpret(id self, SEL op, ...) |
| 85 | /// |
| 86 | /// The messenger used when the return value is returned on the x87 |
| 87 | /// floating-point stack; without a special entrypoint, the nil case |
| 88 | /// would be unbalanced. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 89 | llvm::Constant *getMessageSendFpretFn() const { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 90 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 91 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.DoubleTy, |
| 92 | params, true), |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 93 | "objc_msgSend_fpret"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 94 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 95 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 96 | |
Anders Carlsson | eea6480 | 2011-10-31 16:27:11 +0000 | [diff] [blame] | 97 | /// _Complex long double objc_msgSend_fp2ret(id self, SEL op, ...) |
| 98 | /// |
| 99 | /// The messenger used when the return value is returned in two values on the |
| 100 | /// x87 floating point stack; without a special entrypoint, the nil case |
| 101 | /// would be unbalanced. Only used on 64-bit X86. |
| 102 | llvm::Constant *getMessageSendFp2retFn() const { |
| 103 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
| 104 | llvm::Type *longDoubleType = llvm::Type::getX86_FP80Ty(VMContext); |
| 105 | llvm::Type *resultType = |
| 106 | llvm::StructType::get(longDoubleType, longDoubleType, NULL); |
| 107 | |
| 108 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(resultType, |
| 109 | params, true), |
| 110 | "objc_msgSend_fp2ret"); |
| 111 | } |
| 112 | |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 113 | /// id objc_msgSendSuper(struct objc_super *super, SEL op, ...) |
| 114 | /// |
| 115 | /// The messenger used for super calls, which have different dispatch |
| 116 | /// semantics. The class passed is the superclass of the current |
| 117 | /// class. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 118 | llvm::Constant *getMessageSendSuperFn() const { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 119 | llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 120 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 121 | params, true), |
| 122 | "objc_msgSendSuper"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 123 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 124 | |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 125 | /// id objc_msgSendSuper2(struct objc_super *super, SEL op, ...) |
| 126 | /// |
| 127 | /// A slightly different messenger used for super calls. The class |
| 128 | /// passed is the current class. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 129 | llvm::Constant *getMessageSendSuperFn2() const { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 130 | llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 131 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 132 | params, true), |
| 133 | "objc_msgSendSuper2"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 134 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 135 | |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 136 | /// void objc_msgSendSuper_stret(void *stretAddr, struct objc_super *super, |
| 137 | /// SEL op, ...) |
| 138 | /// |
| 139 | /// The messenger used for super calls which return an aggregate indirectly. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 140 | llvm::Constant *getMessageSendSuperStretFn() const { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 141 | llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 142 | return CGM.CreateRuntimeFunction( |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 143 | llvm::FunctionType::get(CGM.VoidTy, params, true), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 144 | "objc_msgSendSuper_stret"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 145 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 146 | |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 147 | /// void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super, |
| 148 | /// SEL op, ...) |
| 149 | /// |
| 150 | /// objc_msgSendSuper_stret with the super2 semantics. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 151 | llvm::Constant *getMessageSendSuperStretFn2() const { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 152 | llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 153 | return CGM.CreateRuntimeFunction( |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 154 | llvm::FunctionType::get(CGM.VoidTy, params, true), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 155 | "objc_msgSendSuper2_stret"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 156 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 157 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 158 | llvm::Constant *getMessageSendSuperFpretFn() const { |
| 159 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 160 | return getMessageSendSuperFn(); |
| 161 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 162 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 163 | llvm::Constant *getMessageSendSuperFpretFn2() const { |
| 164 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 165 | return getMessageSendSuperFn2(); |
| 166 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 167 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 168 | protected: |
| 169 | CodeGen::CodeGenModule &CGM; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 170 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 171 | public: |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 172 | llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy; |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 173 | llvm::Type *Int8PtrTy, *Int8PtrPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 174 | |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 175 | /// ObjectPtrTy - LLVM type for object handles (typeof(id)) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 176 | llvm::Type *ObjectPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 177 | |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 178 | /// PtrObjectPtrTy - LLVM type for id * |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 179 | llvm::Type *PtrObjectPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 180 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 181 | /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL)) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 182 | llvm::Type *SelectorPtrTy; |
Douglas Gregor | 4c86fdb | 2012-01-17 18:36:30 +0000 | [diff] [blame] | 183 | |
| 184 | private: |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 185 | /// ProtocolPtrTy - LLVM type for external protocol handles |
| 186 | /// (typeof(Protocol)) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 187 | llvm::Type *ExternalProtocolPtrTy; |
Douglas Gregor | 4c86fdb | 2012-01-17 18:36:30 +0000 | [diff] [blame] | 188 | |
| 189 | public: |
| 190 | llvm::Type *getExternalProtocolPtrTy() { |
| 191 | if (!ExternalProtocolPtrTy) { |
| 192 | // FIXME: It would be nice to unify this with the opaque type, so that the |
| 193 | // IR comes out a bit cleaner. |
| 194 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 195 | ASTContext &Ctx = CGM.getContext(); |
| 196 | llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); |
| 197 | ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T); |
| 198 | } |
| 199 | |
| 200 | return ExternalProtocolPtrTy; |
| 201 | } |
| 202 | |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 203 | // SuperCTy - clang type for struct objc_super. |
| 204 | QualType SuperCTy; |
| 205 | // SuperPtrCTy - clang type for struct objc_super *. |
| 206 | QualType SuperPtrCTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 207 | |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 208 | /// SuperTy - LLVM type for struct objc_super. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 209 | llvm::StructType *SuperTy; |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 210 | /// SuperPtrTy - LLVM type for struct objc_super *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 211 | llvm::Type *SuperPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 212 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 213 | /// PropertyTy - LLVM type for struct objc_property (struct _prop_t |
| 214 | /// in GCC parlance). |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 215 | llvm::StructType *PropertyTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 216 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 217 | /// PropertyListTy - LLVM type for struct objc_property_list |
| 218 | /// (_prop_list_t in GCC parlance). |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 219 | llvm::StructType *PropertyListTy; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 220 | /// PropertyListPtrTy - LLVM type for struct objc_property_list*. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 221 | llvm::Type *PropertyListPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 222 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 223 | // MethodTy - LLVM type for struct objc_method. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 224 | llvm::StructType *MethodTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 225 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 226 | /// CacheTy - LLVM type for struct objc_cache. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 227 | llvm::Type *CacheTy; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 228 | /// CachePtrTy - LLVM type for struct objc_cache *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 229 | llvm::Type *CachePtrTy; |
Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 230 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 231 | llvm::Constant *getGetPropertyFn() { |
| 232 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 233 | ASTContext &Ctx = CGM.getContext(); |
| 234 | // id objc_getProperty (id, SEL, ptrdiff_t, bool) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 235 | SmallVector<CanQualType,4> Params; |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 236 | CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType()); |
| 237 | CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType()); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 238 | Params.push_back(IdType); |
| 239 | Params.push_back(SelType); |
David Chisnall | ab5824e | 2011-03-22 20:03:13 +0000 | [diff] [blame] | 240 | Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified()); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 241 | Params.push_back(Ctx.BoolTy); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 242 | llvm::FunctionType *FTy = |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 243 | Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(IdType, Params, |
| 244 | FunctionType::ExtInfo(), |
| 245 | RequiredArgs::All)); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 246 | return CGM.CreateRuntimeFunction(FTy, "objc_getProperty"); |
| 247 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 248 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 249 | llvm::Constant *getSetPropertyFn() { |
| 250 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 251 | ASTContext &Ctx = CGM.getContext(); |
| 252 | // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 253 | SmallVector<CanQualType,6> Params; |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 254 | CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType()); |
| 255 | CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType()); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 256 | Params.push_back(IdType); |
| 257 | Params.push_back(SelType); |
David Chisnall | ab5824e | 2011-03-22 20:03:13 +0000 | [diff] [blame] | 258 | Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified()); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 259 | Params.push_back(IdType); |
| 260 | Params.push_back(Ctx.BoolTy); |
| 261 | Params.push_back(Ctx.BoolTy); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 262 | llvm::FunctionType *FTy = |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 263 | Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params, |
| 264 | FunctionType::ExtInfo(), |
| 265 | RequiredArgs::All)); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 266 | return CGM.CreateRuntimeFunction(FTy, "objc_setProperty"); |
| 267 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 268 | |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 269 | llvm::Constant *getOptimizedSetPropertyFn(bool atomic, bool copy) { |
| 270 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 271 | ASTContext &Ctx = CGM.getContext(); |
| 272 | // void objc_setProperty_atomic(id self, SEL _cmd, |
| 273 | // id newValue, ptrdiff_t offset); |
| 274 | // void objc_setProperty_nonatomic(id self, SEL _cmd, |
| 275 | // id newValue, ptrdiff_t offset); |
| 276 | // void objc_setProperty_atomic_copy(id self, SEL _cmd, |
| 277 | // id newValue, ptrdiff_t offset); |
| 278 | // void objc_setProperty_nonatomic_copy(id self, SEL _cmd, |
| 279 | // id newValue, ptrdiff_t offset); |
| 280 | |
| 281 | SmallVector<CanQualType,4> Params; |
| 282 | CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType()); |
| 283 | CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType()); |
| 284 | Params.push_back(IdType); |
| 285 | Params.push_back(SelType); |
| 286 | Params.push_back(IdType); |
| 287 | Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified()); |
| 288 | llvm::FunctionType *FTy = |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 289 | Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params, |
| 290 | FunctionType::ExtInfo(), |
| 291 | RequiredArgs::All)); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 292 | const char *name; |
| 293 | if (atomic && copy) |
| 294 | name = "objc_setProperty_atomic_copy"; |
| 295 | else if (atomic && !copy) |
| 296 | name = "objc_setProperty_atomic"; |
| 297 | else if (!atomic && copy) |
| 298 | name = "objc_setProperty_nonatomic_copy"; |
| 299 | else |
| 300 | name = "objc_setProperty_nonatomic"; |
| 301 | |
| 302 | return CGM.CreateRuntimeFunction(FTy, name); |
| 303 | } |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 304 | |
| 305 | llvm::Constant *getCopyStructFn() { |
| 306 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 307 | ASTContext &Ctx = CGM.getContext(); |
| 308 | // void objc_copyStruct (void *, const void *, size_t, bool, bool) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 309 | SmallVector<CanQualType,5> Params; |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 310 | Params.push_back(Ctx.VoidPtrTy); |
| 311 | Params.push_back(Ctx.VoidPtrTy); |
| 312 | Params.push_back(Ctx.LongTy); |
| 313 | Params.push_back(Ctx.BoolTy); |
| 314 | Params.push_back(Ctx.BoolTy); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 315 | llvm::FunctionType *FTy = |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 316 | Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params, |
| 317 | FunctionType::ExtInfo(), |
| 318 | RequiredArgs::All)); |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 319 | return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct"); |
| 320 | } |
| 321 | |
Fariborz Jahanian | e317302 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 322 | /// This routine declares and returns address of: |
| 323 | /// void objc_copyCppObjectAtomic( |
| 324 | /// void *dest, const void *src, |
| 325 | /// void (*copyHelper) (void *dest, const void *source)); |
| 326 | llvm::Constant *getCppAtomicObjectFunction() { |
| 327 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 328 | ASTContext &Ctx = CGM.getContext(); |
| 329 | /// void objc_copyCppObjectAtomic(void *dest, const void *src, void *helper); |
| 330 | SmallVector<CanQualType,3> Params; |
| 331 | Params.push_back(Ctx.VoidPtrTy); |
| 332 | Params.push_back(Ctx.VoidPtrTy); |
| 333 | Params.push_back(Ctx.VoidPtrTy); |
| 334 | llvm::FunctionType *FTy = |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 335 | Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params, |
| 336 | FunctionType::ExtInfo(), |
| 337 | RequiredArgs::All)); |
Fariborz Jahanian | e317302 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 338 | return CGM.CreateRuntimeFunction(FTy, "objc_copyCppObjectAtomic"); |
| 339 | } |
| 340 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 341 | llvm::Constant *getEnumerationMutationFn() { |
Daniel Dunbar | c1ab900 | 2009-07-11 20:32:50 +0000 | [diff] [blame] | 342 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 343 | ASTContext &Ctx = CGM.getContext(); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 344 | // void objc_enumerationMutation (id) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 345 | SmallVector<CanQualType,1> Params; |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 346 | Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType())); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 347 | llvm::FunctionType *FTy = |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 348 | Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params, |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 349 | FunctionType::ExtInfo(), |
| 350 | RequiredArgs::All)); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 351 | return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation"); |
| 352 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 353 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 354 | /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function. |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 355 | llvm::Constant *getGcReadWeakFn() { |
| 356 | // id objc_read_weak (id *) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 357 | llvm::Type *args[] = { ObjectPtrTy->getPointerTo() }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 358 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 359 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 360 | return CGM.CreateRuntimeFunction(FTy, "objc_read_weak"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 363 | /// GcAssignWeakFn -- LLVM objc_assign_weak function. |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 364 | llvm::Constant *getGcAssignWeakFn() { |
| 365 | // id objc_assign_weak (id, id *) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 366 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() }; |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 367 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 368 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 369 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak"); |
| 370 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 371 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 372 | /// GcAssignGlobalFn -- LLVM objc_assign_global function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 373 | llvm::Constant *getGcAssignGlobalFn() { |
| 374 | // id objc_assign_global(id, id *) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 375 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 376 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 377 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 378 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_global"); |
| 379 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 380 | |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 381 | /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function. |
| 382 | llvm::Constant *getGcAssignThreadLocalFn() { |
| 383 | // id objc_assign_threadlocal(id src, id * dest) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 384 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() }; |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 385 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 386 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 387 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal"); |
| 388 | } |
| 389 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 390 | /// GcAssignIvarFn -- LLVM objc_assign_ivar function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 391 | llvm::Constant *getGcAssignIvarFn() { |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 392 | // id objc_assign_ivar(id, id *, ptrdiff_t) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 393 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo(), |
| 394 | CGM.PtrDiffTy }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 395 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 396 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 397 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar"); |
| 398 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 399 | |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 400 | /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function. |
| 401 | llvm::Constant *GcMemmoveCollectableFn() { |
| 402 | // void *objc_memmove_collectable(void *dst, const void *src, size_t size) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 403 | llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, LongTy }; |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 404 | llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, args, false); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 405 | return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable"); |
| 406 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 407 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 408 | /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 409 | llvm::Constant *getGcAssignStrongCastFn() { |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 410 | // id objc_assign_strongCast(id, id *) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 411 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 412 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 413 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 414 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast"); |
| 415 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 416 | |
| 417 | /// ExceptionThrowFn - LLVM objc_exception_throw function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 418 | llvm::Constant *getExceptionThrowFn() { |
| 419 | // void objc_exception_throw(id) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 420 | llvm::Type *args[] = { ObjectPtrTy }; |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 421 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 422 | llvm::FunctionType::get(CGM.VoidTy, args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 423 | return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw"); |
| 424 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 425 | |
Fariborz Jahanian | 69677ea | 2010-05-28 17:34:43 +0000 | [diff] [blame] | 426 | /// ExceptionRethrowFn - LLVM objc_exception_rethrow function. |
| 427 | llvm::Constant *getExceptionRethrowFn() { |
| 428 | // void objc_exception_rethrow(void) |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 429 | llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, false); |
Fariborz Jahanian | 69677ea | 2010-05-28 17:34:43 +0000 | [diff] [blame] | 430 | return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow"); |
| 431 | } |
| 432 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 433 | /// SyncEnterFn - LLVM object_sync_enter function. |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 434 | llvm::Constant *getSyncEnterFn() { |
Aaron Ballman | 2d234d73 | 2012-09-06 16:44:16 +0000 | [diff] [blame] | 435 | // int objc_sync_enter (id) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 436 | llvm::Type *args[] = { ObjectPtrTy }; |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 437 | llvm::FunctionType *FTy = |
Aaron Ballman | 2d234d73 | 2012-09-06 16:44:16 +0000 | [diff] [blame] | 438 | llvm::FunctionType::get(CGM.IntTy, args, false); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 439 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter"); |
| 440 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 441 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 442 | /// SyncExitFn - LLVM object_sync_exit function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 443 | llvm::Constant *getSyncExitFn() { |
Aaron Ballman | 2d234d73 | 2012-09-06 16:44:16 +0000 | [diff] [blame] | 444 | // int objc_sync_exit (id) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 445 | llvm::Type *args[] = { ObjectPtrTy }; |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 446 | llvm::FunctionType *FTy = |
Aaron Ballman | 2d234d73 | 2012-09-06 16:44:16 +0000 | [diff] [blame] | 447 | llvm::FunctionType::get(CGM.IntTy, args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 448 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit"); |
| 449 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 450 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 451 | llvm::Constant *getSendFn(bool IsSuper) const { |
| 452 | return IsSuper ? getMessageSendSuperFn() : getMessageSendFn(); |
| 453 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 454 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 455 | llvm::Constant *getSendFn2(bool IsSuper) const { |
| 456 | return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn(); |
| 457 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 458 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 459 | llvm::Constant *getSendStretFn(bool IsSuper) const { |
| 460 | return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn(); |
| 461 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 462 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 463 | llvm::Constant *getSendStretFn2(bool IsSuper) const { |
| 464 | return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn(); |
| 465 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 466 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 467 | llvm::Constant *getSendFpretFn(bool IsSuper) const { |
| 468 | return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn(); |
| 469 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 470 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 471 | llvm::Constant *getSendFpretFn2(bool IsSuper) const { |
| 472 | return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn(); |
| 473 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 474 | |
Anders Carlsson | eea6480 | 2011-10-31 16:27:11 +0000 | [diff] [blame] | 475 | llvm::Constant *getSendFp2retFn(bool IsSuper) const { |
| 476 | return IsSuper ? getMessageSendSuperFn() : getMessageSendFp2retFn(); |
| 477 | } |
| 478 | |
| 479 | llvm::Constant *getSendFp2RetFn2(bool IsSuper) const { |
| 480 | return IsSuper ? getMessageSendSuperFn2() : getMessageSendFp2retFn(); |
| 481 | } |
| 482 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 483 | ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm); |
| 484 | ~ObjCCommonTypesHelper(){} |
| 485 | }; |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 486 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 487 | /// ObjCTypesHelper - Helper class that encapsulates lazy |
| 488 | /// construction of varies types used during ObjC generation. |
| 489 | class ObjCTypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 490 | public: |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 491 | /// SymtabTy - LLVM type for struct objc_symtab. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 492 | llvm::StructType *SymtabTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 493 | /// SymtabPtrTy - LLVM type for struct objc_symtab *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 494 | llvm::Type *SymtabPtrTy; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 495 | /// ModuleTy - LLVM type for struct objc_module. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 496 | llvm::StructType *ModuleTy; |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 497 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 498 | /// ProtocolTy - LLVM type for struct objc_protocol. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 499 | llvm::StructType *ProtocolTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 500 | /// ProtocolPtrTy - LLVM type for struct objc_protocol *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 501 | llvm::Type *ProtocolPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 502 | /// ProtocolExtensionTy - LLVM type for struct |
| 503 | /// objc_protocol_extension. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 504 | llvm::StructType *ProtocolExtensionTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 505 | /// ProtocolExtensionTy - LLVM type for struct |
| 506 | /// objc_protocol_extension *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 507 | llvm::Type *ProtocolExtensionPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 508 | /// MethodDescriptionTy - LLVM type for struct |
| 509 | /// objc_method_description. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 510 | llvm::StructType *MethodDescriptionTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 511 | /// MethodDescriptionListTy - LLVM type for struct |
| 512 | /// objc_method_description_list. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 513 | llvm::StructType *MethodDescriptionListTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 514 | /// MethodDescriptionListPtrTy - LLVM type for struct |
| 515 | /// objc_method_description_list *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 516 | llvm::Type *MethodDescriptionListPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 517 | /// ProtocolListTy - LLVM type for struct objc_property_list. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 518 | llvm::StructType *ProtocolListTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 519 | /// ProtocolListPtrTy - LLVM type for struct objc_property_list*. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 520 | llvm::Type *ProtocolListPtrTy; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 521 | /// CategoryTy - LLVM type for struct objc_category. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 522 | llvm::StructType *CategoryTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 523 | /// ClassTy - LLVM type for struct objc_class. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 524 | llvm::StructType *ClassTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 525 | /// ClassPtrTy - LLVM type for struct objc_class *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 526 | llvm::Type *ClassPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 527 | /// ClassExtensionTy - LLVM type for struct objc_class_ext. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 528 | llvm::StructType *ClassExtensionTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 529 | /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 530 | llvm::Type *ClassExtensionPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 531 | // IvarTy - LLVM type for struct objc_ivar. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 532 | llvm::StructType *IvarTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 533 | /// IvarListTy - LLVM type for struct objc_ivar_list. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 534 | llvm::Type *IvarListTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 535 | /// IvarListPtrTy - LLVM type for struct objc_ivar_list *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 536 | llvm::Type *IvarListPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 537 | /// MethodListTy - LLVM type for struct objc_method_list. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 538 | llvm::Type *MethodListTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 539 | /// MethodListPtrTy - LLVM type for struct objc_method_list *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 540 | llvm::Type *MethodListPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 541 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 542 | /// ExceptionDataTy - LLVM type for struct _objc_exception_data. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 543 | llvm::Type *ExceptionDataTy; |
Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 544 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 545 | /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 546 | llvm::Constant *getExceptionTryEnterFn() { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 547 | llvm::Type *params[] = { ExceptionDataTy->getPointerTo() }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 548 | return CGM.CreateRuntimeFunction( |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 549 | llvm::FunctionType::get(CGM.VoidTy, params, false), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 550 | "objc_exception_try_enter"); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 551 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 552 | |
| 553 | /// ExceptionTryExitFn - LLVM objc_exception_try_exit function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 554 | llvm::Constant *getExceptionTryExitFn() { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 555 | llvm::Type *params[] = { ExceptionDataTy->getPointerTo() }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 556 | return CGM.CreateRuntimeFunction( |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 557 | llvm::FunctionType::get(CGM.VoidTy, params, false), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 558 | "objc_exception_try_exit"); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 559 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 560 | |
| 561 | /// ExceptionExtractFn - LLVM objc_exception_extract function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 562 | llvm::Constant *getExceptionExtractFn() { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 563 | llvm::Type *params[] = { ExceptionDataTy->getPointerTo() }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 564 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 565 | params, false), |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 566 | "objc_exception_extract"); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 567 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 568 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 569 | /// ExceptionMatchFn - LLVM objc_exception_match function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 570 | llvm::Constant *getExceptionMatchFn() { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 571 | llvm::Type *params[] = { ClassPtrTy, ObjectPtrTy }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 572 | return CGM.CreateRuntimeFunction( |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 573 | llvm::FunctionType::get(CGM.Int32Ty, params, false), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 574 | "objc_exception_match"); |
| 575 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 576 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 577 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 578 | /// SetJmpFn - LLVM _setjmp function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 579 | llvm::Constant *getSetJmpFn() { |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 580 | // This is specifically the prototype for x86. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 581 | llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() }; |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 582 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty, |
| 583 | params, false), |
Bill Wendling | a9e269e | 2011-11-29 00:10:10 +0000 | [diff] [blame] | 584 | "_setjmp", |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 585 | llvm::Attribute::get(CGM.getLLVMContext(), |
| 586 | llvm::Attribute::NonLazyBind)); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 587 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 588 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 589 | public: |
| 590 | ObjCTypesHelper(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 591 | ~ObjCTypesHelper() {} |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 592 | }; |
| 593 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 594 | /// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 595 | /// modern abi |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 596 | class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 597 | public: |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 598 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 599 | // MethodListnfABITy - LLVM for struct _method_list_t |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 600 | llvm::StructType *MethodListnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 601 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 602 | // MethodListnfABIPtrTy - LLVM for struct _method_list_t* |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 603 | llvm::Type *MethodListnfABIPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 604 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 605 | // ProtocolnfABITy = LLVM for struct _protocol_t |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 606 | llvm::StructType *ProtocolnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 607 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 608 | // ProtocolnfABIPtrTy = LLVM for struct _protocol_t* |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 609 | llvm::Type *ProtocolnfABIPtrTy; |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 610 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 611 | // ProtocolListnfABITy - LLVM for struct _objc_protocol_list |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 612 | llvm::StructType *ProtocolListnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 613 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 614 | // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list* |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 615 | llvm::Type *ProtocolListnfABIPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 616 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 617 | // ClassnfABITy - LLVM for struct _class_t |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 618 | llvm::StructType *ClassnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 619 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 620 | // ClassnfABIPtrTy - LLVM for struct _class_t* |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 621 | llvm::Type *ClassnfABIPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 622 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 623 | // IvarnfABITy - LLVM for struct _ivar_t |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 624 | llvm::StructType *IvarnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 625 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 626 | // IvarListnfABITy - LLVM for struct _ivar_list_t |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 627 | llvm::StructType *IvarListnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 628 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 629 | // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t* |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 630 | llvm::Type *IvarListnfABIPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 631 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 632 | // ClassRonfABITy - LLVM for struct _class_ro_t |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 633 | llvm::StructType *ClassRonfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 634 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 635 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 636 | llvm::Type *ImpnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 637 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 638 | // CategorynfABITy - LLVM for struct _category_t |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 639 | llvm::StructType *CategorynfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 640 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 641 | // New types for nonfragile abi messaging. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 642 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 643 | // MessageRefTy - LLVM for: |
| 644 | // struct _message_ref_t { |
| 645 | // IMP messenger; |
| 646 | // SEL name; |
| 647 | // }; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 648 | llvm::StructType *MessageRefTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 649 | // MessageRefCTy - clang type for struct _message_ref_t |
| 650 | QualType MessageRefCTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 651 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 652 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 653 | llvm::Type *MessageRefPtrTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 654 | // MessageRefCPtrTy - clang type for struct _message_ref_t* |
| 655 | QualType MessageRefCPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 656 | |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 657 | // MessengerTy - Type of the messenger (shown as IMP above) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 658 | llvm::FunctionType *MessengerTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 659 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 660 | // SuperMessageRefTy - LLVM for: |
| 661 | // struct _super_message_ref_t { |
| 662 | // SUPER_IMP messenger; |
| 663 | // SEL name; |
| 664 | // }; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 665 | llvm::StructType *SuperMessageRefTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 666 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 667 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 668 | llvm::Type *SuperMessageRefPtrTy; |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 669 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 670 | llvm::Constant *getMessageSendFixupFn() { |
| 671 | // id objc_msgSend_fixup(id, struct message_ref_t*, ...) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 672 | llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 673 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 674 | params, true), |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 675 | "objc_msgSend_fixup"); |
| 676 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 677 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 678 | llvm::Constant *getMessageSendFpretFixupFn() { |
| 679 | // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 680 | llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 681 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 682 | params, true), |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 683 | "objc_msgSend_fpret_fixup"); |
| 684 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 685 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 686 | llvm::Constant *getMessageSendStretFixupFn() { |
| 687 | // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 688 | llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 689 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 690 | params, true), |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 691 | "objc_msgSend_stret_fixup"); |
| 692 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 693 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 694 | llvm::Constant *getMessageSendSuper2FixupFn() { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 695 | // id objc_msgSendSuper2_fixup (struct objc_super *, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 696 | // struct _super_message_ref_t*, ...) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 697 | llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 698 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 699 | params, true), |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 700 | "objc_msgSendSuper2_fixup"); |
| 701 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 702 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 703 | llvm::Constant *getMessageSendSuper2StretFixupFn() { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 704 | // id objc_msgSendSuper2_stret_fixup(struct objc_super *, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 705 | // struct _super_message_ref_t*, ...) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 706 | llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 707 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 708 | params, true), |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 709 | "objc_msgSendSuper2_stret_fixup"); |
| 710 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 711 | |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 712 | llvm::Constant *getObjCEndCatchFn() { |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 713 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false), |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 714 | "objc_end_catch"); |
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 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 717 | |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 718 | llvm::Constant *getObjCBeginCatchFn() { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 719 | llvm::Type *params[] = { Int8PtrTy }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 720 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 721 | params, false), |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 722 | "objc_begin_catch"); |
| 723 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 724 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 725 | llvm::StructType *EHTypeTy; |
| 726 | llvm::Type *EHTypePtrTy; |
Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 727 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 728 | ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm); |
| 729 | ~ObjCNonFragileABITypesHelper(){} |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 730 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 731 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 732 | class CGObjCCommonMac : public CodeGen::CGObjCRuntime { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 733 | public: |
| 734 | // FIXME - accessibility |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 735 | class GC_IVAR { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 736 | public: |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 737 | unsigned ivar_bytepos; |
| 738 | unsigned ivar_size; |
| 739 | GC_IVAR(unsigned bytepos = 0, unsigned size = 0) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 740 | : ivar_bytepos(bytepos), ivar_size(size) {} |
Daniel Dunbar | 0941b49 | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 741 | |
| 742 | // Allow sorting based on byte pos. |
| 743 | bool operator<(const GC_IVAR &b) const { |
| 744 | return ivar_bytepos < b.ivar_bytepos; |
| 745 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 746 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 747 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 748 | class SKIP_SCAN { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 749 | public: |
| 750 | unsigned skip; |
| 751 | unsigned scan; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 752 | SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0) |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 753 | : skip(_skip), scan(_scan) {} |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 754 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 755 | |
Fariborz Jahanian | e11dba8 | 2012-10-25 21:15:04 +0000 | [diff] [blame] | 756 | /// opcode for captured block variables layout 'instructions'. |
| 757 | /// In the following descriptions, 'I' is the value of the immediate field. |
| 758 | /// (field following the opcode). |
| 759 | /// |
| 760 | enum BLOCK_LAYOUT_OPCODE { |
| 761 | /// An operator which affects how the following layout should be |
| 762 | /// interpreted. |
| 763 | /// I == 0: Halt interpretation and treat everything else as |
| 764 | /// a non-pointer. Note that this instruction is equal |
| 765 | /// to '\0'. |
| 766 | /// I != 0: Currently unused. |
| 767 | BLOCK_LAYOUT_OPERATOR = 0, |
| 768 | |
| 769 | /// The next I+1 bytes do not contain a value of object pointer type. |
| 770 | /// Note that this can leave the stream unaligned, meaning that |
| 771 | /// subsequent word-size instructions do not begin at a multiple of |
| 772 | /// the pointer size. |
| 773 | BLOCK_LAYOUT_NON_OBJECT_BYTES = 1, |
| 774 | |
| 775 | /// The next I+1 words do not contain a value of object pointer type. |
| 776 | /// This is simply an optimized version of BLOCK_LAYOUT_BYTES for |
| 777 | /// when the required skip quantity is a multiple of the pointer size. |
| 778 | BLOCK_LAYOUT_NON_OBJECT_WORDS = 2, |
| 779 | |
| 780 | /// The next I+1 words are __strong pointers to Objective-C |
| 781 | /// objects or blocks. |
| 782 | BLOCK_LAYOUT_STRONG = 3, |
| 783 | |
| 784 | /// The next I+1 words are pointers to __block variables. |
| 785 | BLOCK_LAYOUT_BYREF = 4, |
| 786 | |
| 787 | /// The next I+1 words are __weak pointers to Objective-C |
| 788 | /// objects or blocks. |
| 789 | BLOCK_LAYOUT_WEAK = 5, |
| 790 | |
| 791 | /// The next I+1 words are __unsafe_unretained pointers to |
| 792 | /// Objective-C objects or blocks. |
| 793 | BLOCK_LAYOUT_UNRETAINED = 6 |
| 794 | |
| 795 | /// The next I+1 words are block or object pointers with some |
| 796 | /// as-yet-unspecified ownership semantics. If we add more |
| 797 | /// flavors of ownership semantics, values will be taken from |
| 798 | /// this range. |
| 799 | /// |
| 800 | /// This is included so that older tools can at least continue |
| 801 | /// processing the layout past such things. |
| 802 | //BLOCK_LAYOUT_OWNERSHIP_UNKNOWN = 7..10, |
| 803 | |
| 804 | /// All other opcodes are reserved. Halt interpretation and |
| 805 | /// treat everything else as opaque. |
| 806 | }; |
| 807 | |
| 808 | class RUN_SKIP { |
| 809 | public: |
| 810 | enum BLOCK_LAYOUT_OPCODE opcode; |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 811 | CharUnits block_var_bytepos; |
| 812 | CharUnits block_var_size; |
Fariborz Jahanian | e11dba8 | 2012-10-25 21:15:04 +0000 | [diff] [blame] | 813 | RUN_SKIP(enum BLOCK_LAYOUT_OPCODE Opcode = BLOCK_LAYOUT_OPERATOR, |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 814 | CharUnits BytePos = CharUnits::Zero(), |
| 815 | CharUnits Size = CharUnits::Zero()) |
Fariborz Jahanian | c46b435 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 816 | : opcode(Opcode), block_var_bytepos(BytePos), block_var_size(Size) {} |
Fariborz Jahanian | e11dba8 | 2012-10-25 21:15:04 +0000 | [diff] [blame] | 817 | |
| 818 | // Allow sorting based on byte pos. |
| 819 | bool operator<(const RUN_SKIP &b) const { |
| 820 | return block_var_bytepos < b.block_var_bytepos; |
| 821 | } |
| 822 | }; |
| 823 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 824 | protected: |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 825 | llvm::LLVMContext &VMContext; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 826 | // FIXME! May not be needing this after all. |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 827 | unsigned ObjCABI; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 828 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 829 | // gc ivar layout bitmap calculation helper caches. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 830 | SmallVector<GC_IVAR, 16> SkipIvars; |
| 831 | SmallVector<GC_IVAR, 16> IvarsInfo; |
Fariborz Jahanian | e11dba8 | 2012-10-25 21:15:04 +0000 | [diff] [blame] | 832 | |
| 833 | // arc/mrr layout of captured block literal variables. |
| 834 | SmallVector<RUN_SKIP, 16> RunSkipBlockVars; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 835 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 836 | /// LazySymbols - Symbols to generate a lazy reference for. See |
| 837 | /// DefinedSymbols and FinishModule(). |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 838 | llvm::SetVector<IdentifierInfo*> LazySymbols; |
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 | /// DefinedSymbols - External symbols which are defined by this |
| 841 | /// module. The symbols in this list and LazySymbols are used to add |
| 842 | /// special linker symbols which ensure that Objective-C modules are |
| 843 | /// linked properly. |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 844 | llvm::SetVector<IdentifierInfo*> DefinedSymbols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 845 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 846 | /// ClassNames - uniqued class names. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 847 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 848 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 849 | /// MethodVarNames - uniqued method variable names. |
| 850 | llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 851 | |
Fariborz Jahanian | b9c5b3d | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 852 | /// DefinedCategoryNames - list of category names in form Class_Category. |
| 853 | llvm::SetVector<std::string> DefinedCategoryNames; |
| 854 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 855 | /// MethodVarTypes - uniqued method type signatures. We have to use |
| 856 | /// a StringMap here because have no other unique reference. |
| 857 | llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 858 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 859 | /// MethodDefinitions - map of methods which have been defined in |
| 860 | /// this translation unit. |
| 861 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 862 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 863 | /// PropertyNames - uniqued method variable names. |
| 864 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 865 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 866 | /// ClassReferences - uniqued class references. |
| 867 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 868 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 869 | /// SelectorReferences - uniqued selector references. |
| 870 | llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 871 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 872 | /// Protocols - Protocols for which an objc_protocol structure has |
| 873 | /// been emitted. Forward declarations are handled by creating an |
| 874 | /// empty structure whose initializer is filled in when/if defined. |
| 875 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 876 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 877 | /// DefinedProtocols - Protocols which have actually been |
| 878 | /// defined. We should not need this, see FIXME in GenerateProtocol. |
| 879 | llvm::DenseSet<IdentifierInfo*> DefinedProtocols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 880 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 881 | /// DefinedClasses - List of defined classes. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame^] | 882 | SmallVector<llvm::GlobalValue*, 16> DefinedClasses; |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 883 | |
| 884 | /// DefinedNonLazyClasses - List of defined "non-lazy" classes. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame^] | 885 | SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyClasses; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 886 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 887 | /// DefinedCategories - List of defined categories. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame^] | 888 | SmallVector<llvm::GlobalValue*, 16> DefinedCategories; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 889 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 890 | /// DefinedNonLazyCategories - List of defined "non-lazy" categories. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame^] | 891 | SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyCategories; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 892 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 893 | /// GetNameForMethod - Return a name for the given method. |
| 894 | /// \param[out] NameOut - The return value. |
| 895 | void GetNameForMethod(const ObjCMethodDecl *OMD, |
| 896 | const ObjCContainerDecl *CD, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 897 | SmallVectorImpl<char> &NameOut); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 898 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 899 | /// GetMethodVarName - Return a unique constant for the given |
| 900 | /// selector's name. The return value has type char *. |
| 901 | llvm::Constant *GetMethodVarName(Selector Sel); |
| 902 | llvm::Constant *GetMethodVarName(IdentifierInfo *Ident); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 903 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 904 | /// GetMethodVarType - Return a unique constant for the given |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 905 | /// method's type encoding string. The return value has type char *. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 906 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 907 | // FIXME: This is a horrible name. |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 908 | llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D, |
| 909 | bool Extended = false); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 910 | llvm::Constant *GetMethodVarType(const FieldDecl *D); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 911 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 912 | /// GetPropertyName - Return a unique constant for the given |
| 913 | /// name. The return value has type char *. |
| 914 | llvm::Constant *GetPropertyName(IdentifierInfo *Ident); |
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 | // FIXME: This can be dropped once string functions are unified. |
| 917 | llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 918 | const Decl *Container); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 919 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 920 | /// GetClassName - Return a unique constant for the given selector's |
| 921 | /// name. The return value has type char *. |
| 922 | llvm::Constant *GetClassName(IdentifierInfo *Ident); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 923 | |
Argyrios Kyrtzidis | 9d50c06 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 924 | llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD); |
| 925 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 926 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 927 | /// implementation for the __strong or __weak case. |
| 928 | /// |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 929 | llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI, |
| 930 | bool ForStrongLayout); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 931 | |
Fariborz Jahanian | b8fd2eb | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 932 | llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 933 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 934 | void BuildAggrIvarRecordLayout(const RecordType *RT, |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 935 | unsigned int BytePos, bool ForStrongLayout, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 936 | bool &HasUnion); |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 937 | void BuildAggrIvarLayout(const ObjCImplementationDecl *OI, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 938 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 939 | const RecordDecl *RD, |
Bill Wendling | 795b100 | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 940 | ArrayRef<const FieldDecl*> RecFields, |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 941 | unsigned int BytePos, bool ForStrongLayout, |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 942 | bool &HasUnion); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 943 | |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 944 | Qualifiers::ObjCLifetime getBlockCaptureLifetime(QualType QT, bool ByrefLayout); |
Fariborz Jahanian | 44fcff9 | 2012-11-02 22:51:18 +0000 | [diff] [blame] | 945 | |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 946 | void UpdateRunSkipBlockVars(bool IsByref, |
| 947 | Qualifiers::ObjCLifetime LifeTime, |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 948 | CharUnits FieldOffset, |
| 949 | CharUnits FieldSize); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 950 | |
| 951 | void BuildRCBlockVarRecordLayout(const RecordType *RT, |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 952 | CharUnits BytePos, bool &HasUnion, |
| 953 | bool ByrefLayout=false); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 954 | |
| 955 | void BuildRCRecordLayout(const llvm::StructLayout *RecLayout, |
| 956 | const RecordDecl *RD, |
| 957 | ArrayRef<const FieldDecl*> RecFields, |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 958 | CharUnits BytePos, bool &HasUnion, |
| 959 | bool ByrefLayout); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 960 | |
Fariborz Jahanian | f22ae65 | 2012-11-01 18:32:55 +0000 | [diff] [blame] | 961 | uint64_t InlineLayoutInstruction(SmallVectorImpl<unsigned char> &Layout); |
| 962 | |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 963 | llvm::Constant *getBitmapBlockLayout(bool ComputeByrefLayout); |
| 964 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 965 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 966 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 967 | /// ivar layout bitmap. |
| 968 | llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident, |
| 969 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 970 | |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 971 | /// EmitPropertyList - Emit the given property list. The return |
| 972 | /// value has type PropertyListPtrTy. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 973 | llvm::Constant *EmitPropertyList(Twine Name, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 974 | const Decl *Container, |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 975 | const ObjCContainerDecl *OCD, |
| 976 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 977 | |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 978 | /// EmitProtocolMethodTypes - Generate the array of extended method type |
| 979 | /// strings. The return value has type Int8PtrPtrTy. |
| 980 | llvm::Constant *EmitProtocolMethodTypes(Twine Name, |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 981 | ArrayRef<llvm::Constant*> MethodTypes, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 982 | const ObjCCommonTypesHelper &ObjCTypes); |
| 983 | |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 984 | /// PushProtocolProperties - Push protocol's property on the input stack. |
Bill Wendling | 3964e62 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 985 | void PushProtocolProperties( |
| 986 | llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame^] | 987 | SmallVectorImpl<llvm::Constant*> &Properties, |
Bill Wendling | 3964e62 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 988 | const Decl *Container, |
| 989 | const ObjCProtocolDecl *PROTO, |
| 990 | const ObjCCommonTypesHelper &ObjCTypes); |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 991 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 992 | /// GetProtocolRef - Return a reference to the internal protocol |
| 993 | /// description, creating an empty one if it has not been |
| 994 | /// defined. The return value has type ProtocolPtrTy. |
| 995 | llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD); |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 996 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 997 | /// CreateMetadataVar - Create a global variable with internal |
| 998 | /// linkage for use by the Objective-C runtime. |
| 999 | /// |
| 1000 | /// This is a convenience wrapper which not only creates the |
| 1001 | /// variable, but also sets the section and alignment and adds the |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 1002 | /// global to the "llvm.used" list. |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 1003 | /// |
| 1004 | /// \param Name - The variable name. |
| 1005 | /// \param Init - The variable initializer; this is also used to |
| 1006 | /// define the type of the variable. |
| 1007 | /// \param Section - The section the variable should go into, or 0. |
| 1008 | /// \param Align - The alignment for the variable, or 0. |
| 1009 | /// \param AddToUsed - Whether the variable should be added to |
Daniel Dunbar | c158306 | 2009-04-14 17:42:51 +0000 | [diff] [blame] | 1010 | /// "llvm.used". |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1011 | llvm::GlobalVariable *CreateMetadataVar(Twine Name, |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 1012 | llvm::Constant *Init, |
| 1013 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 1014 | unsigned Align, |
| 1015 | bool AddToUsed); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 1016 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1017 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1018 | ReturnValueSlot Return, |
| 1019 | QualType ResultType, |
| 1020 | llvm::Value *Sel, |
| 1021 | llvm::Value *Arg0, |
| 1022 | QualType Arg0Ty, |
| 1023 | bool IsSuper, |
| 1024 | const CallArgList &CallArgs, |
| 1025 | const ObjCMethodDecl *OMD, |
| 1026 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 1027 | |
Daniel Dunbar | fce176b | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 1028 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 1029 | /// level information. |
| 1030 | void EmitImageInfo(); |
| 1031 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1032 | public: |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 1033 | CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1034 | CGObjCRuntime(cgm), VMContext(cgm.getLLVMContext()) { } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1035 | |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 1036 | virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1037 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1038 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
| 1039 | const ObjCContainerDecl *CD=0); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1040 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1041 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1042 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1043 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1044 | /// declaration, emitting it if necessary. The return value has type |
| 1045 | /// ProtocolPtrTy. |
| 1046 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1047 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1048 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1049 | /// object for the given declaration, emitting it if needed. These |
| 1050 | /// forward references will be filled in with empty bodies if no |
| 1051 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 1052 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0; |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1053 | virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM, |
| 1054 | const CGBlockInfo &blockInfo); |
Fariborz Jahanian | c46b435 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 1055 | virtual llvm::Constant *BuildRCBlockLayout(CodeGen::CodeGenModule &CGM, |
| 1056 | const CGBlockInfo &blockInfo); |
Fariborz Jahanian | 89ecd41 | 2010-08-04 16:57:49 +0000 | [diff] [blame] | 1057 | |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 1058 | virtual llvm::Constant *BuildByrefLayout(CodeGen::CodeGenModule &CGM, |
| 1059 | QualType T); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1060 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1061 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1062 | class CGObjCMac : public CGObjCCommonMac { |
| 1063 | private: |
| 1064 | ObjCTypesHelper ObjCTypes; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1065 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1066 | /// EmitModuleInfo - Another marker encoding module level |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1067 | /// information. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1068 | void EmitModuleInfo(); |
| 1069 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1070 | /// EmitModuleSymols - Emit module symbols, the list of defined |
| 1071 | /// classes and categories. The result has type SymtabPtrTy. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1072 | llvm::Constant *EmitModuleSymbols(); |
| 1073 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1074 | /// FinishModule - Write out global data structures at the end of |
| 1075 | /// processing a translation unit. |
| 1076 | void FinishModule(); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1077 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1078 | /// EmitClassExtension - Generate the class extension structure used |
| 1079 | /// to store the weak ivar layout and properties. The return value |
| 1080 | /// has type ClassExtensionPtrTy. |
| 1081 | llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID); |
| 1082 | |
| 1083 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 1084 | /// for the given class. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1085 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1086 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 1087 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1088 | llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder, |
| 1089 | IdentifierInfo *II); |
| 1090 | |
| 1091 | llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder); |
| 1092 | |
Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 1093 | /// EmitSuperClassRef - Emits reference to class's main metadata class. |
| 1094 | llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1095 | |
| 1096 | /// EmitIvarList - Emit the ivar list for the given |
| 1097 | /// implementation. If ForClass is true the list of class ivars |
| 1098 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 1099 | /// interface ivars will be emitted. The return value has type |
| 1100 | /// IvarListPtrTy. |
| 1101 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1102 | bool ForClass); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1103 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1104 | /// EmitMetaClass - Emit a forward reference to the class structure |
| 1105 | /// for the metaclass of the given interface. The return value has |
| 1106 | /// type ClassPtrTy. |
| 1107 | llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID); |
| 1108 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1109 | /// EmitMetaClass - Emit a class structure for the metaclass of the |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1110 | /// given implementation. The return value has type ClassPtrTy. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1111 | llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID, |
| 1112 | llvm::Constant *Protocols, |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 1113 | ArrayRef<llvm::Constant*> Methods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1114 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1115 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1116 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1117 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1118 | |
| 1119 | /// EmitMethodList - Emit the method list for the given |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1120 | /// implementation. The return value has type MethodListPtrTy. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1121 | llvm::Constant *EmitMethodList(Twine Name, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1122 | const char *Section, |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 1123 | ArrayRef<llvm::Constant*> Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1124 | |
| 1125 | /// EmitMethodDescList - Emit a method description list for a list of |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1126 | /// method declarations. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1127 | /// - TypeName: The name for the type containing the methods. |
Sylvestre Ledru | f3477c1 | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 1128 | /// - IsProtocol: True iff these methods are for a protocol. |
| 1129 | /// - ClassMethds: True iff these are class methods. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1130 | /// - Required: When true, only "required" methods are |
| 1131 | /// listed. Similarly, when false only "optional" methods are |
| 1132 | /// listed. For classes this should always be true. |
| 1133 | /// - begin, end: The method list to output. |
| 1134 | /// |
| 1135 | /// The return value has type MethodDescriptionListPtrTy. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1136 | llvm::Constant *EmitMethodDescList(Twine Name, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1137 | const char *Section, |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 1138 | ArrayRef<llvm::Constant*> Methods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1139 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1140 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1141 | /// declaration, emitting it if necessary. The return value has type |
| 1142 | /// ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1143 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1144 | |
| 1145 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1146 | /// object for the given declaration, emitting it if needed. These |
| 1147 | /// forward references will be filled in with empty bodies if no |
| 1148 | /// definition is seen. The return value has type ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1149 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1150 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1151 | /// EmitProtocolExtension - Generate the protocol extension |
| 1152 | /// structure used to store optional instance and class methods, and |
| 1153 | /// protocol properties. The return value has type |
| 1154 | /// ProtocolExtensionPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1155 | llvm::Constant * |
| 1156 | EmitProtocolExtension(const ObjCProtocolDecl *PD, |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 1157 | ArrayRef<llvm::Constant*> OptInstanceMethods, |
| 1158 | ArrayRef<llvm::Constant*> OptClassMethods, |
| 1159 | ArrayRef<llvm::Constant*> MethodTypesExt); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1160 | |
| 1161 | /// EmitProtocolList - Generate the list of referenced |
| 1162 | /// protocols. The return value has type ProtocolListPtrTy. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1163 | llvm::Constant *EmitProtocolList(Twine Name, |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1164 | ObjCProtocolDecl::protocol_iterator begin, |
| 1165 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1166 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1167 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1168 | /// for the given selector. |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1169 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel, |
| 1170 | bool lval=false); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1171 | |
| 1172 | public: |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1173 | CGObjCMac(CodeGen::CodeGenModule &cgm); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1174 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1175 | virtual llvm::Function *ModuleInitFunction(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1176 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1177 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1178 | ReturnValueSlot Return, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1179 | QualType ResultType, |
| 1180 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1181 | llvm::Value *Receiver, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1182 | const CallArgList &CallArgs, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1183 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1184 | const ObjCMethodDecl *Method); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1185 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1186 | virtual CodeGen::RValue |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1187 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1188 | ReturnValueSlot Return, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1189 | QualType ResultType, |
| 1190 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1191 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1192 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1193 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1194 | bool IsClassMessage, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1195 | const CallArgList &CallArgs, |
| 1196 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1197 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1198 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1199 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1200 | |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1201 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel, |
| 1202 | bool lval = false); |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1203 | |
| 1204 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an |
| 1205 | /// untyped one. |
| 1206 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, |
| 1207 | const ObjCMethodDecl *Method); |
| 1208 | |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1209 | virtual llvm::Constant *GetEHType(QualType T); |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1210 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1211 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1212 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1213 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1214 | |
Daniel Dunbar | 85fdea0 | 2012-02-28 15:36:15 +0000 | [diff] [blame] | 1215 | virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {} |
David Chisnall | 29254f4 | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 1216 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1217 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1218 | const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1219 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1220 | virtual llvm::Constant *GetPropertyGetFunction(); |
| 1221 | virtual llvm::Constant *GetPropertySetFunction(); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1222 | virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic, |
| 1223 | bool copy); |
David Chisnall | 8fac25d | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 1224 | virtual llvm::Constant *GetGetStructFunction(); |
| 1225 | virtual llvm::Constant *GetSetStructFunction(); |
David Chisnall | d397cfe | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 1226 | virtual llvm::Constant *GetCppAtomicObjectGetFunction(); |
| 1227 | virtual llvm::Constant *GetCppAtomicObjectSetFunction(); |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1228 | virtual llvm::Constant *EnumerationMutationFunction(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1229 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1230 | virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
| 1231 | const ObjCAtTryStmt &S); |
| 1232 | virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1233 | const ObjCAtSynchronizedStmt &S); |
| 1234 | void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1235 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6a3c70e | 2013-01-10 19:02:56 +0000 | [diff] [blame] | 1236 | const ObjCAtThrowStmt &S, |
| 1237 | bool ClearInsertionPoint=true); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1238 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1239 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1240 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1241 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1242 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 1243 | llvm::Value *src, llvm::Value *dest, |
| 1244 | bool threadlocal = false); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 1245 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1246 | llvm::Value *src, llvm::Value *dest, |
| 1247 | llvm::Value *ivarOffset); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1248 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 1249 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 1250 | virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
| 1251 | llvm::Value *dest, llvm::Value *src, |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 1252 | llvm::Value *size); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1253 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1254 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1255 | QualType ObjectTy, |
| 1256 | llvm::Value *BaseValue, |
| 1257 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1258 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1259 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1260 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1261 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | 6f40e22 | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1262 | |
| 1263 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 1264 | /// class of the given name. |
| 1265 | virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1266 | llvm_unreachable("CGObjCMac::GetClassGlobal"); |
Fariborz Jahanian | 6f40e22 | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1267 | } |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1268 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1269 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1270 | class CGObjCNonFragileABIMac : public CGObjCCommonMac { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1271 | private: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1272 | ObjCNonFragileABITypesHelper ObjCTypes; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1273 | llvm::GlobalVariable* ObjCEmptyCacheVar; |
| 1274 | llvm::GlobalVariable* ObjCEmptyVtableVar; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1275 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1276 | /// SuperClassReferences - uniqued super class references. |
| 1277 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1278 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1279 | /// MetaClassReferences - uniqued meta class references. |
| 1280 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1281 | |
| 1282 | /// EHTypeReferences - uniqued class ehtype references. |
| 1283 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1284 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1285 | /// VTableDispatchMethods - List of methods for which we generate |
| 1286 | /// vtable-based message dispatch. |
| 1287 | llvm::DenseSet<Selector> VTableDispatchMethods; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1288 | |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 1289 | /// DefinedMetaClasses - List of defined meta-classes. |
| 1290 | std::vector<llvm::GlobalValue*> DefinedMetaClasses; |
| 1291 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1292 | /// isVTableDispatchedSelector - Returns true if SEL is a |
| 1293 | /// vtable-based selector. |
| 1294 | bool isVTableDispatchedSelector(Selector Sel); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1295 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1296 | /// FinishNonFragileABIModule - Write out global data structures at the end of |
| 1297 | /// processing a translation unit. |
| 1298 | void FinishNonFragileABIModule(); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1299 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 1300 | /// AddModuleClassList - Add the given list of class pointers to the |
| 1301 | /// module with the provided symbol and section names. |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 1302 | void AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 1303 | const char *SymbolName, |
| 1304 | const char *SectionName); |
| 1305 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1306 | llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags, |
| 1307 | unsigned InstanceStart, |
| 1308 | unsigned InstanceSize, |
| 1309 | const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1310 | llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1311 | llvm::Constant *IsAGV, |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1312 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 1313 | llvm::Constant *ClassRoGV, |
| 1314 | bool HiddenVisibility); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1315 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1316 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1317 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 1318 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
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 | /// EmitMethodList - Emit the method list for the given |
| 1321 | /// implementation. The return value has type MethodListnfABITy. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1322 | llvm::Constant *EmitMethodList(Twine Name, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1323 | const char *Section, |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 1324 | ArrayRef<llvm::Constant*> Methods); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 1325 | /// EmitIvarList - Emit the ivar list for the given |
| 1326 | /// implementation. If ForClass is true the list of class ivars |
| 1327 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 1328 | /// interface ivars will be emitted. The return value has type |
| 1329 | /// IvarListnfABIPtrTy. |
| 1330 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1331 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1332 | llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 1333 | const ObjCIvarDecl *Ivar, |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 1334 | unsigned long int offset); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1335 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1336 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1337 | /// declaration, emitting it if necessary. The return value has type |
| 1338 | /// ProtocolPtrTy. |
| 1339 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1340 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1341 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1342 | /// object for the given declaration, emitting it if needed. These |
| 1343 | /// forward references will be filled in with empty bodies if no |
| 1344 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 1345 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1346 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1347 | /// EmitProtocolList - Generate the list of referenced |
| 1348 | /// protocols. The return value has type ProtocolListPtrTy. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1349 | llvm::Constant *EmitProtocolList(Twine Name, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1350 | ObjCProtocolDecl::protocol_iterator begin, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1351 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1352 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1353 | CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1354 | ReturnValueSlot Return, |
| 1355 | QualType ResultType, |
| 1356 | Selector Sel, |
| 1357 | llvm::Value *Receiver, |
| 1358 | QualType Arg0Ty, |
| 1359 | bool IsSuper, |
| 1360 | const CallArgList &CallArgs, |
| 1361 | const ObjCMethodDecl *Method); |
Fariborz Jahanian | 6f40e22 | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1362 | |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 1363 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 1364 | /// class of the given name. |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 1365 | llvm::GlobalVariable *GetClassGlobal(const std::string &Name); |
Fariborz Jahanian | 6f40e22 | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1366 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1367 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1368 | /// for the given class reference. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1369 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1370 | const ObjCInterfaceDecl *ID); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1371 | |
| 1372 | llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder, |
| 1373 | IdentifierInfo *II); |
| 1374 | |
| 1375 | llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1376 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1377 | /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 1378 | /// for the given super class reference. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1379 | llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder, |
| 1380 | const ObjCInterfaceDecl *ID); |
| 1381 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1382 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 1383 | /// meta-data |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1384 | llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1385 | const ObjCInterfaceDecl *ID); |
| 1386 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1387 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 1388 | /// the given ivar. |
| 1389 | /// |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 1390 | llvm::GlobalVariable * ObjCIvarOffsetVariable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1391 | const ObjCInterfaceDecl *ID, |
| 1392 | const ObjCIvarDecl *Ivar); |
| 1393 | |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 1394 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1395 | /// for the given selector. |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1396 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel, |
| 1397 | bool lval=false); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1398 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1399 | /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1400 | /// interface. The return value has type EHTypePtrTy. |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1401 | llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1402 | bool ForDefinition); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1403 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1404 | const char *getMetaclassSymbolPrefix() const { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1405 | return "OBJC_METACLASS_$_"; |
| 1406 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1407 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1408 | const char *getClassSymbolPrefix() const { |
| 1409 | return "OBJC_CLASS_$_"; |
| 1410 | } |
| 1411 | |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 1412 | void GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1413 | uint32_t &InstanceStart, |
| 1414 | uint32_t &InstanceSize); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1415 | |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1416 | // Shamelessly stolen from Analysis/CFRefCount.cpp |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1417 | Selector GetNullarySelector(const char* name) const { |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1418 | IdentifierInfo* II = &CGM.getContext().Idents.get(name); |
| 1419 | return CGM.getContext().Selectors.getSelector(0, &II); |
| 1420 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1421 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1422 | Selector GetUnarySelector(const char* name) const { |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1423 | IdentifierInfo* II = &CGM.getContext().Idents.get(name); |
| 1424 | return CGM.getContext().Selectors.getSelector(1, &II); |
| 1425 | } |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1426 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1427 | /// ImplementationIsNonLazy - Check whether the given category or |
| 1428 | /// class implementation is "non-lazy". |
Fariborz Jahanian | ecfbdcb | 2009-05-21 01:03:45 +0000 | [diff] [blame] | 1429 | bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const; |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1430 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1431 | public: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1432 | CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1433 | // FIXME. All stubs for now! |
| 1434 | virtual llvm::Function *ModuleInitFunction(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1435 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1436 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1437 | ReturnValueSlot Return, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1438 | QualType ResultType, |
| 1439 | Selector Sel, |
| 1440 | llvm::Value *Receiver, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1441 | const CallArgList &CallArgs, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1442 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1443 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1444 | |
| 1445 | virtual CodeGen::RValue |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1446 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1447 | ReturnValueSlot Return, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1448 | QualType ResultType, |
| 1449 | Selector Sel, |
| 1450 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1451 | bool isCategoryImpl, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1452 | llvm::Value *Receiver, |
| 1453 | bool IsClassMessage, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1454 | const CallArgList &CallArgs, |
| 1455 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1456 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1457 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1458 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1459 | |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1460 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel, |
| 1461 | bool lvalue = false) |
| 1462 | { return EmitSelector(Builder, Sel, lvalue); } |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1463 | |
| 1464 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an |
| 1465 | /// untyped one. |
| 1466 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, |
| 1467 | const ObjCMethodDecl *Method) |
| 1468 | { return EmitSelector(Builder, Method->getSelector()); } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1469 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 1470 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1471 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1472 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
David Chisnall | 29254f4 | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 1473 | |
Daniel Dunbar | 85fdea0 | 2012-02-28 15:36:15 +0000 | [diff] [blame] | 1474 | virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {} |
David Chisnall | 29254f4 | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 1475 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1476 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 1477 | const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1478 | |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1479 | virtual llvm::Constant *GetEHType(QualType T); |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1480 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1481 | virtual llvm::Constant *GetPropertyGetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1482 | return ObjCTypes.getGetPropertyFn(); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1483 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1484 | virtual llvm::Constant *GetPropertySetFunction() { |
| 1485 | return ObjCTypes.getSetPropertyFn(); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1486 | } |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 1487 | |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1488 | virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic, |
| 1489 | bool copy) { |
| 1490 | return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy); |
| 1491 | } |
| 1492 | |
David Chisnall | 8fac25d | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 1493 | virtual llvm::Constant *GetSetStructFunction() { |
| 1494 | return ObjCTypes.getCopyStructFn(); |
| 1495 | } |
| 1496 | virtual llvm::Constant *GetGetStructFunction() { |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 1497 | return ObjCTypes.getCopyStructFn(); |
| 1498 | } |
David Chisnall | d397cfe | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 1499 | virtual llvm::Constant *GetCppAtomicObjectSetFunction() { |
| 1500 | return ObjCTypes.getCppAtomicObjectFunction(); |
| 1501 | } |
| 1502 | virtual llvm::Constant *GetCppAtomicObjectGetFunction() { |
Fariborz Jahanian | e317302 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 1503 | return ObjCTypes.getCppAtomicObjectFunction(); |
| 1504 | } |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 1505 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1506 | virtual llvm::Constant *EnumerationMutationFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1507 | return ObjCTypes.getEnumerationMutationFn(); |
Daniel Dunbar | 28ed084 | 2009-02-16 18:48:45 +0000 | [diff] [blame] | 1508 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1509 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1510 | virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
| 1511 | const ObjCAtTryStmt &S); |
| 1512 | virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1513 | const ObjCAtSynchronizedStmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1514 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6a3c70e | 2013-01-10 19:02:56 +0000 | [diff] [blame] | 1515 | const ObjCAtThrowStmt &S, |
| 1516 | bool ClearInsertionPoint=true); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1517 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1518 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1519 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1520 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1521 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 1522 | llvm::Value *src, llvm::Value *dest, |
| 1523 | bool threadlocal = false); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1524 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1525 | llvm::Value *src, llvm::Value *dest, |
| 1526 | llvm::Value *ivarOffset); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1527 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1528 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 1529 | virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
| 1530 | llvm::Value *dest, llvm::Value *src, |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 1531 | llvm::Value *size); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1532 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1533 | QualType ObjectTy, |
| 1534 | llvm::Value *BaseValue, |
| 1535 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1536 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1537 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1538 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1539 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1540 | }; |
Fariborz Jahanian | 4e1524b | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1541 | |
| 1542 | /// A helper class for performing the null-initialization of a return |
| 1543 | /// value. |
| 1544 | struct NullReturnState { |
| 1545 | llvm::BasicBlock *NullBB; |
| 1546 | llvm::BasicBlock *callBB; |
| 1547 | NullReturnState() : NullBB(0), callBB(0) {} |
| 1548 | |
| 1549 | void init(CodeGenFunction &CGF, llvm::Value *receiver) { |
| 1550 | // Make blocks for the null-init and call edges. |
| 1551 | NullBB = CGF.createBasicBlock("msgSend.nullinit"); |
| 1552 | callBB = CGF.createBasicBlock("msgSend.call"); |
| 1553 | |
| 1554 | // Check for a null receiver and, if there is one, jump to the |
| 1555 | // null-init test. |
| 1556 | llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver); |
| 1557 | CGF.Builder.CreateCondBr(isNull, NullBB, callBB); |
| 1558 | |
| 1559 | // Otherwise, start performing the call. |
| 1560 | CGF.EmitBlock(callBB); |
| 1561 | } |
| 1562 | |
Fariborz Jahanian | 3c267e6 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1563 | RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType, |
| 1564 | const CallArgList &CallArgs, |
| 1565 | const ObjCMethodDecl *Method) { |
Fariborz Jahanian | 4e1524b | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1566 | if (!NullBB) return result; |
Fariborz Jahanian | 3c267e6 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1567 | |
| 1568 | llvm::Value *NullInitPtr = 0; |
| 1569 | if (result.isScalar() && !resultType->isVoidType()) { |
| 1570 | NullInitPtr = CGF.CreateTempAlloca(result.getScalarVal()->getType()); |
| 1571 | CGF.Builder.CreateStore(result.getScalarVal(), NullInitPtr); |
| 1572 | } |
Fariborz Jahanian | 4e1524b | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1573 | |
| 1574 | // Finish the call path. |
| 1575 | llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont"); |
| 1576 | if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB); |
| 1577 | |
| 1578 | // Emit the null-init block and perform the null-initialization there. |
| 1579 | CGF.EmitBlock(NullBB); |
Fariborz Jahanian | 3c267e6 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1580 | |
| 1581 | // Release consumed arguments along the null-receiver path. |
| 1582 | if (Method) { |
| 1583 | CallArgList::const_iterator I = CallArgs.begin(); |
| 1584 | for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(), |
| 1585 | e = Method->param_end(); i != e; ++i, ++I) { |
| 1586 | const ParmVarDecl *ParamDecl = (*i); |
| 1587 | if (ParamDecl->hasAttr<NSConsumedAttr>()) { |
| 1588 | RValue RV = I->RV; |
| 1589 | assert(RV.isScalar() && |
| 1590 | "NullReturnState::complete - arg not on object"); |
| 1591 | CGF.EmitARCRelease(RV.getScalarVal(), true); |
| 1592 | } |
| 1593 | } |
| 1594 | } |
| 1595 | |
| 1596 | if (result.isScalar()) { |
| 1597 | if (NullInitPtr) |
| 1598 | CGF.EmitNullInitialization(NullInitPtr, resultType); |
| 1599 | // Jump to the continuation block. |
| 1600 | CGF.EmitBlock(contBB); |
| 1601 | return NullInitPtr ? RValue::get(CGF.Builder.CreateLoad(NullInitPtr)) |
| 1602 | : result; |
| 1603 | } |
| 1604 | |
Fariborz Jahanian | 4e1524b | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1605 | if (!resultType->isAnyComplexType()) { |
| 1606 | assert(result.isAggregate() && "null init of non-aggregate result?"); |
| 1607 | CGF.EmitNullInitialization(result.getAggregateAddr(), resultType); |
| 1608 | // Jump to the continuation block. |
| 1609 | CGF.EmitBlock(contBB); |
| 1610 | return result; |
| 1611 | } |
| 1612 | |
| 1613 | // _Complex type |
| 1614 | // FIXME. Now easy to handle any other scalar type whose result is returned |
| 1615 | // in memory due to ABI limitations. |
| 1616 | CGF.EmitBlock(contBB); |
| 1617 | CodeGenFunction::ComplexPairTy CallCV = result.getComplexVal(); |
| 1618 | llvm::Type *MemberType = CallCV.first->getType(); |
| 1619 | llvm::Constant *ZeroCV = llvm::Constant::getNullValue(MemberType); |
| 1620 | // Create phi instruction for scalar complex value. |
| 1621 | llvm::PHINode *PHIReal = CGF.Builder.CreatePHI(MemberType, 2); |
| 1622 | PHIReal->addIncoming(ZeroCV, NullBB); |
| 1623 | PHIReal->addIncoming(CallCV.first, callBB); |
| 1624 | llvm::PHINode *PHIImag = CGF.Builder.CreatePHI(MemberType, 2); |
| 1625 | PHIImag->addIncoming(ZeroCV, NullBB); |
| 1626 | PHIImag->addIncoming(CallCV.second, callBB); |
| 1627 | return RValue::getComplex(PHIReal, PHIImag); |
| 1628 | } |
| 1629 | }; |
| 1630 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1631 | } // end anonymous namespace |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1632 | |
| 1633 | /* *** Helper Functions *** */ |
| 1634 | |
| 1635 | /// getConstantGEP() - Help routine to construct simple GEPs. |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1636 | static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1637 | llvm::Constant *C, |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1638 | unsigned idx0, |
| 1639 | unsigned idx1) { |
| 1640 | llvm::Value *Idxs[] = { |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1641 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0), |
| 1642 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1643 | }; |
Jay Foad | a5c0434 | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 1644 | return llvm::ConstantExpr::getGetElementPtr(C, Idxs); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1645 | } |
| 1646 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1647 | /// hasObjCExceptionAttribute - Return true if this class or any super |
| 1648 | /// class has the __objc_exception__ attribute. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1649 | static bool hasObjCExceptionAttribute(ASTContext &Context, |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 1650 | const ObjCInterfaceDecl *OID) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1651 | if (OID->hasAttr<ObjCExceptionAttr>()) |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1652 | return true; |
| 1653 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 1654 | return hasObjCExceptionAttribute(Context, Super); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1655 | return false; |
| 1656 | } |
| 1657 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1658 | /* *** CGObjCMac Public Interface *** */ |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1659 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1660 | CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1661 | ObjCTypes(cgm) { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1662 | ObjCABI = 1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1663 | EmitImageInfo(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 1666 | /// GetClass - Return a reference to the class for the given interface |
| 1667 | /// decl. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1668 | llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1669 | const ObjCInterfaceDecl *ID) { |
| 1670 | return EmitClassRef(Builder, ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1671 | } |
| 1672 | |
| 1673 | /// GetSelector - Return the pointer to the unique'd string for this selector. |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1674 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel, |
| 1675 | bool lval) { |
| 1676 | return EmitSelector(Builder, Sel, lval); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1677 | } |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1678 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1679 | *Method) { |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1680 | return EmitSelector(Builder, Method->getSelector()); |
| 1681 | } |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1682 | |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1683 | llvm::Constant *CGObjCMac::GetEHType(QualType T) { |
Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 1684 | if (T->isObjCIdType() || |
| 1685 | T->isObjCQualifiedIdType()) { |
| 1686 | return CGM.GetAddrOfRTTIDescriptor( |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 1687 | CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true); |
Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 1688 | } |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1689 | if (T->isObjCClassType() || |
| 1690 | T->isObjCQualifiedClassType()) { |
| 1691 | return CGM.GetAddrOfRTTIDescriptor( |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 1692 | CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true); |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1693 | } |
| 1694 | if (T->isObjCObjectPointerType()) |
| 1695 | return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true); |
| 1696 | |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1697 | llvm_unreachable("asking for catch type for ObjC type in fragile runtime"); |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1698 | } |
| 1699 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1700 | /// Generate a constant CFString object. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1701 | /* |
| 1702 | struct __builtin_CFString { |
| 1703 | const int *isa; // point to __CFConstantStringClassReference |
| 1704 | int flags; |
| 1705 | const char *str; |
| 1706 | long length; |
| 1707 | }; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1708 | */ |
| 1709 | |
Fariborz Jahanian | 33e982b | 2010-04-22 20:26:39 +0000 | [diff] [blame] | 1710 | /// or Generate a constant NSString object. |
| 1711 | /* |
| 1712 | struct __builtin_NSString { |
| 1713 | const int *isa; // point to __NSConstantStringClassReference |
| 1714 | const char *str; |
| 1715 | unsigned int length; |
| 1716 | }; |
| 1717 | */ |
| 1718 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1719 | llvm::Constant *CGObjCCommonMac::GenerateConstantString( |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 1720 | const StringLiteral *SL) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1721 | return (CGM.getLangOpts().NoConstantCFStrings == 0 ? |
Fariborz Jahanian | 33e982b | 2010-04-22 20:26:39 +0000 | [diff] [blame] | 1722 | CGM.GetAddrOfConstantCFString(SL) : |
Fariborz Jahanian | 4c73307 | 2010-10-19 17:19:29 +0000 | [diff] [blame] | 1723 | CGM.GetAddrOfConstantString(SL)); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1724 | } |
| 1725 | |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1726 | enum { |
| 1727 | kCFTaggedObjectID_Integer = (1 << 1) + 1 |
| 1728 | }; |
| 1729 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1730 | /// Generates a message send where the super is the receiver. This is |
| 1731 | /// a message send to self with special delivery semantics indicating |
| 1732 | /// which class's method should be called. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1733 | CodeGen::RValue |
| 1734 | CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1735 | ReturnValueSlot Return, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1736 | QualType ResultType, |
| 1737 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1738 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1739 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1740 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1741 | bool IsClassMessage, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1742 | const CodeGen::CallArgList &CallArgs, |
| 1743 | const ObjCMethodDecl *Method) { |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1744 | // Create and init a super structure; this is a (receiver, class) |
| 1745 | // pair we will pass to objc_msgSendSuper. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1746 | llvm::Value *ObjCSuper = |
John McCall | 604da29 | 2011-03-04 08:00:29 +0000 | [diff] [blame] | 1747 | CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1748 | llvm::Value *ReceiverAsObject = |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1749 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1750 | CGF.Builder.CreateStore(ReceiverAsObject, |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1751 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1752 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1753 | // If this is a class message the metaclass is passed as the target. |
| 1754 | llvm::Value *Target; |
| 1755 | if (IsClassMessage) { |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1756 | if (isCategoryImpl) { |
| 1757 | // Message sent to 'super' in a class method defined in a category |
| 1758 | // implementation requires an odd treatment. |
| 1759 | // If we are in a class method, we must retrieve the |
| 1760 | // _metaclass_ for the current class, pointed at by |
| 1761 | // the class's "isa" pointer. The following assumes that |
| 1762 | // isa" is the first ivar in a class (which it must be). |
| 1763 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1764 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 1765 | Target = CGF.Builder.CreateLoad(Target); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 1766 | } else { |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1767 | llvm::Value *MetaClassPtr = EmitMetaClassRef(Class); |
| 1768 | llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1); |
| 1769 | llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr); |
| 1770 | Target = Super; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1771 | } |
Fariborz Jahanian | 182f268 | 2009-11-14 02:18:31 +0000 | [diff] [blame] | 1772 | } |
| 1773 | else if (isCategoryImpl) |
| 1774 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1775 | else { |
Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 1776 | llvm::Value *ClassPtr = EmitSuperClassRef(Class); |
| 1777 | ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1); |
| 1778 | Target = CGF.Builder.CreateLoad(ClassPtr); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1779 | } |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1780 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext and |
| 1781 | // ObjCTypes types. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1782 | llvm::Type *ClassTy = |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1783 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1784 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1785 | CGF.Builder.CreateStore(Target, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1786 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1787 | return EmitMessageSend(CGF, Return, ResultType, |
| 1788 | EmitSelector(CGF.Builder, Sel), |
| 1789 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 1790 | true, CallArgs, Method, ObjCTypes); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1791 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1792 | |
| 1793 | /// Generate code for a message send expression. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1794 | CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1795 | ReturnValueSlot Return, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1796 | QualType ResultType, |
| 1797 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1798 | llvm::Value *Receiver, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1799 | const CallArgList &CallArgs, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1800 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1801 | const ObjCMethodDecl *Method) { |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1802 | return EmitMessageSend(CGF, Return, ResultType, |
| 1803 | EmitSelector(CGF.Builder, Sel), |
| 1804 | Receiver, CGF.getContext().getObjCIdType(), |
| 1805 | false, CallArgs, Method, ObjCTypes); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1806 | } |
| 1807 | |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1808 | CodeGen::RValue |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1809 | CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1810 | ReturnValueSlot Return, |
| 1811 | QualType ResultType, |
| 1812 | llvm::Value *Sel, |
| 1813 | llvm::Value *Arg0, |
| 1814 | QualType Arg0Ty, |
| 1815 | bool IsSuper, |
| 1816 | const CallArgList &CallArgs, |
| 1817 | const ObjCMethodDecl *Method, |
| 1818 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1819 | CallArgList ActualArgs; |
Fariborz Jahanian | d019d96 | 2009-04-24 21:07:43 +0000 | [diff] [blame] | 1820 | if (!IsSuper) |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 1821 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy); |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1822 | ActualArgs.add(RValue::get(Arg0), Arg0Ty); |
| 1823 | ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1824 | ActualArgs.addFrom(CallArgs); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1825 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1826 | // If we're calling a method, use the formal signature. |
| 1827 | MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1828 | |
Anders Carlsson | 7e70fb2 | 2010-06-21 20:59:55 +0000 | [diff] [blame] | 1829 | if (Method) |
| 1830 | assert(CGM.getContext().getCanonicalType(Method->getResultType()) == |
| 1831 | CGM.getContext().getCanonicalType(ResultType) && |
| 1832 | "Result type mismatch!"); |
| 1833 | |
John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 1834 | NullReturnState nullReturn; |
| 1835 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1836 | llvm::Constant *Fn = NULL; |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1837 | if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) { |
Fariborz Jahanian | 4e1524b | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1838 | if (!IsSuper) nullReturn.init(CGF, Arg0); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1839 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1840 | : ObjCTypes.getSendStretFn(IsSuper); |
Daniel Dunbar | dacf9dd | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 1841 | } else if (CGM.ReturnTypeUsesFPRet(ResultType)) { |
| 1842 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper) |
| 1843 | : ObjCTypes.getSendFpretFn(IsSuper); |
Anders Carlsson | eea6480 | 2011-10-31 16:27:11 +0000 | [diff] [blame] | 1844 | } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) { |
| 1845 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper) |
| 1846 | : ObjCTypes.getSendFp2retFn(IsSuper); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1847 | } else { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1848 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1849 | : ObjCTypes.getSendFn(IsSuper); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1850 | } |
Fariborz Jahanian | 3c267e6 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1851 | |
| 1852 | bool requiresnullCheck = false; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1853 | if (CGM.getLangOpts().ObjCAutoRefCount && Method) |
Fariborz Jahanian | 3c267e6 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1854 | for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(), |
| 1855 | e = Method->param_end(); i != e; ++i) { |
| 1856 | const ParmVarDecl *ParamDecl = (*i); |
| 1857 | if (ParamDecl->hasAttr<NSConsumedAttr>()) { |
| 1858 | if (!nullReturn.NullBB) |
| 1859 | nullReturn.init(CGF, Arg0); |
| 1860 | requiresnullCheck = true; |
| 1861 | break; |
| 1862 | } |
| 1863 | } |
| 1864 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1865 | Fn = llvm::ConstantExpr::getBitCast(Fn, MSI.MessengerType); |
| 1866 | RValue rvalue = CGF.EmitCall(MSI.CallInfo, Fn, Return, ActualArgs); |
Fariborz Jahanian | 3c267e6 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1867 | return nullReturn.complete(CGF, rvalue, ResultType, CallArgs, |
| 1868 | requiresnullCheck ? Method : 0); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1869 | } |
| 1870 | |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1871 | static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) { |
| 1872 | if (FQT.isObjCGCStrong()) |
| 1873 | return Qualifiers::Strong; |
| 1874 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1875 | if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak) |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1876 | return Qualifiers::Weak; |
| 1877 | |
Fariborz Jahanian | ba83c95 | 2012-02-16 00:15:02 +0000 | [diff] [blame] | 1878 | // check for __unsafe_unretained |
| 1879 | if (FQT.getObjCLifetime() == Qualifiers::OCL_ExplicitNone) |
| 1880 | return Qualifiers::GCNone; |
| 1881 | |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1882 | if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType()) |
| 1883 | return Qualifiers::Strong; |
| 1884 | |
| 1885 | if (const PointerType *PT = FQT->getAs<PointerType>()) |
| 1886 | return GetGCAttrTypeForType(Ctx, PT->getPointeeType()); |
| 1887 | |
| 1888 | return Qualifiers::GCNone; |
| 1889 | } |
| 1890 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1891 | llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM, |
| 1892 | const CGBlockInfo &blockInfo) { |
Fariborz Jahanian | c46b435 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 1893 | |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1894 | llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1895 | if (CGM.getLangOpts().getGC() == LangOptions::NonGC && |
| 1896 | !CGM.getLangOpts().ObjCAutoRefCount) |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1897 | return nullPtr; |
| 1898 | |
Fariborz Jahanian | a1f024c | 2010-08-06 16:28:55 +0000 | [diff] [blame] | 1899 | bool hasUnion = false; |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1900 | SkipIvars.clear(); |
| 1901 | IvarsInfo.clear(); |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 1902 | unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0); |
| 1903 | unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth(); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1904 | |
Fariborz Jahanian | 8197982 | 2010-09-09 00:21:45 +0000 | [diff] [blame] | 1905 | // __isa is the first field in block descriptor and must assume by runtime's |
| 1906 | // convention that it is GC'able. |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 1907 | IvarsInfo.push_back(GC_IVAR(0, 1)); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1908 | |
| 1909 | const BlockDecl *blockDecl = blockInfo.getBlockDecl(); |
| 1910 | |
| 1911 | // Calculate the basic layout of the block structure. |
| 1912 | const llvm::StructLayout *layout = |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 1913 | CGM.getDataLayout().getStructLayout(blockInfo.StructureType); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1914 | |
| 1915 | // Ignore the optional 'this' capture: C++ objects are not assumed |
| 1916 | // to be GC'ed. |
| 1917 | |
| 1918 | // Walk the captured variables. |
| 1919 | for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(), |
| 1920 | ce = blockDecl->capture_end(); ci != ce; ++ci) { |
| 1921 | const VarDecl *variable = ci->getVariable(); |
| 1922 | QualType type = variable->getType(); |
| 1923 | |
| 1924 | const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); |
| 1925 | |
| 1926 | // Ignore constant captures. |
| 1927 | if (capture.isConstant()) continue; |
| 1928 | |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 1929 | uint64_t fieldOffset = layout->getElementOffset(capture.getIndex()); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1930 | |
| 1931 | // __block variables are passed by their descriptor address. |
| 1932 | if (ci->isByRef()) { |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 1933 | IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1)); |
Fariborz Jahanian | c5904b4 | 2010-09-11 01:27:29 +0000 | [diff] [blame] | 1934 | continue; |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1935 | } |
| 1936 | |
| 1937 | assert(!type->isArrayType() && "array variable should not be caught"); |
| 1938 | if (const RecordType *record = type->getAs<RecordType>()) { |
| 1939 | BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion); |
Fariborz Jahanian | e1a4898 | 2010-08-05 21:00:25 +0000 | [diff] [blame] | 1940 | continue; |
| 1941 | } |
Fariborz Jahanian | a1f024c | 2010-08-06 16:28:55 +0000 | [diff] [blame] | 1942 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1943 | Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type); |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 1944 | unsigned fieldSize = CGM.getContext().getTypeSize(type); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1945 | |
| 1946 | if (GCAttr == Qualifiers::Strong) |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 1947 | IvarsInfo.push_back(GC_IVAR(fieldOffset, |
| 1948 | fieldSize / WordSizeInBits)); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1949 | else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak) |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 1950 | SkipIvars.push_back(GC_IVAR(fieldOffset, |
| 1951 | fieldSize / ByteSizeInBits)); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | if (IvarsInfo.empty()) |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1955 | return nullPtr; |
| 1956 | |
| 1957 | // Sort on byte position; captures might not be allocated in order, |
| 1958 | // and unions can do funny things. |
| 1959 | llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end()); |
| 1960 | llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end()); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1961 | |
| 1962 | std::string BitMap; |
Fariborz Jahanian | b8fd2eb | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 1963 | llvm::Constant *C = BuildIvarLayoutBitmap(BitMap); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1964 | if (CGM.getLangOpts().ObjCGCBitmapPrint) { |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1965 | printf("\n block variable layout for block: "); |
Roman Divacky | 31ba613 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 1966 | const unsigned char *s = (const unsigned char*)BitMap.c_str(); |
Bill Wendling | 13562a1 | 2012-02-07 09:06:01 +0000 | [diff] [blame] | 1967 | for (unsigned i = 0, e = BitMap.size(); i < e; i++) |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1968 | if (!(s[i] & 0xf0)) |
| 1969 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 1970 | else |
| 1971 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 1972 | printf("\n"); |
| 1973 | } |
| 1974 | |
| 1975 | return C; |
Fariborz Jahanian | 89ecd41 | 2010-08-04 16:57:49 +0000 | [diff] [blame] | 1976 | } |
| 1977 | |
Fariborz Jahanian | c441cd3 | 2012-11-04 18:19:40 +0000 | [diff] [blame] | 1978 | /// getBlockCaptureLifetime - This routine returns life time of the captured |
| 1979 | /// block variable for the purpose of block layout meta-data generation. FQT is |
| 1980 | /// the type of the variable captured in the block. |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 1981 | Qualifiers::ObjCLifetime CGObjCCommonMac::getBlockCaptureLifetime(QualType FQT, |
| 1982 | bool ByrefLayout) { |
Fariborz Jahanian | 44fcff9 | 2012-11-02 22:51:18 +0000 | [diff] [blame] | 1983 | if (CGM.getLangOpts().ObjCAutoRefCount) |
| 1984 | return FQT.getObjCLifetime(); |
| 1985 | |
Fariborz Jahanian | c441cd3 | 2012-11-04 18:19:40 +0000 | [diff] [blame] | 1986 | // MRR. |
Fariborz Jahanian | 44fcff9 | 2012-11-02 22:51:18 +0000 | [diff] [blame] | 1987 | if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType()) |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 1988 | return ByrefLayout ? Qualifiers::OCL_ExplicitNone : Qualifiers::OCL_Strong; |
Fariborz Jahanian | 44fcff9 | 2012-11-02 22:51:18 +0000 | [diff] [blame] | 1989 | |
| 1990 | return Qualifiers::OCL_None; |
| 1991 | } |
| 1992 | |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 1993 | void CGObjCCommonMac::UpdateRunSkipBlockVars(bool IsByref, |
| 1994 | Qualifiers::ObjCLifetime LifeTime, |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 1995 | CharUnits FieldOffset, |
| 1996 | CharUnits FieldSize) { |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 1997 | // __block variables are passed by their descriptor address. |
| 1998 | if (IsByref) |
| 1999 | RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_BYREF, FieldOffset, |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2000 | FieldSize)); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2001 | else if (LifeTime == Qualifiers::OCL_Strong) |
| 2002 | RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_STRONG, FieldOffset, |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2003 | FieldSize)); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2004 | else if (LifeTime == Qualifiers::OCL_Weak) |
| 2005 | RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_WEAK, FieldOffset, |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2006 | FieldSize)); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2007 | else if (LifeTime == Qualifiers::OCL_ExplicitNone) |
| 2008 | RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_UNRETAINED, FieldOffset, |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2009 | FieldSize)); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2010 | else |
| 2011 | RunSkipBlockVars.push_back(RUN_SKIP(BLOCK_LAYOUT_NON_OBJECT_BYTES, |
| 2012 | 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 | } |
| 2015 | |
| 2016 | void CGObjCCommonMac::BuildRCRecordLayout(const llvm::StructLayout *RecLayout, |
| 2017 | const RecordDecl *RD, |
| 2018 | ArrayRef<const FieldDecl*> RecFields, |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2019 | CharUnits BytePos, bool &HasUnion, |
| 2020 | bool ByrefLayout) { |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2021 | bool IsUnion = (RD && RD->isUnion()); |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2022 | CharUnits MaxUnionSize = CharUnits::Zero(); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2023 | const FieldDecl *MaxField = 0; |
| 2024 | const FieldDecl *LastFieldBitfieldOrUnnamed = 0; |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2025 | CharUnits MaxFieldOffset = CharUnits::Zero(); |
| 2026 | CharUnits LastBitfieldOrUnnamedOffset = CharUnits::Zero(); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2027 | |
| 2028 | if (RecFields.empty()) |
| 2029 | return; |
| 2030 | unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth(); |
| 2031 | |
| 2032 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
| 2033 | const FieldDecl *Field = RecFields[i]; |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2034 | // Note that 'i' here is actually the field index inside RD of Field, |
| 2035 | // although this dependency is hidden. |
| 2036 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2037 | CharUnits FieldOffset = |
| 2038 | CGM.getContext().toCharUnitsFromBits(RL.getFieldOffset(i)); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2039 | |
| 2040 | // Skip over unnamed or bitfields |
| 2041 | if (!Field->getIdentifier() || Field->isBitField()) { |
| 2042 | LastFieldBitfieldOrUnnamed = Field; |
| 2043 | LastBitfieldOrUnnamedOffset = FieldOffset; |
| 2044 | continue; |
| 2045 | } |
| 2046 | |
| 2047 | LastFieldBitfieldOrUnnamed = 0; |
| 2048 | QualType FQT = Field->getType(); |
| 2049 | if (FQT->isRecordType() || FQT->isUnionType()) { |
| 2050 | if (FQT->isUnionType()) |
| 2051 | HasUnion = true; |
| 2052 | |
| 2053 | BuildRCBlockVarRecordLayout(FQT->getAs<RecordType>(), |
| 2054 | BytePos + FieldOffset, HasUnion); |
| 2055 | continue; |
| 2056 | } |
| 2057 | |
| 2058 | if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 2059 | const ConstantArrayType *CArray = |
| 2060 | dyn_cast_or_null<ConstantArrayType>(Array); |
| 2061 | uint64_t ElCount = CArray->getSize().getZExtValue(); |
| 2062 | assert(CArray && "only array with known element size is supported"); |
| 2063 | FQT = CArray->getElementType(); |
| 2064 | while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 2065 | const ConstantArrayType *CArray = |
| 2066 | dyn_cast_or_null<ConstantArrayType>(Array); |
| 2067 | ElCount *= CArray->getSize().getZExtValue(); |
| 2068 | FQT = CArray->getElementType(); |
| 2069 | } |
| 2070 | |
| 2071 | assert(!FQT->isUnionType() && |
| 2072 | "layout for array of unions not supported"); |
| 2073 | if (FQT->isRecordType() && ElCount) { |
| 2074 | int OldIndex = RunSkipBlockVars.size() - 1; |
| 2075 | const RecordType *RT = FQT->getAs<RecordType>(); |
| 2076 | BuildRCBlockVarRecordLayout(RT, BytePos + FieldOffset, |
| 2077 | HasUnion); |
| 2078 | |
| 2079 | // Replicate layout information for each array element. Note that |
| 2080 | // one element is already done. |
| 2081 | uint64_t ElIx = 1; |
| 2082 | for (int FirstIndex = RunSkipBlockVars.size() - 1 ;ElIx < ElCount; ElIx++) { |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2083 | CharUnits Size = CGM.getContext().getTypeSizeInChars(RT); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2084 | for (int i = OldIndex+1; i <= FirstIndex; ++i) |
| 2085 | RunSkipBlockVars.push_back( |
| 2086 | RUN_SKIP(RunSkipBlockVars[i].opcode, |
| 2087 | RunSkipBlockVars[i].block_var_bytepos + Size*ElIx, |
| 2088 | RunSkipBlockVars[i].block_var_size)); |
| 2089 | } |
| 2090 | continue; |
| 2091 | } |
| 2092 | } |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2093 | CharUnits FieldSize = CGM.getContext().getTypeSizeInChars(Field->getType()); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2094 | if (IsUnion) { |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2095 | CharUnits UnionIvarSize = FieldSize; |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2096 | if (UnionIvarSize > MaxUnionSize) { |
| 2097 | MaxUnionSize = UnionIvarSize; |
| 2098 | MaxField = Field; |
| 2099 | MaxFieldOffset = FieldOffset; |
| 2100 | } |
| 2101 | } else { |
| 2102 | UpdateRunSkipBlockVars(false, |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2103 | getBlockCaptureLifetime(FQT, ByrefLayout), |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2104 | BytePos + FieldOffset, |
| 2105 | FieldSize); |
| 2106 | } |
| 2107 | } |
| 2108 | |
| 2109 | if (LastFieldBitfieldOrUnnamed) { |
| 2110 | if (LastFieldBitfieldOrUnnamed->isBitField()) { |
| 2111 | // Last field was a bitfield. Must update the info. |
| 2112 | uint64_t BitFieldSize |
| 2113 | = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext()); |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2114 | unsigned UnsSize = (BitFieldSize / ByteSizeInBits) + |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 2115 | ((BitFieldSize % ByteSizeInBits) != 0); |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2116 | CharUnits Size = CharUnits::fromQuantity(UnsSize); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2117 | Size += LastBitfieldOrUnnamedOffset; |
| 2118 | UpdateRunSkipBlockVars(false, |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2119 | getBlockCaptureLifetime(LastFieldBitfieldOrUnnamed->getType(), |
| 2120 | ByrefLayout), |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2121 | BytePos + LastBitfieldOrUnnamedOffset, |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2122 | Size); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2123 | } else { |
| 2124 | assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed"); |
| 2125 | // Last field was unnamed. Must update skip info. |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2126 | CharUnits FieldSize |
| 2127 | = CGM.getContext().getTypeSizeInChars(LastFieldBitfieldOrUnnamed->getType()); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2128 | UpdateRunSkipBlockVars(false, |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2129 | getBlockCaptureLifetime(LastFieldBitfieldOrUnnamed->getType(), |
| 2130 | ByrefLayout), |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2131 | BytePos + LastBitfieldOrUnnamedOffset, |
| 2132 | FieldSize); |
| 2133 | } |
| 2134 | } |
| 2135 | |
| 2136 | if (MaxField) |
| 2137 | UpdateRunSkipBlockVars(false, |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2138 | getBlockCaptureLifetime(MaxField->getType(), ByrefLayout), |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2139 | BytePos + MaxFieldOffset, |
| 2140 | MaxUnionSize); |
| 2141 | } |
| 2142 | |
| 2143 | void CGObjCCommonMac::BuildRCBlockVarRecordLayout(const RecordType *RT, |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2144 | CharUnits BytePos, |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2145 | bool &HasUnion, |
| 2146 | bool ByrefLayout) { |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2147 | const RecordDecl *RD = RT->getDecl(); |
| 2148 | SmallVector<const FieldDecl*, 16> Fields; |
| 2149 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 2150 | e = RD->field_end(); i != e; ++i) |
| 2151 | Fields.push_back(*i); |
| 2152 | llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0)); |
| 2153 | const llvm::StructLayout *RecLayout = |
| 2154 | CGM.getDataLayout().getStructLayout(cast<llvm::StructType>(Ty)); |
| 2155 | |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2156 | BuildRCRecordLayout(RecLayout, RD, Fields, BytePos, HasUnion, ByrefLayout); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2157 | } |
| 2158 | |
Fariborz Jahanian | f22ae65 | 2012-11-01 18:32:55 +0000 | [diff] [blame] | 2159 | /// InlineLayoutInstruction - This routine produce an inline instruction for the |
| 2160 | /// block variable layout if it can. If not, it returns 0. Rules are as follow: |
| 2161 | /// If ((uintptr_t) layout) < (1 << 12), the layout is inline. In the 64bit world, |
| 2162 | /// an inline layout of value 0x0000000000000xyz is interpreted as follows: |
| 2163 | /// x captured object pointers of BLOCK_LAYOUT_STRONG. Followed by |
| 2164 | /// y captured object of BLOCK_LAYOUT_BYREF. Followed by |
| 2165 | /// z captured object of BLOCK_LAYOUT_WEAK. If any of the above is missing, zero |
| 2166 | /// replaces it. For example, 0x00000x00 means x BLOCK_LAYOUT_STRONG and no |
| 2167 | /// BLOCK_LAYOUT_BYREF and no BLOCK_LAYOUT_WEAK objects are captured. |
| 2168 | uint64_t CGObjCCommonMac::InlineLayoutInstruction( |
| 2169 | SmallVectorImpl<unsigned char> &Layout) { |
| 2170 | uint64_t Result = 0; |
| 2171 | if (Layout.size() <= 3) { |
| 2172 | unsigned size = Layout.size(); |
| 2173 | unsigned strong_word_count = 0, byref_word_count=0, weak_word_count=0; |
| 2174 | unsigned char inst; |
| 2175 | enum BLOCK_LAYOUT_OPCODE opcode ; |
| 2176 | switch (size) { |
| 2177 | case 3: |
| 2178 | inst = Layout[0]; |
| 2179 | opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2180 | if (opcode == BLOCK_LAYOUT_STRONG) |
| 2181 | strong_word_count = (inst & 0xF)+1; |
| 2182 | else |
| 2183 | return 0; |
| 2184 | inst = Layout[1]; |
| 2185 | opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2186 | if (opcode == BLOCK_LAYOUT_BYREF) |
| 2187 | byref_word_count = (inst & 0xF)+1; |
| 2188 | else |
| 2189 | return 0; |
| 2190 | inst = Layout[2]; |
| 2191 | opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2192 | if (opcode == BLOCK_LAYOUT_WEAK) |
| 2193 | weak_word_count = (inst & 0xF)+1; |
| 2194 | else |
| 2195 | return 0; |
| 2196 | break; |
| 2197 | |
| 2198 | case 2: |
| 2199 | inst = Layout[0]; |
| 2200 | opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2201 | if (opcode == BLOCK_LAYOUT_STRONG) { |
| 2202 | strong_word_count = (inst & 0xF)+1; |
| 2203 | inst = Layout[1]; |
| 2204 | opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2205 | if (opcode == BLOCK_LAYOUT_BYREF) |
| 2206 | byref_word_count = (inst & 0xF)+1; |
| 2207 | else if (opcode == BLOCK_LAYOUT_WEAK) |
| 2208 | weak_word_count = (inst & 0xF)+1; |
| 2209 | else |
| 2210 | return 0; |
| 2211 | } |
| 2212 | else if (opcode == BLOCK_LAYOUT_BYREF) { |
| 2213 | byref_word_count = (inst & 0xF)+1; |
| 2214 | inst = Layout[1]; |
| 2215 | opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2216 | if (opcode == BLOCK_LAYOUT_WEAK) |
| 2217 | weak_word_count = (inst & 0xF)+1; |
| 2218 | else |
| 2219 | return 0; |
| 2220 | } |
| 2221 | else |
| 2222 | return 0; |
| 2223 | break; |
| 2224 | |
| 2225 | case 1: |
| 2226 | inst = Layout[0]; |
| 2227 | opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2228 | if (opcode == BLOCK_LAYOUT_STRONG) |
| 2229 | strong_word_count = (inst & 0xF)+1; |
| 2230 | else if (opcode == BLOCK_LAYOUT_BYREF) |
| 2231 | byref_word_count = (inst & 0xF)+1; |
| 2232 | else if (opcode == BLOCK_LAYOUT_WEAK) |
| 2233 | weak_word_count = (inst & 0xF)+1; |
| 2234 | else |
| 2235 | return 0; |
| 2236 | break; |
| 2237 | |
| 2238 | default: |
| 2239 | return 0; |
| 2240 | } |
| 2241 | |
| 2242 | // Cannot inline when any of the word counts is 15. Because this is one less |
| 2243 | // than the actual work count (so 15 means 16 actual word counts), |
| 2244 | // and we can only display 0 thru 15 word counts. |
| 2245 | if (strong_word_count == 16 || byref_word_count == 16 || weak_word_count == 16) |
| 2246 | return 0; |
| 2247 | |
| 2248 | unsigned count = |
| 2249 | (strong_word_count != 0) + (byref_word_count != 0) + (weak_word_count != 0); |
| 2250 | |
| 2251 | if (size == count) { |
| 2252 | if (strong_word_count) |
| 2253 | Result = strong_word_count; |
| 2254 | Result <<= 4; |
| 2255 | if (byref_word_count) |
| 2256 | Result += byref_word_count; |
| 2257 | Result <<= 4; |
| 2258 | if (weak_word_count) |
| 2259 | Result += weak_word_count; |
| 2260 | } |
| 2261 | } |
| 2262 | return Result; |
| 2263 | } |
| 2264 | |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2265 | llvm::Constant *CGObjCCommonMac::getBitmapBlockLayout(bool ComputeByrefLayout) { |
| 2266 | llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy); |
| 2267 | if (RunSkipBlockVars.empty()) |
| 2268 | return nullPtr; |
| 2269 | unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0); |
| 2270 | unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth(); |
| 2271 | unsigned WordSizeInBytes = WordSizeInBits/ByteSizeInBits; |
| 2272 | |
| 2273 | // Sort on byte position; captures might not be allocated in order, |
| 2274 | // and unions can do funny things. |
| 2275 | llvm::array_pod_sort(RunSkipBlockVars.begin(), RunSkipBlockVars.end()); |
| 2276 | SmallVector<unsigned char, 16> Layout; |
| 2277 | |
| 2278 | unsigned size = RunSkipBlockVars.size(); |
| 2279 | for (unsigned i = 0; i < size; i++) { |
| 2280 | enum BLOCK_LAYOUT_OPCODE opcode = RunSkipBlockVars[i].opcode; |
| 2281 | CharUnits start_byte_pos = RunSkipBlockVars[i].block_var_bytepos; |
| 2282 | CharUnits end_byte_pos = start_byte_pos; |
| 2283 | unsigned j = i+1; |
| 2284 | while (j < size) { |
| 2285 | if (opcode == RunSkipBlockVars[j].opcode) { |
| 2286 | end_byte_pos = RunSkipBlockVars[j++].block_var_bytepos; |
| 2287 | i++; |
| 2288 | } |
| 2289 | else |
| 2290 | break; |
| 2291 | } |
| 2292 | CharUnits size_in_bytes = |
| 2293 | end_byte_pos - start_byte_pos + RunSkipBlockVars[j-1].block_var_size; |
| 2294 | if (j < size) { |
| 2295 | CharUnits gap = |
| 2296 | RunSkipBlockVars[j].block_var_bytepos - |
| 2297 | RunSkipBlockVars[j-1].block_var_bytepos - RunSkipBlockVars[j-1].block_var_size; |
| 2298 | size_in_bytes += gap; |
| 2299 | } |
| 2300 | CharUnits residue_in_bytes = CharUnits::Zero(); |
| 2301 | if (opcode == BLOCK_LAYOUT_NON_OBJECT_BYTES) { |
| 2302 | residue_in_bytes = size_in_bytes % WordSizeInBytes; |
| 2303 | size_in_bytes -= residue_in_bytes; |
| 2304 | opcode = BLOCK_LAYOUT_NON_OBJECT_WORDS; |
| 2305 | } |
| 2306 | |
| 2307 | unsigned size_in_words = size_in_bytes.getQuantity() / WordSizeInBytes; |
| 2308 | while (size_in_words >= 16) { |
| 2309 | // Note that value in imm. is one less that the actual |
| 2310 | // value. So, 0xf means 16 words follow! |
| 2311 | unsigned char inst = (opcode << 4) | 0xf; |
| 2312 | Layout.push_back(inst); |
| 2313 | size_in_words -= 16; |
| 2314 | } |
| 2315 | if (size_in_words > 0) { |
| 2316 | // Note that value in imm. is one less that the actual |
| 2317 | // value. So, we subtract 1 away! |
| 2318 | unsigned char inst = (opcode << 4) | (size_in_words-1); |
| 2319 | Layout.push_back(inst); |
| 2320 | } |
| 2321 | if (residue_in_bytes > CharUnits::Zero()) { |
| 2322 | unsigned char inst = |
| 2323 | (BLOCK_LAYOUT_NON_OBJECT_BYTES << 4) | (residue_in_bytes.getQuantity()-1); |
| 2324 | Layout.push_back(inst); |
| 2325 | } |
| 2326 | } |
| 2327 | |
| 2328 | int e = Layout.size()-1; |
| 2329 | while (e >= 0) { |
| 2330 | unsigned char inst = Layout[e--]; |
| 2331 | enum BLOCK_LAYOUT_OPCODE opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2332 | if (opcode == BLOCK_LAYOUT_NON_OBJECT_BYTES || opcode == BLOCK_LAYOUT_NON_OBJECT_WORDS) |
| 2333 | Layout.pop_back(); |
| 2334 | else |
| 2335 | break; |
| 2336 | } |
| 2337 | |
| 2338 | uint64_t Result = InlineLayoutInstruction(Layout); |
| 2339 | if (Result != 0) { |
| 2340 | // Block variable layout instruction has been inlined. |
| 2341 | if (CGM.getLangOpts().ObjCGCBitmapPrint) { |
| 2342 | if (ComputeByrefLayout) |
| 2343 | printf("\n Inline instruction for BYREF variable layout: "); |
| 2344 | else |
| 2345 | printf("\n Inline instruction for block variable layout: "); |
| 2346 | printf("0x0%llx\n", (unsigned long long)Result); |
| 2347 | } |
| 2348 | if (WordSizeInBytes == 8) { |
| 2349 | const llvm::APInt Instruction(64, Result); |
| 2350 | return llvm::Constant::getIntegerValue(CGM.Int64Ty, Instruction); |
| 2351 | } |
| 2352 | else { |
| 2353 | const llvm::APInt Instruction(32, Result); |
| 2354 | return llvm::Constant::getIntegerValue(CGM.Int32Ty, Instruction); |
| 2355 | } |
| 2356 | } |
| 2357 | |
| 2358 | unsigned char inst = (BLOCK_LAYOUT_OPERATOR << 4) | 0; |
| 2359 | Layout.push_back(inst); |
| 2360 | std::string BitMap; |
| 2361 | for (unsigned i = 0, e = Layout.size(); i != e; i++) |
| 2362 | BitMap += Layout[i]; |
| 2363 | |
| 2364 | if (CGM.getLangOpts().ObjCGCBitmapPrint) { |
| 2365 | if (ComputeByrefLayout) |
| 2366 | printf("\n BYREF variable layout: "); |
| 2367 | else |
| 2368 | printf("\n block variable layout: "); |
| 2369 | for (unsigned i = 0, e = BitMap.size(); i != e; i++) { |
| 2370 | unsigned char inst = BitMap[i]; |
| 2371 | enum BLOCK_LAYOUT_OPCODE opcode = (enum BLOCK_LAYOUT_OPCODE) (inst >> 4); |
| 2372 | unsigned delta = 1; |
| 2373 | switch (opcode) { |
| 2374 | case BLOCK_LAYOUT_OPERATOR: |
| 2375 | printf("BL_OPERATOR:"); |
| 2376 | delta = 0; |
| 2377 | break; |
| 2378 | case BLOCK_LAYOUT_NON_OBJECT_BYTES: |
| 2379 | printf("BL_NON_OBJECT_BYTES:"); |
| 2380 | break; |
| 2381 | case BLOCK_LAYOUT_NON_OBJECT_WORDS: |
| 2382 | printf("BL_NON_OBJECT_WORD:"); |
| 2383 | break; |
| 2384 | case BLOCK_LAYOUT_STRONG: |
| 2385 | printf("BL_STRONG:"); |
| 2386 | break; |
| 2387 | case BLOCK_LAYOUT_BYREF: |
| 2388 | printf("BL_BYREF:"); |
| 2389 | break; |
| 2390 | case BLOCK_LAYOUT_WEAK: |
| 2391 | printf("BL_WEAK:"); |
| 2392 | break; |
| 2393 | case BLOCK_LAYOUT_UNRETAINED: |
| 2394 | printf("BL_UNRETAINED:"); |
| 2395 | break; |
| 2396 | } |
| 2397 | // Actual value of word count is one more that what is in the imm. |
| 2398 | // field of the instruction |
| 2399 | printf("%d", (inst & 0xf) + delta); |
| 2400 | if (i < e-1) |
| 2401 | printf(", "); |
| 2402 | else |
| 2403 | printf("\n"); |
| 2404 | } |
| 2405 | } |
| 2406 | |
| 2407 | llvm::GlobalVariable * Entry = |
| 2408 | CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
| 2409 | llvm::ConstantDataArray::getString(VMContext, BitMap,false), |
| 2410 | "__TEXT,__objc_classname,cstring_literals", 1, true); |
| 2411 | return getConstantGEP(VMContext, Entry, 0, 0); |
| 2412 | } |
| 2413 | |
Fariborz Jahanian | c46b435 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2414 | llvm::Constant *CGObjCCommonMac::BuildRCBlockLayout(CodeGenModule &CGM, |
| 2415 | const CGBlockInfo &blockInfo) { |
Fariborz Jahanian | c46b435 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2416 | assert(CGM.getLangOpts().getGC() == LangOptions::NonGC); |
| 2417 | |
Fariborz Jahanian | c46b435 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2418 | RunSkipBlockVars.clear(); |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2419 | bool hasUnion = false; |
| 2420 | |
Fariborz Jahanian | c46b435 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2421 | unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0); |
| 2422 | unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth(); |
| 2423 | unsigned WordSizeInBytes = WordSizeInBits/ByteSizeInBits; |
| 2424 | |
| 2425 | const BlockDecl *blockDecl = blockInfo.getBlockDecl(); |
| 2426 | |
| 2427 | // Calculate the basic layout of the block structure. |
| 2428 | const llvm::StructLayout *layout = |
| 2429 | CGM.getDataLayout().getStructLayout(blockInfo.StructureType); |
| 2430 | |
| 2431 | // Ignore the optional 'this' capture: C++ objects are not assumed |
| 2432 | // to be GC'ed. |
Fariborz Jahanian | ff685c5 | 2012-12-04 17:20:57 +0000 | [diff] [blame] | 2433 | if (blockInfo.BlockHeaderForcedGapSize != CharUnits::Zero()) |
| 2434 | UpdateRunSkipBlockVars(false, Qualifiers::OCL_None, |
| 2435 | blockInfo.BlockHeaderForcedGapOffset, |
| 2436 | blockInfo.BlockHeaderForcedGapSize); |
Fariborz Jahanian | c46b435 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2437 | // Walk the captured variables. |
| 2438 | for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(), |
| 2439 | ce = blockDecl->capture_end(); ci != ce; ++ci) { |
| 2440 | const VarDecl *variable = ci->getVariable(); |
| 2441 | QualType type = variable->getType(); |
| 2442 | |
| 2443 | const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); |
| 2444 | |
| 2445 | // Ignore constant captures. |
| 2446 | if (capture.isConstant()) continue; |
| 2447 | |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2448 | CharUnits fieldOffset = |
| 2449 | CharUnits::fromQuantity(layout->getElementOffset(capture.getIndex())); |
Fariborz Jahanian | c46b435 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2450 | |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2451 | assert(!type->isArrayType() && "array variable should not be caught"); |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2452 | if (!ci->isByRef()) |
| 2453 | if (const RecordType *record = type->getAs<RecordType>()) { |
| 2454 | BuildRCBlockVarRecordLayout(record, fieldOffset, hasUnion); |
| 2455 | continue; |
| 2456 | } |
Fariborz Jahanian | 1da01d6 | 2012-11-07 20:00:32 +0000 | [diff] [blame] | 2457 | CharUnits fieldSize; |
| 2458 | if (ci->isByRef()) |
| 2459 | fieldSize = CharUnits::fromQuantity(WordSizeInBytes); |
| 2460 | else |
| 2461 | fieldSize = CGM.getContext().getTypeSizeInChars(type); |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2462 | UpdateRunSkipBlockVars(ci->isByRef(), getBlockCaptureLifetime(type, false), |
Fariborz Jahanian | c6abe9e | 2012-10-30 20:05:29 +0000 | [diff] [blame] | 2463 | fieldOffset, fieldSize); |
Fariborz Jahanian | c46b435 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2464 | } |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2465 | return getBitmapBlockLayout(false); |
| 2466 | } |
Fariborz Jahanian | c46b435 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2467 | |
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 | llvm::Constant *CGObjCCommonMac::BuildByrefLayout(CodeGen::CodeGenModule &CGM, |
| 2470 | QualType T) { |
| 2471 | assert(CGM.getLangOpts().getGC() == LangOptions::NonGC); |
| 2472 | assert(!T->isArrayType() && "__block array variable should not be caught"); |
| 2473 | CharUnits fieldOffset; |
| 2474 | RunSkipBlockVars.clear(); |
| 2475 | bool hasUnion = false; |
| 2476 | if (const RecordType *record = T->getAs<RecordType>()) { |
| 2477 | BuildRCBlockVarRecordLayout(record, fieldOffset, hasUnion, true /*ByrefLayout */); |
| 2478 | llvm::Constant *Result = getBitmapBlockLayout(true); |
| 2479 | return Result; |
Fariborz Jahanian | c46b435 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2480 | } |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 2481 | llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy); |
| 2482 | return nullPtr; |
Fariborz Jahanian | c46b435 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 2483 | } |
| 2484 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2485 | llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 2486 | const ObjCProtocolDecl *PD) { |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 2487 | // 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] | 2488 | // resolved. Investigate. Its also wasteful to look this up over and over. |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 2489 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 2490 | |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2491 | return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), |
Douglas Gregor | 4c86fdb | 2012-01-17 18:36:30 +0000 | [diff] [blame] | 2492 | ObjCTypes.getExternalProtocolPtrTy()); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2493 | } |
| 2494 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 2495 | void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2496 | // FIXME: We shouldn't need this, the protocol decl should contain enough |
| 2497 | // information to tell us whether this was a declaration or a definition. |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2498 | DefinedProtocols.insert(PD->getIdentifier()); |
| 2499 | |
| 2500 | // If we have generated a forward reference to this protocol, emit |
| 2501 | // it now. Otherwise do nothing, the protocol objects are lazily |
| 2502 | // emitted. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2503 | if (Protocols.count(PD->getIdentifier())) |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2504 | GetOrEmitProtocol(PD); |
| 2505 | } |
| 2506 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 2507 | llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2508 | if (DefinedProtocols.count(PD->getIdentifier())) |
| 2509 | return GetOrEmitProtocol(PD); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 2510 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2511 | return GetOrEmitProtocolRef(PD); |
| 2512 | } |
| 2513 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2514 | /* |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2515 | // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions |
| 2516 | struct _objc_protocol { |
| 2517 | struct _objc_protocol_extension *isa; |
| 2518 | char *protocol_name; |
| 2519 | struct _objc_protocol_list *protocol_list; |
| 2520 | struct _objc__method_prototype_list *instance_methods; |
| 2521 | struct _objc__method_prototype_list *class_methods |
| 2522 | }; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2523 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2524 | See EmitProtocolExtension(). |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2525 | */ |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2526 | llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) { |
John McCall | 50651b9 | 2012-03-30 21:29:05 +0000 | [diff] [blame] | 2527 | llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()]; |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2528 | |
| 2529 | // Early exit if a defining object has already been generated. |
| 2530 | if (Entry && Entry->hasInitializer()) |
| 2531 | return Entry; |
| 2532 | |
Douglas Gregor | 1d784b2 | 2012-01-01 19:51:50 +0000 | [diff] [blame] | 2533 | // Use the protocol definition, if there is one. |
| 2534 | if (const ObjCProtocolDecl *Def = PD->getDefinition()) |
| 2535 | PD = Def; |
| 2536 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2537 | // 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] | 2538 | // resolved. Investigate. Its also wasteful to look this up over and over. |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2539 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 2540 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2541 | // Construct method lists. |
| 2542 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 2543 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2544 | std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2545 | for (ObjCProtocolDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2546 | i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2547 | ObjCMethodDecl *MD = *i; |
| 2548 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 2549 | if (!C) |
| 2550 | return GetOrEmitProtocolRef(PD); |
| 2551 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2552 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 2553 | OptInstanceMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2554 | OptMethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2555 | } else { |
| 2556 | InstanceMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2557 | MethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2558 | } |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2559 | } |
| 2560 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2561 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2562 | i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2563 | ObjCMethodDecl *MD = *i; |
| 2564 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 2565 | if (!C) |
| 2566 | return GetOrEmitProtocolRef(PD); |
| 2567 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2568 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 2569 | OptClassMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2570 | OptMethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2571 | } else { |
| 2572 | ClassMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2573 | MethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2574 | } |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2575 | } |
| 2576 | |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2577 | MethodTypesExt.insert(MethodTypesExt.end(), |
| 2578 | OptMethodTypesExt.begin(), OptMethodTypesExt.end()); |
| 2579 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2580 | llvm::Constant *Values[] = { |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2581 | EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods, |
| 2582 | MethodTypesExt), |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2583 | GetClassName(PD->getIdentifier()), |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2584 | EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 2585 | PD->protocol_begin(), |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2586 | PD->protocol_end()), |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2587 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2588 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2589 | InstanceMethods), |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2590 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2591 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2592 | ClassMethods) |
| 2593 | }; |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2594 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2595 | Values); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2596 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2597 | if (Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2598 | // Already created, fix the linkage and update the initializer. |
| 2599 | Entry->setLinkage(llvm::GlobalValue::InternalLinkage); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2600 | Entry->setInitializer(Init); |
| 2601 | } else { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2602 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2603 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2604 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2605 | Init, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2606 | "\01L_OBJC_PROTOCOL_" + PD->getName()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2607 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2608 | // FIXME: Is this necessary? Why only for protocol? |
| 2609 | Entry->setAlignment(4); |
John McCall | 50651b9 | 2012-03-30 21:29:05 +0000 | [diff] [blame] | 2610 | |
| 2611 | Protocols[PD->getIdentifier()] = Entry; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2612 | } |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2613 | CGM.AddUsedGlobal(Entry); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2614 | |
| 2615 | return Entry; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2616 | } |
| 2617 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2618 | llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2619 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 2620 | |
| 2621 | if (!Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2622 | // We use the initializer as a marker of whether this is a forward |
| 2623 | // reference or not. At module finalization we add the empty |
| 2624 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2625 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2626 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2627 | llvm::GlobalValue::ExternalLinkage, |
| 2628 | 0, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2629 | "\01L_OBJC_PROTOCOL_" + PD->getName()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2630 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2631 | // FIXME: Is this necessary? Why only for protocol? |
| 2632 | Entry->setAlignment(4); |
| 2633 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2634 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2635 | return Entry; |
| 2636 | } |
| 2637 | |
| 2638 | /* |
| 2639 | struct _objc_protocol_extension { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2640 | uint32_t size; |
| 2641 | struct objc_method_description_list *optional_instance_methods; |
| 2642 | struct objc_method_description_list *optional_class_methods; |
| 2643 | struct objc_property_list *instance_properties; |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2644 | const char ** extendedMethodTypes; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2645 | }; |
| 2646 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2647 | llvm::Constant * |
| 2648 | CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD, |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2649 | ArrayRef<llvm::Constant*> OptInstanceMethods, |
| 2650 | ArrayRef<llvm::Constant*> OptClassMethods, |
| 2651 | ArrayRef<llvm::Constant*> MethodTypesExt) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2652 | uint64_t Size = |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2653 | CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy); |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2654 | llvm::Constant *Values[] = { |
| 2655 | llvm::ConstantInt::get(ObjCTypes.IntTy, Size), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2656 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2657 | + PD->getName(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2658 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2659 | OptInstanceMethods), |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2660 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2661 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2662 | OptClassMethods), |
| 2663 | EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2664 | ObjCTypes), |
| 2665 | EmitProtocolMethodTypes("\01L_OBJC_PROTOCOL_METHOD_TYPES_" + PD->getName(), |
| 2666 | MethodTypesExt, ObjCTypes) |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2667 | }; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2668 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2669 | // Return null if no extension bits are used. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2670 | if (Values[1]->isNullValue() && Values[2]->isNullValue() && |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2671 | Values[3]->isNullValue() && Values[4]->isNullValue()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2672 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2673 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2674 | llvm::Constant *Init = |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2675 | llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2676 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2677 | // No special section, but goes in llvm.used |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2678 | return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2679 | Init, |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2680 | 0, 0, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2681 | } |
| 2682 | |
| 2683 | /* |
| 2684 | struct objc_protocol_list { |
Bill Wendling | 3964e62 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2685 | struct objc_protocol_list *next; |
| 2686 | long count; |
| 2687 | Protocol *list[]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2688 | }; |
| 2689 | */ |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 2690 | llvm::Constant * |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2691 | CGObjCMac::EmitProtocolList(Twine Name, |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 2692 | ObjCProtocolDecl::protocol_iterator begin, |
| 2693 | ObjCProtocolDecl::protocol_iterator end) { |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame^] | 2694 | SmallVector<llvm::Constant *, 16> ProtocolRefs; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2695 | |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 2696 | for (; begin != end; ++begin) |
| 2697 | ProtocolRefs.push_back(GetProtocolRef(*begin)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2698 | |
| 2699 | // Just return null for empty protocol lists |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2700 | if (ProtocolRefs.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2701 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2702 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2703 | // This list is null terminated. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2704 | ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2705 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2706 | llvm::Constant *Values[3]; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2707 | // This field is only used by the runtime. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2708 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2709 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2710 | ProtocolRefs.size() - 1); |
| 2711 | Values[2] = |
| 2712 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy, |
| 2713 | ProtocolRefs.size()), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2714 | ProtocolRefs); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2715 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2716 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2717 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2718 | CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2719 | 4, false); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2720 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2721 | } |
| 2722 | |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2723 | void CGObjCCommonMac:: |
| 2724 | PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*,16> &PropertySet, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame^] | 2725 | SmallVectorImpl<llvm::Constant *> &Properties, |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2726 | const Decl *Container, |
| 2727 | const ObjCProtocolDecl *PROTO, |
| 2728 | const ObjCCommonTypesHelper &ObjCTypes) { |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2729 | for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(), |
| 2730 | E = PROTO->protocol_end(); P != E; ++P) |
| 2731 | PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes); |
| 2732 | for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(), |
| 2733 | E = PROTO->prop_end(); I != E; ++I) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2734 | const ObjCPropertyDecl *PD = *I; |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2735 | if (!PropertySet.insert(PD->getIdentifier())) |
| 2736 | continue; |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2737 | llvm::Constant *Prop[] = { |
| 2738 | GetPropertyName(PD->getIdentifier()), |
| 2739 | GetPropertyTypeString(PD, Container) |
| 2740 | }; |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2741 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop)); |
| 2742 | } |
| 2743 | } |
| 2744 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2745 | /* |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2746 | struct _objc_property { |
Bill Wendling | 3964e62 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2747 | const char * const name; |
| 2748 | const char * const attributes; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2749 | }; |
| 2750 | |
| 2751 | struct _objc_property_list { |
Bill Wendling | 3964e62 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2752 | uint32_t entsize; // sizeof (struct _objc_property) |
| 2753 | uint32_t prop_count; |
| 2754 | struct _objc_property[prop_count]; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2755 | }; |
| 2756 | */ |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2757 | llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2758 | const Decl *Container, |
| 2759 | const ObjCContainerDecl *OCD, |
| 2760 | const ObjCCommonTypesHelper &ObjCTypes) { |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame^] | 2761 | SmallVector<llvm::Constant *, 16> Properties; |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2762 | llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2763 | for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(), |
| 2764 | E = OCD->prop_end(); I != E; ++I) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2765 | const ObjCPropertyDecl *PD = *I; |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2766 | PropertySet.insert(PD->getIdentifier()); |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2767 | llvm::Constant *Prop[] = { |
| 2768 | GetPropertyName(PD->getIdentifier()), |
| 2769 | GetPropertyTypeString(PD, Container) |
| 2770 | }; |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2771 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2772 | Prop)); |
| 2773 | } |
Fariborz Jahanian | 6afbdf5 | 2010-06-22 16:33:55 +0000 | [diff] [blame] | 2774 | if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 2775 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 2776 | P = OID->all_referenced_protocol_begin(), |
| 2777 | E = OID->all_referenced_protocol_end(); P != E; ++P) |
Fariborz Jahanian | 6afbdf5 | 2010-06-22 16:33:55 +0000 | [diff] [blame] | 2778 | PushProtocolProperties(PropertySet, Properties, Container, (*P), |
| 2779 | ObjCTypes); |
| 2780 | } |
| 2781 | else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) { |
| 2782 | for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(), |
| 2783 | E = CD->protocol_end(); P != E; ++P) |
| 2784 | PushProtocolProperties(PropertySet, Properties, Container, (*P), |
| 2785 | ObjCTypes); |
| 2786 | } |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2787 | |
| 2788 | // Return null for empty list. |
| 2789 | if (Properties.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2790 | return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2791 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2792 | unsigned PropertySize = |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2793 | CGM.getDataLayout().getTypeAllocSize(ObjCTypes.PropertyTy); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2794 | llvm::Constant *Values[3]; |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2795 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize); |
| 2796 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2797 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy, |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2798 | Properties.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2799 | Values[2] = llvm::ConstantArray::get(AT, Properties); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2800 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2801 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2802 | llvm::GlobalVariable *GV = |
| 2803 | CreateMetadataVar(Name, Init, |
| 2804 | (ObjCABI == 2) ? "__DATA, __objc_const" : |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2805 | "__OBJC,__property,regular,no_dead_strip", |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2806 | (ObjCABI == 2) ? 8 : 4, |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2807 | true); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2808 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2809 | } |
| 2810 | |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2811 | llvm::Constant * |
| 2812 | CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name, |
| 2813 | ArrayRef<llvm::Constant*> MethodTypes, |
| 2814 | const ObjCCommonTypesHelper &ObjCTypes) { |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2815 | // Return null for empty list. |
| 2816 | if (MethodTypes.empty()) |
| 2817 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy); |
| 2818 | |
| 2819 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 2820 | MethodTypes.size()); |
| 2821 | llvm::Constant *Init = llvm::ConstantArray::get(AT, MethodTypes); |
| 2822 | |
| 2823 | llvm::GlobalVariable *GV = |
| 2824 | CreateMetadataVar(Name, Init, |
| 2825 | (ObjCABI == 2) ? "__DATA, __objc_const" : 0, |
| 2826 | (ObjCABI == 2) ? 8 : 4, |
| 2827 | true); |
| 2828 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.Int8PtrPtrTy); |
| 2829 | } |
| 2830 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2831 | /* |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2832 | struct objc_method_description_list { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2833 | int count; |
| 2834 | struct objc_method_description list[]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2835 | }; |
| 2836 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2837 | llvm::Constant * |
| 2838 | CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2839 | llvm::Constant *Desc[] = { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2840 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2841 | ObjCTypes.SelectorPtrTy), |
| 2842 | GetMethodVarType(MD) |
| 2843 | }; |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 2844 | if (!Desc[1]) |
| 2845 | return 0; |
| 2846 | |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2847 | return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2848 | Desc); |
| 2849 | } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2850 | |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2851 | llvm::Constant * |
| 2852 | CGObjCMac::EmitMethodDescList(Twine Name, const char *Section, |
| 2853 | ArrayRef<llvm::Constant*> Methods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2854 | // Return null for empty list. |
| 2855 | if (Methods.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2856 | return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2857 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2858 | llvm::Constant *Values[2]; |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2859 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2860 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2861 | Methods.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2862 | Values[1] = llvm::ConstantArray::get(AT, Methods); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2863 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2864 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2865 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2866 | return llvm::ConstantExpr::getBitCast(GV, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2867 | ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2868 | } |
| 2869 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2870 | /* |
| 2871 | struct _objc_category { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2872 | char *category_name; |
| 2873 | char *class_name; |
| 2874 | struct _objc_method_list *instance_methods; |
| 2875 | struct _objc_method_list *class_methods; |
| 2876 | struct _objc_protocol_list *protocols; |
| 2877 | uint32_t size; // <rdar://4585769> |
| 2878 | struct _objc_property_list *instance_properties; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2879 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2880 | */ |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2881 | void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2882 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.CategoryTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2883 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2884 | // FIXME: This is poor design, the OCD should have a pointer to the category |
| 2885 | // decl. Additionally, note that Category can be null for the @implementation |
| 2886 | // w/o an @interface case. Sema should just create one for us as it does for |
| 2887 | // @implementation so everyone else can live life under a clear blue sky. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2888 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2889 | const ObjCCategoryDecl *Category = |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2890 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2891 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2892 | SmallString<256> ExtName; |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2893 | llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_' |
| 2894 | << OCD->getName(); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2895 | |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame^] | 2896 | SmallVector<llvm::Constant *, 16> InstanceMethods, ClassMethods; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2897 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2898 | i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2899 | // Instance methods should always be defined. |
| 2900 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 2901 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2902 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2903 | i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2904 | // Class methods should always be defined. |
| 2905 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 2906 | } |
| 2907 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2908 | llvm::Constant *Values[7]; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2909 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 2910 | Values[1] = GetClassName(Interface->getIdentifier()); |
Fariborz Jahanian | 679cd7f | 2009-04-29 20:40:05 +0000 | [diff] [blame] | 2911 | LazySymbols.insert(Interface->getIdentifier()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2912 | Values[2] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2913 | EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(), |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2914 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2915 | InstanceMethods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2916 | Values[3] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2917 | EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2918 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2919 | ClassMethods); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2920 | if (Category) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2921 | Values[4] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2922 | EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2923 | Category->protocol_begin(), |
| 2924 | Category->protocol_end()); |
| 2925 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2926 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2927 | } |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2928 | Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2929 | |
| 2930 | // If there is no category @interface then there can be no properties. |
| 2931 | if (Category) { |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2932 | Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 2933 | OCD, Category, ObjCTypes); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2934 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2935 | Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2936 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2937 | |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2938 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2939 | Values); |
| 2940 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2941 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2942 | CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init, |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2943 | "__OBJC,__category,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2944 | 4, true); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2945 | DefinedCategories.push_back(GV); |
Fariborz Jahanian | b9c5b3d | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 2946 | DefinedCategoryNames.insert(ExtName.str()); |
Fariborz Jahanian | 64089ce | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 2947 | // method definition entries must be clear for next implementation. |
| 2948 | MethodDefinitions.clear(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2949 | } |
| 2950 | |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 2951 | enum FragileClassFlags { |
| 2952 | FragileABI_Class_Factory = 0x00001, |
| 2953 | FragileABI_Class_Meta = 0x00002, |
| 2954 | FragileABI_Class_HasCXXStructors = 0x02000, |
| 2955 | FragileABI_Class_Hidden = 0x20000 |
| 2956 | }; |
| 2957 | |
| 2958 | enum NonFragileClassFlags { |
| 2959 | /// Is a meta-class. |
| 2960 | NonFragileABI_Class_Meta = 0x00001, |
| 2961 | |
| 2962 | /// Is a root class. |
| 2963 | NonFragileABI_Class_Root = 0x00002, |
| 2964 | |
| 2965 | /// Has a C++ constructor and destructor. |
| 2966 | NonFragileABI_Class_HasCXXStructors = 0x00004, |
| 2967 | |
| 2968 | /// Has hidden visibility. |
| 2969 | NonFragileABI_Class_Hidden = 0x00010, |
| 2970 | |
| 2971 | /// Has the exception attribute. |
| 2972 | NonFragileABI_Class_Exception = 0x00020, |
| 2973 | |
| 2974 | /// (Obsolete) ARC-specific: this class has a .release_ivars method |
| 2975 | NonFragileABI_Class_HasIvarReleaser = 0x00040, |
| 2976 | |
| 2977 | /// Class implementation was compiled under ARC. |
John McCall | b03527a | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 2978 | NonFragileABI_Class_CompiledByARC = 0x00080, |
| 2979 | |
| 2980 | /// Class has non-trivial destructors, but zero-initialization is okay. |
| 2981 | NonFragileABI_Class_HasCXXDestructorOnly = 0x00100 |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2982 | }; |
| 2983 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2984 | /* |
| 2985 | struct _objc_class { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2986 | Class isa; |
| 2987 | Class super_class; |
| 2988 | const char *name; |
| 2989 | long version; |
| 2990 | long info; |
| 2991 | long instance_size; |
| 2992 | struct _objc_ivar_list *ivars; |
| 2993 | struct _objc_method_list *methods; |
| 2994 | struct _objc_cache *cache; |
| 2995 | struct _objc_protocol_list *protocols; |
| 2996 | // Objective-C 1.0 extensions (<rdr://4585769>) |
| 2997 | const char *ivar_layout; |
| 2998 | struct _objc_class_ext *ext; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2999 | }; |
| 3000 | |
| 3001 | See EmitClassExtension(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3002 | */ |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3003 | void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3004 | DefinedSymbols.insert(ID->getIdentifier()); |
| 3005 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3006 | std::string ClassName = ID->getNameAsString(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3007 | // FIXME: Gross |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3008 | ObjCInterfaceDecl *Interface = |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3009 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3010 | llvm::Constant *Protocols = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 3011 | EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(), |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 3012 | Interface->all_referenced_protocol_begin(), |
| 3013 | Interface->all_referenced_protocol_end()); |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 3014 | unsigned Flags = FragileABI_Class_Factory; |
John McCall | b03527a | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 3015 | if (ID->hasNonZeroConstructors() || ID->hasDestructors()) |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 3016 | Flags |= FragileABI_Class_HasCXXStructors; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3017 | unsigned Size = |
Ken Dyck | 5f022d8 | 2011-02-09 01:59:34 +0000 | [diff] [blame] | 3018 | CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3019 | |
| 3020 | // FIXME: Set CXX-structors flag. |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 3021 | if (ID->getClassInterface()->getVisibility() == HiddenVisibility) |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 3022 | Flags |= FragileABI_Class_Hidden; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3023 | |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame^] | 3024 | SmallVector<llvm::Constant *, 16> InstanceMethods, ClassMethods; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3025 | for (ObjCImplementationDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3026 | i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3027 | // Instance methods should always be defined. |
| 3028 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 3029 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3030 | for (ObjCImplementationDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3031 | i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3032 | // Class methods should always be defined. |
| 3033 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 3034 | } |
| 3035 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3036 | for (ObjCImplementationDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3037 | i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3038 | ObjCPropertyImplDecl *PID = *i; |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3039 | |
| 3040 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 3041 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 3042 | |
| 3043 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 3044 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 3045 | InstanceMethods.push_back(C); |
| 3046 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 3047 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 3048 | InstanceMethods.push_back(C); |
| 3049 | } |
| 3050 | } |
| 3051 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3052 | llvm::Constant *Values[12]; |
Daniel Dunbar | 5384b09 | 2009-05-03 08:56:52 +0000 | [diff] [blame] | 3053 | Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3054 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3055 | // Record a reference to the super class. |
| 3056 | LazySymbols.insert(Super->getIdentifier()); |
| 3057 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3058 | Values[ 1] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3059 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3060 | ObjCTypes.ClassPtrTy); |
| 3061 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3062 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3063 | } |
| 3064 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 3065 | // Version is always 0. |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3066 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 3067 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 3068 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 3069 | Values[ 6] = EmitIvarList(ID, false); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3070 | Values[ 7] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 3071 | EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(), |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3072 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3073 | InstanceMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3074 | // cache is always NULL. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3075 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3076 | Values[ 9] = Protocols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3077 | Values[10] = BuildIvarLayout(ID, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3078 | Values[11] = EmitClassExtension(ID); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 3079 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3080 | Values); |
Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 3081 | std::string Name("\01L_OBJC_CLASS_"); |
| 3082 | Name += ClassName; |
| 3083 | const char *Section = "__OBJC,__class,regular,no_dead_strip"; |
| 3084 | // Check for a forward reference. |
| 3085 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 3086 | if (GV) { |
| 3087 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 3088 | "Forward metaclass reference has incorrect type."); |
| 3089 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 3090 | GV->setInitializer(Init); |
| 3091 | GV->setSection(Section); |
| 3092 | GV->setAlignment(4); |
| 3093 | CGM.AddUsedGlobal(GV); |
| 3094 | } |
| 3095 | else |
| 3096 | GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3097 | DefinedClasses.push_back(GV); |
Fariborz Jahanian | 64089ce | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 3098 | // method definition entries must be clear for next implementation. |
| 3099 | MethodDefinitions.clear(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3100 | } |
| 3101 | |
| 3102 | llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID, |
| 3103 | llvm::Constant *Protocols, |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 3104 | ArrayRef<llvm::Constant*> Methods) { |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 3105 | unsigned Flags = FragileABI_Class_Meta; |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3106 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3107 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 3108 | if (ID->getClassInterface()->getVisibility() == HiddenVisibility) |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 3109 | Flags |= FragileABI_Class_Hidden; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3110 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3111 | llvm::Constant *Values[12]; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3112 | // The isa for the metaclass is the root of the hierarchy. |
| 3113 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 3114 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 3115 | Root = Super; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3116 | Values[ 0] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3117 | llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3118 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3119 | // The super class for the metaclass is emitted as the name of the |
| 3120 | // super class. The runtime fixes this up to point to the |
| 3121 | // *metaclass* for the super class. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3122 | if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3123 | Values[ 1] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3124 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3125 | ObjCTypes.ClassPtrTy); |
| 3126 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3127 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3128 | } |
| 3129 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 3130 | // Version is always 0. |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3131 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 3132 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 3133 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 3134 | Values[ 6] = EmitIvarList(ID, true); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3135 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3136 | EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 3137 | "__OBJC,__cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3138 | Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3139 | // cache is always NULL. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3140 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3141 | Values[ 9] = Protocols; |
| 3142 | // ivar_layout for metaclass is always NULL. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3143 | Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3144 | // The class extension is always unused for metaclasses. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3145 | Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 3146 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3147 | Values); |
| 3148 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 3149 | std::string Name("\01L_OBJC_METACLASS_"); |
Benjamin Kramer | 94be8ea | 2012-07-31 11:45:39 +0000 | [diff] [blame] | 3150 | Name += ID->getName(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 3151 | |
| 3152 | // Check for a forward reference. |
| 3153 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 3154 | if (GV) { |
| 3155 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 3156 | "Forward metaclass reference has incorrect type."); |
| 3157 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 3158 | GV->setInitializer(Init); |
| 3159 | } else { |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 3160 | GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 3161 | llvm::GlobalValue::InternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 3162 | Init, Name); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 3163 | } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3164 | GV->setSection("__OBJC,__meta_class,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 3165 | GV->setAlignment(4); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 3166 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3167 | |
| 3168 | return GV; |
| 3169 | } |
| 3170 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3171 | llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 3172 | std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 3173 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 3174 | // FIXME: Should we look these up somewhere other than the module. Its a bit |
| 3175 | // silly since we only generate these while processing an implementation, so |
| 3176 | // exactly one pointer would work if know when we entered/exitted an |
| 3177 | // implementation block. |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 3178 | |
| 3179 | // Check for an existing forward reference. |
Fariborz Jahanian | b0d2794 | 2009-01-07 20:11:22 +0000 | [diff] [blame] | 3180 | // Previously, metaclass with internal linkage may have been defined. |
| 3181 | // pass 'true' as 2nd argument so it is returned. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3182 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, |
| 3183 | true)) { |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 3184 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 3185 | "Forward metaclass reference has incorrect type."); |
| 3186 | return GV; |
| 3187 | } else { |
| 3188 | // Generate as an external reference to keep a consistent |
| 3189 | // module. This will be patched up when we emit the metaclass. |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 3190 | return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 3191 | llvm::GlobalValue::ExternalLinkage, |
| 3192 | 0, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 3193 | Name); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 3194 | } |
| 3195 | } |
| 3196 | |
Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 3197 | llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) { |
| 3198 | std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString(); |
| 3199 | |
| 3200 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, |
| 3201 | true)) { |
| 3202 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 3203 | "Forward class metadata reference has incorrect type."); |
| 3204 | return GV; |
| 3205 | } else { |
| 3206 | return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
| 3207 | llvm::GlobalValue::ExternalLinkage, |
| 3208 | 0, |
| 3209 | Name); |
| 3210 | } |
| 3211 | } |
| 3212 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3213 | /* |
| 3214 | struct objc_class_ext { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3215 | uint32_t size; |
| 3216 | const char *weak_ivar_layout; |
| 3217 | struct _objc_property_list *properties; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3218 | }; |
| 3219 | */ |
| 3220 | llvm::Constant * |
| 3221 | CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3222 | uint64_t Size = |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 3223 | CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassExtensionTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3224 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3225 | llvm::Constant *Values[3]; |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3226 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 3227 | Values[1] = BuildIvarLayout(ID, false); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 3228 | Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 3229 | ID, ID->getClassInterface(), ObjCTypes); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3230 | |
| 3231 | // Return null if no extension bits are used. |
| 3232 | if (Values[1]->isNullValue() && Values[2]->isNullValue()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3233 | return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3234 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3235 | llvm::Constant *Init = |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 3236 | llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 3237 | return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3238 | Init, "__OBJC,__class_ext,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 3239 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3240 | } |
| 3241 | |
| 3242 | /* |
| 3243 | struct objc_ivar { |
Bill Wendling | 795b100 | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 3244 | char *ivar_name; |
| 3245 | char *ivar_type; |
| 3246 | int ivar_offset; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3247 | }; |
| 3248 | |
| 3249 | struct objc_ivar_list { |
Bill Wendling | 795b100 | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 3250 | int ivar_count; |
| 3251 | struct objc_ivar list[count]; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3252 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3253 | */ |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3254 | llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 3255 | bool ForClass) { |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 3256 | std::vector<llvm::Constant*> Ivars; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3257 | |
| 3258 | // When emitting the root class GCC emits ivar entries for the |
| 3259 | // actual class structure. It is not clear if we need to follow this |
| 3260 | // behavior; for now lets try and get away with not doing it. If so, |
| 3261 | // the cleanest solution would be to make up an ObjCInterfaceDecl |
| 3262 | // for the class. |
| 3263 | if (ForClass) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3264 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3265 | |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3266 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3267 | |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3268 | for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin(); |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 3269 | IVD; IVD = IVD->getNextIvar()) { |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 3270 | // Ignore unnamed bit-fields. |
| 3271 | if (!IVD->getDeclName()) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3272 | continue; |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 3273 | llvm::Constant *Ivar[] = { |
| 3274 | GetMethodVarName(IVD->getIdentifier()), |
| 3275 | GetMethodVarType(IVD), |
| 3276 | llvm::ConstantInt::get(ObjCTypes.IntTy, |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 3277 | ComputeIvarBaseOffset(CGM, OID, IVD)) |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 3278 | }; |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 3279 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3280 | } |
| 3281 | |
| 3282 | // Return null for empty list. |
| 3283 | if (Ivars.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3284 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3285 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3286 | llvm::Constant *Values[2]; |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3287 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3288 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3289 | Ivars.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 3290 | Values[1] = llvm::ConstantArray::get(AT, Ivars); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3291 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3292 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3293 | llvm::GlobalVariable *GV; |
| 3294 | if (ForClass) |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 3295 | GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3296 | Init, "__OBJC,__class_vars,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 3297 | 4, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3298 | else |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 3299 | GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3300 | Init, "__OBJC,__instance_vars,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 3301 | 4, true); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3302 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3303 | } |
| 3304 | |
| 3305 | /* |
| 3306 | struct objc_method { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3307 | SEL method_name; |
| 3308 | char *method_types; |
| 3309 | void *method; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3310 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3311 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3312 | struct objc_method_list { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3313 | struct objc_method_list *obsolete; |
| 3314 | int count; |
| 3315 | struct objc_method methods_list[count]; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3316 | }; |
| 3317 | */ |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3318 | |
| 3319 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 3320 | /// given method if it has been defined. The result is null if the |
| 3321 | /// method has not been defined. The return value has type MethodPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 3322 | llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) { |
Argyrios Kyrtzidis | 9d50c06 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 3323 | llvm::Function *Fn = GetMethodDefinition(MD); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3324 | if (!Fn) |
| 3325 | return 0; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3326 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 3327 | llvm::Constant *Method[] = { |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3328 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 3329 | ObjCTypes.SelectorPtrTy), |
| 3330 | GetMethodVarType(MD), |
| 3331 | llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy) |
| 3332 | }; |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 3333 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3334 | } |
| 3335 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3336 | llvm::Constant *CGObjCMac::EmitMethodList(Twine Name, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3337 | const char *Section, |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 3338 | ArrayRef<llvm::Constant*> Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3339 | // Return null for empty list. |
| 3340 | if (Methods.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3341 | return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3342 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3343 | llvm::Constant *Values[3]; |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3344 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3345 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3346 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3347 | Methods.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 3348 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3349 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3350 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 3351 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3352 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3353 | } |
| 3354 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 3355 | llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3356 | const ObjCContainerDecl *CD) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 3357 | SmallString<256> Name; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 3358 | GetNameForMethod(OMD, CD, Name); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3359 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 3360 | CodeGenTypes &Types = CGM.getTypes(); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3361 | llvm::FunctionType *MethodTy = |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 3362 | Types.GetFunctionType(Types.arrangeObjCMethodDeclaration(OMD)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3363 | llvm::Function *Method = |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 3364 | llvm::Function::Create(MethodTy, |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3365 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 3366 | Name.str(), |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3367 | &CGM.getModule()); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3368 | MethodDefinitions.insert(std::make_pair(OMD, Method)); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3369 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3370 | return Method; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 3371 | } |
| 3372 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 3373 | llvm::GlobalVariable * |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3374 | CGObjCCommonMac::CreateMetadataVar(Twine Name, |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 3375 | llvm::Constant *Init, |
| 3376 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 3377 | unsigned Align, |
| 3378 | bool AddToUsed) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3379 | llvm::Type *Ty = Init->getType(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3380 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 3381 | new llvm::GlobalVariable(CGM.getModule(), Ty, false, |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 3382 | llvm::GlobalValue::InternalLinkage, Init, Name); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 3383 | if (Section) |
| 3384 | GV->setSection(Section); |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 3385 | if (Align) |
| 3386 | GV->setAlignment(Align); |
| 3387 | if (AddToUsed) |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 3388 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 3389 | return GV; |
| 3390 | } |
| 3391 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3392 | llvm::Function *CGObjCMac::ModuleInitFunction() { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3393 | // Abuse this interface function as a place to finalize. |
| 3394 | FinishModule(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 3395 | return NULL; |
| 3396 | } |
| 3397 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 3398 | llvm::Constant *CGObjCMac::GetPropertyGetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 3399 | return ObjCTypes.getGetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 3400 | } |
| 3401 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 3402 | llvm::Constant *CGObjCMac::GetPropertySetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 3403 | return ObjCTypes.getSetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 3404 | } |
| 3405 | |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 3406 | llvm::Constant *CGObjCMac::GetOptimizedPropertySetFunction(bool atomic, |
| 3407 | bool copy) { |
| 3408 | return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy); |
| 3409 | } |
| 3410 | |
David Chisnall | 8fac25d | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 3411 | llvm::Constant *CGObjCMac::GetGetStructFunction() { |
| 3412 | return ObjCTypes.getCopyStructFn(); |
| 3413 | } |
| 3414 | llvm::Constant *CGObjCMac::GetSetStructFunction() { |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 3415 | return ObjCTypes.getCopyStructFn(); |
| 3416 | } |
| 3417 | |
David Chisnall | d397cfe | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 3418 | llvm::Constant *CGObjCMac::GetCppAtomicObjectGetFunction() { |
| 3419 | return ObjCTypes.getCppAtomicObjectFunction(); |
| 3420 | } |
| 3421 | llvm::Constant *CGObjCMac::GetCppAtomicObjectSetFunction() { |
Fariborz Jahanian | e317302 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 3422 | return ObjCTypes.getCppAtomicObjectFunction(); |
| 3423 | } |
| 3424 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 3425 | llvm::Constant *CGObjCMac::EnumerationMutationFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 3426 | return ObjCTypes.getEnumerationMutationFn(); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 3427 | } |
| 3428 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3429 | void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) { |
| 3430 | return EmitTryOrSynchronizedStmt(CGF, S); |
| 3431 | } |
| 3432 | |
| 3433 | void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF, |
| 3434 | const ObjCAtSynchronizedStmt &S) { |
| 3435 | return EmitTryOrSynchronizedStmt(CGF, S); |
| 3436 | } |
| 3437 | |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3438 | namespace { |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 3439 | struct PerformFragileFinally : EHScopeStack::Cleanup { |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3440 | const Stmt &S; |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3441 | llvm::Value *SyncArgSlot; |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3442 | llvm::Value *CallTryExitVar; |
| 3443 | llvm::Value *ExceptionData; |
| 3444 | ObjCTypesHelper &ObjCTypes; |
| 3445 | PerformFragileFinally(const Stmt *S, |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3446 | llvm::Value *SyncArgSlot, |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3447 | llvm::Value *CallTryExitVar, |
| 3448 | llvm::Value *ExceptionData, |
| 3449 | ObjCTypesHelper *ObjCTypes) |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3450 | : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar), |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3451 | ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {} |
| 3452 | |
John McCall | ad346f4 | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 3453 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3454 | // Check whether we need to call objc_exception_try_exit. |
| 3455 | // In optimized code, this branch will always be folded. |
| 3456 | llvm::BasicBlock *FinallyCallExit = |
| 3457 | CGF.createBasicBlock("finally.call_exit"); |
| 3458 | llvm::BasicBlock *FinallyNoCallExit = |
| 3459 | CGF.createBasicBlock("finally.no_call_exit"); |
| 3460 | CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar), |
| 3461 | FinallyCallExit, FinallyNoCallExit); |
| 3462 | |
| 3463 | CGF.EmitBlock(FinallyCallExit); |
| 3464 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData) |
| 3465 | ->setDoesNotThrow(); |
| 3466 | |
| 3467 | CGF.EmitBlock(FinallyNoCallExit); |
| 3468 | |
| 3469 | if (isa<ObjCAtTryStmt>(S)) { |
| 3470 | if (const ObjCAtFinallyStmt* FinallyStmt = |
John McCall | d96a8e7 | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 3471 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) { |
| 3472 | // Save the current cleanup destination in case there's |
| 3473 | // control flow inside the finally statement. |
| 3474 | llvm::Value *CurCleanupDest = |
| 3475 | CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot()); |
| 3476 | |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3477 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
| 3478 | |
John McCall | d96a8e7 | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 3479 | if (CGF.HaveInsertPoint()) { |
| 3480 | CGF.Builder.CreateStore(CurCleanupDest, |
| 3481 | CGF.getNormalCleanupDestSlot()); |
| 3482 | } else { |
| 3483 | // Currently, the end of the cleanup must always exist. |
| 3484 | CGF.EnsureInsertPoint(); |
| 3485 | } |
| 3486 | } |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3487 | } else { |
| 3488 | // Emit objc_sync_exit(expr); as finally's sole statement for |
| 3489 | // @synchronized. |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3490 | llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot); |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3491 | CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg) |
| 3492 | ->setDoesNotThrow(); |
| 3493 | } |
| 3494 | } |
| 3495 | }; |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3496 | |
| 3497 | class FragileHazards { |
| 3498 | CodeGenFunction &CGF; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3499 | SmallVector<llvm::Value*, 20> Locals; |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3500 | llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry; |
| 3501 | |
| 3502 | llvm::InlineAsm *ReadHazard; |
| 3503 | llvm::InlineAsm *WriteHazard; |
| 3504 | |
| 3505 | llvm::FunctionType *GetAsmFnType(); |
| 3506 | |
| 3507 | void collectLocals(); |
| 3508 | void emitReadHazard(CGBuilderTy &Builder); |
| 3509 | |
| 3510 | public: |
| 3511 | FragileHazards(CodeGenFunction &CGF); |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3512 | |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3513 | void emitWriteHazard(); |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3514 | void emitHazardsInNewBlocks(); |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3515 | }; |
| 3516 | } |
| 3517 | |
| 3518 | /// Create the fragile-ABI read and write hazards based on the current |
| 3519 | /// state of the function, which is presumed to be immediately prior |
| 3520 | /// to a @try block. These hazards are used to maintain correct |
| 3521 | /// semantics in the face of optimization and the fragile ABI's |
| 3522 | /// cavalier use of setjmp/longjmp. |
| 3523 | FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) { |
| 3524 | collectLocals(); |
| 3525 | |
| 3526 | if (Locals.empty()) return; |
| 3527 | |
| 3528 | // Collect all the blocks in the function. |
| 3529 | for (llvm::Function::iterator |
| 3530 | I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I) |
| 3531 | BlocksBeforeTry.insert(&*I); |
| 3532 | |
| 3533 | llvm::FunctionType *AsmFnTy = GetAsmFnType(); |
| 3534 | |
| 3535 | // Create a read hazard for the allocas. This inhibits dead-store |
| 3536 | // optimizations and forces the values to memory. This hazard is |
| 3537 | // inserted before any 'throwing' calls in the protected scope to |
| 3538 | // reflect the possibility that the variables might be read from the |
| 3539 | // catch block if the call throws. |
| 3540 | { |
| 3541 | std::string Constraint; |
| 3542 | for (unsigned I = 0, E = Locals.size(); I != E; ++I) { |
| 3543 | if (I) Constraint += ','; |
| 3544 | Constraint += "*m"; |
| 3545 | } |
| 3546 | |
| 3547 | ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false); |
| 3548 | } |
| 3549 | |
| 3550 | // Create a write hazard for the allocas. This inhibits folding |
| 3551 | // loads across the hazard. This hazard is inserted at the |
| 3552 | // beginning of the catch path to reflect the possibility that the |
| 3553 | // variables might have been written within the protected scope. |
| 3554 | { |
| 3555 | std::string Constraint; |
| 3556 | for (unsigned I = 0, E = Locals.size(); I != E; ++I) { |
| 3557 | if (I) Constraint += ','; |
| 3558 | Constraint += "=*m"; |
| 3559 | } |
| 3560 | |
| 3561 | WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false); |
| 3562 | } |
| 3563 | } |
| 3564 | |
| 3565 | /// Emit a write hazard at the current location. |
| 3566 | void FragileHazards::emitWriteHazard() { |
| 3567 | if (Locals.empty()) return; |
| 3568 | |
Jay Foad | 4c7d9f1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 3569 | CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow(); |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3570 | } |
| 3571 | |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3572 | void FragileHazards::emitReadHazard(CGBuilderTy &Builder) { |
| 3573 | assert(!Locals.empty()); |
Jay Foad | 4c7d9f1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 3574 | Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow(); |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3575 | } |
| 3576 | |
| 3577 | /// Emit read hazards in all the protected blocks, i.e. all the blocks |
| 3578 | /// which have been inserted since the beginning of the try. |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3579 | void FragileHazards::emitHazardsInNewBlocks() { |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3580 | if (Locals.empty()) return; |
| 3581 | |
| 3582 | CGBuilderTy Builder(CGF.getLLVMContext()); |
| 3583 | |
| 3584 | // Iterate through all blocks, skipping those prior to the try. |
| 3585 | for (llvm::Function::iterator |
| 3586 | FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) { |
| 3587 | llvm::BasicBlock &BB = *FI; |
| 3588 | if (BlocksBeforeTry.count(&BB)) continue; |
| 3589 | |
| 3590 | // Walk through all the calls in the block. |
| 3591 | for (llvm::BasicBlock::iterator |
| 3592 | BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) { |
| 3593 | llvm::Instruction &I = *BI; |
| 3594 | |
| 3595 | // Ignore instructions that aren't non-intrinsic calls. |
| 3596 | // These are the only calls that can possibly call longjmp. |
| 3597 | if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue; |
| 3598 | if (isa<llvm::IntrinsicInst>(I)) |
| 3599 | continue; |
| 3600 | |
| 3601 | // Ignore call sites marked nounwind. This may be questionable, |
| 3602 | // since 'nounwind' doesn't necessarily mean 'does not call longjmp'. |
| 3603 | llvm::CallSite CS(&I); |
| 3604 | if (CS.doesNotThrow()) continue; |
| 3605 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3606 | // Insert a read hazard before the call. This will ensure that |
| 3607 | // any writes to the locals are performed before making the |
| 3608 | // call. If the call throws, then this is sufficient to |
| 3609 | // guarantee correctness as long as it doesn't also write to any |
| 3610 | // locals. |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3611 | Builder.SetInsertPoint(&BB, BI); |
| 3612 | emitReadHazard(Builder); |
| 3613 | } |
| 3614 | } |
| 3615 | } |
| 3616 | |
| 3617 | static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) { |
| 3618 | if (V) S.insert(V); |
| 3619 | } |
| 3620 | |
| 3621 | void FragileHazards::collectLocals() { |
| 3622 | // Compute a set of allocas to ignore. |
| 3623 | llvm::DenseSet<llvm::Value*> AllocasToIgnore; |
| 3624 | addIfPresent(AllocasToIgnore, CGF.ReturnValue); |
| 3625 | addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest); |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3626 | |
| 3627 | // Collect all the allocas currently in the function. This is |
| 3628 | // probably way too aggressive. |
| 3629 | llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock(); |
| 3630 | for (llvm::BasicBlock::iterator |
| 3631 | I = Entry.begin(), E = Entry.end(); I != E; ++I) |
| 3632 | if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I)) |
| 3633 | Locals.push_back(&*I); |
| 3634 | } |
| 3635 | |
| 3636 | llvm::FunctionType *FragileHazards::GetAsmFnType() { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3637 | SmallVector<llvm::Type *, 16> tys(Locals.size()); |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 3638 | for (unsigned i = 0, e = Locals.size(); i != e; ++i) |
| 3639 | tys[i] = Locals[i]->getType(); |
| 3640 | return llvm::FunctionType::get(CGF.VoidTy, tys, false); |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3641 | } |
| 3642 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3643 | /* |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3644 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3645 | Objective-C setjmp-longjmp (sjlj) Exception Handling |
| 3646 | -- |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3647 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3648 | A catch buffer is a setjmp buffer plus: |
| 3649 | - a pointer to the exception that was caught |
| 3650 | - a pointer to the previous exception data buffer |
| 3651 | - two pointers of reserved storage |
| 3652 | Therefore catch buffers form a stack, with a pointer to the top |
| 3653 | of the stack kept in thread-local storage. |
| 3654 | |
| 3655 | objc_exception_try_enter pushes a catch buffer onto the EH stack. |
| 3656 | objc_exception_try_exit pops the given catch buffer, which is |
| 3657 | required to be the top of the EH stack. |
| 3658 | objc_exception_throw pops the top of the EH stack, writes the |
| 3659 | thrown exception into the appropriate field, and longjmps |
| 3660 | to the setjmp buffer. It crashes the process (with a printf |
| 3661 | and an abort()) if there are no catch buffers on the stack. |
| 3662 | objc_exception_extract just reads the exception pointer out of the |
| 3663 | catch buffer. |
| 3664 | |
| 3665 | There's no reason an implementation couldn't use a light-weight |
| 3666 | setjmp here --- something like __builtin_setjmp, but API-compatible |
| 3667 | with the heavyweight setjmp. This will be more important if we ever |
| 3668 | want to implement correct ObjC/C++ exception interactions for the |
| 3669 | fragile ABI. |
| 3670 | |
| 3671 | Note that for this use of setjmp/longjmp to be correct, we may need |
| 3672 | to mark some local variables volatile: if a non-volatile local |
| 3673 | variable is modified between the setjmp and the longjmp, it has |
| 3674 | indeterminate value. For the purposes of LLVM IR, it may be |
| 3675 | sufficient to make loads and stores within the @try (to variables |
| 3676 | declared outside the @try) volatile. This is necessary for |
| 3677 | optimized correctness, but is not currently being done; this is |
| 3678 | being tracked as rdar://problem/8160285 |
| 3679 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3680 | The basic framework for a @try-catch-finally is as follows: |
| 3681 | { |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3682 | objc_exception_data d; |
| 3683 | id _rethrow = null; |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 3684 | bool _call_try_exit = true; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3685 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3686 | objc_exception_try_enter(&d); |
| 3687 | if (!setjmp(d.jmp_buf)) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3688 | ... try body ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3689 | } else { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3690 | // exception path |
| 3691 | id _caught = objc_exception_extract(&d); |
| 3692 | |
| 3693 | // enter new try scope for handlers |
| 3694 | if (!setjmp(d.jmp_buf)) { |
| 3695 | ... match exception and execute catch blocks ... |
| 3696 | |
| 3697 | // fell off end, rethrow. |
| 3698 | _rethrow = _caught; |
| 3699 | ... jump-through-finally to finally_rethrow ... |
| 3700 | } else { |
| 3701 | // exception in catch block |
| 3702 | _rethrow = objc_exception_extract(&d); |
| 3703 | _call_try_exit = false; |
| 3704 | ... jump-through-finally to finally_rethrow ... |
| 3705 | } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3706 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3707 | ... jump-through-finally to finally_end ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3708 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3709 | finally: |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 3710 | if (_call_try_exit) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3711 | objc_exception_try_exit(&d); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 3712 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3713 | ... finally block .... |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3714 | ... dispatch to finally destination ... |
| 3715 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3716 | finally_rethrow: |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3717 | objc_exception_throw(_rethrow); |
| 3718 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3719 | finally_end: |
| 3720 | } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3721 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3722 | This framework differs slightly from the one gcc uses, in that gcc |
| 3723 | uses _rethrow to determine if objc_exception_try_exit should be called |
| 3724 | and if the object should be rethrown. This breaks in the face of |
| 3725 | throwing nil and introduces unnecessary branches. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3726 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3727 | We specialize this framework for a few particular circumstances: |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3728 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3729 | - If there are no catch blocks, then we avoid emitting the second |
| 3730 | exception handling context. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3731 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3732 | - If there is a catch-all catch block (i.e. @catch(...) or @catch(id |
| 3733 | e)) we avoid emitting the code to rethrow an uncaught exception. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3734 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3735 | - FIXME: If there is no @finally block we can do a few more |
| 3736 | simplifications. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3737 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3738 | Rethrows and Jumps-Through-Finally |
| 3739 | -- |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3740 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3741 | '@throw;' is supported by pushing the currently-caught exception |
| 3742 | onto ObjCEHStack while the @catch blocks are emitted. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3743 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3744 | Branches through the @finally block are handled with an ordinary |
| 3745 | normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC |
| 3746 | exceptions are not compatible with C++ exceptions, and this is |
| 3747 | hardly the only place where this will go wrong. |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3748 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3749 | @synchronized(expr) { stmt; } is emitted as if it were: |
| 3750 | id synch_value = expr; |
| 3751 | objc_sync_enter(synch_value); |
| 3752 | @try { stmt; } @finally { objc_sync_exit(synch_value); } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3753 | */ |
| 3754 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 3755 | void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 3756 | const Stmt &S) { |
| 3757 | bool isTry = isa<ObjCAtTryStmt>(S); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3758 | |
| 3759 | // A destination for the fall-through edges of the catch handlers to |
| 3760 | // jump to. |
| 3761 | CodeGenFunction::JumpDest FinallyEnd = |
| 3762 | CGF.getJumpDestInCurrentScope("finally.end"); |
| 3763 | |
| 3764 | // A destination for the rethrow edge of the catch handlers to jump |
| 3765 | // to. |
| 3766 | CodeGenFunction::JumpDest FinallyRethrow = |
| 3767 | CGF.getJumpDestInCurrentScope("finally.rethrow"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3768 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3769 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 3770 | // evaluation of the expression must occur before we enter the |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3771 | // @synchronized. We can't avoid a temp here because we need the |
| 3772 | // value to be preserved. If the backend ever does liveness |
| 3773 | // correctly after setjmp, this will be unnecessary. |
| 3774 | llvm::Value *SyncArgSlot = 0; |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3775 | if (!isTry) { |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3776 | llvm::Value *SyncArg = |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3777 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 3778 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3779 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg) |
| 3780 | ->setDoesNotThrow(); |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3781 | |
| 3782 | SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg"); |
| 3783 | CGF.Builder.CreateStore(SyncArg, SyncArgSlot); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3784 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3785 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3786 | // Allocate memory for the setjmp buffer. This needs to be kept |
| 3787 | // live throughout the try and catch blocks. |
| 3788 | llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy, |
| 3789 | "exceptiondata.ptr"); |
| 3790 | |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3791 | // Create the fragile hazards. Note that this will not capture any |
| 3792 | // of the allocas required for exception processing, but will |
| 3793 | // capture the current basic block (which extends all the way to the |
| 3794 | // setjmp call) as "before the @try". |
| 3795 | FragileHazards Hazards(CGF); |
| 3796 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3797 | // Create a flag indicating whether the cleanup needs to call |
| 3798 | // objc_exception_try_exit. This is true except when |
| 3799 | // - no catches match and we're branching through the cleanup |
| 3800 | // just to rethrow the exception, or |
| 3801 | // - a catch matched and we're falling out of the catch handler. |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3802 | // The setjmp-safety rule here is that we should always store to this |
| 3803 | // variable in a place that dominates the branch through the cleanup |
| 3804 | // without passing through any setjmps. |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3805 | llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(), |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 3806 | "_call_try_exit"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3807 | |
John McCall | 9e2213d | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3808 | // A slot containing the exception to rethrow. Only needed when we |
| 3809 | // have both a @catch and a @finally. |
| 3810 | llvm::Value *PropagatingExnVar = 0; |
| 3811 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3812 | // Push a normal cleanup to leave the try scope. |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 3813 | CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S, |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3814 | SyncArgSlot, |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 3815 | CallTryExitVar, |
| 3816 | ExceptionData, |
| 3817 | &ObjCTypes); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3818 | |
| 3819 | // Enter a try block: |
| 3820 | // - Call objc_exception_try_enter to push ExceptionData on top of |
| 3821 | // the EH stack. |
| 3822 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData) |
| 3823 | ->setDoesNotThrow(); |
| 3824 | |
| 3825 | // - Call setjmp on the exception data buffer. |
| 3826 | llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0); |
| 3827 | llvm::Value *GEPIndexes[] = { Zero, Zero, Zero }; |
| 3828 | llvm::Value *SetJmpBuffer = |
Jay Foad | 0f6ac7c | 2011-07-22 08:16:57 +0000 | [diff] [blame] | 3829 | CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer"); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3830 | llvm::CallInst *SetJmpResult = |
| 3831 | CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result"); |
| 3832 | SetJmpResult->setDoesNotThrow(); |
Bill Wendling | 6446c3e | 2011-12-19 23:53:28 +0000 | [diff] [blame] | 3833 | SetJmpResult->setCanReturnTwice(); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3834 | |
| 3835 | // If setjmp returned 0, enter the protected block; otherwise, |
| 3836 | // branch to the handler. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 3837 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 3838 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3839 | llvm::Value *DidCatch = |
John McCall | d96a8e7 | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 3840 | CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception"); |
| 3841 | CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3842 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3843 | // Emit the protected block. |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3844 | CGF.EmitBlock(TryBlock); |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3845 | CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3846 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3847 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3848 | |
| 3849 | CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3850 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3851 | // Emit the exception handler block. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 3852 | CGF.EmitBlock(TryHandler); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3853 | |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3854 | // Don't optimize loads of the in-scope locals across this point. |
| 3855 | Hazards.emitWriteHazard(); |
| 3856 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3857 | // For a @synchronized (or a @try with no catches), just branch |
| 3858 | // through the cleanup to the rethrow block. |
| 3859 | if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) { |
| 3860 | // Tell the cleanup not to re-pop the exit. |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3861 | CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3862 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3863 | |
| 3864 | // Otherwise, we have to match against the caught exceptions. |
| 3865 | } else { |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3866 | // Retrieve the exception object. We may emit multiple blocks but |
| 3867 | // nothing can cross this so the value is already in SSA form. |
| 3868 | llvm::CallInst *Caught = |
| 3869 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 3870 | ExceptionData, "caught"); |
| 3871 | Caught->setDoesNotThrow(); |
| 3872 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3873 | // Push the exception to rethrow onto the EH value stack for the |
| 3874 | // benefit of any @throws in the handlers. |
| 3875 | CGF.ObjCEHValueStack.push_back(Caught); |
| 3876 | |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 3877 | const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3878 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3879 | bool HasFinally = (AtTryStmt->getFinallyStmt() != 0); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3880 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3881 | llvm::BasicBlock *CatchBlock = 0; |
| 3882 | llvm::BasicBlock *CatchHandler = 0; |
| 3883 | if (HasFinally) { |
John McCall | 9e2213d | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3884 | // Save the currently-propagating exception before |
| 3885 | // objc_exception_try_enter clears the exception slot. |
| 3886 | PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(), |
| 3887 | "propagating_exception"); |
| 3888 | CGF.Builder.CreateStore(Caught, PropagatingExnVar); |
| 3889 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3890 | // Enter a new exception try block (in case a @catch block |
| 3891 | // throws an exception). |
| 3892 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData) |
| 3893 | ->setDoesNotThrow(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3894 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3895 | llvm::CallInst *SetJmpResult = |
| 3896 | CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, |
| 3897 | "setjmp.result"); |
| 3898 | SetJmpResult->setDoesNotThrow(); |
Bill Wendling | 6446c3e | 2011-12-19 23:53:28 +0000 | [diff] [blame] | 3899 | SetJmpResult->setCanReturnTwice(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3900 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3901 | llvm::Value *Threw = |
| 3902 | CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception"); |
| 3903 | |
| 3904 | CatchBlock = CGF.createBasicBlock("catch"); |
| 3905 | CatchHandler = CGF.createBasicBlock("catch_for_catch"); |
| 3906 | CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock); |
| 3907 | |
| 3908 | CGF.EmitBlock(CatchBlock); |
| 3909 | } |
| 3910 | |
| 3911 | CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3912 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3913 | // Handle catch list. As a special case we check if everything is |
| 3914 | // matched and avoid generating code for falling off the end if |
| 3915 | // so. |
| 3916 | bool AllMatched = false; |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 3917 | for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) { |
| 3918 | const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3919 | |
Douglas Gregor | c00d8e1 | 2010-04-26 16:46:50 +0000 | [diff] [blame] | 3920 | const VarDecl *CatchParam = CatchStmt->getCatchParamDecl(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3921 | const ObjCObjectPointerType *OPT = 0; |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 3922 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3923 | // catch(...) always matches. |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3924 | if (!CatchParam) { |
| 3925 | AllMatched = true; |
| 3926 | } else { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3927 | OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3928 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3929 | // catch(id e) always matches under this ABI, since only |
| 3930 | // ObjC exceptions end up here in the first place. |
Daniel Dunbar | 97f61d1 | 2008-09-27 22:21:14 +0000 | [diff] [blame] | 3931 | // FIXME: For the time being we also match id<X>; this should |
| 3932 | // be rejected by Sema instead. |
Eli Friedman | 818e96f | 2009-07-11 00:57:02 +0000 | [diff] [blame] | 3933 | if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType())) |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3934 | AllMatched = true; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3935 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3936 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3937 | // 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] | 3938 | if (AllMatched) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3939 | CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF); |
| 3940 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3941 | if (CatchParam) { |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 3942 | CGF.EmitAutoVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 3943 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3944 | |
| 3945 | // These types work out because ConvertType(id) == i8*. |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 3946 | CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3947 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3948 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3949 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3950 | |
| 3951 | // The scope of the catch variable ends right here. |
| 3952 | CatchVarCleanups.ForceCleanup(); |
| 3953 | |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3954 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3955 | break; |
| 3956 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3957 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3958 | assert(OPT && "Unexpected non-object pointer type in @catch"); |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 3959 | const ObjCObjectType *ObjTy = OPT->getObjectType(); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3960 | |
| 3961 | // FIXME: @catch (Class c) ? |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 3962 | ObjCInterfaceDecl *IDecl = ObjTy->getInterface(); |
| 3963 | assert(IDecl && "Catch parameter must have Objective-C type!"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3964 | |
| 3965 | // Check if the @catch block matches the exception object. |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 3966 | llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3967 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3968 | llvm::CallInst *Match = |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 3969 | CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(), |
| 3970 | Class, Caught, "match"); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3971 | Match->setDoesNotThrow(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3972 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3973 | llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match"); |
| 3974 | llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3975 | |
| 3976 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 3977 | MatchedBlock, NextCatchBlock); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3978 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3979 | // Emit the @catch block. |
| 3980 | CGF.EmitBlock(MatchedBlock); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3981 | |
| 3982 | // Collect any cleanups for the catch variable. The scope lasts until |
| 3983 | // the end of the catch body. |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3984 | CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3985 | |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 3986 | CGF.EmitAutoVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 3987 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3988 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3989 | // Initialize the catch variable. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3990 | llvm::Value *Tmp = |
| 3991 | CGF.Builder.CreateBitCast(Caught, |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 3992 | CGF.ConvertType(CatchParam->getType())); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 3993 | CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3994 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3995 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3996 | |
| 3997 | // We're done with the catch variable. |
| 3998 | CatchVarCleanups.ForceCleanup(); |
| 3999 | |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 4000 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4001 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 4002 | CGF.EmitBlock(NextCatchBlock); |
| 4003 | } |
| 4004 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4005 | CGF.ObjCEHValueStack.pop_back(); |
| 4006 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4007 | // If nothing wanted anything to do with the caught exception, |
| 4008 | // kill the extract call. |
| 4009 | if (Caught->use_empty()) |
| 4010 | Caught->eraseFromParent(); |
| 4011 | |
| 4012 | if (!AllMatched) |
| 4013 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 4014 | |
| 4015 | if (HasFinally) { |
| 4016 | // Emit the exception handler for the @catch blocks. |
| 4017 | CGF.EmitBlock(CatchHandler); |
| 4018 | |
| 4019 | // In theory we might now need a write hazard, but actually it's |
| 4020 | // unnecessary because there's no local-accessing code between |
| 4021 | // the try's write hazard and here. |
| 4022 | //Hazards.emitWriteHazard(); |
| 4023 | |
John McCall | 9e2213d | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 4024 | // Extract the new exception and save it to the |
| 4025 | // propagating-exception slot. |
| 4026 | assert(PropagatingExnVar); |
| 4027 | llvm::CallInst *NewCaught = |
| 4028 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 4029 | ExceptionData, "caught"); |
| 4030 | NewCaught->setDoesNotThrow(); |
| 4031 | CGF.Builder.CreateStore(NewCaught, PropagatingExnVar); |
| 4032 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4033 | // Don't pop the catch handler; the throw already did. |
| 4034 | CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 4035 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 4036 | } |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 4037 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4038 | |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 4039 | // Insert read hazards as required in the new blocks. |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4040 | Hazards.emitHazardsInNewBlocks(); |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 4041 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4042 | // Pop the cleanup. |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4043 | CGF.Builder.restoreIP(TryFallthroughIP); |
| 4044 | if (CGF.HaveInsertPoint()) |
| 4045 | CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4046 | CGF.PopCleanupBlock(); |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4047 | CGF.EmitBlock(FinallyEnd.getBlock(), true); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 4048 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4049 | // Emit the rethrow block. |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 4050 | CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP(); |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 4051 | CGF.EmitBlock(FinallyRethrow.getBlock(), true); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4052 | if (CGF.HaveInsertPoint()) { |
John McCall | 9e2213d | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 4053 | // If we have a propagating-exception variable, check it. |
| 4054 | llvm::Value *PropagatingExn; |
| 4055 | if (PropagatingExnVar) { |
| 4056 | PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar); |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 4057 | |
John McCall | 9e2213d | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 4058 | // Otherwise, just look in the buffer for the exception to throw. |
| 4059 | } else { |
| 4060 | llvm::CallInst *Caught = |
| 4061 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 4062 | ExceptionData); |
| 4063 | Caught->setDoesNotThrow(); |
| 4064 | PropagatingExn = Caught; |
| 4065 | } |
| 4066 | |
| 4067 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn) |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4068 | ->setDoesNotThrow(); |
| 4069 | CGF.Builder.CreateUnreachable(); |
Fariborz Jahanian | f2878e5 | 2008-11-21 19:21:53 +0000 | [diff] [blame] | 4070 | } |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 4071 | |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 4072 | CGF.Builder.restoreIP(SavedIP); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 4073 | } |
| 4074 | |
| 4075 | void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6a3c70e | 2013-01-10 19:02:56 +0000 | [diff] [blame] | 4076 | const ObjCAtThrowStmt &S, |
| 4077 | bool ClearInsertionPoint) { |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 4078 | llvm::Value *ExceptionAsObject; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4079 | |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 4080 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
John McCall | 2b014d6 | 2011-10-01 10:32:24 +0000 | [diff] [blame] | 4081 | llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4082 | ExceptionAsObject = |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 4083 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy); |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 4084 | } else { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4085 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 4086 | "Unexpected rethrow outside @catch block."); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 4087 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 4088 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4089 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 4090 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject) |
| 4091 | ->setDoesNotReturn(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 4092 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 4093 | |
| 4094 | // Clear the insertion point to indicate we are in unreachable code. |
Fariborz Jahanian | 6a3c70e | 2013-01-10 19:02:56 +0000 | [diff] [blame] | 4095 | if (ClearInsertionPoint) |
| 4096 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 4097 | } |
| 4098 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 4099 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 4100 | /// object: objc_read_weak (id *src) |
| 4101 | /// |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 4102 | llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4103 | llvm::Value *AddrWeakObj) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4104 | llvm::Type* DestTy = |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4105 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
| 4106 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, |
| 4107 | ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 4108 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 4109 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 4110 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 4111 | return read_weak; |
| 4112 | } |
| 4113 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 4114 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 4115 | /// objc_assign_weak (id src, id *dst) |
| 4116 | /// |
| 4117 | void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4118 | llvm::Value *src, llvm::Value *dst) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4119 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4120 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 4121 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4122 | assert(Size <= 8 && "does not support size > 8"); |
| 4123 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4124 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 4125 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 4126 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 4127 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 4128 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 4129 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 4130 | src, dst, "weakassign"); |
| 4131 | return; |
| 4132 | } |
| 4133 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 4134 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 4135 | /// objc_assign_global (id src, id *dst) |
| 4136 | /// |
| 4137 | void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 4138 | llvm::Value *src, llvm::Value *dst, |
| 4139 | bool threadlocal) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4140 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4141 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 4142 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4143 | assert(Size <= 8 && "does not support size > 8"); |
| 4144 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4145 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 4146 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 4147 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 4148 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 4149 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 4150 | if (!threadlocal) |
| 4151 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
| 4152 | src, dst, "globalassign"); |
| 4153 | else |
| 4154 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(), |
| 4155 | src, dst, "threadlocalassign"); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 4156 | return; |
| 4157 | } |
| 4158 | |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 4159 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 4160 | /// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset) |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 4161 | /// |
| 4162 | void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 4163 | llvm::Value *src, llvm::Value *dst, |
| 4164 | llvm::Value *ivarOffset) { |
| 4165 | assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL"); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4166 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4167 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 4168 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4169 | assert(Size <= 8 && "does not support size > 8"); |
| 4170 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4171 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 4172 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 4173 | } |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 4174 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 4175 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 4176 | CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(), |
| 4177 | src, dst, ivarOffset); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 4178 | return; |
| 4179 | } |
| 4180 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 4181 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 4182 | /// objc_assign_strongCast (id src, id *dst) |
| 4183 | /// |
| 4184 | void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4185 | llvm::Value *src, llvm::Value *dst) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4186 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4187 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 4188 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4189 | assert(Size <= 8 && "does not support size > 8"); |
| 4190 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4191 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 4192 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 4193 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 4194 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 4195 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 4196 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 4197 | src, dst, "weakassign"); |
| 4198 | return; |
| 4199 | } |
| 4200 | |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 4201 | void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4202 | llvm::Value *DestPtr, |
| 4203 | llvm::Value *SrcPtr, |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 4204 | llvm::Value *size) { |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 4205 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy); |
| 4206 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 4207 | CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(), |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 4208 | DestPtr, SrcPtr, size); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 4209 | return; |
| 4210 | } |
| 4211 | |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 4212 | /// EmitObjCValueForIvar - Code Gen for ivar reference. |
| 4213 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4214 | LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 4215 | QualType ObjectTy, |
| 4216 | llvm::Value *BaseValue, |
| 4217 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4218 | unsigned CVRQualifiers) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4219 | const ObjCInterfaceDecl *ID = |
| 4220 | ObjectTy->getAs<ObjCObjectType>()->getInterface(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 4221 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 4222 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 4223 | } |
| 4224 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4225 | llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 4226 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4227 | const ObjCIvarDecl *Ivar) { |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4228 | uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar); |
| 4229 | return llvm::ConstantInt::get( |
| 4230 | CGM.getTypes().ConvertType(CGM.getContext().LongTy), |
| 4231 | Offset); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4232 | } |
| 4233 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4234 | /* *** Private Interface *** */ |
| 4235 | |
| 4236 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 4237 | /// level information. |
| 4238 | /// |
| 4239 | /// See: <rdr://4810609&4810587&4810587> |
| 4240 | /// struct IMAGE_INFO { |
| 4241 | /// unsigned version; |
| 4242 | /// unsigned flags; |
| 4243 | /// }; |
| 4244 | enum ImageInfoFlags { |
Daniel Dunbar | fce176b | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 4245 | eImageInfo_FixAndContinue = (1 << 0), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4246 | eImageInfo_GarbageCollected = (1 << 1), |
| 4247 | eImageInfo_GCOnly = (1 << 2), |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 4248 | eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set. |
| 4249 | |
Daniel Dunbar | fce176b | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 4250 | // A flag indicating that the module has no instances of a @synthesize of a |
| 4251 | // superclass variable. <rdar://problem/6803242> |
Bill Wendling | 0982251 | 2012-04-24 11:04:57 +0000 | [diff] [blame] | 4252 | eImageInfo_CorrectedSynthesize = (1 << 4), |
| 4253 | eImageInfo_ImageIsSimulated = (1 << 5) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4254 | }; |
| 4255 | |
Daniel Dunbar | fce176b | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 4256 | void CGObjCCommonMac::EmitImageInfo() { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4257 | unsigned version = 0; // Version is unused? |
Bill Wendling | 3973acc | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 4258 | const char *Section = (ObjCABI == 1) ? |
| 4259 | "__OBJC, __image_info,regular" : |
| 4260 | "__DATA, __objc_imageinfo, regular, no_dead_strip"; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4261 | |
Bill Wendling | 3973acc | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 4262 | // Generate module-level named metadata to convey this information to the |
| 4263 | // linker and code-gen. |
| 4264 | llvm::Module &Mod = CGM.getModule(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4265 | |
Bill Wendling | 3973acc | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 4266 | // Add the ObjC ABI version to the module flags. |
| 4267 | Mod.addModuleFlag(llvm::Module::Error, "Objective-C Version", ObjCABI); |
| 4268 | Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Version", |
| 4269 | version); |
| 4270 | Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Section", |
| 4271 | llvm::MDString::get(VMContext,Section)); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4272 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4273 | if (CGM.getLangOpts().getGC() == LangOptions::NonGC) { |
Bill Wendling | 3973acc | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 4274 | // Non-GC overrides those files which specify GC. |
| 4275 | Mod.addModuleFlag(llvm::Module::Override, |
| 4276 | "Objective-C Garbage Collection", (uint32_t)0); |
| 4277 | } else { |
| 4278 | // Add the ObjC garbage collection value. |
| 4279 | Mod.addModuleFlag(llvm::Module::Error, |
| 4280 | "Objective-C Garbage Collection", |
| 4281 | eImageInfo_GarbageCollected); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4282 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4283 | if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) { |
Bill Wendling | 3973acc | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 4284 | // Add the ObjC GC Only value. |
| 4285 | Mod.addModuleFlag(llvm::Module::Error, "Objective-C GC Only", |
| 4286 | eImageInfo_GCOnly); |
| 4287 | |
| 4288 | // Require that GC be specified and set to eImageInfo_GarbageCollected. |
| 4289 | llvm::Value *Ops[2] = { |
| 4290 | llvm::MDString::get(VMContext, "Objective-C Garbage Collection"), |
| 4291 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), |
| 4292 | eImageInfo_GarbageCollected) |
| 4293 | }; |
| 4294 | Mod.addModuleFlag(llvm::Module::Require, "Objective-C GC Only", |
| 4295 | llvm::MDNode::get(VMContext, Ops)); |
| 4296 | } |
| 4297 | } |
Bill Wendling | 0982251 | 2012-04-24 11:04:57 +0000 | [diff] [blame] | 4298 | |
| 4299 | // Indicate whether we're compiling this to run on a simulator. |
| 4300 | const llvm::Triple &Triple = CGM.getTarget().getTriple(); |
| 4301 | if (Triple.getOS() == llvm::Triple::IOS && |
| 4302 | (Triple.getArch() == llvm::Triple::x86 || |
| 4303 | Triple.getArch() == llvm::Triple::x86_64)) |
| 4304 | Mod.addModuleFlag(llvm::Module::Error, "Objective-C Is Simulated", |
| 4305 | eImageInfo_ImageIsSimulated); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4306 | } |
| 4307 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4308 | // struct objc_module { |
| 4309 | // unsigned long version; |
| 4310 | // unsigned long size; |
| 4311 | // const char *name; |
| 4312 | // Symtab symtab; |
| 4313 | // }; |
| 4314 | |
| 4315 | // FIXME: Get from somewhere |
| 4316 | static const int ModuleVersion = 7; |
| 4317 | |
| 4318 | void CGObjCMac::EmitModuleInfo() { |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 4319 | uint64_t Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ModuleTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4320 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 4321 | llvm::Constant *Values[] = { |
| 4322 | llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion), |
| 4323 | llvm::ConstantInt::get(ObjCTypes.LongTy, Size), |
| 4324 | // This used to be the filename, now it is unused. <rdr://4327263> |
| 4325 | GetClassName(&CGM.getContext().Idents.get("")), |
| 4326 | EmitModuleSymbols() |
| 4327 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4328 | CreateMetadataVar("\01L_OBJC_MODULES", |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 4329 | llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4330 | "__OBJC,__module_info,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 4331 | 4, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4332 | } |
| 4333 | |
| 4334 | llvm::Constant *CGObjCMac::EmitModuleSymbols() { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4335 | unsigned NumClasses = DefinedClasses.size(); |
| 4336 | unsigned NumCategories = DefinedCategories.size(); |
| 4337 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4338 | // Return null if no symbols were defined. |
| 4339 | if (!NumClasses && !NumCategories) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4340 | return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4341 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 4342 | llvm::Constant *Values[5]; |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4343 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4344 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4345 | Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses); |
| 4346 | Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4347 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 4348 | // The runtime expects exactly the list of defined classes followed |
| 4349 | // by the list of defined categories, in a single array. |
Chris Lattner | 0b23971 | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 4350 | SmallVector<llvm::Constant*, 8> Symbols(NumClasses + NumCategories); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 4351 | for (unsigned i=0; i<NumClasses; i++) |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4352 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 4353 | ObjCTypes.Int8PtrTy); |
| 4354 | for (unsigned i=0; i<NumCategories; i++) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4355 | Symbols[NumClasses + i] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4356 | llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 4357 | ObjCTypes.Int8PtrTy); |
| 4358 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4359 | Values[4] = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4360 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Chris Lattner | 0b23971 | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 4361 | Symbols.size()), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4362 | Symbols); |
| 4363 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 4364 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4365 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4366 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4367 | CreateMetadataVar("\01L_OBJC_SYMBOLS", Init, |
| 4368 | "__OBJC,__symbols,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 4369 | 4, true); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4370 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4371 | } |
| 4372 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4373 | llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder, |
| 4374 | IdentifierInfo *II) { |
| 4375 | LazySymbols.insert(II); |
| 4376 | |
| 4377 | llvm::GlobalVariable *&Entry = ClassReferences[II]; |
| 4378 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4379 | if (!Entry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4380 | llvm::Constant *Casted = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4381 | llvm::ConstantExpr::getBitCast(GetClassName(II), |
| 4382 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4383 | Entry = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4384 | CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted, |
| 4385 | "__OBJC,__cls_refs,literal_pointers,no_dead_strip", |
| 4386 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4387 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4388 | |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 4389 | return Builder.CreateLoad(Entry); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4390 | } |
| 4391 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4392 | llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder, |
| 4393 | const ObjCInterfaceDecl *ID) { |
| 4394 | return EmitClassRefFromId(Builder, ID->getIdentifier()); |
| 4395 | } |
| 4396 | |
| 4397 | llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) { |
| 4398 | IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool"); |
| 4399 | return EmitClassRefFromId(Builder, II); |
| 4400 | } |
| 4401 | |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 4402 | llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel, |
| 4403 | bool lvalue) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4404 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4405 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4406 | if (!Entry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4407 | llvm::Constant *Casted = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4408 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4409 | ObjCTypes.SelectorPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4410 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4411 | CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted, |
| 4412 | "__OBJC,__message_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 4413 | 4, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4414 | } |
| 4415 | |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 4416 | if (lvalue) |
| 4417 | return Entry; |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 4418 | return Builder.CreateLoad(Entry); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4419 | } |
| 4420 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4421 | llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4422 | llvm::GlobalVariable *&Entry = ClassNames[Ident]; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4423 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4424 | if (!Entry) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4425 | Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
Chris Lattner | 9401069 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4426 | llvm::ConstantDataArray::getString(VMContext, |
| 4427 | Ident->getNameStart()), |
Daniel Dunbar | af19ac4 | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4428 | ((ObjCABI == 2) ? |
| 4429 | "__TEXT,__objc_classname,cstring_literals" : |
| 4430 | "__TEXT,__cstring,cstring_literals"), |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4431 | 1, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4432 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4433 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4434 | } |
| 4435 | |
Argyrios Kyrtzidis | 9d50c06 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 4436 | llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) { |
| 4437 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator |
| 4438 | I = MethodDefinitions.find(MD); |
| 4439 | if (I != MethodDefinitions.end()) |
| 4440 | return I->second; |
| 4441 | |
Argyrios Kyrtzidis | 9d50c06 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 4442 | return NULL; |
| 4443 | } |
| 4444 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 4445 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 4446 | /// ivar layout bitmap. |
| 4447 | llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4448 | const ObjCCommonTypesHelper &ObjCTypes) { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4449 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 4450 | } |
| 4451 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 4452 | void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT, |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4453 | unsigned int BytePos, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 4454 | bool ForStrongLayout, |
| 4455 | bool &HasUnion) { |
| 4456 | const RecordDecl *RD = RT->getDecl(); |
| 4457 | // FIXME - Use iterator. |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 4458 | SmallVector<const FieldDecl*, 16> Fields; |
| 4459 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 4460 | e = RD->field_end(); i != e; ++i) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 4461 | Fields.push_back(*i); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4462 | llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4463 | const llvm::StructLayout *RecLayout = |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 4464 | CGM.getDataLayout().getStructLayout(cast<llvm::StructType>(Ty)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4465 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 4466 | BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos, |
| 4467 | ForStrongLayout, HasUnion); |
| 4468 | } |
| 4469 | |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 4470 | void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4471 | const llvm::StructLayout *Layout, |
| 4472 | const RecordDecl *RD, |
Bill Wendling | 795b100 | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 4473 | ArrayRef<const FieldDecl*> RecFields, |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4474 | unsigned int BytePos, bool ForStrongLayout, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4475 | bool &HasUnion) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4476 | bool IsUnion = (RD && RD->isUnion()); |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4477 | uint64_t MaxUnionIvarSize = 0; |
| 4478 | uint64_t MaxSkippedUnionIvarSize = 0; |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4479 | const FieldDecl *MaxField = 0; |
| 4480 | const FieldDecl *MaxSkippedField = 0; |
| 4481 | const FieldDecl *LastFieldBitfieldOrUnnamed = 0; |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4482 | uint64_t MaxFieldOffset = 0; |
| 4483 | uint64_t MaxSkippedFieldOffset = 0; |
| 4484 | uint64_t LastBitfieldOrUnnamedOffset = 0; |
| 4485 | uint64_t FirstFieldDelta = 0; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4486 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 4487 | if (RecFields.empty()) |
| 4488 | return; |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4489 | unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4490 | unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4491 | if (!RD && CGM.getLangOpts().ObjCAutoRefCount) { |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4492 | const FieldDecl *FirstField = RecFields[0]; |
| 4493 | FirstFieldDelta = |
| 4494 | ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4495 | } |
| 4496 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 4497 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4498 | const FieldDecl *Field = RecFields[i]; |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4499 | uint64_t FieldOffset; |
Anders Carlsson | fc514ab | 2009-07-24 17:23:54 +0000 | [diff] [blame] | 4500 | if (RD) { |
Daniel Dunbar | f8f8eba | 2010-04-14 17:02:21 +0000 | [diff] [blame] | 4501 | // Note that 'i' here is actually the field index inside RD of Field, |
| 4502 | // although this dependency is hidden. |
| 4503 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4504 | FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta; |
Anders Carlsson | fc514ab | 2009-07-24 17:23:54 +0000 | [diff] [blame] | 4505 | } else |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4506 | FieldOffset = |
| 4507 | ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta; |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 4508 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 4509 | // Skip over unnamed or bitfields |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 4510 | if (!Field->getIdentifier() || Field->isBitField()) { |
Argyrios Kyrtzidis | 0dc7509 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 4511 | LastFieldBitfieldOrUnnamed = Field; |
| 4512 | LastBitfieldOrUnnamedOffset = FieldOffset; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 4513 | continue; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 4514 | } |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 4515 | |
Argyrios Kyrtzidis | 0dc7509 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 4516 | LastFieldBitfieldOrUnnamed = 0; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 4517 | QualType FQT = Field->getType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 4518 | if (FQT->isRecordType() || FQT->isUnionType()) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 4519 | if (FQT->isUnionType()) |
| 4520 | HasUnion = true; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4521 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4522 | BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(), |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 4523 | BytePos + FieldOffset, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 4524 | ForStrongLayout, HasUnion); |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 4525 | continue; |
| 4526 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4527 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 4528 | if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4529 | const ConstantArrayType *CArray = |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 4530 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 4531 | uint64_t ElCount = CArray->getSize().getZExtValue(); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 4532 | assert(CArray && "only array with known element size is supported"); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4533 | FQT = CArray->getElementType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 4534 | while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 4535 | const ConstantArrayType *CArray = |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 4536 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 4537 | ElCount *= CArray->getSize().getZExtValue(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 4538 | FQT = CArray->getElementType(); |
| 4539 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4540 | |
| 4541 | assert(!FQT->isUnionType() && |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4542 | "layout for array of unions not supported"); |
Fariborz Jahanian | f96bdf4 | 2011-01-03 19:23:18 +0000 | [diff] [blame] | 4543 | if (FQT->isRecordType() && ElCount) { |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4544 | int OldIndex = IvarsInfo.size() - 1; |
| 4545 | int OldSkIndex = SkipIvars.size() -1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4546 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4547 | const RecordType *RT = FQT->getAs<RecordType>(); |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 4548 | BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 4549 | ForStrongLayout, HasUnion); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4550 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4551 | // Replicate layout information for each array element. Note that |
| 4552 | // one element is already done. |
| 4553 | uint64_t ElIx = 1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4554 | for (int FirstIndex = IvarsInfo.size() - 1, |
| 4555 | FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) { |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4556 | uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits; |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 4557 | for (int i = OldIndex+1; i <= FirstIndex; ++i) |
| 4558 | IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx, |
| 4559 | IvarsInfo[i].ivar_size)); |
| 4560 | for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i) |
| 4561 | SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx, |
| 4562 | SkipIvars[i].ivar_size)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4563 | } |
| 4564 | continue; |
| 4565 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 4566 | } |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4567 | // At this point, we are done with Record/Union and array there of. |
| 4568 | // For other arrays we are down to its element type. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4569 | Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 4570 | |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4571 | unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4572 | if ((ForStrongLayout && GCAttr == Qualifiers::Strong) |
| 4573 | || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) { |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 4574 | if (IsUnion) { |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4575 | uint64_t UnionIvarSize = FieldSize / WordSizeInBits; |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 4576 | if (UnionIvarSize > MaxUnionIvarSize) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4577 | MaxUnionIvarSize = UnionIvarSize; |
| 4578 | MaxField = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 4579 | MaxFieldOffset = FieldOffset; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4580 | } |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 4581 | } else { |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4582 | IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset, |
| 4583 | FieldSize / WordSizeInBits)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4584 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4585 | } else if ((ForStrongLayout && |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4586 | (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)) |
| 4587 | || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) { |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 4588 | if (IsUnion) { |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4589 | // FIXME: Why the asymmetry? We divide by word size in bits on other |
| 4590 | // side. |
| 4591 | uint64_t UnionIvarSize = FieldSize / ByteSizeInBits; |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 4592 | if (UnionIvarSize > MaxSkippedUnionIvarSize) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4593 | MaxSkippedUnionIvarSize = UnionIvarSize; |
| 4594 | MaxSkippedField = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 4595 | MaxSkippedFieldOffset = FieldOffset; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4596 | } |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 4597 | } else { |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4598 | // FIXME: Why the asymmetry, we divide by byte size in bits here? |
| 4599 | SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset, |
| 4600 | FieldSize / ByteSizeInBits)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 4601 | } |
| 4602 | } |
| 4603 | } |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 4604 | |
Argyrios Kyrtzidis | 0dc7509 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 4605 | if (LastFieldBitfieldOrUnnamed) { |
| 4606 | if (LastFieldBitfieldOrUnnamed->isBitField()) { |
| 4607 | // Last field was a bitfield. Must update skip info. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4608 | uint64_t BitFieldSize |
| 4609 | = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext()); |
Argyrios Kyrtzidis | 0dc7509 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 4610 | GC_IVAR skivar; |
| 4611 | skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset; |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4612 | skivar.ivar_size = (BitFieldSize / ByteSizeInBits) |
| 4613 | + ((BitFieldSize % ByteSizeInBits) != 0); |
Argyrios Kyrtzidis | 0dc7509 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 4614 | SkipIvars.push_back(skivar); |
| 4615 | } else { |
| 4616 | assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed"); |
| 4617 | // Last field was unnamed. Must update skip info. |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4618 | unsigned FieldSize |
| 4619 | = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType()); |
Argyrios Kyrtzidis | 0dc7509 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 4620 | SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset, |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4621 | FieldSize / ByteSizeInBits)); |
Argyrios Kyrtzidis | 0dc7509 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 4622 | } |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 4623 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4624 | |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 4625 | if (MaxField) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4626 | IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 4627 | MaxUnionIvarSize)); |
| 4628 | if (MaxSkippedField) |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 4629 | SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 4630 | MaxSkippedUnionIvarSize)); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 4631 | } |
| 4632 | |
Fariborz Jahanian | b8fd2eb | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 4633 | /// BuildIvarLayoutBitmap - This routine is the horsework for doing all |
| 4634 | /// the computations and returning the layout bitmap (for ivar or blocks) in |
| 4635 | /// the given argument BitMap string container. Routine reads |
| 4636 | /// two containers, IvarsInfo and SkipIvars which are assumed to be |
| 4637 | /// filled already by the caller. |
Chris Lattner | 9401069 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4638 | llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string &BitMap) { |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4639 | unsigned int WordsToScan, WordsToSkip; |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4640 | llvm::Type *PtrTy = CGM.Int8PtrTy; |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4641 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4642 | // Build the string of skip/scan nibbles |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4643 | SmallVector<SKIP_SCAN, 32> SkipScanIvars; |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4644 | unsigned int WordSize = |
| 4645 | CGM.getTypes().getDataLayout().getTypeAllocSize(PtrTy); |
| 4646 | if (IvarsInfo[0].ivar_bytepos == 0) { |
| 4647 | WordsToSkip = 0; |
| 4648 | WordsToScan = IvarsInfo[0].ivar_size; |
| 4649 | } else { |
| 4650 | WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize; |
| 4651 | WordsToScan = IvarsInfo[0].ivar_size; |
| 4652 | } |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4653 | for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) { |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4654 | unsigned int TailPrevGCObjC = |
| 4655 | IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4656 | if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4657 | // consecutive 'scanned' object pointers. |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4658 | WordsToScan += IvarsInfo[i].ivar_size; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4659 | } else { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4660 | // Skip over 'gc'able object pointer which lay over each other. |
| 4661 | if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos) |
| 4662 | continue; |
| 4663 | // Must skip over 1 or more words. We save current skip/scan values |
| 4664 | // and start a new pair. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 4665 | SKIP_SCAN SkScan; |
| 4666 | SkScan.skip = WordsToSkip; |
| 4667 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4668 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4669 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4670 | // Skip the hole. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 4671 | SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize; |
| 4672 | SkScan.scan = 0; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4673 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4674 | WordsToSkip = 0; |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4675 | WordsToScan = IvarsInfo[i].ivar_size; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4676 | } |
| 4677 | } |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4678 | if (WordsToScan > 0) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 4679 | SKIP_SCAN SkScan; |
| 4680 | SkScan.skip = WordsToSkip; |
| 4681 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4682 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4683 | } |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4684 | |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4685 | if (!SkipIvars.empty()) { |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4686 | unsigned int LastIndex = SkipIvars.size()-1; |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4687 | int LastByteSkipped = |
| 4688 | SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4689 | LastIndex = IvarsInfo.size()-1; |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4690 | int LastByteScanned = |
| 4691 | IvarsInfo[LastIndex].ivar_bytepos + |
| 4692 | IvarsInfo[LastIndex].ivar_size * WordSize; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4693 | // 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] | 4694 | if (LastByteSkipped > LastByteScanned) { |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4695 | unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 4696 | SKIP_SCAN SkScan; |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4697 | SkScan.skip = TotalWords - (LastByteScanned/WordSize); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 4698 | SkScan.scan = 0; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4699 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4700 | } |
| 4701 | } |
| 4702 | // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced |
| 4703 | // as 0xMN. |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4704 | int SkipScan = SkipScanIvars.size()-1; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4705 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4706 | if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0 |
| 4707 | && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) { |
| 4708 | // 0xM0 followed by 0x0N detected. |
| 4709 | SkipScanIvars[i].scan = SkipScanIvars[i+1].scan; |
| 4710 | for (int j = i+1; j < SkipScan; j++) |
| 4711 | SkipScanIvars[j] = SkipScanIvars[j+1]; |
| 4712 | --SkipScan; |
| 4713 | } |
| 4714 | } |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4715 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4716 | // Generate the string. |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4717 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4718 | unsigned char byte; |
| 4719 | unsigned int skip_small = SkipScanIvars[i].skip % 0xf; |
| 4720 | unsigned int scan_small = SkipScanIvars[i].scan % 0xf; |
| 4721 | unsigned int skip_big = SkipScanIvars[i].skip / 0xf; |
| 4722 | unsigned int scan_big = SkipScanIvars[i].scan / 0xf; |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4723 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4724 | // first skip big. |
| 4725 | for (unsigned int ix = 0; ix < skip_big; ix++) |
| 4726 | BitMap += (unsigned char)(0xf0); |
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 | // next (skip small, scan) |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4729 | if (skip_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4730 | byte = skip_small << 4; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4731 | if (scan_big > 0) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4732 | byte |= 0xf; |
| 4733 | --scan_big; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4734 | } else if (scan_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4735 | byte |= scan_small; |
| 4736 | scan_small = 0; |
| 4737 | } |
| 4738 | BitMap += byte; |
| 4739 | } |
| 4740 | // next scan big |
| 4741 | for (unsigned int ix = 0; ix < scan_big; ix++) |
| 4742 | BitMap += (unsigned char)(0x0f); |
| 4743 | // last scan small |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4744 | if (scan_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4745 | byte = scan_small; |
| 4746 | BitMap += byte; |
| 4747 | } |
| 4748 | } |
| 4749 | // null terminate string. |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 4750 | unsigned char zero = 0; |
| 4751 | BitMap += zero; |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4752 | |
| 4753 | llvm::GlobalVariable * Entry = |
| 4754 | CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
Chris Lattner | 9401069 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4755 | llvm::ConstantDataArray::getString(VMContext, BitMap,false), |
Daniel Dunbar | af19ac4 | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4756 | ((ObjCABI == 2) ? |
| 4757 | "__TEXT,__objc_classname,cstring_literals" : |
| 4758 | "__TEXT,__cstring,cstring_literals"), |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4759 | 1, true); |
| 4760 | return getConstantGEP(VMContext, Entry, 0, 0); |
| 4761 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4762 | |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4763 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 4764 | /// implementation for the __strong or __weak case. |
| 4765 | /// The layout map displays which words in ivar list must be skipped |
| 4766 | /// and which must be scanned by GC (see below). String is built of bytes. |
| 4767 | /// Each byte is divided up in two nibbles (4-bit each). Left nibble is count |
| 4768 | /// of words to skip and right nibble is count of words to scan. So, each |
| 4769 | /// nibble represents up to 15 workds to skip or scan. Skipping the rest is |
| 4770 | /// represented by a 0x00 byte which also ends the string. |
| 4771 | /// 1. when ForStrongLayout is true, following ivars are scanned: |
| 4772 | /// - id, Class |
| 4773 | /// - object * |
| 4774 | /// - __strong anything |
| 4775 | /// |
| 4776 | /// 2. When ForStrongLayout is false, following ivars are scanned: |
| 4777 | /// - __weak anything |
| 4778 | /// |
| 4779 | llvm::Constant *CGObjCCommonMac::BuildIvarLayout( |
| 4780 | const ObjCImplementationDecl *OMD, |
| 4781 | bool ForStrongLayout) { |
| 4782 | bool hasUnion = false; |
| 4783 | |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4784 | llvm::Type *PtrTy = CGM.Int8PtrTy; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4785 | if (CGM.getLangOpts().getGC() == LangOptions::NonGC && |
| 4786 | !CGM.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4787 | return llvm::Constant::getNullValue(PtrTy); |
| 4788 | |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4789 | const ObjCInterfaceDecl *OI = OMD->getClassInterface(); |
| 4790 | SmallVector<const FieldDecl*, 32> RecFields; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4791 | if (CGM.getLangOpts().ObjCAutoRefCount) { |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4792 | for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin(); |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 4793 | IVD; IVD = IVD->getNextIvar()) |
| 4794 | RecFields.push_back(cast<FieldDecl>(IVD)); |
| 4795 | } |
| 4796 | else { |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4797 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4798 | CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4799 | |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4800 | // FIXME: This is not ideal; we shouldn't have to do this copy. |
| 4801 | RecFields.append(Ivars.begin(), Ivars.end()); |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 4802 | } |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4803 | |
| 4804 | if (RecFields.empty()) |
| 4805 | return llvm::Constant::getNullValue(PtrTy); |
| 4806 | |
| 4807 | SkipIvars.clear(); |
| 4808 | IvarsInfo.clear(); |
| 4809 | |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4810 | BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4811 | if (IvarsInfo.empty()) |
| 4812 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | b8fd2eb | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 4813 | // Sort on byte position in case we encounterred a union nested in |
| 4814 | // the ivar list. |
| 4815 | if (hasUnion && !IvarsInfo.empty()) |
| 4816 | std::sort(IvarsInfo.begin(), IvarsInfo.end()); |
| 4817 | if (hasUnion && !SkipIvars.empty()) |
| 4818 | std::sort(SkipIvars.begin(), SkipIvars.end()); |
| 4819 | |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4820 | std::string BitMap; |
Fariborz Jahanian | b8fd2eb | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 4821 | llvm::Constant *C = BuildIvarLayoutBitmap(BitMap); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4822 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4823 | if (CGM.getLangOpts().ObjCGCBitmapPrint) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4824 | printf("\n%s ivar layout for class '%s': ", |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 4825 | ForStrongLayout ? "strong" : "weak", |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4826 | OMD->getClassInterface()->getName().data()); |
Roman Divacky | 31ba613 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 4827 | const unsigned char *s = (const unsigned char*)BitMap.c_str(); |
Bill Wendling | 13562a1 | 2012-02-07 09:06:01 +0000 | [diff] [blame] | 4828 | for (unsigned i = 0, e = BitMap.size(); i < e; i++) |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 4829 | if (!(s[i] & 0xf0)) |
| 4830 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 4831 | else |
| 4832 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 4833 | printf("\n"); |
| 4834 | } |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4835 | return C; |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 4836 | } |
| 4837 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4838 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4839 | llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; |
| 4840 | |
Chris Lattner | 0b23971 | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 4841 | // FIXME: Avoid std::string in "Sel.getAsString()" |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4842 | if (!Entry) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4843 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_", |
Chris Lattner | 9401069 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4844 | llvm::ConstantDataArray::getString(VMContext, Sel.getAsString()), |
Daniel Dunbar | af19ac4 | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4845 | ((ObjCABI == 2) ? |
| 4846 | "__TEXT,__objc_methname,cstring_literals" : |
| 4847 | "__TEXT,__cstring,cstring_literals"), |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4848 | 1, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4849 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4850 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4851 | } |
| 4852 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4853 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4854 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4855 | return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); |
| 4856 | } |
| 4857 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4858 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) { |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4859 | std::string TypeStr; |
| 4860 | CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); |
| 4861 | |
| 4862 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4863 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4864 | if (!Entry) |
| 4865 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
Chris Lattner | 9401069 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4866 | llvm::ConstantDataArray::getString(VMContext, TypeStr), |
Daniel Dunbar | af19ac4 | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4867 | ((ObjCABI == 2) ? |
| 4868 | "__TEXT,__objc_methtype,cstring_literals" : |
| 4869 | "__TEXT,__cstring,cstring_literals"), |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4870 | 1, true); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4871 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4872 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4873 | } |
| 4874 | |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4875 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D, |
| 4876 | bool Extended) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4877 | std::string TypeStr; |
Bill Wendling | a4dc693 | 2012-02-09 22:45:21 +0000 | [diff] [blame] | 4878 | if (CGM.getContext().getObjCEncodingForMethodDecl(D, TypeStr, Extended)) |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4879 | return 0; |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4880 | |
| 4881 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
| 4882 | |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4883 | if (!Entry) |
| 4884 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
Chris Lattner | 9401069 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4885 | llvm::ConstantDataArray::getString(VMContext, TypeStr), |
Daniel Dunbar | af19ac4 | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4886 | ((ObjCABI == 2) ? |
| 4887 | "__TEXT,__objc_methtype,cstring_literals" : |
| 4888 | "__TEXT,__cstring,cstring_literals"), |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4889 | 1, true); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4890 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4891 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4892 | } |
| 4893 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4894 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4895 | llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4896 | llvm::GlobalVariable *&Entry = PropertyNames[Ident]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4897 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4898 | if (!Entry) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4899 | Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_", |
Chris Lattner | 9401069 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4900 | llvm::ConstantDataArray::getString(VMContext, |
| 4901 | Ident->getNameStart()), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4902 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4903 | 1, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4904 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4905 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4906 | } |
| 4907 | |
| 4908 | // FIXME: Merge into a single cstring creation function. |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4909 | // FIXME: This Decl should be more precise. |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4910 | llvm::Constant * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4911 | CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 4912 | const Decl *Container) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4913 | std::string TypeStr; |
| 4914 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4915 | return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); |
| 4916 | } |
| 4917 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4918 | void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D, |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4919 | const ObjCContainerDecl *CD, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4920 | SmallVectorImpl<char> &Name) { |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4921 | llvm::raw_svector_ostream OS(Name); |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 4922 | assert (CD && "Missing container decl in GetNameForMethod"); |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4923 | OS << '\01' << (D->isInstanceMethod() ? '-' : '+') |
| 4924 | << '[' << CD->getName(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4925 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4926 | dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) |
Benjamin Kramer | f978059 | 2012-02-07 11:57:45 +0000 | [diff] [blame] | 4927 | OS << '(' << *CID << ')'; |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4928 | OS << ' ' << D->getSelector().getAsString() << ']'; |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 4929 | } |
| 4930 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4931 | void CGObjCMac::FinishModule() { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4932 | EmitModuleInfo(); |
| 4933 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4934 | // Emit the dummy bodies for any protocols which were referenced but |
| 4935 | // never defined. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4936 | for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4937 | I = Protocols.begin(), e = Protocols.end(); I != e; ++I) { |
| 4938 | if (I->second->hasInitializer()) |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4939 | continue; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4940 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 4941 | llvm::Constant *Values[5]; |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4942 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4943 | Values[1] = GetClassName(I->first); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4944 | Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4945 | Values[3] = Values[4] = |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4946 | llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4947 | I->second->setLinkage(llvm::GlobalValue::InternalLinkage); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 4948 | I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4949 | Values)); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4950 | CGM.AddUsedGlobal(I->second); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4951 | } |
| 4952 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4953 | // Add assembler directives to add lazy undefined symbol references |
| 4954 | // for classes which are referenced but not defined. This is |
| 4955 | // important for correct linker interaction. |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4956 | // |
| 4957 | // FIXME: It would be nice if we had an LLVM construct for this. |
| 4958 | if (!LazySymbols.empty() || !DefinedSymbols.empty()) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 4959 | SmallString<256> Asm; |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4960 | Asm += CGM.getModule().getModuleInlineAsm(); |
| 4961 | if (!Asm.empty() && Asm.back() != '\n') |
| 4962 | Asm += '\n'; |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4963 | |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4964 | llvm::raw_svector_ostream OS(Asm); |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4965 | for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(), |
| 4966 | e = DefinedSymbols.end(); I != e; ++I) |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 4967 | OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n" |
| 4968 | << "\t.globl .objc_class_name_" << (*I)->getName() << "\n"; |
Chris Lattner | afd5eda | 2010-04-17 18:26:20 +0000 | [diff] [blame] | 4969 | for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(), |
Fariborz Jahanian | b9c5b3d | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 4970 | e = LazySymbols.end(); I != e; ++I) { |
Chris Lattner | afd5eda | 2010-04-17 18:26:20 +0000 | [diff] [blame] | 4971 | OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n"; |
Fariborz Jahanian | b9c5b3d | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 4972 | } |
| 4973 | |
Bill Wendling | 13562a1 | 2012-02-07 09:06:01 +0000 | [diff] [blame] | 4974 | for (size_t i = 0, e = DefinedCategoryNames.size(); i < e; ++i) { |
Fariborz Jahanian | b9c5b3d | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 4975 | OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n" |
| 4976 | << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n"; |
| 4977 | } |
Chris Lattner | afd5eda | 2010-04-17 18:26:20 +0000 | [diff] [blame] | 4978 | |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4979 | CGM.getModule().setModuleInlineAsm(OS.str()); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4980 | } |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4981 | } |
| 4982 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4983 | CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4984 | : CGObjCCommonMac(cgm), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4985 | ObjCTypes(cgm) { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4986 | ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4987 | ObjCABI = 2; |
| 4988 | } |
| 4989 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4990 | /* *** */ |
| 4991 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4992 | ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm) |
Douglas Gregor | 65a1e67 | 2012-01-17 23:38:32 +0000 | [diff] [blame] | 4993 | : VMContext(cgm.getLLVMContext()), CGM(cgm), ExternalProtocolPtrTy(0) |
| 4994 | { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4995 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 4996 | ASTContext &Ctx = CGM.getContext(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4997 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4998 | ShortTy = Types.ConvertType(Ctx.ShortTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4999 | IntTy = Types.ConvertType(Ctx.IntTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 5000 | LongTy = Types.ConvertType(Ctx.LongTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5001 | LongLongTy = Types.ConvertType(Ctx.LongLongTy); |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5002 | Int8PtrTy = CGM.Int8PtrTy; |
| 5003 | Int8PtrPtrTy = CGM.Int8PtrPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5004 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 5005 | ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5006 | PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 5007 | SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5008 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5009 | // I'm not sure I like this. The implicit coordination is a bit |
| 5010 | // gross. We should solve this in a reasonable fashion because this |
| 5011 | // is a pretty common task (match some runtime data structure with |
| 5012 | // an LLVM data structure). |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5013 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5014 | // FIXME: This is leaked. |
| 5015 | // FIXME: Merge with rewriter code? |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5016 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5017 | // struct _objc_super { |
| 5018 | // id self; |
| 5019 | // Class cls; |
| 5020 | // } |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5021 | RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct, |
Daniel Dunbar | daa3ac5 | 2010-04-29 16:29:11 +0000 | [diff] [blame] | 5022 | Ctx.getTranslationUnitDecl(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5023 | SourceLocation(), SourceLocation(), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5024 | &Ctx.Idents.get("_objc_super")); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5025 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 5026 | Ctx.getObjCIdType(), 0, 0, false, ICIS_NoInit)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5027 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 5028 | Ctx.getObjCClassType(), 0, 0, false, |
| 5029 | ICIS_NoInit)); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 5030 | RD->completeDefinition(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5031 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5032 | SuperCTy = Ctx.getTagDeclType(RD); |
| 5033 | SuperPtrCTy = Ctx.getPointerType(SuperCTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5034 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5035 | SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5036 | SuperPtrTy = llvm::PointerType::getUnqual(SuperTy); |
| 5037 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5038 | // struct _prop_t { |
| 5039 | // char *name; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5040 | // char *attributes; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5041 | // } |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5042 | PropertyTy = llvm::StructType::create("struct._prop_t", |
| 5043 | Int8PtrTy, Int8PtrTy, NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5044 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5045 | // struct _prop_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5046 | // uint32_t entsize; // sizeof(struct _prop_t) |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5047 | // uint32_t count_of_properties; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5048 | // struct _prop_t prop_list[count_of_properties]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5049 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5050 | PropertyListTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5051 | llvm::StructType::create("struct._prop_list_t", IntTy, IntTy, |
| 5052 | llvm::ArrayType::get(PropertyTy, 0), NULL); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5053 | // struct _prop_list_t * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5054 | PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5055 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5056 | // struct _objc_method { |
| 5057 | // SEL _cmd; |
| 5058 | // char *method_type; |
| 5059 | // char *_imp; |
| 5060 | // } |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5061 | MethodTy = llvm::StructType::create("struct._objc_method", |
| 5062 | SelectorPtrTy, Int8PtrTy, Int8PtrTy, |
| 5063 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5064 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5065 | // struct _objc_cache * |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5066 | CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache"); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5067 | CachePtrTy = llvm::PointerType::getUnqual(CacheTy); |
Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 5068 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5069 | } |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 5070 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5071 | ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5072 | : ObjCCommonTypesHelper(cgm) { |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5073 | // struct _objc_method_description { |
| 5074 | // SEL name; |
| 5075 | // char *types; |
| 5076 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5077 | MethodDescriptionTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5078 | llvm::StructType::create("struct._objc_method_description", |
| 5079 | SelectorPtrTy, Int8PtrTy, NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5080 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5081 | // struct _objc_method_description_list { |
| 5082 | // int count; |
| 5083 | // struct _objc_method_description[1]; |
| 5084 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5085 | MethodDescriptionListTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5086 | llvm::StructType::create("struct._objc_method_description_list", |
| 5087 | IntTy, |
| 5088 | llvm::ArrayType::get(MethodDescriptionTy, 0),NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5089 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5090 | // struct _objc_method_description_list * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5091 | MethodDescriptionListPtrTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5092 | llvm::PointerType::getUnqual(MethodDescriptionListTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5093 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5094 | // Protocol description structures |
| 5095 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5096 | // struct _objc_protocol_extension { |
| 5097 | // uint32_t size; // sizeof(struct _objc_protocol_extension) |
| 5098 | // struct _objc_method_description_list *optional_instance_methods; |
| 5099 | // struct _objc_method_description_list *optional_class_methods; |
| 5100 | // struct _objc_property_list *instance_properties; |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5101 | // const char ** extendedMethodTypes; |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5102 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5103 | ProtocolExtensionTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5104 | llvm::StructType::create("struct._objc_protocol_extension", |
| 5105 | IntTy, MethodDescriptionListPtrTy, |
| 5106 | MethodDescriptionListPtrTy, PropertyListPtrTy, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5107 | Int8PtrPtrTy, NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5108 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5109 | // struct _objc_protocol_extension * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5110 | ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5111 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 5112 | // Handle recursive construction of Protocol and ProtocolList types |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5113 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5114 | ProtocolTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5115 | llvm::StructType::create(VMContext, "struct._objc_protocol"); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5116 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5117 | ProtocolListTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5118 | llvm::StructType::create(VMContext, "struct._objc_protocol_list"); |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5119 | ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy), |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5120 | LongTy, |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5121 | llvm::ArrayType::get(ProtocolTy, 0), |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5122 | NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5123 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5124 | // struct _objc_protocol { |
| 5125 | // struct _objc_protocol_extension *isa; |
| 5126 | // char *protocol_name; |
| 5127 | // struct _objc_protocol **_objc_protocol_list; |
| 5128 | // struct _objc_method_description_list *instance_methods; |
| 5129 | // struct _objc_method_description_list *class_methods; |
| 5130 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5131 | ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy, |
| 5132 | llvm::PointerType::getUnqual(ProtocolListTy), |
| 5133 | MethodDescriptionListPtrTy, |
| 5134 | MethodDescriptionListPtrTy, |
| 5135 | NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5136 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5137 | // struct _objc_protocol_list * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5138 | ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5139 | |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5140 | ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5141 | |
| 5142 | // Class description structures |
| 5143 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5144 | // struct _objc_ivar { |
| 5145 | // char *ivar_name; |
| 5146 | // char *ivar_type; |
| 5147 | // int ivar_offset; |
| 5148 | // } |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5149 | IvarTy = llvm::StructType::create("struct._objc_ivar", |
| 5150 | Int8PtrTy, Int8PtrTy, IntTy, NULL); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5151 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5152 | // struct _objc_ivar_list * |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5153 | IvarListTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5154 | llvm::StructType::create(VMContext, "struct._objc_ivar_list"); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5155 | IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5156 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5157 | // struct _objc_method_list * |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5158 | MethodListTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5159 | llvm::StructType::create(VMContext, "struct._objc_method_list"); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5160 | MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5161 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5162 | // struct _objc_class_extension * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5163 | ClassExtensionTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5164 | llvm::StructType::create("struct._objc_class_extension", |
| 5165 | IntTy, Int8PtrTy, PropertyListPtrTy, NULL); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5166 | ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5167 | |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5168 | ClassTy = llvm::StructType::create(VMContext, "struct._objc_class"); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5169 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5170 | // struct _objc_class { |
| 5171 | // Class isa; |
| 5172 | // Class super_class; |
| 5173 | // char *name; |
| 5174 | // long version; |
| 5175 | // long info; |
| 5176 | // long instance_size; |
| 5177 | // struct _objc_ivar_list *ivars; |
| 5178 | // struct _objc_method_list *methods; |
| 5179 | // struct _objc_cache *cache; |
| 5180 | // struct _objc_protocol_list *protocols; |
| 5181 | // char *ivar_layout; |
| 5182 | // struct _objc_class_ext *ext; |
| 5183 | // }; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5184 | ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy), |
| 5185 | llvm::PointerType::getUnqual(ClassTy), |
| 5186 | Int8PtrTy, |
| 5187 | LongTy, |
| 5188 | LongTy, |
| 5189 | LongTy, |
| 5190 | IvarListPtrTy, |
| 5191 | MethodListPtrTy, |
| 5192 | CachePtrTy, |
| 5193 | ProtocolListPtrTy, |
| 5194 | Int8PtrTy, |
| 5195 | ClassExtensionPtrTy, |
| 5196 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5197 | |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5198 | ClassPtrTy = llvm::PointerType::getUnqual(ClassTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5199 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5200 | // struct _objc_category { |
| 5201 | // char *category_name; |
| 5202 | // char *class_name; |
| 5203 | // struct _objc_method_list *instance_method; |
| 5204 | // struct _objc_method_list *class_method; |
| 5205 | // uint32_t size; // sizeof(struct _objc_category) |
| 5206 | // struct _objc_property_list *instance_properties;// category's @property |
| 5207 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5208 | CategoryTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5209 | llvm::StructType::create("struct._objc_category", |
| 5210 | Int8PtrTy, Int8PtrTy, MethodListPtrTy, |
| 5211 | MethodListPtrTy, ProtocolListPtrTy, |
| 5212 | IntTy, PropertyListPtrTy, NULL); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 5213 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5214 | // Global metadata structures |
| 5215 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 5216 | // struct _objc_symtab { |
| 5217 | // long sel_ref_cnt; |
| 5218 | // SEL *refs; |
| 5219 | // short cls_def_cnt; |
| 5220 | // short cat_def_cnt; |
| 5221 | // char *defs[cls_def_cnt + cat_def_cnt]; |
| 5222 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5223 | SymtabTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5224 | llvm::StructType::create("struct._objc_symtab", |
| 5225 | LongTy, SelectorPtrTy, ShortTy, ShortTy, |
| 5226 | llvm::ArrayType::get(Int8PtrTy, 0), NULL); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5227 | SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 5228 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 5229 | // struct _objc_module { |
| 5230 | // long version; |
| 5231 | // long size; // sizeof(struct _objc_module) |
| 5232 | // char *name; |
| 5233 | // struct _objc_symtab* symtab; |
| 5234 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5235 | ModuleTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5236 | llvm::StructType::create("struct._objc_module", |
| 5237 | LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 5238 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5239 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 5240 | // FIXME: This is the size of the setjmp buffer and should be target |
| 5241 | // specific. 18 is what's used on 32-bit X86. |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 5242 | uint64_t SetJmpBufferSize = 18; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5243 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 5244 | // Exceptions |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5245 | llvm::Type *StackPtrTy = llvm::ArrayType::get(CGM.Int8PtrTy, 4); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5246 | |
| 5247 | ExceptionDataTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5248 | llvm::StructType::create("struct._objc_exception_data", |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 5249 | llvm::ArrayType::get(CGM.Int32Ty,SetJmpBufferSize), |
| 5250 | StackPtrTy, NULL); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 5251 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 5252 | } |
| 5253 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5254 | ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5255 | : ObjCCommonTypesHelper(cgm) { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5256 | // struct _method_list_t { |
| 5257 | // uint32_t entsize; // sizeof(struct _objc_method) |
| 5258 | // uint32_t method_count; |
| 5259 | // struct _objc_method method_list[method_count]; |
| 5260 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5261 | MethodListnfABITy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5262 | llvm::StructType::create("struct.__method_list_t", IntTy, IntTy, |
| 5263 | llvm::ArrayType::get(MethodTy, 0), NULL); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5264 | // struct method_list_t * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5265 | MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5266 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5267 | // struct _protocol_t { |
| 5268 | // id isa; // NULL |
| 5269 | // const char * const protocol_name; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5270 | // const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5271 | // const struct method_list_t * const instance_methods; |
| 5272 | // const struct method_list_t * const class_methods; |
| 5273 | // const struct method_list_t *optionalInstanceMethods; |
| 5274 | // const struct method_list_t *optionalClassMethods; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5275 | // const struct _prop_list_t * properties; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5276 | // const uint32_t size; // sizeof(struct _protocol_t) |
| 5277 | // const uint32_t flags; // = 0 |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5278 | // const char ** extendedMethodTypes; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5279 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5280 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5281 | // Holder for struct _protocol_list_t * |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5282 | ProtocolListnfABITy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5283 | llvm::StructType::create(VMContext, "struct._objc_protocol_list"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5284 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5285 | ProtocolnfABITy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5286 | llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy, |
| 5287 | llvm::PointerType::getUnqual(ProtocolListnfABITy), |
| 5288 | MethodListnfABIPtrTy, MethodListnfABIPtrTy, |
| 5289 | MethodListnfABIPtrTy, MethodListnfABIPtrTy, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5290 | PropertyListPtrTy, IntTy, IntTy, Int8PtrPtrTy, |
| 5291 | NULL); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5292 | |
| 5293 | // struct _protocol_t* |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5294 | ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5295 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5296 | // struct _protocol_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5297 | // long protocol_count; // Note, this is 32/64 bit |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5298 | // struct _protocol_t *[protocol_count]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5299 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5300 | ProtocolListnfABITy->setBody(LongTy, |
| 5301 | llvm::ArrayType::get(ProtocolnfABIPtrTy, 0), |
| 5302 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5303 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5304 | // struct _objc_protocol_list* |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5305 | ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5306 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5307 | // struct _ivar_t { |
| 5308 | // unsigned long int *offset; // pointer to ivar offset location |
| 5309 | // char *name; |
| 5310 | // char *type; |
| 5311 | // uint32_t alignment; |
| 5312 | // uint32_t size; |
| 5313 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5314 | IvarnfABITy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5315 | llvm::StructType::create("struct._ivar_t", |
| 5316 | llvm::PointerType::getUnqual(LongTy), |
| 5317 | Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5318 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5319 | // struct _ivar_list_t { |
| 5320 | // uint32 entsize; // sizeof(struct _ivar_t) |
| 5321 | // uint32 count; |
| 5322 | // struct _iver_t list[count]; |
| 5323 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5324 | IvarListnfABITy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5325 | llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy, |
| 5326 | llvm::ArrayType::get(IvarnfABITy, 0), NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5327 | |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5328 | IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5329 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5330 | // struct _class_ro_t { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5331 | // uint32_t const flags; |
| 5332 | // uint32_t const instanceStart; |
| 5333 | // uint32_t const instanceSize; |
| 5334 | // uint32_t const reserved; // only when building for 64bit targets |
| 5335 | // const uint8_t * const ivarLayout; |
| 5336 | // const char *const name; |
| 5337 | // const struct _method_list_t * const baseMethods; |
| 5338 | // const struct _objc_protocol_list *const baseProtocols; |
| 5339 | // const struct _ivar_list_t *const ivars; |
| 5340 | // const uint8_t * const weakIvarLayout; |
| 5341 | // const struct _prop_list_t * const properties; |
| 5342 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5343 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5344 | // FIXME. Add 'reserved' field in 64bit abi mode! |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5345 | ClassRonfABITy = llvm::StructType::create("struct._class_ro_t", |
| 5346 | IntTy, IntTy, IntTy, Int8PtrTy, |
| 5347 | Int8PtrTy, MethodListnfABIPtrTy, |
| 5348 | ProtocolListnfABIPtrTy, |
| 5349 | IvarListnfABIPtrTy, |
| 5350 | Int8PtrTy, PropertyListPtrTy, NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5351 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5352 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5353 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 5354 | ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false) |
| 5355 | ->getPointerTo(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5356 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5357 | // struct _class_t { |
| 5358 | // struct _class_t *isa; |
| 5359 | // struct _class_t * const superclass; |
| 5360 | // void *cache; |
| 5361 | // IMP *vtable; |
| 5362 | // struct class_ro_t *ro; |
| 5363 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5364 | |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5365 | ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t"); |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5366 | ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy), |
| 5367 | llvm::PointerType::getUnqual(ClassnfABITy), |
| 5368 | CachePtrTy, |
| 5369 | llvm::PointerType::getUnqual(ImpnfABITy), |
| 5370 | llvm::PointerType::getUnqual(ClassRonfABITy), |
| 5371 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5372 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5373 | // LLVM for struct _class_t * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5374 | ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5375 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 5376 | // struct _category_t { |
| 5377 | // const char * const name; |
| 5378 | // struct _class_t *const cls; |
| 5379 | // const struct _method_list_t * const instance_methods; |
| 5380 | // const struct _method_list_t * const class_methods; |
| 5381 | // const struct _protocol_list_t * const protocols; |
| 5382 | // const struct _prop_list_t * const properties; |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 5383 | // } |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5384 | CategorynfABITy = llvm::StructType::create("struct._category_t", |
| 5385 | Int8PtrTy, ClassnfABIPtrTy, |
| 5386 | MethodListnfABIPtrTy, |
| 5387 | MethodListnfABIPtrTy, |
| 5388 | ProtocolListnfABIPtrTy, |
| 5389 | PropertyListPtrTy, |
| 5390 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5391 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 5392 | // New types for nonfragile abi messaging. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5393 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 5394 | ASTContext &Ctx = CGM.getContext(); |
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 | // MessageRefTy - LLVM for: |
| 5397 | // struct _message_ref_t { |
| 5398 | // IMP messenger; |
| 5399 | // SEL name; |
| 5400 | // }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5401 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5402 | // First the clang type for struct _message_ref_t |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 5403 | RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct, |
Daniel Dunbar | daa3ac5 | 2010-04-29 16:29:11 +0000 | [diff] [blame] | 5404 | Ctx.getTranslationUnitDecl(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 5405 | SourceLocation(), SourceLocation(), |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5406 | &Ctx.Idents.get("_message_ref_t")); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5407 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 5408 | Ctx.VoidPtrTy, 0, 0, false, ICIS_NoInit)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5409 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 5410 | Ctx.getObjCSelType(), 0, 0, false, |
| 5411 | ICIS_NoInit)); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 5412 | RD->completeDefinition(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5413 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5414 | MessageRefCTy = Ctx.getTagDeclType(RD); |
| 5415 | MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy); |
| 5416 | MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5417 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 5418 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5419 | MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5420 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 5421 | // SuperMessageRefTy - LLVM for: |
| 5422 | // struct _super_message_ref_t { |
| 5423 | // SUPER_IMP messenger; |
| 5424 | // SEL name; |
| 5425 | // }; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5426 | SuperMessageRefTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5427 | llvm::StructType::create("struct._super_message_ref_t", |
| 5428 | ImpnfABITy, SelectorPtrTy, NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5429 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 5430 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5431 | SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy); |
Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 5432 | |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5433 | |
| 5434 | // struct objc_typeinfo { |
| 5435 | // const void** vtable; // objc_ehtype_vtable + 2 |
| 5436 | // const char* name; // c++ typeinfo string |
| 5437 | // Class cls; |
| 5438 | // }; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 5439 | EHTypeTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 5440 | llvm::StructType::create("struct._objc_typeinfo", |
| 5441 | llvm::PointerType::getUnqual(Int8PtrTy), |
| 5442 | Int8PtrTy, ClassnfABIPtrTy, NULL); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5443 | EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 5444 | } |
| 5445 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5446 | llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5447 | FinishNonFragileABIModule(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5448 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5449 | return NULL; |
| 5450 | } |
| 5451 | |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 5452 | void CGObjCNonFragileABIMac:: |
| 5453 | AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container, |
| 5454 | const char *SymbolName, |
| 5455 | const char *SectionName) { |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5456 | unsigned NumClasses = Container.size(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5457 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5458 | if (!NumClasses) |
| 5459 | return; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5460 | |
Chris Lattner | 0b23971 | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 5461 | SmallVector<llvm::Constant*, 8> Symbols(NumClasses); |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5462 | for (unsigned i=0; i<NumClasses; i++) |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 5463 | Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i], |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5464 | ObjCTypes.Int8PtrTy); |
Chris Lattner | 0b23971 | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 5465 | llvm::Constant *Init = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5466 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Chris Lattner | 0b23971 | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 5467 | Symbols.size()), |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5468 | Symbols); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5469 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5470 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5471 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5472 | llvm::GlobalValue::InternalLinkage, |
| 5473 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5474 | SymbolName); |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 5475 | GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType())); |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5476 | GV->setSection(SectionName); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5477 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5478 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5479 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5480 | void CGObjCNonFragileABIMac::FinishNonFragileABIModule() { |
| 5481 | // nonfragile abi has no module definition. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5482 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5483 | // Build list of all implemented class addresses in array |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 5484 | // L_OBJC_LABEL_CLASS_$. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5485 | AddModuleClassList(DefinedClasses, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5486 | "\01L_OBJC_LABEL_CLASS_$", |
| 5487 | "__DATA, __objc_classlist, regular, no_dead_strip"); |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 5488 | |
Bill Wendling | 13562a1 | 2012-02-07 09:06:01 +0000 | [diff] [blame] | 5489 | for (unsigned i = 0, e = DefinedClasses.size(); i < e; i++) { |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 5490 | llvm::GlobalValue *IMPLGV = DefinedClasses[i]; |
| 5491 | if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage) |
| 5492 | continue; |
| 5493 | IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 5494 | } |
| 5495 | |
Bill Wendling | 13562a1 | 2012-02-07 09:06:01 +0000 | [diff] [blame] | 5496 | for (unsigned i = 0, e = DefinedMetaClasses.size(); i < e; i++) { |
Fariborz Jahanian | 0e93d25 | 2009-12-01 18:25:24 +0000 | [diff] [blame] | 5497 | llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i]; |
| 5498 | if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage) |
| 5499 | continue; |
| 5500 | IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 5501 | } |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 5502 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5503 | AddModuleClassList(DefinedNonLazyClasses, |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5504 | "\01L_OBJC_LABEL_NONLAZY_CLASS_$", |
| 5505 | "__DATA, __objc_nlclslist, regular, no_dead_strip"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5506 | |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 5507 | // Build list of all implemented category addresses in array |
| 5508 | // L_OBJC_LABEL_CATEGORY_$. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5509 | AddModuleClassList(DefinedCategories, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 5510 | "\01L_OBJC_LABEL_CATEGORY_$", |
| 5511 | "__DATA, __objc_catlist, regular, no_dead_strip"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5512 | AddModuleClassList(DefinedNonLazyCategories, |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5513 | "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$", |
| 5514 | "__DATA, __objc_nlcatlist, regular, no_dead_strip"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5515 | |
Daniel Dunbar | fce176b | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 5516 | EmitImageInfo(); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5517 | } |
| 5518 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5519 | /// isVTableDispatchedSelector - Returns true if SEL is not in the list of |
| 5520 | /// VTableDispatchMethods; false otherwise. What this means is that |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5521 | /// except for the 19 selectors in the list, we generate 32bit-style |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5522 | /// message dispatch call for all the rest. |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5523 | bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) { |
| 5524 | // At various points we've experimented with using vtable-based |
| 5525 | // dispatch for all methods. |
Daniel Dunbar | f643b9b | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 5526 | switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) { |
Daniel Dunbar | f643b9b | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 5527 | case CodeGenOptions::Legacy: |
Fariborz Jahanian | 776dbf9 | 2010-04-19 17:53:30 +0000 | [diff] [blame] | 5528 | return false; |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5529 | case CodeGenOptions::NonLegacy: |
| 5530 | return true; |
Daniel Dunbar | f643b9b | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 5531 | case CodeGenOptions::Mixed: |
| 5532 | break; |
| 5533 | } |
| 5534 | |
| 5535 | // If so, see whether this selector is in the white-list of things which must |
| 5536 | // 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] | 5537 | if (VTableDispatchMethods.empty()) { |
| 5538 | VTableDispatchMethods.insert(GetNullarySelector("alloc")); |
| 5539 | VTableDispatchMethods.insert(GetNullarySelector("class")); |
| 5540 | VTableDispatchMethods.insert(GetNullarySelector("self")); |
| 5541 | VTableDispatchMethods.insert(GetNullarySelector("isFlipped")); |
| 5542 | VTableDispatchMethods.insert(GetNullarySelector("length")); |
| 5543 | VTableDispatchMethods.insert(GetNullarySelector("count")); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5544 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5545 | // These are vtable-based if GC is disabled. |
| 5546 | // Optimistically use vtable dispatch for hybrid compiles. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5547 | if (CGM.getLangOpts().getGC() != LangOptions::GCOnly) { |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5548 | VTableDispatchMethods.insert(GetNullarySelector("retain")); |
| 5549 | VTableDispatchMethods.insert(GetNullarySelector("release")); |
| 5550 | VTableDispatchMethods.insert(GetNullarySelector("autorelease")); |
| 5551 | } |
| 5552 | |
| 5553 | VTableDispatchMethods.insert(GetUnarySelector("allocWithZone")); |
| 5554 | VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass")); |
| 5555 | VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector")); |
| 5556 | VTableDispatchMethods.insert(GetUnarySelector("objectForKey")); |
| 5557 | VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex")); |
| 5558 | VTableDispatchMethods.insert(GetUnarySelector("isEqualToString")); |
| 5559 | VTableDispatchMethods.insert(GetUnarySelector("isEqual")); |
| 5560 | |
| 5561 | // These are vtable-based if GC is enabled. |
| 5562 | // Optimistically use vtable dispatch for hybrid compiles. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5563 | if (CGM.getLangOpts().getGC() != LangOptions::NonGC) { |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5564 | VTableDispatchMethods.insert(GetNullarySelector("hash")); |
| 5565 | VTableDispatchMethods.insert(GetUnarySelector("addObject")); |
| 5566 | |
| 5567 | // "countByEnumeratingWithState:objects:count" |
| 5568 | IdentifierInfo *KeyIdents[] = { |
| 5569 | &CGM.getContext().Idents.get("countByEnumeratingWithState"), |
| 5570 | &CGM.getContext().Idents.get("objects"), |
| 5571 | &CGM.getContext().Idents.get("count") |
| 5572 | }; |
| 5573 | VTableDispatchMethods.insert( |
| 5574 | CGM.getContext().Selectors.getSelector(3, KeyIdents)); |
| 5575 | } |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5576 | } |
Daniel Dunbar | f643b9b | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 5577 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5578 | return VTableDispatchMethods.count(Sel); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5579 | } |
| 5580 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5581 | /// BuildClassRoTInitializer - generate meta-data for: |
| 5582 | /// struct _class_ro_t { |
| 5583 | /// uint32_t const flags; |
| 5584 | /// uint32_t const instanceStart; |
| 5585 | /// uint32_t const instanceSize; |
| 5586 | /// uint32_t const reserved; // only when building for 64bit targets |
| 5587 | /// const uint8_t * const ivarLayout; |
| 5588 | /// const char *const name; |
| 5589 | /// const struct _method_list_t * const baseMethods; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5590 | /// const struct _protocol_list_t *const baseProtocols; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5591 | /// const struct _ivar_list_t *const ivars; |
| 5592 | /// const uint8_t * const weakIvarLayout; |
| 5593 | /// const struct _prop_list_t * const properties; |
| 5594 | /// } |
| 5595 | /// |
| 5596 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5597 | unsigned flags, |
| 5598 | unsigned InstanceStart, |
| 5599 | unsigned InstanceSize, |
| 5600 | const ObjCImplementationDecl *ID) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5601 | std::string ClassName = ID->getNameAsString(); |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5602 | llvm::Constant *Values[10]; // 11 for 64bit targets! |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5603 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5604 | if (CGM.getLangOpts().ObjCAutoRefCount) |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5605 | flags |= NonFragileABI_Class_CompiledByARC; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5606 | |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5607 | Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
| 5608 | Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart); |
| 5609 | Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5610 | // FIXME. For 64bit targets add 0 here. |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5611 | Values[ 3] = (flags & NonFragileABI_Class_Meta) |
| 5612 | ? GetIvarLayoutName(0, ObjCTypes) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5613 | : BuildIvarLayout(ID, true); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5614 | Values[ 4] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5615 | // const struct _method_list_t * const baseMethods; |
| 5616 | std::vector<llvm::Constant*> Methods; |
| 5617 | std::string MethodListName("\01l_OBJC_$_"); |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5618 | if (flags & NonFragileABI_Class_Meta) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5619 | MethodListName += "CLASS_METHODS_" + ID->getNameAsString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5620 | for (ObjCImplementationDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5621 | i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5622 | // Class methods should always be defined. |
| 5623 | Methods.push_back(GetMethodConstant(*i)); |
| 5624 | } |
| 5625 | } else { |
| 5626 | MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5627 | for (ObjCImplementationDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5628 | i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5629 | // Instance methods should always be defined. |
| 5630 | Methods.push_back(GetMethodConstant(*i)); |
| 5631 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5632 | for (ObjCImplementationDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5633 | i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5634 | ObjCPropertyImplDecl *PID = *i; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5635 | |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 5636 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){ |
| 5637 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5638 | |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 5639 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 5640 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 5641 | Methods.push_back(C); |
| 5642 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 5643 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 5644 | Methods.push_back(C); |
| 5645 | } |
| 5646 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5647 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5648 | Values[ 5] = EmitMethodList(MethodListName, |
| 5649 | "__DATA, __objc_const", Methods); |
| 5650 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5651 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 5652 | assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5653 | Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5654 | + OID->getName(), |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 5655 | OID->all_referenced_protocol_begin(), |
| 5656 | OID->all_referenced_protocol_end()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5657 | |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5658 | if (flags & NonFragileABI_Class_Meta) { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5659 | Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5660 | Values[ 8] = GetIvarLayoutName(0, ObjCTypes); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5661 | Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5662 | } else { |
| 5663 | Values[ 7] = EmitIvarList(ID); |
| 5664 | Values[ 8] = BuildIvarLayout(ID, false); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5665 | Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(), |
| 5666 | ID, ID->getClassInterface(), ObjCTypes); |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5667 | } |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5668 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5669 | Values); |
| 5670 | llvm::GlobalVariable *CLASS_RO_GV = |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5671 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false, |
| 5672 | llvm::GlobalValue::InternalLinkage, |
| 5673 | Init, |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5674 | (flags & NonFragileABI_Class_Meta) ? |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5675 | std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName : |
| 5676 | std::string("\01l_OBJC_CLASS_RO_$_")+ClassName); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5677 | CLASS_RO_GV->setAlignment( |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 5678 | CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassRonfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5679 | CLASS_RO_GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5680 | return CLASS_RO_GV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5681 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5682 | } |
| 5683 | |
| 5684 | /// BuildClassMetaData - This routine defines that to-level meta-data |
| 5685 | /// for the given ClassName for: |
| 5686 | /// struct _class_t { |
| 5687 | /// struct _class_t *isa; |
| 5688 | /// struct _class_t * const superclass; |
| 5689 | /// void *cache; |
| 5690 | /// IMP *vtable; |
| 5691 | /// struct class_ro_t *ro; |
| 5692 | /// } |
| 5693 | /// |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5694 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5695 | std::string &ClassName, |
| 5696 | llvm::Constant *IsAGV, |
| 5697 | llvm::Constant *SuperClassGV, |
| 5698 | llvm::Constant *ClassRoGV, |
| 5699 | bool HiddenVisibility) { |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5700 | llvm::Constant *Values[] = { |
| 5701 | IsAGV, |
| 5702 | SuperClassGV, |
| 5703 | ObjCEmptyCacheVar, // &ObjCEmptyCacheVar |
| 5704 | ObjCEmptyVtableVar, // &ObjCEmptyVtableVar |
| 5705 | ClassRoGV // &CLASS_RO_GV |
| 5706 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5707 | if (!Values[1]) |
| 5708 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5709 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5710 | Values); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5711 | llvm::GlobalVariable *GV = GetClassGlobal(ClassName); |
| 5712 | GV->setInitializer(Init); |
Fariborz Jahanian | dd0db2a | 2009-01-31 01:07:39 +0000 | [diff] [blame] | 5713 | GV->setSection("__DATA, __objc_data"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5714 | GV->setAlignment( |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 5715 | CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ClassnfABITy)); |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5716 | if (HiddenVisibility) |
| 5717 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5718 | return GV; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5719 | } |
| 5720 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5721 | bool |
Fariborz Jahanian | ecfbdcb | 2009-05-21 01:03:45 +0000 | [diff] [blame] | 5722 | CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5723 | return OD->getClassMethod(GetNullarySelector("load")) != 0; |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5724 | } |
| 5725 | |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 5726 | void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 5727 | uint32_t &InstanceStart, |
| 5728 | uint32_t &InstanceSize) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5729 | const ASTRecordLayout &RL = |
Daniel Dunbar | b4c79e0 | 2009-05-04 21:26:30 +0000 | [diff] [blame] | 5730 | CGM.getContext().getASTObjCImplementationLayout(OID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5731 | |
Daniel Dunbar | 6e8575b | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 5732 | // InstanceSize is really instance end. |
Ken Dyck | ec29903 | 2011-02-11 02:20:09 +0000 | [diff] [blame] | 5733 | InstanceSize = RL.getDataSize().getQuantity(); |
Daniel Dunbar | 6e8575b | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 5734 | |
| 5735 | // If there are no fields, the start is the same as the end. |
| 5736 | if (!RL.getFieldCount()) |
| 5737 | InstanceStart = InstanceSize; |
| 5738 | else |
Ken Dyck | fb67ccd | 2011-04-14 00:43:09 +0000 | [diff] [blame] | 5739 | InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth(); |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 5740 | } |
| 5741 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5742 | void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) { |
| 5743 | std::string ClassName = ID->getNameAsString(); |
| 5744 | if (!ObjCEmptyCacheVar) { |
| 5745 | ObjCEmptyCacheVar = new llvm::GlobalVariable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5746 | CGM.getModule(), |
| 5747 | ObjCTypes.CacheTy, |
| 5748 | false, |
| 5749 | llvm::GlobalValue::ExternalLinkage, |
| 5750 | 0, |
| 5751 | "_objc_empty_cache"); |
| 5752 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5753 | ObjCEmptyVtableVar = new llvm::GlobalVariable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5754 | CGM.getModule(), |
| 5755 | ObjCTypes.ImpnfABITy, |
| 5756 | false, |
| 5757 | llvm::GlobalValue::ExternalLinkage, |
| 5758 | 0, |
| 5759 | "_objc_empty_vtable"); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5760 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5761 | assert(ID->getClassInterface() && |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5762 | "CGObjCNonFragileABIMac::GenerateClass - class is 0"); |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 5763 | // FIXME: Is this correct (that meta class size is never computed)? |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5764 | uint32_t InstanceStart = |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 5765 | CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ClassnfABITy); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5766 | uint32_t InstanceSize = InstanceStart; |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5767 | uint32_t flags = NonFragileABI_Class_Meta; |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5768 | std::string ObjCMetaClassName(getMetaclassSymbolPrefix()); |
| 5769 | std::string ObjCClassName(getClassSymbolPrefix()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5770 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5771 | llvm::GlobalVariable *SuperClassGV, *IsAGV; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5772 | |
John McCall | b03527a | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 5773 | // Build the flags for the metaclass. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5774 | bool classIsHidden = |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 5775 | ID->getClassInterface()->getVisibility() == HiddenVisibility; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5776 | if (classIsHidden) |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5777 | flags |= NonFragileABI_Class_Hidden; |
John McCall | b03527a | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 5778 | |
| 5779 | // FIXME: why is this flag set on the metaclass? |
| 5780 | // ObjC metaclasses have no fields and don't really get constructed. |
| 5781 | if (ID->hasNonZeroConstructors() || ID->hasDestructors()) { |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5782 | flags |= NonFragileABI_Class_HasCXXStructors; |
John McCall | b03527a | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 5783 | if (!ID->hasNonZeroConstructors()) |
| 5784 | flags |= NonFragileABI_Class_HasCXXDestructorOnly; |
| 5785 | } |
| 5786 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5787 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5788 | // class is root |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5789 | flags |= NonFragileABI_Class_Root; |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5790 | SuperClassGV = GetClassGlobal(ObjCClassName + ClassName); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5791 | IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5792 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5793 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 5794 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 5795 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 5796 | Root = Super; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5797 | IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString()); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5798 | if (Root->isWeakImported()) |
Fariborz Jahanian | 0e93d25 | 2009-12-01 18:25:24 +0000 | [diff] [blame] | 5799 | IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 5800 | // work on super class metadata symbol. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5801 | std::string SuperClassName = |
Fariborz Jahanian | 0e93d25 | 2009-12-01 18:25:24 +0000 | [diff] [blame] | 5802 | ObjCMetaClassName + |
| 5803 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5804 | SuperClassGV = GetClassGlobal(SuperClassName); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5805 | if (ID->getClassInterface()->getSuperClass()->isWeakImported()) |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 5806 | SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5807 | } |
| 5808 | llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags, |
| 5809 | InstanceStart, |
| 5810 | InstanceSize,ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5811 | std::string TClassName = ObjCMetaClassName + ClassName; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5812 | llvm::GlobalVariable *MetaTClass = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5813 | BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV, |
| 5814 | classIsHidden); |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 5815 | DefinedMetaClasses.push_back(MetaTClass); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5816 | |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5817 | // Metadata for the class |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5818 | flags = 0; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5819 | if (classIsHidden) |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5820 | flags |= NonFragileABI_Class_Hidden; |
John McCall | b03527a | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 5821 | |
| 5822 | if (ID->hasNonZeroConstructors() || ID->hasDestructors()) { |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5823 | flags |= NonFragileABI_Class_HasCXXStructors; |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5824 | |
John McCall | b03527a | 2012-10-17 04:53:31 +0000 | [diff] [blame] | 5825 | // Set a flag to enable a runtime optimization when a class has |
| 5826 | // fields that require destruction but which don't require |
| 5827 | // anything except zero-initialization during construction. This |
| 5828 | // is most notably true of __strong and __weak types, but you can |
| 5829 | // also imagine there being C++ types with non-trivial default |
| 5830 | // constructors that merely set all fields to null. |
| 5831 | if (!ID->hasNonZeroConstructors()) |
| 5832 | flags |= NonFragileABI_Class_HasCXXDestructorOnly; |
| 5833 | } |
| 5834 | |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 5835 | if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface())) |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5836 | flags |= NonFragileABI_Class_Exception; |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5837 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5838 | if (!ID->getClassInterface()->getSuperClass()) { |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5839 | flags |= NonFragileABI_Class_Root; |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5840 | SuperClassGV = 0; |
Chris Lattner | b7b58b1 | 2009-04-19 06:02:28 +0000 | [diff] [blame] | 5841 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5842 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 5843 | std::string RootClassName = |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5844 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5845 | SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5846 | if (ID->getClassInterface()->getSuperClass()->isWeakImported()) |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 5847 | SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5848 | } |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 5849 | GetClassSizeInfo(ID, InstanceStart, InstanceSize); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5850 | CLASS_RO_GV = BuildClassRoTInitializer(flags, |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 5851 | InstanceStart, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5852 | InstanceSize, |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 5853 | ID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5854 | |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5855 | TClassName = ObjCClassName + ClassName; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5856 | llvm::GlobalVariable *ClassMD = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5857 | BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV, |
| 5858 | classIsHidden); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 5859 | DefinedClasses.push_back(ClassMD); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5860 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5861 | // Determine if this class is also "non-lazy". |
| 5862 | if (ImplementationIsNonLazy(ID)) |
| 5863 | DefinedNonLazyClasses.push_back(ClassMD); |
| 5864 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5865 | // Force the definition of the EHType if necessary. |
John McCall | 621915c | 2012-10-17 04:53:23 +0000 | [diff] [blame] | 5866 | if (flags & NonFragileABI_Class_Exception) |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5867 | GetInterfaceEHType(ID->getClassInterface(), true); |
Fariborz Jahanian | 64089ce | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 5868 | // Make sure method definition entries are all clear for next implementation. |
| 5869 | MethodDefinitions.clear(); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5870 | } |
| 5871 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5872 | /// GenerateProtocolRef - This routine is called to generate code for |
| 5873 | /// a protocol reference expression; as in: |
| 5874 | /// @code |
| 5875 | /// @protocol(Proto1); |
| 5876 | /// @endcode |
| 5877 | /// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1 |
| 5878 | /// which will hold address of the protocol meta-data. |
| 5879 | /// |
| 5880 | llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5881 | const ObjCProtocolDecl *PD) { |
| 5882 | |
Fariborz Jahanian | 960cd06 | 2009-04-10 18:47:34 +0000 | [diff] [blame] | 5883 | // This routine is called for @protocol only. So, we must build definition |
| 5884 | // of protocol's meta-data (not a reference to it!) |
| 5885 | // |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5886 | llvm::Constant *Init = |
| 5887 | llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD), |
Douglas Gregor | 4c86fdb | 2012-01-17 18:36:30 +0000 | [diff] [blame] | 5888 | ObjCTypes.getExternalProtocolPtrTy()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5889 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5890 | std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_"); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5891 | ProtocolName += PD->getName(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5892 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5893 | llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName); |
| 5894 | if (PTGV) |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 5895 | return Builder.CreateLoad(PTGV); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5896 | PTGV = new llvm::GlobalVariable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5897 | CGM.getModule(), |
| 5898 | Init->getType(), false, |
| 5899 | llvm::GlobalValue::WeakAnyLinkage, |
| 5900 | Init, |
| 5901 | ProtocolName); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5902 | PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip"); |
| 5903 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5904 | CGM.AddUsedGlobal(PTGV); |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 5905 | return Builder.CreateLoad(PTGV); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5906 | } |
| 5907 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5908 | /// GenerateCategory - Build metadata for a category implementation. |
| 5909 | /// struct _category_t { |
| 5910 | /// const char * const name; |
| 5911 | /// struct _class_t *const cls; |
| 5912 | /// const struct _method_list_t * const instance_methods; |
| 5913 | /// const struct _method_list_t * const class_methods; |
| 5914 | /// const struct _protocol_list_t * const protocols; |
| 5915 | /// const struct _prop_list_t * const properties; |
| 5916 | /// } |
| 5917 | /// |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5918 | void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5919 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5920 | const char *Prefix = "\01l_OBJC_$_CATEGORY_"; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5921 | std::string ExtCatName(Prefix + Interface->getNameAsString()+ |
| 5922 | "_$_" + OCD->getNameAsString()); |
| 5923 | std::string ExtClassName(getClassSymbolPrefix() + |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5924 | Interface->getNameAsString()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5925 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5926 | llvm::Constant *Values[6]; |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5927 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 5928 | // meta-class entry symbol |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5929 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5930 | if (Interface->isWeakImported()) |
Fariborz Jahanian | 2cdcc4c | 2009-11-17 22:02:21 +0000 | [diff] [blame] | 5931 | ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
| 5932 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5933 | Values[1] = ClassGV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5934 | std::vector<llvm::Constant*> Methods; |
| 5935 | std::string MethodListName(Prefix); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5936 | MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() + |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5937 | "_$_" + OCD->getNameAsString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5938 | |
| 5939 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5940 | i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5941 | // Instance methods should always be defined. |
| 5942 | Methods.push_back(GetMethodConstant(*i)); |
| 5943 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5944 | |
| 5945 | Values[2] = EmitMethodList(MethodListName, |
| 5946 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5947 | Methods); |
| 5948 | |
| 5949 | MethodListName = Prefix; |
| 5950 | MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" + |
| 5951 | OCD->getNameAsString(); |
| 5952 | Methods.clear(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5953 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5954 | i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5955 | // Class methods should always be defined. |
| 5956 | Methods.push_back(GetMethodConstant(*i)); |
| 5957 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5958 | |
| 5959 | Values[3] = EmitMethodList(MethodListName, |
| 5960 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5961 | Methods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5962 | const ObjCCategoryDecl *Category = |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 5963 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5964 | if (Category) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 5965 | SmallString<256> ExtName; |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5966 | llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_" |
| 5967 | << OCD->getName(); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5968 | Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5969 | + Interface->getName() + "_$_" |
| 5970 | + Category->getName(), |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5971 | Category->protocol_begin(), |
| 5972 | Category->protocol_end()); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5973 | Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(), |
| 5974 | OCD, Category, ObjCTypes); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5975 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5976 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 5977 | Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5978 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5979 | |
| 5980 | llvm::Constant *Init = |
| 5981 | llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy, |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5982 | Values); |
| 5983 | llvm::GlobalVariable *GCATV |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5984 | = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy, |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5985 | false, |
| 5986 | llvm::GlobalValue::InternalLinkage, |
| 5987 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5988 | ExtCatName); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5989 | GCATV->setAlignment( |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 5990 | CGM.getDataLayout().getABITypeAlignment(ObjCTypes.CategorynfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5991 | GCATV->setSection("__DATA, __objc_const"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5992 | CGM.AddUsedGlobal(GCATV); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5993 | DefinedCategories.push_back(GCATV); |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5994 | |
| 5995 | // Determine if this category is also "non-lazy". |
| 5996 | if (ImplementationIsNonLazy(OCD)) |
| 5997 | DefinedNonLazyCategories.push_back(GCATV); |
Fariborz Jahanian | 64089ce | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 5998 | // method definition entries must be clear for next implementation. |
| 5999 | MethodDefinitions.clear(); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 6000 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 6001 | |
| 6002 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 6003 | /// given method if it has been defined. The result is null if the |
| 6004 | /// method has not been defined. The return value has type MethodPtrTy. |
| 6005 | llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6006 | const ObjCMethodDecl *MD) { |
Argyrios Kyrtzidis | 9d50c06 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 6007 | llvm::Function *Fn = GetMethodDefinition(MD); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 6008 | if (!Fn) |
| 6009 | return 0; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6010 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 6011 | llvm::Constant *Method[] = { |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 6012 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 6013 | ObjCTypes.SelectorPtrTy), |
| 6014 | GetMethodVarType(MD), |
| 6015 | llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy) |
| 6016 | }; |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 6017 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 6018 | } |
| 6019 | |
| 6020 | /// EmitMethodList - Build meta-data for method declarations |
| 6021 | /// struct _method_list_t { |
| 6022 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 6023 | /// uint32_t method_count; |
| 6024 | /// struct _objc_method method_list[method_count]; |
| 6025 | /// } |
| 6026 | /// |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 6027 | llvm::Constant * |
| 6028 | CGObjCNonFragileABIMac::EmitMethodList(Twine Name, |
| 6029 | const char *Section, |
| 6030 | ArrayRef<llvm::Constant*> Methods) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 6031 | // Return null for empty list. |
| 6032 | if (Methods.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 6033 | return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6034 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 6035 | llvm::Constant *Values[3]; |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 6036 | // sizeof(struct _objc_method) |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6037 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.MethodTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 6038 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 6039 | // method_count |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 6040 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 6041 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 6042 | Methods.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 6043 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 6044 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6045 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 6046 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6047 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 6048 | llvm::GlobalValue::InternalLinkage, Init, Name); |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6049 | GV->setAlignment(CGM.getDataLayout().getABITypeAlignment(Init->getType())); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 6050 | GV->setSection(Section); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 6051 | CGM.AddUsedGlobal(GV); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 6052 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 6053 | } |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6054 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 6055 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 6056 | /// the given ivar. |
Daniel Dunbar | e83be12 | 2010-04-02 21:14:02 +0000 | [diff] [blame] | 6057 | llvm::GlobalVariable * |
| 6058 | CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID, |
| 6059 | const ObjCIvarDecl *Ivar) { |
| 6060 | const ObjCInterfaceDecl *Container = Ivar->getContainingInterface(); |
Daniel Dunbar | a81419d | 2009-05-05 00:36:57 +0000 | [diff] [blame] | 6061 | std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() + |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 6062 | '.' + Ivar->getNameAsString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6063 | llvm::GlobalVariable *IvarOffsetGV = |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 6064 | CGM.getModule().getGlobalVariable(Name); |
| 6065 | if (!IvarOffsetGV) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6066 | IvarOffsetGV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6067 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 6068 | false, |
| 6069 | llvm::GlobalValue::ExternalLinkage, |
| 6070 | 0, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6071 | Name); |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 6072 | return IvarOffsetGV; |
| 6073 | } |
| 6074 | |
Daniel Dunbar | e83be12 | 2010-04-02 21:14:02 +0000 | [diff] [blame] | 6075 | llvm::Constant * |
| 6076 | CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
| 6077 | const ObjCIvarDecl *Ivar, |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 6078 | unsigned long int Offset) { |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 6079 | llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6080 | IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy, |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 6081 | Offset)); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 6082 | IvarOffsetGV->setAlignment( |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6083 | CGM.getDataLayout().getABITypeAlignment(ObjCTypes.LongTy)); |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 6084 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 6085 | // FIXME: This matches gcc, but shouldn't the visibility be set on the use as |
| 6086 | // well (i.e., in ObjCIvarOffsetVariable). |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 6087 | if (Ivar->getAccessControl() == ObjCIvarDecl::Private || |
| 6088 | Ivar->getAccessControl() == ObjCIvarDecl::Package || |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 6089 | ID->getVisibility() == HiddenVisibility) |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 6090 | IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 6091 | else |
Fariborz Jahanian | 77c9fd2 | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 6092 | IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Bill Wendling | 1f38251 | 2011-05-04 21:37:25 +0000 | [diff] [blame] | 6093 | IvarOffsetGV->setSection("__DATA, __objc_ivar"); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 6094 | return IvarOffsetGV; |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 6095 | } |
| 6096 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6097 | /// EmitIvarList - Emit the ivar list for the given |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6098 | /// implementation. The return value has type |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6099 | /// IvarListnfABIPtrTy. |
| 6100 | /// struct _ivar_t { |
| 6101 | /// unsigned long int *offset; // pointer to ivar offset location |
| 6102 | /// char *name; |
| 6103 | /// char *type; |
| 6104 | /// uint32_t alignment; |
| 6105 | /// uint32_t size; |
| 6106 | /// } |
| 6107 | /// struct _ivar_list_t { |
| 6108 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 6109 | /// uint32 count; |
| 6110 | /// struct _iver_t list[count]; |
| 6111 | /// } |
| 6112 | /// |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 6113 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6114 | llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6115 | const ObjCImplementationDecl *ID) { |
| 6116 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 6117 | std::vector<llvm::Constant*> Ivars; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6118 | |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 6119 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6120 | assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6121 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 6122 | // FIXME. Consolidate this with similar code in GenerateClass. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6123 | |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 6124 | for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin(); |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 6125 | IVD; IVD = IVD->getNextIvar()) { |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 6126 | // Ignore unnamed bit-fields. |
| 6127 | if (!IVD->getDeclName()) |
| 6128 | continue; |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 6129 | llvm::Constant *Ivar[5]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6130 | Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD, |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 6131 | ComputeIvarBaseOffset(CGM, ID, IVD)); |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 6132 | Ivar[1] = GetMethodVarName(IVD->getIdentifier()); |
| 6133 | Ivar[2] = GetMethodVarType(IVD); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6134 | llvm::Type *FieldTy = |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 6135 | CGM.getTypes().ConvertTypeForMem(IVD->getType()); |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6136 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(FieldTy); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6137 | unsigned Align = CGM.getContext().getPreferredTypeAlign( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6138 | IVD->getType().getTypePtr()) >> 3; |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6139 | Align = llvm::Log2_32(Align); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 6140 | Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align); |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 6141 | // NOTE. Size of a bitfield does not match gcc's, because of the |
| 6142 | // way bitfields are treated special in each. But I am told that |
| 6143 | // 'size' for bitfield ivars is ignored by the runtime so it does |
| 6144 | // not matter. If it matters, there is enough info to get the |
| 6145 | // bitfield right! |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 6146 | Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 6147 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar)); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6148 | } |
| 6149 | // Return null for empty list. |
| 6150 | if (Ivars.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 6151 | return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 6152 | |
| 6153 | llvm::Constant *Values[3]; |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6154 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(ObjCTypes.IvarnfABITy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 6155 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 6156 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 6157 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6158 | Ivars.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 6159 | Values[2] = llvm::ConstantArray::get(AT, Ivars); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 6160 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6161 | const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_"; |
| 6162 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6163 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 6164 | llvm::GlobalValue::InternalLinkage, |
| 6165 | Init, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6166 | Prefix + OID->getName()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 6167 | GV->setAlignment( |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6168 | CGM.getDataLayout().getABITypeAlignment(Init->getType())); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 6169 | GV->setSection("__DATA, __objc_const"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6170 | |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 6171 | CGM.AddUsedGlobal(GV); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 6172 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6173 | } |
| 6174 | |
| 6175 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6176 | const ObjCProtocolDecl *PD) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6177 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6178 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6179 | if (!Entry) { |
| 6180 | // We use the initializer as a marker of whether this is a forward |
| 6181 | // reference or not. At module finalization we add the empty |
| 6182 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6183 | Entry = |
| 6184 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false, |
| 6185 | llvm::GlobalValue::ExternalLinkage, |
| 6186 | 0, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6187 | "\01l_OBJC_PROTOCOL_$_" + PD->getName()); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6188 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6189 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6190 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6191 | return Entry; |
| 6192 | } |
| 6193 | |
| 6194 | /// GetOrEmitProtocol - Generate the protocol meta-data: |
| 6195 | /// @code |
| 6196 | /// struct _protocol_t { |
| 6197 | /// id isa; // NULL |
| 6198 | /// const char * const protocol_name; |
| 6199 | /// const struct _protocol_list_t * protocol_list; // super protocols |
| 6200 | /// const struct method_list_t * const instance_methods; |
| 6201 | /// const struct method_list_t * const class_methods; |
| 6202 | /// const struct method_list_t *optionalInstanceMethods; |
| 6203 | /// const struct method_list_t *optionalClassMethods; |
| 6204 | /// const struct _prop_list_t * properties; |
| 6205 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 6206 | /// const uint32_t flags; // = 0 |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6207 | /// const char ** extendedMethodTypes; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6208 | /// } |
| 6209 | /// @endcode |
| 6210 | /// |
| 6211 | |
| 6212 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6213 | const ObjCProtocolDecl *PD) { |
John McCall | 50651b9 | 2012-03-30 21:29:05 +0000 | [diff] [blame] | 6214 | llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6215 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6216 | // Early exit if a defining object has already been generated. |
| 6217 | if (Entry && Entry->hasInitializer()) |
| 6218 | return Entry; |
| 6219 | |
Douglas Gregor | 1d784b2 | 2012-01-01 19:51:50 +0000 | [diff] [blame] | 6220 | // Use the protocol definition, if there is one. |
| 6221 | if (const ObjCProtocolDecl *Def = PD->getDefinition()) |
| 6222 | PD = Def; |
| 6223 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6224 | // Construct method lists. |
| 6225 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 6226 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6227 | std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6228 | for (ObjCProtocolDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 6229 | i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6230 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 6231 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 6232 | if (!C) |
| 6233 | return GetOrEmitProtocolRef(PD); |
| 6234 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6235 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 6236 | OptInstanceMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6237 | OptMethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6238 | } else { |
| 6239 | InstanceMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6240 | MethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6241 | } |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6242 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6243 | |
| 6244 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 6245 | i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6246 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 6247 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 6248 | if (!C) |
| 6249 | return GetOrEmitProtocolRef(PD); |
| 6250 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6251 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 6252 | OptClassMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6253 | OptMethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6254 | } else { |
| 6255 | ClassMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6256 | MethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6257 | } |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6258 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6259 | |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6260 | MethodTypesExt.insert(MethodTypesExt.end(), |
| 6261 | OptMethodTypesExt.begin(), OptMethodTypesExt.end()); |
| 6262 | |
| 6263 | llvm::Constant *Values[11]; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6264 | // isa is NULL |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 6265 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6266 | Values[1] = GetClassName(PD->getIdentifier()); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6267 | Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(), |
| 6268 | PD->protocol_begin(), |
| 6269 | PD->protocol_end()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6270 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 6271 | Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6272 | + PD->getName(), |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6273 | "__DATA, __objc_const", |
| 6274 | InstanceMethods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6275 | Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_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 | ClassMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 6279 | Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_" |
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 | OptInstanceMethods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6283 | Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_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 | OptClassMethods); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6287 | Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(), |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6288 | 0, PD, ObjCTypes); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6289 | uint32_t Size = |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6290 | CGM.getDataLayout().getTypeAllocSize(ObjCTypes.ProtocolnfABITy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 6291 | Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 6292 | Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 6293 | Values[10] = EmitProtocolMethodTypes("\01l_OBJC_$_PROTOCOL_METHOD_TYPES_" |
| 6294 | + PD->getName(), |
| 6295 | MethodTypesExt, ObjCTypes); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 6296 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6297 | Values); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6298 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6299 | if (Entry) { |
| 6300 | // Already created, fix the linkage and update the initializer. |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 6301 | Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6302 | Entry->setInitializer(Init); |
| 6303 | } else { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6304 | Entry = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6305 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, |
| 6306 | false, llvm::GlobalValue::WeakAnyLinkage, Init, |
| 6307 | "\01l_OBJC_PROTOCOL_$_" + PD->getName()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 6308 | Entry->setAlignment( |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6309 | CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABITy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6310 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
John McCall | 50651b9 | 2012-03-30 21:29:05 +0000 | [diff] [blame] | 6311 | |
| 6312 | Protocols[PD->getIdentifier()] = Entry; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6313 | } |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 6314 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 6315 | CGM.AddUsedGlobal(Entry); |
| 6316 | |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 6317 | // Use this protocol meta-data to build protocol list table in section |
| 6318 | // __DATA, __objc_protolist |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6319 | llvm::GlobalVariable *PTGV = |
| 6320 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy, |
| 6321 | false, llvm::GlobalValue::WeakAnyLinkage, Entry, |
| 6322 | "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 6323 | PTGV->setAlignment( |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6324 | CGM.getDataLayout().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy)); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 6325 | PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip"); |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 6326 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 6327 | CGM.AddUsedGlobal(PTGV); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6328 | return Entry; |
| 6329 | } |
| 6330 | |
| 6331 | /// EmitProtocolList - Generate protocol list meta-data: |
| 6332 | /// @code |
| 6333 | /// struct _protocol_list_t { |
| 6334 | /// long protocol_count; // Note, this is 32/64 bit |
| 6335 | /// struct _protocol_t[protocol_count]; |
| 6336 | /// } |
| 6337 | /// @endcode |
| 6338 | /// |
| 6339 | llvm::Constant * |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6340 | CGObjCNonFragileABIMac::EmitProtocolList(Twine Name, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6341 | ObjCProtocolDecl::protocol_iterator begin, |
| 6342 | ObjCProtocolDecl::protocol_iterator end) { |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame^] | 6343 | SmallVector<llvm::Constant *, 16> ProtocolRefs; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6344 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6345 | // Just return null for empty protocol lists |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6346 | if (begin == end) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 6347 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6348 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 6349 | // FIXME: We shouldn't need to do this lookup here, should we? |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 6350 | SmallString<256> TmpName; |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6351 | Name.toVector(TmpName); |
| 6352 | llvm::GlobalVariable *GV = |
| 6353 | CGM.getModule().getGlobalVariable(TmpName.str(), true); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6354 | if (GV) |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6355 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6356 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 6357 | for (; begin != end; ++begin) |
| 6358 | ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented??? |
| 6359 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6360 | // This list is null terminated. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 6361 | ProtocolRefs.push_back(llvm::Constant::getNullValue( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6362 | ObjCTypes.ProtocolnfABIPtrTy)); |
| 6363 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 6364 | llvm::Constant *Values[2]; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 6365 | Values[0] = |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 6366 | llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6367 | Values[1] = |
Bill Wendling | 3964e62 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 6368 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy, |
| 6369 | ProtocolRefs.size()), |
| 6370 | ProtocolRefs); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6371 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 6372 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6373 | GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6374 | llvm::GlobalValue::InternalLinkage, |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 6375 | Init, Name); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6376 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 6377 | GV->setAlignment( |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6378 | CGM.getDataLayout().getABITypeAlignment(Init->getType())); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 6379 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6380 | return llvm::ConstantExpr::getBitCast(GV, |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 6381 | ObjCTypes.ProtocolListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 6382 | } |
| 6383 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 6384 | /// GetMethodDescriptionConstant - This routine build following meta-data: |
| 6385 | /// struct _objc_method { |
| 6386 | /// SEL _cmd; |
| 6387 | /// char *method_type; |
| 6388 | /// char *_imp; |
| 6389 | /// } |
| 6390 | |
| 6391 | llvm::Constant * |
| 6392 | CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 6393 | llvm::Constant *Desc[3]; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 6394 | Desc[0] = |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6395 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 6396 | ObjCTypes.SelectorPtrTy); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 6397 | Desc[1] = GetMethodVarType(MD); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 6398 | if (!Desc[1]) |
| 6399 | return 0; |
| 6400 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 6401 | // Protocol methods have no implementation. So, this entry is always NULL. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 6402 | Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 6403 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 6404 | } |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 6405 | |
| 6406 | /// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference. |
| 6407 | /// This code gen. amounts to generating code for: |
| 6408 | /// @code |
| 6409 | /// (type *)((char *)base + _OBJC_IVAR_$_.ivar; |
| 6410 | /// @encode |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6411 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 6412 | LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar( |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 6413 | CodeGen::CodeGenFunction &CGF, |
| 6414 | QualType ObjectTy, |
| 6415 | llvm::Value *BaseValue, |
| 6416 | const ObjCIvarDecl *Ivar, |
| 6417 | unsigned CVRQualifiers) { |
| 6418 | ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface(); |
Fariborz Jahanian | cd285d0 | 2012-02-20 22:42:22 +0000 | [diff] [blame] | 6419 | llvm::Value *Offset = EmitIvarOffset(CGF, ID, Ivar); |
| 6420 | if (llvm::LoadInst *LI = dyn_cast<llvm::LoadInst>(Offset)) |
| 6421 | LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"), |
| 6422 | llvm::MDNode::get(VMContext, |
| 6423 | ArrayRef<llvm::Value*>())); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 6424 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
Fariborz Jahanian | cd285d0 | 2012-02-20 22:42:22 +0000 | [diff] [blame] | 6425 | Offset); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 6426 | } |
| 6427 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 6428 | llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6429 | CodeGen::CodeGenFunction &CGF, |
| 6430 | const ObjCInterfaceDecl *Interface, |
| 6431 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 2da84ff | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 6432 | return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar"); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 6433 | } |
| 6434 | |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6435 | static void appendSelectorForMessageRefTable(std::string &buffer, |
| 6436 | Selector selector) { |
| 6437 | if (selector.isUnarySelector()) { |
| 6438 | buffer += selector.getNameForSlot(0); |
| 6439 | return; |
| 6440 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6441 | |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6442 | for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) { |
| 6443 | buffer += selector.getNameForSlot(i); |
| 6444 | buffer += '_'; |
| 6445 | } |
| 6446 | } |
| 6447 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6448 | /// Emit a "v-table" message send. We emit a weak hidden-visibility |
| 6449 | /// struct, initially containing the selector pointer and a pointer to |
| 6450 | /// a "fixup" variant of the appropriate objc_msgSend. To call, we |
| 6451 | /// load and call the function pointer, passing the address of the |
| 6452 | /// struct as the second parameter. The runtime determines whether |
| 6453 | /// the selector is currently emitted using vtable dispatch; if so, it |
| 6454 | /// substitutes a stub function which simply tail-calls through the |
| 6455 | /// appropriate vtable slot, and if not, it substitues a stub function |
| 6456 | /// which tail-calls objc_msgSend. Both stubs adjust the selector |
| 6457 | /// argument to correctly point to the selector. |
| 6458 | RValue |
| 6459 | CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF, |
| 6460 | ReturnValueSlot returnSlot, |
| 6461 | QualType resultType, |
| 6462 | Selector selector, |
| 6463 | llvm::Value *arg0, |
| 6464 | QualType arg0Type, |
| 6465 | bool isSuper, |
| 6466 | const CallArgList &formalArgs, |
| 6467 | const ObjCMethodDecl *method) { |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6468 | // Compute the actual arguments. |
| 6469 | CallArgList args; |
| 6470 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6471 | // First argument: the receiver / super-call structure. |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6472 | if (!isSuper) |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6473 | arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy); |
| 6474 | args.add(RValue::get(arg0), arg0Type); |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6475 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6476 | // Second argument: a pointer to the message ref structure. Leave |
| 6477 | // the actual argument value blank for now. |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6478 | args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy); |
| 6479 | |
| 6480 | args.insert(args.end(), formalArgs.begin(), formalArgs.end()); |
| 6481 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 6482 | MessageSendInfo MSI = getMessageSendInfo(method, resultType, args); |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6483 | |
John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 6484 | NullReturnState nullReturn; |
| 6485 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6486 | // Find the function to call and the mangled name for the message |
| 6487 | // ref structure. Using a different mangled name wouldn't actually |
| 6488 | // be a problem; it would just be a waste. |
| 6489 | // |
| 6490 | // The runtime currently never uses vtable dispatch for anything |
| 6491 | // except normal, non-super message-sends. |
| 6492 | // FIXME: don't use this for that. |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6493 | llvm::Constant *fn = 0; |
| 6494 | std::string messageRefName("\01l_"); |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 6495 | if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) { |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6496 | if (isSuper) { |
| 6497 | fn = ObjCTypes.getMessageSendSuper2StretFixupFn(); |
| 6498 | messageRefName += "objc_msgSendSuper2_stret_fixup"; |
Chris Lattner | 9b7d670 | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 6499 | } else { |
John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 6500 | nullReturn.init(CGF, arg0); |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6501 | fn = ObjCTypes.getMessageSendStretFixupFn(); |
| 6502 | messageRefName += "objc_msgSend_stret_fixup"; |
Chris Lattner | 9b7d670 | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 6503 | } |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6504 | } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) { |
| 6505 | fn = ObjCTypes.getMessageSendFpretFixupFn(); |
| 6506 | messageRefName += "objc_msgSend_fpret_fixup"; |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 6507 | } else { |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6508 | if (isSuper) { |
| 6509 | fn = ObjCTypes.getMessageSendSuper2FixupFn(); |
| 6510 | messageRefName += "objc_msgSendSuper2_fixup"; |
Chris Lattner | 9b7d670 | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 6511 | } else { |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6512 | fn = ObjCTypes.getMessageSendFixupFn(); |
| 6513 | messageRefName += "objc_msgSend_fixup"; |
Chris Lattner | 9b7d670 | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 6514 | } |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 6515 | } |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6516 | assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend"); |
| 6517 | messageRefName += '_'; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6518 | |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6519 | // Append the selector name, except use underscores anywhere we |
| 6520 | // would have used colons. |
| 6521 | appendSelectorForMessageRefTable(messageRefName, selector); |
| 6522 | |
| 6523 | llvm::GlobalVariable *messageRef |
| 6524 | = CGM.getModule().getGlobalVariable(messageRefName); |
| 6525 | if (!messageRef) { |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6526 | // Build the message ref structure. |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6527 | llvm::Constant *values[] = { fn, GetMethodVarName(selector) }; |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 6528 | llvm::Constant *init = llvm::ConstantStruct::getAnon(values); |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6529 | messageRef = new llvm::GlobalVariable(CGM.getModule(), |
| 6530 | init->getType(), |
| 6531 | /*constant*/ false, |
| 6532 | llvm::GlobalValue::WeakAnyLinkage, |
| 6533 | init, |
| 6534 | messageRefName); |
| 6535 | messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 6536 | messageRef->setAlignment(16); |
| 6537 | messageRef->setSection("__DATA, __objc_msgrefs, coalesced"); |
| 6538 | } |
Fariborz Jahanian | 3c52d36 | 2012-01-30 23:39:30 +0000 | [diff] [blame] | 6539 | |
| 6540 | bool requiresnullCheck = false; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6541 | if (CGM.getLangOpts().ObjCAutoRefCount && method) |
Fariborz Jahanian | 3c52d36 | 2012-01-30 23:39:30 +0000 | [diff] [blame] | 6542 | for (ObjCMethodDecl::param_const_iterator i = method->param_begin(), |
| 6543 | e = method->param_end(); i != e; ++i) { |
| 6544 | const ParmVarDecl *ParamDecl = (*i); |
| 6545 | if (ParamDecl->hasAttr<NSConsumedAttr>()) { |
| 6546 | if (!nullReturn.NullBB) |
| 6547 | nullReturn.init(CGF, arg0); |
| 6548 | requiresnullCheck = true; |
| 6549 | break; |
| 6550 | } |
| 6551 | } |
| 6552 | |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6553 | llvm::Value *mref = |
| 6554 | CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy); |
| 6555 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6556 | // Update the message ref argument. |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6557 | args[1].RV = RValue::get(mref); |
| 6558 | |
| 6559 | // Load the function to call from the message ref table. |
| 6560 | llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0); |
| 6561 | callee = CGF.Builder.CreateLoad(callee, "msgSend_fn"); |
| 6562 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 6563 | callee = CGF.Builder.CreateBitCast(callee, MSI.MessengerType); |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 6564 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 6565 | RValue result = CGF.EmitCall(MSI.CallInfo, callee, returnSlot, args); |
Fariborz Jahanian | 3c52d36 | 2012-01-30 23:39:30 +0000 | [diff] [blame] | 6566 | return nullReturn.complete(CGF, result, resultType, formalArgs, |
| 6567 | requiresnullCheck ? method : 0); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 6568 | } |
| 6569 | |
| 6570 | /// Generate code for a message send expression in the nonfragile abi. |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 6571 | CodeGen::RValue |
| 6572 | CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 6573 | ReturnValueSlot Return, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 6574 | QualType ResultType, |
| 6575 | Selector Sel, |
| 6576 | llvm::Value *Receiver, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 6577 | const CallArgList &CallArgs, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 6578 | const ObjCInterfaceDecl *Class, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 6579 | const ObjCMethodDecl *Method) { |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6580 | return isVTableDispatchedSelector(Sel) |
| 6581 | ? EmitVTableMessageSend(CGF, Return, ResultType, Sel, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6582 | Receiver, CGF.getContext().getObjCIdType(), |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6583 | false, CallArgs, Method) |
| 6584 | : EmitMessageSend(CGF, Return, ResultType, |
| 6585 | EmitSelector(CGF.Builder, 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, ObjCTypes); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 6588 | } |
| 6589 | |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 6590 | llvm::GlobalVariable * |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 6591 | CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 6592 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 6593 | |
Daniel Dunbar | dfff230 | 2009-03-02 05:18:14 +0000 | [diff] [blame] | 6594 | if (!GV) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6595 | GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6596 | false, llvm::GlobalValue::ExternalLinkage, |
| 6597 | 0, Name); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 6598 | } |
| 6599 | |
| 6600 | return GV; |
| 6601 | } |
| 6602 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6603 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder, |
| 6604 | IdentifierInfo *II) { |
| 6605 | llvm::GlobalVariable *&Entry = ClassReferences[II]; |
| 6606 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 6607 | if (!Entry) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6608 | std::string ClassName(getClassSymbolPrefix() + II->getName().str()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 6609 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6610 | Entry = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6611 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, |
| 6612 | false, llvm::GlobalValue::InternalLinkage, |
| 6613 | ClassGV, |
| 6614 | "\01L_OBJC_CLASSLIST_REFERENCES_$_"); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 6615 | Entry->setAlignment( |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6616 | CGM.getDataLayout().getABITypeAlignment( |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6617 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6618 | Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 6619 | CGM.AddUsedGlobal(Entry); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6620 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6621 | |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 6622 | return Builder.CreateLoad(Entry); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6623 | } |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 6624 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6625 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder, |
| 6626 | const ObjCInterfaceDecl *ID) { |
| 6627 | return EmitClassRefFromId(Builder, ID->getIdentifier()); |
| 6628 | } |
| 6629 | |
| 6630 | llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef( |
| 6631 | CGBuilderTy &Builder) { |
| 6632 | IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool"); |
| 6633 | return EmitClassRefFromId(Builder, II); |
| 6634 | } |
| 6635 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6636 | llvm::Value * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6637 | CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6638 | const ObjCInterfaceDecl *ID) { |
| 6639 | llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6640 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6641 | if (!Entry) { |
| 6642 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
| 6643 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6644 | Entry = |
| 6645 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6646 | false, llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6647 | ClassGV, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6648 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_"); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6649 | Entry->setAlignment( |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6650 | CGM.getDataLayout().getABITypeAlignment( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6651 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6652 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 6653 | CGM.AddUsedGlobal(Entry); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 6654 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6655 | |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 6656 | return Builder.CreateLoad(Entry); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 6657 | } |
| 6658 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6659 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 6660 | /// meta-data |
| 6661 | /// |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6662 | llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder, |
| 6663 | const ObjCInterfaceDecl *ID) { |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6664 | llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()]; |
| 6665 | if (Entry) |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 6666 | return Builder.CreateLoad(Entry); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6667 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 6668 | std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 6669 | llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6670 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6671 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6672 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6673 | MetaClassGV, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6674 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_"); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6675 | Entry->setAlignment( |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6676 | CGM.getDataLayout().getABITypeAlignment( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6677 | ObjCTypes.ClassnfABIPtrTy)); |
| 6678 | |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 6679 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 6680 | CGM.AddUsedGlobal(Entry); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6681 | |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 6682 | return Builder.CreateLoad(Entry); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6683 | } |
| 6684 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 6685 | /// GetClass - Return a reference to the class for the given interface |
| 6686 | /// decl. |
| 6687 | llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder, |
| 6688 | const ObjCInterfaceDecl *ID) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 6689 | if (ID->isWeakImported()) { |
Fariborz Jahanian | 269f8bc | 2009-11-17 22:42:00 +0000 | [diff] [blame] | 6690 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
| 6691 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
| 6692 | ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
| 6693 | } |
| 6694 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 6695 | return EmitClassRef(Builder, ID); |
| 6696 | } |
| 6697 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6698 | /// Generates a message send where the super is the receiver. This is |
| 6699 | /// a message send to self with special delivery semantics indicating |
| 6700 | /// which class's method should be called. |
| 6701 | CodeGen::RValue |
| 6702 | CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 6703 | ReturnValueSlot Return, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6704 | QualType ResultType, |
| 6705 | Selector Sel, |
| 6706 | const ObjCInterfaceDecl *Class, |
| 6707 | bool isCategoryImpl, |
| 6708 | llvm::Value *Receiver, |
| 6709 | bool IsClassMessage, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 6710 | const CodeGen::CallArgList &CallArgs, |
| 6711 | const ObjCMethodDecl *Method) { |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6712 | // ... |
| 6713 | // Create and init a super structure; this is a (receiver, class) |
| 6714 | // pair we will pass to objc_msgSendSuper. |
| 6715 | llvm::Value *ObjCSuper = |
John McCall | 604da29 | 2011-03-04 08:00:29 +0000 | [diff] [blame] | 6716 | CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6717 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6718 | llvm::Value *ReceiverAsObject = |
| 6719 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 6720 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 6721 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6722 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6723 | // 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] | 6724 | llvm::Value *Target; |
Fariborz Jahanian | b9966bd | 2012-10-10 23:11:18 +0000 | [diff] [blame] | 6725 | if (IsClassMessage) |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 6726 | Target = EmitMetaClassRef(CGF.Builder, Class); |
Fariborz Jahanian | b9966bd | 2012-10-10 23:11:18 +0000 | [diff] [blame] | 6727 | else |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6728 | Target = EmitSuperClassRef(CGF.Builder, Class); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6729 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 6730 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext and |
| 6731 | // ObjCTypes types. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6732 | llvm::Type *ClassTy = |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6733 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
| 6734 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
| 6735 | CGF.Builder.CreateStore(Target, |
| 6736 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6737 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6738 | return (isVTableDispatchedSelector(Sel)) |
| 6739 | ? EmitVTableMessageSend(CGF, Return, ResultType, Sel, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6740 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6741 | true, CallArgs, Method) |
| 6742 | : EmitMessageSend(CGF, Return, ResultType, |
| 6743 | EmitSelector(CGF.Builder, 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, ObjCTypes); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6746 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6747 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6748 | llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder, |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 6749 | Selector Sel, bool lval) { |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6750 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6751 | |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6752 | if (!Entry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6753 | llvm::Constant *Casted = |
| 6754 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 6755 | ObjCTypes.SelectorPtrTy); |
| 6756 | Entry = |
| 6757 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false, |
| 6758 | llvm::GlobalValue::InternalLinkage, |
| 6759 | Casted, "\01L_OBJC_SELECTOR_REFERENCES_"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 6760 | Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 6761 | CGM.AddUsedGlobal(Entry); |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6762 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6763 | |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 6764 | if (lval) |
| 6765 | return Entry; |
Pete Cooper | b6d7114 | 2011-11-10 21:45:06 +0000 | [diff] [blame] | 6766 | llvm::LoadInst* LI = Builder.CreateLoad(Entry); |
| 6767 | |
| 6768 | LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"), |
| 6769 | llvm::MDNode::get(VMContext, |
| 6770 | ArrayRef<llvm::Value*>())); |
| 6771 | return LI; |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6772 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6773 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 6774 | /// objc_assign_ivar (id src, id *dst, ptrdiff_t) |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6775 | /// |
| 6776 | void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6777 | llvm::Value *src, |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 6778 | llvm::Value *dst, |
| 6779 | llvm::Value *ivarOffset) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6780 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6781 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6782 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6783 | assert(Size <= 8 && "does not support size > 8"); |
| 6784 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 6785 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 6786 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 6787 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6788 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 6789 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 6790 | CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(), |
| 6791 | src, dst, ivarOffset); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6792 | return; |
| 6793 | } |
| 6794 | |
| 6795 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 6796 | /// objc_assign_strongCast (id src, id *dst) |
| 6797 | /// |
| 6798 | void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6799 | CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6800 | llvm::Value *src, llvm::Value *dst) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6801 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6802 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6803 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6804 | assert(Size <= 8 && "does not support size > 8"); |
| 6805 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6806 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 6807 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 6808 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6809 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 6810 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 6811 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6812 | src, dst, "weakassign"); |
| 6813 | return; |
| 6814 | } |
| 6815 | |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 6816 | void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6817 | CodeGen::CodeGenFunction &CGF, |
| 6818 | llvm::Value *DestPtr, |
| 6819 | llvm::Value *SrcPtr, |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 6820 | llvm::Value *Size) { |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 6821 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy); |
| 6822 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 6823 | CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(), |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 6824 | DestPtr, SrcPtr, Size); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 6825 | return; |
| 6826 | } |
| 6827 | |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6828 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
| 6829 | /// object: objc_read_weak (id *src) |
| 6830 | /// |
| 6831 | llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6832 | CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6833 | llvm::Value *AddrWeakObj) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6834 | llvm::Type* DestTy = |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6835 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
| 6836 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 6837 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6838 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 6839 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6840 | return read_weak; |
| 6841 | } |
| 6842 | |
| 6843 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 6844 | /// objc_assign_weak (id src, id *dst) |
| 6845 | /// |
| 6846 | void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6847 | llvm::Value *src, llvm::Value *dst) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6848 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6849 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6850 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6851 | assert(Size <= 8 && "does not support size > 8"); |
| 6852 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 6853 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 6854 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 6855 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6856 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 6857 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 6858 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6859 | src, dst, "weakassign"); |
| 6860 | return; |
| 6861 | } |
| 6862 | |
| 6863 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 6864 | /// objc_assign_global (id src, id *dst) |
| 6865 | /// |
| 6866 | void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 6867 | llvm::Value *src, llvm::Value *dst, |
| 6868 | bool threadlocal) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6869 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6870 | if (!isa<llvm::PointerType>(SrcTy)) { |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 6871 | unsigned Size = CGM.getDataLayout().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6872 | assert(Size <= 8 && "does not support size > 8"); |
| 6873 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 6874 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 6875 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 6876 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6877 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 6878 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 6879 | if (!threadlocal) |
| 6880 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
| 6881 | src, dst, "globalassign"); |
| 6882 | else |
| 6883 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(), |
| 6884 | src, dst, "threadlocalassign"); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6885 | return; |
| 6886 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6887 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6888 | void |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6889 | CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 6890 | const ObjCAtSynchronizedStmt &S) { |
David Chisnall | 05dc91b | 2011-03-25 17:46:35 +0000 | [diff] [blame] | 6891 | EmitAtSynchronizedStmt(CGF, S, |
| 6892 | cast<llvm::Function>(ObjCTypes.getSyncEnterFn()), |
| 6893 | cast<llvm::Function>(ObjCTypes.getSyncExitFn())); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6894 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6895 | |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6896 | llvm::Constant * |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 6897 | CGObjCNonFragileABIMac::GetEHType(QualType T) { |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6898 | // There's a particular fixed type info for 'id'. |
| 6899 | if (T->isObjCIdType() || |
| 6900 | T->isObjCQualifiedIdType()) { |
| 6901 | llvm::Constant *IDEHType = |
| 6902 | CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id"); |
| 6903 | if (!IDEHType) |
| 6904 | IDEHType = |
| 6905 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, |
| 6906 | false, |
| 6907 | llvm::GlobalValue::ExternalLinkage, |
| 6908 | 0, "OBJC_EHTYPE_id"); |
| 6909 | return IDEHType; |
| 6910 | } |
| 6911 | |
| 6912 | // All other types should be Objective-C interface pointer types. |
| 6913 | const ObjCObjectPointerType *PT = |
| 6914 | T->getAs<ObjCObjectPointerType>(); |
| 6915 | assert(PT && "Invalid @catch type."); |
| 6916 | const ObjCInterfaceType *IT = PT->getInterfaceType(); |
| 6917 | assert(IT && "Invalid @catch type."); |
| 6918 | return GetInterfaceEHType(IT->getDecl(), false); |
| 6919 | } |
| 6920 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6921 | void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
| 6922 | const ObjCAtTryStmt &S) { |
David Chisnall | 05dc91b | 2011-03-25 17:46:35 +0000 | [diff] [blame] | 6923 | EmitTryCatchStmt(CGF, S, |
| 6924 | cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()), |
| 6925 | cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()), |
| 6926 | cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn())); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 6927 | } |
| 6928 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6929 | /// EmitThrowStmt - Generate code for a throw statement. |
| 6930 | void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6a3c70e | 2013-01-10 19:02:56 +0000 | [diff] [blame] | 6931 | const ObjCAtThrowStmt &S, |
| 6932 | bool ClearInsertionPoint) { |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6933 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
John McCall | 2b014d6 | 2011-10-01 10:32:24 +0000 | [diff] [blame] | 6934 | llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr); |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 6935 | Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy); |
Jay Foad | 4c7d9f1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 6936 | CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception) |
John McCall | 7ec404c | 2010-10-16 08:21:07 +0000 | [diff] [blame] | 6937 | .setDoesNotReturn(); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6938 | } else { |
Jay Foad | 4c7d9f1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 6939 | CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn()) |
John McCall | 7ec404c | 2010-10-16 08:21:07 +0000 | [diff] [blame] | 6940 | .setDoesNotReturn(); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6941 | } |
| 6942 | |
John McCall | 7ec404c | 2010-10-16 08:21:07 +0000 | [diff] [blame] | 6943 | CGF.Builder.CreateUnreachable(); |
Fariborz Jahanian | 6a3c70e | 2013-01-10 19:02:56 +0000 | [diff] [blame] | 6944 | if (ClearInsertionPoint) |
| 6945 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6946 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6947 | |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6948 | llvm::Constant * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6949 | CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6950 | bool ForDefinition) { |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6951 | llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()]; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6952 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6953 | // If we don't need a definition, return the entry if found or check |
| 6954 | // if we use an external reference. |
| 6955 | if (!ForDefinition) { |
| 6956 | if (Entry) |
| 6957 | return Entry; |
Daniel Dunbar | 7e075cb | 2009-04-07 06:43:45 +0000 | [diff] [blame] | 6958 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6959 | // If this type (or a super class) has the __objc_exception__ |
| 6960 | // attribute, emit an external reference. |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 6961 | if (hasObjCExceptionAttribute(CGM.getContext(), ID)) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6962 | return Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6963 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false, |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6964 | llvm::GlobalValue::ExternalLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6965 | 0, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6966 | ("OBJC_EHTYPE_$_" + |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 6967 | ID->getIdentifier()->getName())); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6968 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6969 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6970 | // Otherwise we need to either make a new entry or fill in the |
| 6971 | // initializer. |
| 6972 | assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition"); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 6973 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6974 | std::string VTableName = "objc_ehtype_vtable"; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6975 | llvm::GlobalVariable *VTableGV = |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6976 | CGM.getModule().getGlobalVariable(VTableName); |
| 6977 | if (!VTableGV) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6978 | VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy, |
| 6979 | false, |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6980 | llvm::GlobalValue::ExternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6981 | 0, VTableName); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6982 | |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6983 | llvm::Value *VTableIdx = llvm::ConstantInt::get(CGM.Int32Ty, 2); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6984 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 6985 | llvm::Constant *Values[] = { |
| 6986 | llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx), |
| 6987 | GetClassName(ID->getIdentifier()), |
| 6988 | GetClassGlobal(ClassName) |
| 6989 | }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 6990 | llvm::Constant *Init = |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 6991 | llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6992 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6993 | if (Entry) { |
| 6994 | Entry->setInitializer(Init); |
| 6995 | } else { |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6996 | Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false, |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6997 | llvm::GlobalValue::WeakAnyLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6998 | Init, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6999 | ("OBJC_EHTYPE_$_" + |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 7000 | ID->getIdentifier()->getName())); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 7001 | } |
| 7002 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7003 | if (CGM.getLangOpts().getVisibilityMode() == HiddenVisibility) |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 7004 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 7005 | Entry->setAlignment(CGM.getDataLayout().getABITypeAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 7006 | ObjCTypes.EHTypeTy)); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 7007 | |
| 7008 | if (ForDefinition) { |
| 7009 | Entry->setSection("__DATA,__objc_const"); |
| 7010 | Entry->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 7011 | } else { |
| 7012 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 7013 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 7014 | |
| 7015 | return Entry; |
| 7016 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 7017 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 7018 | /* *** */ |
| 7019 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 7020 | CodeGen::CGObjCRuntime * |
| 7021 | CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 7022 | switch (CGM.getLangOpts().ObjCRuntime.getKind()) { |
| 7023 | case ObjCRuntime::FragileMacOSX: |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 7024 | return new CGObjCMac(CGM); |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 7025 | |
| 7026 | case ObjCRuntime::MacOSX: |
| 7027 | case ObjCRuntime::iOS: |
| 7028 | return new CGObjCNonFragileABIMac(CGM); |
| 7029 | |
David Chisnall | 11d3f4c | 2012-07-03 20:49:52 +0000 | [diff] [blame] | 7030 | case ObjCRuntime::GNUstep: |
| 7031 | case ObjCRuntime::GCC: |
John McCall | f7226fb | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 7032 | case ObjCRuntime::ObjFW: |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 7033 | llvm_unreachable("these runtimes are not Mac runtimes"); |
| 7034 | } |
| 7035 | llvm_unreachable("bad runtime"); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 7036 | } |