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" |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 15 | |
Daniel Dunbar | 198bcb4 | 2010-03-31 01:09:11 +0000 | [diff] [blame] | 16 | #include "CGRecordLayout.h" |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 17 | #include "CodeGenModule.h" |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 18 | #include "CodeGenFunction.h" |
John McCall | d16c2cf | 2011-02-08 08:22:06 +0000 | [diff] [blame] | 19 | #include "CGBlocks.h" |
John McCall | 36f893c | 2011-01-28 11:13:47 +0000 | [diff] [blame] | 20 | #include "CGCleanup.h" |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 22 | #include "clang/AST/Decl.h" |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 24 | #include "clang/AST/RecordLayout.h" |
Chris Lattner | 16f0049 | 2009-04-26 01:32:48 +0000 | [diff] [blame] | 25 | #include "clang/AST/StmtObjC.h" |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 26 | #include "clang/Basic/LangOptions.h" |
Chandler Carruth | 06057ce | 2010-06-15 23:19:56 +0000 | [diff] [blame] | 27 | #include "clang/Frontend/CodeGenOptions.h" |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 28 | |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 29 | #include "llvm/InlineAsm.h" |
| 30 | #include "llvm/IntrinsicInst.h" |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 31 | #include "llvm/LLVMContext.h" |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 32 | #include "llvm/Module.h" |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/DenseSet.h" |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/SetVector.h" |
| 35 | #include "llvm/ADT/SmallString.h" |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/SmallPtrSet.h" |
John McCall | 8e3f861 | 2010-07-13 22:12:14 +0000 | [diff] [blame] | 37 | #include "llvm/Support/CallSite.h" |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 38 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 39 | #include "llvm/Target/TargetData.h" |
Torok Edwin | f42e4a6 | 2009-08-24 13:25:12 +0000 | [diff] [blame] | 40 | #include <cstdio> |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 41 | |
| 42 | using namespace clang; |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 43 | using namespace CodeGen; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 44 | |
| 45 | namespace { |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 46 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 47 | // FIXME: We should find a nicer way to make the labels for metadata, string |
| 48 | // concatenation is lame. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 49 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 50 | class ObjCCommonTypesHelper { |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 51 | protected: |
| 52 | llvm::LLVMContext &VMContext; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 53 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 54 | private: |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 55 | // The types of these functions don't really matter because we |
| 56 | // should always bitcast before calling them. |
| 57 | |
| 58 | /// id objc_msgSend (id, SEL, ...) |
| 59 | /// |
| 60 | /// The default messenger, used for sends whose ABI is unchanged from |
| 61 | /// the all-integer/pointer case. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 62 | llvm::Constant *getMessageSendFn() const { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 63 | // Add the non-lazy-bind attribute, since objc_msgSend is likely to |
| 64 | // be called a lot. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 65 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 66 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 67 | params, true), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 68 | "objc_msgSend", |
| 69 | llvm::Attribute::NonLazyBind); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 70 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 71 | |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 72 | /// void objc_msgSend_stret (id, SEL, ...) |
| 73 | /// |
| 74 | /// The messenger used when the return value is an aggregate returned |
| 75 | /// by indirect reference in the first argument, and therefore the |
| 76 | /// self and selector parameters are shifted over by one. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 77 | llvm::Constant *getMessageSendStretFn() const { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 78 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 79 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, |
| 80 | params, true), |
| 81 | "objc_msgSend_stret"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 82 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 83 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 84 | |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 85 | /// [double | long double] objc_msgSend_fpret(id self, SEL op, ...) |
| 86 | /// |
| 87 | /// The messenger used when the return value is returned on the x87 |
| 88 | /// floating-point stack; without a special entrypoint, the nil case |
| 89 | /// would be unbalanced. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 90 | llvm::Constant *getMessageSendFpretFn() const { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 91 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 92 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.DoubleTy, |
| 93 | params, true), |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 94 | "objc_msgSend_fpret"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 95 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 96 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 97 | |
Anders Carlsson | eea6480 | 2011-10-31 16:27:11 +0000 | [diff] [blame] | 98 | /// _Complex long double objc_msgSend_fp2ret(id self, SEL op, ...) |
| 99 | /// |
| 100 | /// The messenger used when the return value is returned in two values on the |
| 101 | /// x87 floating point stack; without a special entrypoint, the nil case |
| 102 | /// would be unbalanced. Only used on 64-bit X86. |
| 103 | llvm::Constant *getMessageSendFp2retFn() const { |
| 104 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
| 105 | llvm::Type *longDoubleType = llvm::Type::getX86_FP80Ty(VMContext); |
| 106 | llvm::Type *resultType = |
| 107 | llvm::StructType::get(longDoubleType, longDoubleType, NULL); |
| 108 | |
| 109 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(resultType, |
| 110 | params, true), |
| 111 | "objc_msgSend_fp2ret"); |
| 112 | } |
| 113 | |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 114 | /// id objc_msgSendSuper(struct objc_super *super, SEL op, ...) |
| 115 | /// |
| 116 | /// The messenger used for super calls, which have different dispatch |
| 117 | /// semantics. The class passed is the superclass of the current |
| 118 | /// class. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 119 | llvm::Constant *getMessageSendSuperFn() const { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 120 | llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 121 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 122 | params, true), |
| 123 | "objc_msgSendSuper"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 124 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 125 | |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 126 | /// id objc_msgSendSuper2(struct objc_super *super, SEL op, ...) |
| 127 | /// |
| 128 | /// A slightly different messenger used for super calls. The class |
| 129 | /// passed is the current class. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 130 | llvm::Constant *getMessageSendSuperFn2() const { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 131 | llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 132 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 133 | params, true), |
| 134 | "objc_msgSendSuper2"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 135 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 136 | |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 137 | /// void objc_msgSendSuper_stret(void *stretAddr, struct objc_super *super, |
| 138 | /// SEL op, ...) |
| 139 | /// |
| 140 | /// The messenger used for super calls which return an aggregate indirectly. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 141 | llvm::Constant *getMessageSendSuperStretFn() const { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 142 | llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 143 | return CGM.CreateRuntimeFunction( |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 144 | llvm::FunctionType::get(CGM.VoidTy, params, true), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 145 | "objc_msgSendSuper_stret"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 146 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 147 | |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 148 | /// void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super, |
| 149 | /// SEL op, ...) |
| 150 | /// |
| 151 | /// objc_msgSendSuper_stret with the super2 semantics. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 152 | llvm::Constant *getMessageSendSuperStretFn2() const { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 153 | llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 154 | return CGM.CreateRuntimeFunction( |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 155 | llvm::FunctionType::get(CGM.VoidTy, params, true), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 156 | "objc_msgSendSuper2_stret"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 157 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 158 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 159 | llvm::Constant *getMessageSendSuperFpretFn() const { |
| 160 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 161 | return getMessageSendSuperFn(); |
| 162 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 163 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 164 | llvm::Constant *getMessageSendSuperFpretFn2() const { |
| 165 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 166 | return getMessageSendSuperFn2(); |
| 167 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 168 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 169 | protected: |
| 170 | CodeGen::CodeGenModule &CGM; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 171 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 172 | public: |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 173 | llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy; |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 174 | llvm::Type *Int8PtrTy, *Int8PtrPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 175 | |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 176 | /// ObjectPtrTy - LLVM type for object handles (typeof(id)) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 177 | llvm::Type *ObjectPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 178 | |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 179 | /// PtrObjectPtrTy - LLVM type for id * |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 180 | llvm::Type *PtrObjectPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 181 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 182 | /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL)) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 183 | llvm::Type *SelectorPtrTy; |
Douglas Gregor | 4c86fdb | 2012-01-17 18:36:30 +0000 | [diff] [blame] | 184 | |
| 185 | private: |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 186 | /// ProtocolPtrTy - LLVM type for external protocol handles |
| 187 | /// (typeof(Protocol)) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 188 | llvm::Type *ExternalProtocolPtrTy; |
Douglas Gregor | 4c86fdb | 2012-01-17 18:36:30 +0000 | [diff] [blame] | 189 | |
| 190 | public: |
| 191 | llvm::Type *getExternalProtocolPtrTy() { |
| 192 | if (!ExternalProtocolPtrTy) { |
| 193 | // FIXME: It would be nice to unify this with the opaque type, so that the |
| 194 | // IR comes out a bit cleaner. |
| 195 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 196 | ASTContext &Ctx = CGM.getContext(); |
| 197 | llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); |
| 198 | ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T); |
| 199 | } |
| 200 | |
| 201 | return ExternalProtocolPtrTy; |
| 202 | } |
| 203 | |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 204 | // SuperCTy - clang type for struct objc_super. |
| 205 | QualType SuperCTy; |
| 206 | // SuperPtrCTy - clang type for struct objc_super *. |
| 207 | QualType SuperPtrCTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 208 | |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 209 | /// SuperTy - LLVM type for struct objc_super. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 210 | llvm::StructType *SuperTy; |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 211 | /// SuperPtrTy - LLVM type for struct objc_super *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 212 | llvm::Type *SuperPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 213 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 214 | /// PropertyTy - LLVM type for struct objc_property (struct _prop_t |
| 215 | /// in GCC parlance). |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 216 | llvm::StructType *PropertyTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 217 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 218 | /// PropertyListTy - LLVM type for struct objc_property_list |
| 219 | /// (_prop_list_t in GCC parlance). |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 220 | llvm::StructType *PropertyListTy; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 221 | /// PropertyListPtrTy - LLVM type for struct objc_property_list*. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 222 | llvm::Type *PropertyListPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 223 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 224 | // MethodTy - LLVM type for struct objc_method. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 225 | llvm::StructType *MethodTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 226 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 227 | /// CacheTy - LLVM type for struct objc_cache. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 228 | llvm::Type *CacheTy; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 229 | /// CachePtrTy - LLVM type for struct objc_cache *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 230 | llvm::Type *CachePtrTy; |
Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 231 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 232 | llvm::Constant *getGetPropertyFn() { |
| 233 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 234 | ASTContext &Ctx = CGM.getContext(); |
| 235 | // id objc_getProperty (id, SEL, ptrdiff_t, bool) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 236 | SmallVector<CanQualType,4> Params; |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 237 | CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType()); |
| 238 | CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType()); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 239 | Params.push_back(IdType); |
| 240 | Params.push_back(SelType); |
David Chisnall | ab5824e | 2011-03-22 20:03:13 +0000 | [diff] [blame] | 241 | Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified()); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 242 | Params.push_back(Ctx.BoolTy); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 243 | llvm::FunctionType *FTy = |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 244 | Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(IdType, Params, |
| 245 | FunctionType::ExtInfo(), |
| 246 | RequiredArgs::All)); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 247 | return CGM.CreateRuntimeFunction(FTy, "objc_getProperty"); |
| 248 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 249 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 250 | llvm::Constant *getSetPropertyFn() { |
| 251 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 252 | ASTContext &Ctx = CGM.getContext(); |
| 253 | // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 254 | SmallVector<CanQualType,6> Params; |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 255 | CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType()); |
| 256 | CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType()); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 257 | Params.push_back(IdType); |
| 258 | Params.push_back(SelType); |
David Chisnall | ab5824e | 2011-03-22 20:03:13 +0000 | [diff] [blame] | 259 | Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified()); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 260 | Params.push_back(IdType); |
| 261 | Params.push_back(Ctx.BoolTy); |
| 262 | Params.push_back(Ctx.BoolTy); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 263 | llvm::FunctionType *FTy = |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 264 | Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params, |
| 265 | FunctionType::ExtInfo(), |
| 266 | RequiredArgs::All)); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 267 | return CGM.CreateRuntimeFunction(FTy, "objc_setProperty"); |
| 268 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 269 | |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 270 | llvm::Constant *getOptimizedSetPropertyFn(bool atomic, bool copy) { |
| 271 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 272 | ASTContext &Ctx = CGM.getContext(); |
| 273 | // void objc_setProperty_atomic(id self, SEL _cmd, |
| 274 | // id newValue, ptrdiff_t offset); |
| 275 | // void objc_setProperty_nonatomic(id self, SEL _cmd, |
| 276 | // id newValue, ptrdiff_t offset); |
| 277 | // void objc_setProperty_atomic_copy(id self, SEL _cmd, |
| 278 | // id newValue, ptrdiff_t offset); |
| 279 | // void objc_setProperty_nonatomic_copy(id self, SEL _cmd, |
| 280 | // id newValue, ptrdiff_t offset); |
| 281 | |
| 282 | SmallVector<CanQualType,4> Params; |
| 283 | CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType()); |
| 284 | CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType()); |
| 285 | Params.push_back(IdType); |
| 286 | Params.push_back(SelType); |
| 287 | Params.push_back(IdType); |
| 288 | Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified()); |
| 289 | llvm::FunctionType *FTy = |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 290 | Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params, |
| 291 | FunctionType::ExtInfo(), |
| 292 | RequiredArgs::All)); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 293 | const char *name; |
| 294 | if (atomic && copy) |
| 295 | name = "objc_setProperty_atomic_copy"; |
| 296 | else if (atomic && !copy) |
| 297 | name = "objc_setProperty_atomic"; |
| 298 | else if (!atomic && copy) |
| 299 | name = "objc_setProperty_nonatomic_copy"; |
| 300 | else |
| 301 | name = "objc_setProperty_nonatomic"; |
| 302 | |
| 303 | return CGM.CreateRuntimeFunction(FTy, name); |
| 304 | } |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 305 | |
| 306 | llvm::Constant *getCopyStructFn() { |
| 307 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 308 | ASTContext &Ctx = CGM.getContext(); |
| 309 | // void objc_copyStruct (void *, const void *, size_t, bool, bool) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 310 | SmallVector<CanQualType,5> Params; |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 311 | Params.push_back(Ctx.VoidPtrTy); |
| 312 | Params.push_back(Ctx.VoidPtrTy); |
| 313 | Params.push_back(Ctx.LongTy); |
| 314 | Params.push_back(Ctx.BoolTy); |
| 315 | Params.push_back(Ctx.BoolTy); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 316 | llvm::FunctionType *FTy = |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 317 | Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params, |
| 318 | FunctionType::ExtInfo(), |
| 319 | RequiredArgs::All)); |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 320 | return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct"); |
| 321 | } |
| 322 | |
Fariborz Jahanian | e317302 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 323 | /// This routine declares and returns address of: |
| 324 | /// void objc_copyCppObjectAtomic( |
| 325 | /// void *dest, const void *src, |
| 326 | /// void (*copyHelper) (void *dest, const void *source)); |
| 327 | llvm::Constant *getCppAtomicObjectFunction() { |
| 328 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 329 | ASTContext &Ctx = CGM.getContext(); |
| 330 | /// void objc_copyCppObjectAtomic(void *dest, const void *src, void *helper); |
| 331 | SmallVector<CanQualType,3> Params; |
| 332 | Params.push_back(Ctx.VoidPtrTy); |
| 333 | Params.push_back(Ctx.VoidPtrTy); |
| 334 | Params.push_back(Ctx.VoidPtrTy); |
| 335 | llvm::FunctionType *FTy = |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 336 | Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params, |
| 337 | FunctionType::ExtInfo(), |
| 338 | RequiredArgs::All)); |
Fariborz Jahanian | e317302 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 339 | return CGM.CreateRuntimeFunction(FTy, "objc_copyCppObjectAtomic"); |
| 340 | } |
| 341 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 342 | llvm::Constant *getEnumerationMutationFn() { |
Daniel Dunbar | c1ab900 | 2009-07-11 20:32:50 +0000 | [diff] [blame] | 343 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 344 | ASTContext &Ctx = CGM.getContext(); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 345 | // void objc_enumerationMutation (id) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 346 | SmallVector<CanQualType,1> Params; |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 347 | Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType())); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 348 | llvm::FunctionType *FTy = |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 349 | Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params, |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 350 | FunctionType::ExtInfo(), |
| 351 | RequiredArgs::All)); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 352 | return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation"); |
| 353 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 354 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 355 | /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function. |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 356 | llvm::Constant *getGcReadWeakFn() { |
| 357 | // id objc_read_weak (id *) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 358 | llvm::Type *args[] = { ObjectPtrTy->getPointerTo() }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 359 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 360 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 361 | return CGM.CreateRuntimeFunction(FTy, "objc_read_weak"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 364 | /// GcAssignWeakFn -- LLVM objc_assign_weak function. |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 365 | llvm::Constant *getGcAssignWeakFn() { |
| 366 | // id objc_assign_weak (id, id *) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 367 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() }; |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 368 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 369 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 370 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak"); |
| 371 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 372 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 373 | /// GcAssignGlobalFn -- LLVM objc_assign_global function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 374 | llvm::Constant *getGcAssignGlobalFn() { |
| 375 | // id objc_assign_global(id, id *) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 376 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 377 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 378 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 379 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_global"); |
| 380 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 381 | |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 382 | /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function. |
| 383 | llvm::Constant *getGcAssignThreadLocalFn() { |
| 384 | // id objc_assign_threadlocal(id src, id * dest) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 385 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() }; |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 386 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 387 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 388 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal"); |
| 389 | } |
| 390 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 391 | /// GcAssignIvarFn -- LLVM objc_assign_ivar function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 392 | llvm::Constant *getGcAssignIvarFn() { |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 393 | // id objc_assign_ivar(id, id *, ptrdiff_t) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 394 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo(), |
| 395 | CGM.PtrDiffTy }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 396 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 397 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 398 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar"); |
| 399 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 400 | |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 401 | /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function. |
| 402 | llvm::Constant *GcMemmoveCollectableFn() { |
| 403 | // void *objc_memmove_collectable(void *dst, const void *src, size_t size) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 404 | llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, LongTy }; |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 405 | llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, args, false); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 406 | return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable"); |
| 407 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 408 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 409 | /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 410 | llvm::Constant *getGcAssignStrongCastFn() { |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 411 | // id objc_assign_strongCast(id, id *) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 412 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 413 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 414 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 415 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast"); |
| 416 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 417 | |
| 418 | /// ExceptionThrowFn - LLVM objc_exception_throw function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 419 | llvm::Constant *getExceptionThrowFn() { |
| 420 | // void objc_exception_throw(id) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 421 | llvm::Type *args[] = { ObjectPtrTy }; |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 422 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 423 | llvm::FunctionType::get(CGM.VoidTy, args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 424 | return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw"); |
| 425 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 426 | |
Fariborz Jahanian | 69677ea | 2010-05-28 17:34:43 +0000 | [diff] [blame] | 427 | /// ExceptionRethrowFn - LLVM objc_exception_rethrow function. |
| 428 | llvm::Constant *getExceptionRethrowFn() { |
| 429 | // void objc_exception_rethrow(void) |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 430 | llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, false); |
Fariborz Jahanian | 69677ea | 2010-05-28 17:34:43 +0000 | [diff] [blame] | 431 | return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow"); |
| 432 | } |
| 433 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 434 | /// SyncEnterFn - LLVM object_sync_enter function. |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 435 | llvm::Constant *getSyncEnterFn() { |
Aaron Ballman | 2d234d73 | 2012-09-06 16:44:16 +0000 | [diff] [blame] | 436 | // int objc_sync_enter (id) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 437 | llvm::Type *args[] = { ObjectPtrTy }; |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 438 | llvm::FunctionType *FTy = |
Aaron Ballman | 2d234d73 | 2012-09-06 16:44:16 +0000 | [diff] [blame] | 439 | llvm::FunctionType::get(CGM.IntTy, args, false); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 440 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter"); |
| 441 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 442 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 443 | /// SyncExitFn - LLVM object_sync_exit function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 444 | llvm::Constant *getSyncExitFn() { |
Aaron Ballman | 2d234d73 | 2012-09-06 16:44:16 +0000 | [diff] [blame] | 445 | // int objc_sync_exit (id) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 446 | llvm::Type *args[] = { ObjectPtrTy }; |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 447 | llvm::FunctionType *FTy = |
Aaron Ballman | 2d234d73 | 2012-09-06 16:44:16 +0000 | [diff] [blame] | 448 | llvm::FunctionType::get(CGM.IntTy, args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 449 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit"); |
| 450 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 451 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 452 | llvm::Constant *getSendFn(bool IsSuper) const { |
| 453 | return IsSuper ? getMessageSendSuperFn() : getMessageSendFn(); |
| 454 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 455 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 456 | llvm::Constant *getSendFn2(bool IsSuper) const { |
| 457 | return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn(); |
| 458 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 459 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 460 | llvm::Constant *getSendStretFn(bool IsSuper) const { |
| 461 | return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn(); |
| 462 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 463 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 464 | llvm::Constant *getSendStretFn2(bool IsSuper) const { |
| 465 | return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn(); |
| 466 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 467 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 468 | llvm::Constant *getSendFpretFn(bool IsSuper) const { |
| 469 | return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn(); |
| 470 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 471 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 472 | llvm::Constant *getSendFpretFn2(bool IsSuper) const { |
| 473 | return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn(); |
| 474 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 475 | |
Anders Carlsson | eea6480 | 2011-10-31 16:27:11 +0000 | [diff] [blame] | 476 | llvm::Constant *getSendFp2retFn(bool IsSuper) const { |
| 477 | return IsSuper ? getMessageSendSuperFn() : getMessageSendFp2retFn(); |
| 478 | } |
| 479 | |
| 480 | llvm::Constant *getSendFp2RetFn2(bool IsSuper) const { |
| 481 | return IsSuper ? getMessageSendSuperFn2() : getMessageSendFp2retFn(); |
| 482 | } |
| 483 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 484 | ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm); |
| 485 | ~ObjCCommonTypesHelper(){} |
| 486 | }; |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 487 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 488 | /// ObjCTypesHelper - Helper class that encapsulates lazy |
| 489 | /// construction of varies types used during ObjC generation. |
| 490 | class ObjCTypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 491 | public: |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 492 | /// SymtabTy - LLVM type for struct objc_symtab. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 493 | llvm::StructType *SymtabTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 494 | /// SymtabPtrTy - LLVM type for struct objc_symtab *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 495 | llvm::Type *SymtabPtrTy; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 496 | /// ModuleTy - LLVM type for struct objc_module. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 497 | llvm::StructType *ModuleTy; |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 498 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 499 | /// ProtocolTy - LLVM type for struct objc_protocol. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 500 | llvm::StructType *ProtocolTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 501 | /// ProtocolPtrTy - LLVM type for struct objc_protocol *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 502 | llvm::Type *ProtocolPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 503 | /// ProtocolExtensionTy - LLVM type for struct |
| 504 | /// objc_protocol_extension. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 505 | llvm::StructType *ProtocolExtensionTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 506 | /// ProtocolExtensionTy - LLVM type for struct |
| 507 | /// objc_protocol_extension *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 508 | llvm::Type *ProtocolExtensionPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 509 | /// MethodDescriptionTy - LLVM type for struct |
| 510 | /// objc_method_description. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 511 | llvm::StructType *MethodDescriptionTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 512 | /// MethodDescriptionListTy - LLVM type for struct |
| 513 | /// objc_method_description_list. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 514 | llvm::StructType *MethodDescriptionListTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 515 | /// MethodDescriptionListPtrTy - LLVM type for struct |
| 516 | /// objc_method_description_list *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 517 | llvm::Type *MethodDescriptionListPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 518 | /// ProtocolListTy - LLVM type for struct objc_property_list. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 519 | llvm::StructType *ProtocolListTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 520 | /// ProtocolListPtrTy - LLVM type for struct objc_property_list*. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 521 | llvm::Type *ProtocolListPtrTy; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 522 | /// CategoryTy - LLVM type for struct objc_category. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 523 | llvm::StructType *CategoryTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 524 | /// ClassTy - LLVM type for struct objc_class. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 525 | llvm::StructType *ClassTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 526 | /// ClassPtrTy - LLVM type for struct objc_class *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 527 | llvm::Type *ClassPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 528 | /// ClassExtensionTy - LLVM type for struct objc_class_ext. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 529 | llvm::StructType *ClassExtensionTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 530 | /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 531 | llvm::Type *ClassExtensionPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 532 | // IvarTy - LLVM type for struct objc_ivar. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 533 | llvm::StructType *IvarTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 534 | /// IvarListTy - LLVM type for struct objc_ivar_list. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 535 | llvm::Type *IvarListTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 536 | /// IvarListPtrTy - LLVM type for struct objc_ivar_list *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 537 | llvm::Type *IvarListPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 538 | /// MethodListTy - LLVM type for struct objc_method_list. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 539 | llvm::Type *MethodListTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 540 | /// MethodListPtrTy - LLVM type for struct objc_method_list *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 541 | llvm::Type *MethodListPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 542 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 543 | /// ExceptionDataTy - LLVM type for struct _objc_exception_data. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 544 | llvm::Type *ExceptionDataTy; |
Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 545 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 546 | /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 547 | llvm::Constant *getExceptionTryEnterFn() { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 548 | llvm::Type *params[] = { ExceptionDataTy->getPointerTo() }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 549 | return CGM.CreateRuntimeFunction( |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 550 | llvm::FunctionType::get(CGM.VoidTy, params, false), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 551 | "objc_exception_try_enter"); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 552 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 553 | |
| 554 | /// ExceptionTryExitFn - LLVM objc_exception_try_exit function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 555 | llvm::Constant *getExceptionTryExitFn() { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 556 | llvm::Type *params[] = { ExceptionDataTy->getPointerTo() }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 557 | return CGM.CreateRuntimeFunction( |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 558 | llvm::FunctionType::get(CGM.VoidTy, params, false), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 559 | "objc_exception_try_exit"); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 560 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 561 | |
| 562 | /// ExceptionExtractFn - LLVM objc_exception_extract function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 563 | llvm::Constant *getExceptionExtractFn() { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 564 | llvm::Type *params[] = { ExceptionDataTy->getPointerTo() }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 565 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 566 | params, false), |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 567 | "objc_exception_extract"); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 568 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 569 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 570 | /// ExceptionMatchFn - LLVM objc_exception_match function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 571 | llvm::Constant *getExceptionMatchFn() { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 572 | llvm::Type *params[] = { ClassPtrTy, ObjectPtrTy }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 573 | return CGM.CreateRuntimeFunction( |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 574 | llvm::FunctionType::get(CGM.Int32Ty, params, false), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 575 | "objc_exception_match"); |
| 576 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 577 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 578 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 579 | /// SetJmpFn - LLVM _setjmp function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 580 | llvm::Constant *getSetJmpFn() { |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 581 | // This is specifically the prototype for x86. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 582 | llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() }; |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 583 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty, |
| 584 | params, false), |
Bill Wendling | a9e269e | 2011-11-29 00:10:10 +0000 | [diff] [blame] | 585 | "_setjmp", |
| 586 | llvm::Attribute::ReturnsTwice); |
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: |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +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 | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 756 | protected: |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 757 | llvm::LLVMContext &VMContext; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 758 | // FIXME! May not be needing this after all. |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 759 | unsigned ObjCABI; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 760 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 761 | // gc ivar layout bitmap calculation helper caches. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 762 | SmallVector<GC_IVAR, 16> SkipIvars; |
| 763 | SmallVector<GC_IVAR, 16> IvarsInfo; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 764 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 765 | /// LazySymbols - Symbols to generate a lazy reference for. See |
| 766 | /// DefinedSymbols and FinishModule(). |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 767 | llvm::SetVector<IdentifierInfo*> LazySymbols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 768 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 769 | /// DefinedSymbols - External symbols which are defined by this |
| 770 | /// module. The symbols in this list and LazySymbols are used to add |
| 771 | /// special linker symbols which ensure that Objective-C modules are |
| 772 | /// linked properly. |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 773 | llvm::SetVector<IdentifierInfo*> DefinedSymbols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 774 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 775 | /// ClassNames - uniqued class names. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 776 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 777 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 778 | /// MethodVarNames - uniqued method variable names. |
| 779 | llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 780 | |
Fariborz Jahanian | b9c5b3d | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 781 | /// DefinedCategoryNames - list of category names in form Class_Category. |
| 782 | llvm::SetVector<std::string> DefinedCategoryNames; |
| 783 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 784 | /// MethodVarTypes - uniqued method type signatures. We have to use |
| 785 | /// a StringMap here because have no other unique reference. |
| 786 | llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 787 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 788 | /// MethodDefinitions - map of methods which have been defined in |
| 789 | /// this translation unit. |
| 790 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 791 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 792 | /// PropertyNames - uniqued method variable names. |
| 793 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 794 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 795 | /// ClassReferences - uniqued class references. |
| 796 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 797 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 798 | /// SelectorReferences - uniqued selector references. |
| 799 | llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 800 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 801 | /// Protocols - Protocols for which an objc_protocol structure has |
| 802 | /// been emitted. Forward declarations are handled by creating an |
| 803 | /// empty structure whose initializer is filled in when/if defined. |
| 804 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 805 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 806 | /// DefinedProtocols - Protocols which have actually been |
| 807 | /// defined. We should not need this, see FIXME in GenerateProtocol. |
| 808 | llvm::DenseSet<IdentifierInfo*> DefinedProtocols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 809 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 810 | /// DefinedClasses - List of defined classes. |
Bill Wendling | 1e01ac4 | 2012-02-07 09:40:07 +0000 | [diff] [blame] | 811 | llvm::SmallVector<llvm::GlobalValue*, 16> DefinedClasses; |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 812 | |
| 813 | /// DefinedNonLazyClasses - List of defined "non-lazy" classes. |
Bill Wendling | 1e01ac4 | 2012-02-07 09:40:07 +0000 | [diff] [blame] | 814 | llvm::SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyClasses; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 815 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 816 | /// DefinedCategories - List of defined categories. |
Bill Wendling | 1e01ac4 | 2012-02-07 09:40:07 +0000 | [diff] [blame] | 817 | llvm::SmallVector<llvm::GlobalValue*, 16> DefinedCategories; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 818 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 819 | /// DefinedNonLazyCategories - List of defined "non-lazy" categories. |
Bill Wendling | 1e01ac4 | 2012-02-07 09:40:07 +0000 | [diff] [blame] | 820 | llvm::SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyCategories; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 821 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 822 | /// GetNameForMethod - Return a name for the given method. |
| 823 | /// \param[out] NameOut - The return value. |
| 824 | void GetNameForMethod(const ObjCMethodDecl *OMD, |
| 825 | const ObjCContainerDecl *CD, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 826 | SmallVectorImpl<char> &NameOut); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 827 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 828 | /// GetMethodVarName - Return a unique constant for the given |
| 829 | /// selector's name. The return value has type char *. |
| 830 | llvm::Constant *GetMethodVarName(Selector Sel); |
| 831 | llvm::Constant *GetMethodVarName(IdentifierInfo *Ident); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 832 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 833 | /// GetMethodVarType - Return a unique constant for the given |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 834 | /// method's type encoding string. The return value has type char *. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 835 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 836 | // FIXME: This is a horrible name. |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 837 | llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D, |
| 838 | bool Extended = false); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 839 | llvm::Constant *GetMethodVarType(const FieldDecl *D); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 840 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 841 | /// GetPropertyName - Return a unique constant for the given |
| 842 | /// name. The return value has type char *. |
| 843 | llvm::Constant *GetPropertyName(IdentifierInfo *Ident); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 844 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 845 | // FIXME: This can be dropped once string functions are unified. |
| 846 | llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 847 | const Decl *Container); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 848 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 849 | /// GetClassName - Return a unique constant for the given selector's |
| 850 | /// name. The return value has type char *. |
| 851 | llvm::Constant *GetClassName(IdentifierInfo *Ident); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 852 | |
Argyrios Kyrtzidis | 9d50c06 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 853 | llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD); |
| 854 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 855 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 856 | /// implementation for the __strong or __weak case. |
| 857 | /// |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 858 | llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI, |
| 859 | bool ForStrongLayout); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 860 | |
Fariborz Jahanian | b8fd2eb | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 861 | llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 862 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 863 | void BuildAggrIvarRecordLayout(const RecordType *RT, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 864 | unsigned int BytePos, bool ForStrongLayout, |
| 865 | bool &HasUnion); |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 866 | void BuildAggrIvarLayout(const ObjCImplementationDecl *OI, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 867 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 868 | const RecordDecl *RD, |
Bill Wendling | 795b100 | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 869 | ArrayRef<const FieldDecl*> RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 870 | unsigned int BytePos, bool ForStrongLayout, |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 871 | bool &HasUnion); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 872 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 873 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 874 | /// ivar layout bitmap. |
| 875 | llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident, |
| 876 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 877 | |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 878 | /// EmitPropertyList - Emit the given property list. The return |
| 879 | /// value has type PropertyListPtrTy. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 880 | llvm::Constant *EmitPropertyList(Twine Name, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 881 | const Decl *Container, |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 882 | const ObjCContainerDecl *OCD, |
| 883 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 884 | |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 885 | /// EmitProtocolMethodTypes - Generate the array of extended method type |
| 886 | /// strings. The return value has type Int8PtrPtrTy. |
| 887 | llvm::Constant *EmitProtocolMethodTypes(Twine Name, |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 888 | ArrayRef<llvm::Constant*> MethodTypes, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 889 | const ObjCCommonTypesHelper &ObjCTypes); |
| 890 | |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 891 | /// PushProtocolProperties - Push protocol's property on the input stack. |
Bill Wendling | 3964e62 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 892 | void PushProtocolProperties( |
| 893 | llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet, |
| 894 | llvm::SmallVectorImpl<llvm::Constant*> &Properties, |
| 895 | const Decl *Container, |
| 896 | const ObjCProtocolDecl *PROTO, |
| 897 | const ObjCCommonTypesHelper &ObjCTypes); |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 898 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 899 | /// GetProtocolRef - Return a reference to the internal protocol |
| 900 | /// description, creating an empty one if it has not been |
| 901 | /// defined. The return value has type ProtocolPtrTy. |
| 902 | llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD); |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 903 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 904 | /// CreateMetadataVar - Create a global variable with internal |
| 905 | /// linkage for use by the Objective-C runtime. |
| 906 | /// |
| 907 | /// This is a convenience wrapper which not only creates the |
| 908 | /// variable, but also sets the section and alignment and adds the |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 909 | /// global to the "llvm.used" list. |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 910 | /// |
| 911 | /// \param Name - The variable name. |
| 912 | /// \param Init - The variable initializer; this is also used to |
| 913 | /// define the type of the variable. |
| 914 | /// \param Section - The section the variable should go into, or 0. |
| 915 | /// \param Align - The alignment for the variable, or 0. |
| 916 | /// \param AddToUsed - Whether the variable should be added to |
Daniel Dunbar | c158306 | 2009-04-14 17:42:51 +0000 | [diff] [blame] | 917 | /// "llvm.used". |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 918 | llvm::GlobalVariable *CreateMetadataVar(Twine Name, |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 919 | llvm::Constant *Init, |
| 920 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 921 | unsigned Align, |
| 922 | bool AddToUsed); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 923 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 924 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 925 | ReturnValueSlot Return, |
| 926 | QualType ResultType, |
| 927 | llvm::Value *Sel, |
| 928 | llvm::Value *Arg0, |
| 929 | QualType Arg0Ty, |
| 930 | bool IsSuper, |
| 931 | const CallArgList &CallArgs, |
| 932 | const ObjCMethodDecl *OMD, |
| 933 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 934 | |
Daniel Dunbar | fce176b | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 935 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 936 | /// level information. |
| 937 | void EmitImageInfo(); |
| 938 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 939 | public: |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 940 | CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 941 | CGObjCRuntime(cgm), VMContext(cgm.getLLVMContext()) { } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 942 | |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 943 | virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 944 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 945 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
| 946 | const ObjCContainerDecl *CD=0); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 947 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 948 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 949 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 950 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 951 | /// declaration, emitting it if necessary. The return value has type |
| 952 | /// ProtocolPtrTy. |
| 953 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 954 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 955 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 956 | /// object for the given declaration, emitting it if needed. These |
| 957 | /// forward references will be filled in with empty bodies if no |
| 958 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 959 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0; |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 960 | virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM, |
| 961 | const CGBlockInfo &blockInfo); |
Fariborz Jahanian | 89ecd41 | 2010-08-04 16:57:49 +0000 | [diff] [blame] | 962 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 963 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 964 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 965 | class CGObjCMac : public CGObjCCommonMac { |
| 966 | private: |
| 967 | ObjCTypesHelper ObjCTypes; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 968 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 969 | /// EmitModuleInfo - Another marker encoding module level |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 970 | /// information. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 971 | void EmitModuleInfo(); |
| 972 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 973 | /// EmitModuleSymols - Emit module symbols, the list of defined |
| 974 | /// classes and categories. The result has type SymtabPtrTy. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 975 | llvm::Constant *EmitModuleSymbols(); |
| 976 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 977 | /// FinishModule - Write out global data structures at the end of |
| 978 | /// processing a translation unit. |
| 979 | void FinishModule(); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 980 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 981 | /// EmitClassExtension - Generate the class extension structure used |
| 982 | /// to store the weak ivar layout and properties. The return value |
| 983 | /// has type ClassExtensionPtrTy. |
| 984 | llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID); |
| 985 | |
| 986 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 987 | /// for the given class. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 988 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 989 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 990 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 991 | llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder, |
| 992 | IdentifierInfo *II); |
| 993 | |
| 994 | llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder); |
| 995 | |
Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 996 | /// EmitSuperClassRef - Emits reference to class's main metadata class. |
| 997 | llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 998 | |
| 999 | /// EmitIvarList - Emit the ivar list for the given |
| 1000 | /// implementation. If ForClass is true the list of class ivars |
| 1001 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 1002 | /// interface ivars will be emitted. The return value has type |
| 1003 | /// IvarListPtrTy. |
| 1004 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1005 | bool ForClass); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1006 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1007 | /// EmitMetaClass - Emit a forward reference to the class structure |
| 1008 | /// for the metaclass of the given interface. The return value has |
| 1009 | /// type ClassPtrTy. |
| 1010 | llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID); |
| 1011 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1012 | /// EmitMetaClass - Emit a class structure for the metaclass of the |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1013 | /// given implementation. The return value has type ClassPtrTy. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1014 | llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID, |
| 1015 | llvm::Constant *Protocols, |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 1016 | ArrayRef<llvm::Constant*> Methods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1017 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1018 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1019 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1020 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1021 | |
| 1022 | /// EmitMethodList - Emit the method list for the given |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1023 | /// implementation. The return value has type MethodListPtrTy. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1024 | llvm::Constant *EmitMethodList(Twine Name, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1025 | const char *Section, |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 1026 | ArrayRef<llvm::Constant*> Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1027 | |
| 1028 | /// EmitMethodDescList - Emit a method description list for a list of |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1029 | /// method declarations. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1030 | /// - TypeName: The name for the type containing the methods. |
Sylvestre Ledru | f3477c1 | 2012-09-27 10:16:10 +0000 | [diff] [blame^] | 1031 | /// - IsProtocol: True iff these methods are for a protocol. |
| 1032 | /// - ClassMethds: True iff these are class methods. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1033 | /// - Required: When true, only "required" methods are |
| 1034 | /// listed. Similarly, when false only "optional" methods are |
| 1035 | /// listed. For classes this should always be true. |
| 1036 | /// - begin, end: The method list to output. |
| 1037 | /// |
| 1038 | /// The return value has type MethodDescriptionListPtrTy. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1039 | llvm::Constant *EmitMethodDescList(Twine Name, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1040 | const char *Section, |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 1041 | ArrayRef<llvm::Constant*> Methods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1042 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +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. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1046 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1047 | |
| 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. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1052 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1053 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1054 | /// EmitProtocolExtension - Generate the protocol extension |
| 1055 | /// structure used to store optional instance and class methods, and |
| 1056 | /// protocol properties. The return value has type |
| 1057 | /// ProtocolExtensionPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1058 | llvm::Constant * |
| 1059 | EmitProtocolExtension(const ObjCProtocolDecl *PD, |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 1060 | ArrayRef<llvm::Constant*> OptInstanceMethods, |
| 1061 | ArrayRef<llvm::Constant*> OptClassMethods, |
| 1062 | ArrayRef<llvm::Constant*> MethodTypesExt); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1063 | |
| 1064 | /// EmitProtocolList - Generate the list of referenced |
| 1065 | /// protocols. The return value has type ProtocolListPtrTy. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1066 | llvm::Constant *EmitProtocolList(Twine Name, |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1067 | ObjCProtocolDecl::protocol_iterator begin, |
| 1068 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1069 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1070 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1071 | /// for the given selector. |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1072 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel, |
| 1073 | bool lval=false); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1074 | |
| 1075 | public: |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1076 | CGObjCMac(CodeGen::CodeGenModule &cgm); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1077 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1078 | virtual llvm::Function *ModuleInitFunction(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1079 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1080 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1081 | ReturnValueSlot Return, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1082 | QualType ResultType, |
| 1083 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1084 | llvm::Value *Receiver, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1085 | const CallArgList &CallArgs, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1086 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1087 | const ObjCMethodDecl *Method); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1088 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1089 | virtual CodeGen::RValue |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1090 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1091 | ReturnValueSlot Return, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1092 | QualType ResultType, |
| 1093 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1094 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1095 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1096 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1097 | bool IsClassMessage, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1098 | const CallArgList &CallArgs, |
| 1099 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1100 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1101 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1102 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1103 | |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1104 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel, |
| 1105 | bool lval = false); |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1106 | |
| 1107 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an |
| 1108 | /// untyped one. |
| 1109 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, |
| 1110 | const ObjCMethodDecl *Method); |
| 1111 | |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1112 | virtual llvm::Constant *GetEHType(QualType T); |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1113 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1114 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1115 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1116 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1117 | |
Daniel Dunbar | 85fdea0 | 2012-02-28 15:36:15 +0000 | [diff] [blame] | 1118 | virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {} |
David Chisnall | 29254f4 | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 1119 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1120 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1121 | const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1122 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1123 | virtual llvm::Constant *GetPropertyGetFunction(); |
| 1124 | virtual llvm::Constant *GetPropertySetFunction(); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1125 | virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic, |
| 1126 | bool copy); |
David Chisnall | 8fac25d | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 1127 | virtual llvm::Constant *GetGetStructFunction(); |
| 1128 | virtual llvm::Constant *GetSetStructFunction(); |
Fariborz Jahanian | e317302 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 1129 | virtual llvm::Constant *GetCppAtomicObjectFunction(); |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1130 | virtual llvm::Constant *EnumerationMutationFunction(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1131 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1132 | virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
| 1133 | const ObjCAtTryStmt &S); |
| 1134 | virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1135 | const ObjCAtSynchronizedStmt &S); |
| 1136 | void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1137 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 1138 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1139 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1140 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1141 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1142 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1143 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 1144 | llvm::Value *src, llvm::Value *dest, |
| 1145 | bool threadlocal = false); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 1146 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1147 | llvm::Value *src, llvm::Value *dest, |
| 1148 | llvm::Value *ivarOffset); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1149 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 1150 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 1151 | virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
| 1152 | llvm::Value *dest, llvm::Value *src, |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 1153 | llvm::Value *size); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1154 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1155 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1156 | QualType ObjectTy, |
| 1157 | llvm::Value *BaseValue, |
| 1158 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1159 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1160 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1161 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1162 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | 6f40e22 | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1163 | |
| 1164 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 1165 | /// class of the given name. |
| 1166 | virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1167 | llvm_unreachable("CGObjCMac::GetClassGlobal"); |
Fariborz Jahanian | 6f40e22 | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1168 | } |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1169 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1170 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1171 | class CGObjCNonFragileABIMac : public CGObjCCommonMac { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1172 | private: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1173 | ObjCNonFragileABITypesHelper ObjCTypes; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1174 | llvm::GlobalVariable* ObjCEmptyCacheVar; |
| 1175 | llvm::GlobalVariable* ObjCEmptyVtableVar; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1176 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1177 | /// SuperClassReferences - uniqued super class references. |
| 1178 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1179 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1180 | /// MetaClassReferences - uniqued meta class references. |
| 1181 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1182 | |
| 1183 | /// EHTypeReferences - uniqued class ehtype references. |
| 1184 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1185 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1186 | /// VTableDispatchMethods - List of methods for which we generate |
| 1187 | /// vtable-based message dispatch. |
| 1188 | llvm::DenseSet<Selector> VTableDispatchMethods; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1189 | |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 1190 | /// DefinedMetaClasses - List of defined meta-classes. |
| 1191 | std::vector<llvm::GlobalValue*> DefinedMetaClasses; |
| 1192 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1193 | /// isVTableDispatchedSelector - Returns true if SEL is a |
| 1194 | /// vtable-based selector. |
| 1195 | bool isVTableDispatchedSelector(Selector Sel); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1196 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1197 | /// FinishNonFragileABIModule - Write out global data structures at the end of |
| 1198 | /// processing a translation unit. |
| 1199 | void FinishNonFragileABIModule(); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1200 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 1201 | /// AddModuleClassList - Add the given list of class pointers to the |
| 1202 | /// module with the provided symbol and section names. |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 1203 | void AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 1204 | const char *SymbolName, |
| 1205 | const char *SectionName); |
| 1206 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1207 | llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags, |
| 1208 | unsigned InstanceStart, |
| 1209 | unsigned InstanceSize, |
| 1210 | const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1211 | llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1212 | llvm::Constant *IsAGV, |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1213 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 1214 | llvm::Constant *ClassRoGV, |
| 1215 | bool HiddenVisibility); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1216 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1217 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1218 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 1219 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1220 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1221 | /// EmitMethodList - Emit the method list for the given |
| 1222 | /// implementation. The return value has type MethodListnfABITy. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1223 | llvm::Constant *EmitMethodList(Twine Name, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1224 | const char *Section, |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 1225 | ArrayRef<llvm::Constant*> Methods); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 1226 | /// EmitIvarList - Emit the ivar list for the given |
| 1227 | /// implementation. If ForClass is true the list of class ivars |
| 1228 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 1229 | /// interface ivars will be emitted. The return value has type |
| 1230 | /// IvarListnfABIPtrTy. |
| 1231 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1232 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1233 | llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 1234 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 1235 | unsigned long int offset); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1236 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1237 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1238 | /// declaration, emitting it if necessary. The return value has type |
| 1239 | /// ProtocolPtrTy. |
| 1240 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1241 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1242 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1243 | /// object for the given declaration, emitting it if needed. These |
| 1244 | /// forward references will be filled in with empty bodies if no |
| 1245 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 1246 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1247 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1248 | /// EmitProtocolList - Generate the list of referenced |
| 1249 | /// protocols. The return value has type ProtocolListPtrTy. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1250 | llvm::Constant *EmitProtocolList(Twine Name, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1251 | ObjCProtocolDecl::protocol_iterator begin, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1252 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1253 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1254 | CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1255 | ReturnValueSlot Return, |
| 1256 | QualType ResultType, |
| 1257 | Selector Sel, |
| 1258 | llvm::Value *Receiver, |
| 1259 | QualType Arg0Ty, |
| 1260 | bool IsSuper, |
| 1261 | const CallArgList &CallArgs, |
| 1262 | const ObjCMethodDecl *Method); |
Fariborz Jahanian | 6f40e22 | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1263 | |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 1264 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 1265 | /// class of the given name. |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 1266 | llvm::GlobalVariable *GetClassGlobal(const std::string &Name); |
Fariborz Jahanian | 6f40e22 | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1267 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1268 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1269 | /// for the given class reference. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1270 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1271 | const ObjCInterfaceDecl *ID); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1272 | |
| 1273 | llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder, |
| 1274 | IdentifierInfo *II); |
| 1275 | |
| 1276 | llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1277 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1278 | /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 1279 | /// for the given super class reference. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1280 | llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder, |
| 1281 | const ObjCInterfaceDecl *ID); |
| 1282 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1283 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 1284 | /// meta-data |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1285 | llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1286 | const ObjCInterfaceDecl *ID); |
| 1287 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1288 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 1289 | /// the given ivar. |
| 1290 | /// |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 1291 | llvm::GlobalVariable * ObjCIvarOffsetVariable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1292 | const ObjCInterfaceDecl *ID, |
| 1293 | const ObjCIvarDecl *Ivar); |
| 1294 | |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 1295 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1296 | /// for the given selector. |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1297 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel, |
| 1298 | bool lval=false); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1299 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1300 | /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1301 | /// interface. The return value has type EHTypePtrTy. |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1302 | llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1303 | bool ForDefinition); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1304 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1305 | const char *getMetaclassSymbolPrefix() const { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1306 | return "OBJC_METACLASS_$_"; |
| 1307 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1308 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1309 | const char *getClassSymbolPrefix() const { |
| 1310 | return "OBJC_CLASS_$_"; |
| 1311 | } |
| 1312 | |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 1313 | void GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1314 | uint32_t &InstanceStart, |
| 1315 | uint32_t &InstanceSize); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1316 | |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1317 | // Shamelessly stolen from Analysis/CFRefCount.cpp |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1318 | Selector GetNullarySelector(const char* name) const { |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1319 | IdentifierInfo* II = &CGM.getContext().Idents.get(name); |
| 1320 | return CGM.getContext().Selectors.getSelector(0, &II); |
| 1321 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1322 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1323 | Selector GetUnarySelector(const char* name) const { |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1324 | IdentifierInfo* II = &CGM.getContext().Idents.get(name); |
| 1325 | return CGM.getContext().Selectors.getSelector(1, &II); |
| 1326 | } |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1327 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1328 | /// ImplementationIsNonLazy - Check whether the given category or |
| 1329 | /// class implementation is "non-lazy". |
Fariborz Jahanian | ecfbdcb | 2009-05-21 01:03:45 +0000 | [diff] [blame] | 1330 | bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const; |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1331 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1332 | public: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1333 | CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1334 | // FIXME. All stubs for now! |
| 1335 | virtual llvm::Function *ModuleInitFunction(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1336 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1337 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1338 | ReturnValueSlot Return, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1339 | QualType ResultType, |
| 1340 | Selector Sel, |
| 1341 | llvm::Value *Receiver, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1342 | const CallArgList &CallArgs, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1343 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1344 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1345 | |
| 1346 | virtual CodeGen::RValue |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1347 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1348 | ReturnValueSlot Return, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1349 | QualType ResultType, |
| 1350 | Selector Sel, |
| 1351 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1352 | bool isCategoryImpl, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1353 | llvm::Value *Receiver, |
| 1354 | bool IsClassMessage, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1355 | const CallArgList &CallArgs, |
| 1356 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1357 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1358 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1359 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1360 | |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1361 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel, |
| 1362 | bool lvalue = false) |
| 1363 | { return EmitSelector(Builder, Sel, lvalue); } |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1364 | |
| 1365 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an |
| 1366 | /// untyped one. |
| 1367 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, |
| 1368 | const ObjCMethodDecl *Method) |
| 1369 | { return EmitSelector(Builder, Method->getSelector()); } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1370 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 1371 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1372 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1373 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
David Chisnall | 29254f4 | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 1374 | |
Daniel Dunbar | 85fdea0 | 2012-02-28 15:36:15 +0000 | [diff] [blame] | 1375 | virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {} |
David Chisnall | 29254f4 | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 1376 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1377 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 1378 | const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1379 | |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1380 | virtual llvm::Constant *GetEHType(QualType T); |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1381 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1382 | virtual llvm::Constant *GetPropertyGetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1383 | return ObjCTypes.getGetPropertyFn(); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1384 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1385 | virtual llvm::Constant *GetPropertySetFunction() { |
| 1386 | return ObjCTypes.getSetPropertyFn(); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1387 | } |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 1388 | |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1389 | virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic, |
| 1390 | bool copy) { |
| 1391 | return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy); |
| 1392 | } |
| 1393 | |
David Chisnall | 8fac25d | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 1394 | virtual llvm::Constant *GetSetStructFunction() { |
| 1395 | return ObjCTypes.getCopyStructFn(); |
| 1396 | } |
| 1397 | virtual llvm::Constant *GetGetStructFunction() { |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 1398 | return ObjCTypes.getCopyStructFn(); |
| 1399 | } |
Fariborz Jahanian | e317302 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 1400 | virtual llvm::Constant *GetCppAtomicObjectFunction() { |
| 1401 | return ObjCTypes.getCppAtomicObjectFunction(); |
| 1402 | } |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 1403 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1404 | virtual llvm::Constant *EnumerationMutationFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1405 | return ObjCTypes.getEnumerationMutationFn(); |
Daniel Dunbar | 28ed084 | 2009-02-16 18:48:45 +0000 | [diff] [blame] | 1406 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1407 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1408 | virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
| 1409 | const ObjCAtTryStmt &S); |
| 1410 | virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1411 | const ObjCAtSynchronizedStmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1412 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 1413 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1414 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1415 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1416 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1417 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1418 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 1419 | llvm::Value *src, llvm::Value *dest, |
| 1420 | bool threadlocal = false); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1421 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1422 | llvm::Value *src, llvm::Value *dest, |
| 1423 | llvm::Value *ivarOffset); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1424 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1425 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 1426 | virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
| 1427 | llvm::Value *dest, llvm::Value *src, |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 1428 | llvm::Value *size); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1429 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1430 | QualType ObjectTy, |
| 1431 | llvm::Value *BaseValue, |
| 1432 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1433 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1434 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1435 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1436 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1437 | }; |
Fariborz Jahanian | 4e1524b | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1438 | |
| 1439 | /// A helper class for performing the null-initialization of a return |
| 1440 | /// value. |
| 1441 | struct NullReturnState { |
| 1442 | llvm::BasicBlock *NullBB; |
| 1443 | llvm::BasicBlock *callBB; |
| 1444 | NullReturnState() : NullBB(0), callBB(0) {} |
| 1445 | |
| 1446 | void init(CodeGenFunction &CGF, llvm::Value *receiver) { |
| 1447 | // Make blocks for the null-init and call edges. |
| 1448 | NullBB = CGF.createBasicBlock("msgSend.nullinit"); |
| 1449 | callBB = CGF.createBasicBlock("msgSend.call"); |
| 1450 | |
| 1451 | // Check for a null receiver and, if there is one, jump to the |
| 1452 | // null-init test. |
| 1453 | llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver); |
| 1454 | CGF.Builder.CreateCondBr(isNull, NullBB, callBB); |
| 1455 | |
| 1456 | // Otherwise, start performing the call. |
| 1457 | CGF.EmitBlock(callBB); |
| 1458 | } |
| 1459 | |
Fariborz Jahanian | 3c267e6 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1460 | RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType, |
| 1461 | const CallArgList &CallArgs, |
| 1462 | const ObjCMethodDecl *Method) { |
Fariborz Jahanian | 4e1524b | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1463 | if (!NullBB) return result; |
Fariborz Jahanian | 3c267e6 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1464 | |
| 1465 | llvm::Value *NullInitPtr = 0; |
| 1466 | if (result.isScalar() && !resultType->isVoidType()) { |
| 1467 | NullInitPtr = CGF.CreateTempAlloca(result.getScalarVal()->getType()); |
| 1468 | CGF.Builder.CreateStore(result.getScalarVal(), NullInitPtr); |
| 1469 | } |
Fariborz Jahanian | 4e1524b | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1470 | |
| 1471 | // Finish the call path. |
| 1472 | llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont"); |
| 1473 | if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB); |
| 1474 | |
| 1475 | // Emit the null-init block and perform the null-initialization there. |
| 1476 | CGF.EmitBlock(NullBB); |
Fariborz Jahanian | 3c267e6 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1477 | |
| 1478 | // Release consumed arguments along the null-receiver path. |
| 1479 | if (Method) { |
| 1480 | CallArgList::const_iterator I = CallArgs.begin(); |
| 1481 | for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(), |
| 1482 | e = Method->param_end(); i != e; ++i, ++I) { |
| 1483 | const ParmVarDecl *ParamDecl = (*i); |
| 1484 | if (ParamDecl->hasAttr<NSConsumedAttr>()) { |
| 1485 | RValue RV = I->RV; |
| 1486 | assert(RV.isScalar() && |
| 1487 | "NullReturnState::complete - arg not on object"); |
| 1488 | CGF.EmitARCRelease(RV.getScalarVal(), true); |
| 1489 | } |
| 1490 | } |
| 1491 | } |
| 1492 | |
| 1493 | if (result.isScalar()) { |
| 1494 | if (NullInitPtr) |
| 1495 | CGF.EmitNullInitialization(NullInitPtr, resultType); |
| 1496 | // Jump to the continuation block. |
| 1497 | CGF.EmitBlock(contBB); |
| 1498 | return NullInitPtr ? RValue::get(CGF.Builder.CreateLoad(NullInitPtr)) |
| 1499 | : result; |
| 1500 | } |
| 1501 | |
Fariborz Jahanian | 4e1524b | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1502 | if (!resultType->isAnyComplexType()) { |
| 1503 | assert(result.isAggregate() && "null init of non-aggregate result?"); |
| 1504 | CGF.EmitNullInitialization(result.getAggregateAddr(), resultType); |
| 1505 | // Jump to the continuation block. |
| 1506 | CGF.EmitBlock(contBB); |
| 1507 | return result; |
| 1508 | } |
| 1509 | |
| 1510 | // _Complex type |
| 1511 | // FIXME. Now easy to handle any other scalar type whose result is returned |
| 1512 | // in memory due to ABI limitations. |
| 1513 | CGF.EmitBlock(contBB); |
| 1514 | CodeGenFunction::ComplexPairTy CallCV = result.getComplexVal(); |
| 1515 | llvm::Type *MemberType = CallCV.first->getType(); |
| 1516 | llvm::Constant *ZeroCV = llvm::Constant::getNullValue(MemberType); |
| 1517 | // Create phi instruction for scalar complex value. |
| 1518 | llvm::PHINode *PHIReal = CGF.Builder.CreatePHI(MemberType, 2); |
| 1519 | PHIReal->addIncoming(ZeroCV, NullBB); |
| 1520 | PHIReal->addIncoming(CallCV.first, callBB); |
| 1521 | llvm::PHINode *PHIImag = CGF.Builder.CreatePHI(MemberType, 2); |
| 1522 | PHIImag->addIncoming(ZeroCV, NullBB); |
| 1523 | PHIImag->addIncoming(CallCV.second, callBB); |
| 1524 | return RValue::getComplex(PHIReal, PHIImag); |
| 1525 | } |
| 1526 | }; |
| 1527 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1528 | } // end anonymous namespace |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1529 | |
| 1530 | /* *** Helper Functions *** */ |
| 1531 | |
| 1532 | /// getConstantGEP() - Help routine to construct simple GEPs. |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1533 | static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1534 | llvm::Constant *C, |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1535 | unsigned idx0, |
| 1536 | unsigned idx1) { |
| 1537 | llvm::Value *Idxs[] = { |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1538 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0), |
| 1539 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1540 | }; |
Jay Foad | a5c0434 | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 1541 | return llvm::ConstantExpr::getGetElementPtr(C, Idxs); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1544 | /// hasObjCExceptionAttribute - Return true if this class or any super |
| 1545 | /// class has the __objc_exception__ attribute. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1546 | static bool hasObjCExceptionAttribute(ASTContext &Context, |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 1547 | const ObjCInterfaceDecl *OID) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1548 | if (OID->hasAttr<ObjCExceptionAttr>()) |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1549 | return true; |
| 1550 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 1551 | return hasObjCExceptionAttribute(Context, Super); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1552 | return false; |
| 1553 | } |
| 1554 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1555 | /* *** CGObjCMac Public Interface *** */ |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1556 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1557 | CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1558 | ObjCTypes(cgm) { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1559 | ObjCABI = 1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1560 | EmitImageInfo(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1561 | } |
| 1562 | |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 1563 | /// GetClass - Return a reference to the class for the given interface |
| 1564 | /// decl. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1565 | llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1566 | const ObjCInterfaceDecl *ID) { |
| 1567 | return EmitClassRef(Builder, ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1568 | } |
| 1569 | |
| 1570 | /// GetSelector - Return the pointer to the unique'd string for this selector. |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1571 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel, |
| 1572 | bool lval) { |
| 1573 | return EmitSelector(Builder, Sel, lval); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1574 | } |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1575 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1576 | *Method) { |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1577 | return EmitSelector(Builder, Method->getSelector()); |
| 1578 | } |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1579 | |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1580 | llvm::Constant *CGObjCMac::GetEHType(QualType T) { |
Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 1581 | if (T->isObjCIdType() || |
| 1582 | T->isObjCQualifiedIdType()) { |
| 1583 | return CGM.GetAddrOfRTTIDescriptor( |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 1584 | CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true); |
Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 1585 | } |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1586 | if (T->isObjCClassType() || |
| 1587 | T->isObjCQualifiedClassType()) { |
| 1588 | return CGM.GetAddrOfRTTIDescriptor( |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 1589 | CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true); |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1590 | } |
| 1591 | if (T->isObjCObjectPointerType()) |
| 1592 | return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true); |
| 1593 | |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1594 | llvm_unreachable("asking for catch type for ObjC type in fragile runtime"); |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1597 | /// Generate a constant CFString object. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1598 | /* |
| 1599 | struct __builtin_CFString { |
| 1600 | const int *isa; // point to __CFConstantStringClassReference |
| 1601 | int flags; |
| 1602 | const char *str; |
| 1603 | long length; |
| 1604 | }; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1605 | */ |
| 1606 | |
Fariborz Jahanian | 33e982b | 2010-04-22 20:26:39 +0000 | [diff] [blame] | 1607 | /// or Generate a constant NSString object. |
| 1608 | /* |
| 1609 | struct __builtin_NSString { |
| 1610 | const int *isa; // point to __NSConstantStringClassReference |
| 1611 | const char *str; |
| 1612 | unsigned int length; |
| 1613 | }; |
| 1614 | */ |
| 1615 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1616 | llvm::Constant *CGObjCCommonMac::GenerateConstantString( |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 1617 | const StringLiteral *SL) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1618 | return (CGM.getLangOpts().NoConstantCFStrings == 0 ? |
Fariborz Jahanian | 33e982b | 2010-04-22 20:26:39 +0000 | [diff] [blame] | 1619 | CGM.GetAddrOfConstantCFString(SL) : |
Fariborz Jahanian | 4c73307 | 2010-10-19 17:19:29 +0000 | [diff] [blame] | 1620 | CGM.GetAddrOfConstantString(SL)); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1621 | } |
| 1622 | |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1623 | enum { |
| 1624 | kCFTaggedObjectID_Integer = (1 << 1) + 1 |
| 1625 | }; |
| 1626 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1627 | /// Generates a message send where the super is the receiver. This is |
| 1628 | /// a message send to self with special delivery semantics indicating |
| 1629 | /// which class's method should be called. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1630 | CodeGen::RValue |
| 1631 | CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1632 | ReturnValueSlot Return, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1633 | QualType ResultType, |
| 1634 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1635 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1636 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1637 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1638 | bool IsClassMessage, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1639 | const CodeGen::CallArgList &CallArgs, |
| 1640 | const ObjCMethodDecl *Method) { |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1641 | // Create and init a super structure; this is a (receiver, class) |
| 1642 | // pair we will pass to objc_msgSendSuper. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1643 | llvm::Value *ObjCSuper = |
John McCall | 604da29 | 2011-03-04 08:00:29 +0000 | [diff] [blame] | 1644 | CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1645 | llvm::Value *ReceiverAsObject = |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1646 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1647 | CGF.Builder.CreateStore(ReceiverAsObject, |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1648 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1649 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1650 | // If this is a class message the metaclass is passed as the target. |
| 1651 | llvm::Value *Target; |
| 1652 | if (IsClassMessage) { |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1653 | if (isCategoryImpl) { |
| 1654 | // Message sent to 'super' in a class method defined in a category |
| 1655 | // implementation requires an odd treatment. |
| 1656 | // If we are in a class method, we must retrieve the |
| 1657 | // _metaclass_ for the current class, pointed at by |
| 1658 | // the class's "isa" pointer. The following assumes that |
| 1659 | // isa" is the first ivar in a class (which it must be). |
| 1660 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1661 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 1662 | Target = CGF.Builder.CreateLoad(Target); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 1663 | } else { |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1664 | llvm::Value *MetaClassPtr = EmitMetaClassRef(Class); |
| 1665 | llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1); |
| 1666 | llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr); |
| 1667 | Target = Super; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1668 | } |
Fariborz Jahanian | 182f268 | 2009-11-14 02:18:31 +0000 | [diff] [blame] | 1669 | } |
| 1670 | else if (isCategoryImpl) |
| 1671 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1672 | else { |
Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 1673 | llvm::Value *ClassPtr = EmitSuperClassRef(Class); |
| 1674 | ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1); |
| 1675 | Target = CGF.Builder.CreateLoad(ClassPtr); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1676 | } |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1677 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext and |
| 1678 | // ObjCTypes types. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1679 | llvm::Type *ClassTy = |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1680 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1681 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1682 | CGF.Builder.CreateStore(Target, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1683 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1684 | return EmitMessageSend(CGF, Return, ResultType, |
| 1685 | EmitSelector(CGF.Builder, Sel), |
| 1686 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 1687 | true, CallArgs, Method, ObjCTypes); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1688 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1689 | |
| 1690 | /// Generate code for a message send expression. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1691 | CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1692 | ReturnValueSlot Return, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1693 | QualType ResultType, |
| 1694 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1695 | llvm::Value *Receiver, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1696 | const CallArgList &CallArgs, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1697 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1698 | const ObjCMethodDecl *Method) { |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1699 | return EmitMessageSend(CGF, Return, ResultType, |
| 1700 | EmitSelector(CGF.Builder, Sel), |
| 1701 | Receiver, CGF.getContext().getObjCIdType(), |
| 1702 | false, CallArgs, Method, ObjCTypes); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1703 | } |
| 1704 | |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1705 | CodeGen::RValue |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1706 | CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1707 | ReturnValueSlot Return, |
| 1708 | QualType ResultType, |
| 1709 | llvm::Value *Sel, |
| 1710 | llvm::Value *Arg0, |
| 1711 | QualType Arg0Ty, |
| 1712 | bool IsSuper, |
| 1713 | const CallArgList &CallArgs, |
| 1714 | const ObjCMethodDecl *Method, |
| 1715 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1716 | CallArgList ActualArgs; |
Fariborz Jahanian | d019d96 | 2009-04-24 21:07:43 +0000 | [diff] [blame] | 1717 | if (!IsSuper) |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 1718 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy); |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1719 | ActualArgs.add(RValue::get(Arg0), Arg0Ty); |
| 1720 | ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1721 | ActualArgs.addFrom(CallArgs); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1722 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1723 | // If we're calling a method, use the formal signature. |
| 1724 | MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1725 | |
Anders Carlsson | 7e70fb2 | 2010-06-21 20:59:55 +0000 | [diff] [blame] | 1726 | if (Method) |
| 1727 | assert(CGM.getContext().getCanonicalType(Method->getResultType()) == |
| 1728 | CGM.getContext().getCanonicalType(ResultType) && |
| 1729 | "Result type mismatch!"); |
| 1730 | |
John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 1731 | NullReturnState nullReturn; |
| 1732 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1733 | llvm::Constant *Fn = NULL; |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1734 | if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) { |
Fariborz Jahanian | 4e1524b | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1735 | if (!IsSuper) nullReturn.init(CGF, Arg0); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1736 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1737 | : ObjCTypes.getSendStretFn(IsSuper); |
Daniel Dunbar | dacf9dd | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 1738 | } else if (CGM.ReturnTypeUsesFPRet(ResultType)) { |
| 1739 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper) |
| 1740 | : ObjCTypes.getSendFpretFn(IsSuper); |
Anders Carlsson | eea6480 | 2011-10-31 16:27:11 +0000 | [diff] [blame] | 1741 | } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) { |
| 1742 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper) |
| 1743 | : ObjCTypes.getSendFp2retFn(IsSuper); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1744 | } else { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1745 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1746 | : ObjCTypes.getSendFn(IsSuper); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1747 | } |
Fariborz Jahanian | 3c267e6 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1748 | |
| 1749 | bool requiresnullCheck = false; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1750 | if (CGM.getLangOpts().ObjCAutoRefCount && Method) |
Fariborz Jahanian | 3c267e6 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1751 | for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(), |
| 1752 | e = Method->param_end(); i != e; ++i) { |
| 1753 | const ParmVarDecl *ParamDecl = (*i); |
| 1754 | if (ParamDecl->hasAttr<NSConsumedAttr>()) { |
| 1755 | if (!nullReturn.NullBB) |
| 1756 | nullReturn.init(CGF, Arg0); |
| 1757 | requiresnullCheck = true; |
| 1758 | break; |
| 1759 | } |
| 1760 | } |
| 1761 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1762 | Fn = llvm::ConstantExpr::getBitCast(Fn, MSI.MessengerType); |
| 1763 | RValue rvalue = CGF.EmitCall(MSI.CallInfo, Fn, Return, ActualArgs); |
Fariborz Jahanian | 3c267e6 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1764 | return nullReturn.complete(CGF, rvalue, ResultType, CallArgs, |
| 1765 | requiresnullCheck ? Method : 0); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1768 | static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) { |
| 1769 | if (FQT.isObjCGCStrong()) |
| 1770 | return Qualifiers::Strong; |
| 1771 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1772 | if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak) |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1773 | return Qualifiers::Weak; |
| 1774 | |
Fariborz Jahanian | ba83c95 | 2012-02-16 00:15:02 +0000 | [diff] [blame] | 1775 | // check for __unsafe_unretained |
| 1776 | if (FQT.getObjCLifetime() == Qualifiers::OCL_ExplicitNone) |
| 1777 | return Qualifiers::GCNone; |
| 1778 | |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1779 | if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType()) |
| 1780 | return Qualifiers::Strong; |
| 1781 | |
| 1782 | if (const PointerType *PT = FQT->getAs<PointerType>()) |
| 1783 | return GetGCAttrTypeForType(Ctx, PT->getPointeeType()); |
| 1784 | |
| 1785 | return Qualifiers::GCNone; |
| 1786 | } |
| 1787 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1788 | llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM, |
| 1789 | const CGBlockInfo &blockInfo) { |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 1790 | llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1791 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1792 | if (CGM.getLangOpts().getGC() == LangOptions::NonGC && |
| 1793 | !CGM.getLangOpts().ObjCAutoRefCount) |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1794 | return nullPtr; |
| 1795 | |
Fariborz Jahanian | a1f024c | 2010-08-06 16:28:55 +0000 | [diff] [blame] | 1796 | bool hasUnion = false; |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1797 | SkipIvars.clear(); |
| 1798 | IvarsInfo.clear(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1799 | unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0); |
| 1800 | unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth(); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1801 | |
Fariborz Jahanian | 8197982 | 2010-09-09 00:21:45 +0000 | [diff] [blame] | 1802 | // __isa is the first field in block descriptor and must assume by runtime's |
| 1803 | // convention that it is GC'able. |
| 1804 | IvarsInfo.push_back(GC_IVAR(0, 1)); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1805 | |
| 1806 | const BlockDecl *blockDecl = blockInfo.getBlockDecl(); |
| 1807 | |
| 1808 | // Calculate the basic layout of the block structure. |
| 1809 | const llvm::StructLayout *layout = |
| 1810 | CGM.getTargetData().getStructLayout(blockInfo.StructureType); |
| 1811 | |
| 1812 | // Ignore the optional 'this' capture: C++ objects are not assumed |
| 1813 | // to be GC'ed. |
| 1814 | |
| 1815 | // Walk the captured variables. |
| 1816 | for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(), |
| 1817 | ce = blockDecl->capture_end(); ci != ce; ++ci) { |
| 1818 | const VarDecl *variable = ci->getVariable(); |
| 1819 | QualType type = variable->getType(); |
| 1820 | |
| 1821 | const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); |
| 1822 | |
| 1823 | // Ignore constant captures. |
| 1824 | if (capture.isConstant()) continue; |
| 1825 | |
| 1826 | uint64_t fieldOffset = layout->getElementOffset(capture.getIndex()); |
| 1827 | |
| 1828 | // __block variables are passed by their descriptor address. |
| 1829 | if (ci->isByRef()) { |
| 1830 | IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1)); |
Fariborz Jahanian | c5904b4 | 2010-09-11 01:27:29 +0000 | [diff] [blame] | 1831 | continue; |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1832 | } |
| 1833 | |
| 1834 | assert(!type->isArrayType() && "array variable should not be caught"); |
| 1835 | if (const RecordType *record = type->getAs<RecordType>()) { |
| 1836 | BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion); |
Fariborz Jahanian | e1a4898 | 2010-08-05 21:00:25 +0000 | [diff] [blame] | 1837 | continue; |
| 1838 | } |
Fariborz Jahanian | a1f024c | 2010-08-06 16:28:55 +0000 | [diff] [blame] | 1839 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1840 | Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type); |
| 1841 | unsigned fieldSize = CGM.getContext().getTypeSize(type); |
| 1842 | |
| 1843 | if (GCAttr == Qualifiers::Strong) |
| 1844 | IvarsInfo.push_back(GC_IVAR(fieldOffset, |
| 1845 | fieldSize / WordSizeInBits)); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1846 | else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak) |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1847 | SkipIvars.push_back(GC_IVAR(fieldOffset, |
| 1848 | fieldSize / ByteSizeInBits)); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1849 | } |
| 1850 | |
| 1851 | if (IvarsInfo.empty()) |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1852 | return nullPtr; |
| 1853 | |
| 1854 | // Sort on byte position; captures might not be allocated in order, |
| 1855 | // and unions can do funny things. |
| 1856 | llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end()); |
| 1857 | llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end()); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1858 | |
| 1859 | std::string BitMap; |
Fariborz Jahanian | b8fd2eb | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 1860 | llvm::Constant *C = BuildIvarLayoutBitmap(BitMap); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1861 | if (CGM.getLangOpts().ObjCGCBitmapPrint) { |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1862 | printf("\n block variable layout for block: "); |
Roman Divacky | 31ba613 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 1863 | const unsigned char *s = (const unsigned char*)BitMap.c_str(); |
Bill Wendling | 13562a1 | 2012-02-07 09:06:01 +0000 | [diff] [blame] | 1864 | for (unsigned i = 0, e = BitMap.size(); i < e; i++) |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1865 | if (!(s[i] & 0xf0)) |
| 1866 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 1867 | else |
| 1868 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 1869 | printf("\n"); |
| 1870 | } |
| 1871 | |
| 1872 | return C; |
Fariborz Jahanian | 89ecd41 | 2010-08-04 16:57:49 +0000 | [diff] [blame] | 1873 | } |
| 1874 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1875 | llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1876 | const ObjCProtocolDecl *PD) { |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1877 | // 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] | 1878 | // resolved. Investigate. Its also wasteful to look this up over and over. |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1879 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1880 | |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1881 | return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), |
Douglas Gregor | 4c86fdb | 2012-01-17 18:36:30 +0000 | [diff] [blame] | 1882 | ObjCTypes.getExternalProtocolPtrTy()); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1883 | } |
| 1884 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1885 | void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1886 | // FIXME: We shouldn't need this, the protocol decl should contain enough |
| 1887 | // information to tell us whether this was a declaration or a definition. |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1888 | DefinedProtocols.insert(PD->getIdentifier()); |
| 1889 | |
| 1890 | // If we have generated a forward reference to this protocol, emit |
| 1891 | // it now. Otherwise do nothing, the protocol objects are lazily |
| 1892 | // emitted. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1893 | if (Protocols.count(PD->getIdentifier())) |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1894 | GetOrEmitProtocol(PD); |
| 1895 | } |
| 1896 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1897 | llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1898 | if (DefinedProtocols.count(PD->getIdentifier())) |
| 1899 | return GetOrEmitProtocol(PD); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 1900 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1901 | return GetOrEmitProtocolRef(PD); |
| 1902 | } |
| 1903 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1904 | /* |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1905 | // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions |
| 1906 | struct _objc_protocol { |
| 1907 | struct _objc_protocol_extension *isa; |
| 1908 | char *protocol_name; |
| 1909 | struct _objc_protocol_list *protocol_list; |
| 1910 | struct _objc__method_prototype_list *instance_methods; |
| 1911 | struct _objc__method_prototype_list *class_methods |
| 1912 | }; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1913 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1914 | See EmitProtocolExtension(). |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1915 | */ |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1916 | llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) { |
John McCall | 50651b9 | 2012-03-30 21:29:05 +0000 | [diff] [blame] | 1917 | llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()]; |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1918 | |
| 1919 | // Early exit if a defining object has already been generated. |
| 1920 | if (Entry && Entry->hasInitializer()) |
| 1921 | return Entry; |
| 1922 | |
Douglas Gregor | 1d784b2 | 2012-01-01 19:51:50 +0000 | [diff] [blame] | 1923 | // Use the protocol definition, if there is one. |
| 1924 | if (const ObjCProtocolDecl *Def = PD->getDefinition()) |
| 1925 | PD = Def; |
| 1926 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1927 | // 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] | 1928 | // resolved. Investigate. Its also wasteful to look this up over and over. |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1929 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1930 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1931 | // Construct method lists. |
| 1932 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1933 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1934 | std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1935 | for (ObjCProtocolDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1936 | i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1937 | ObjCMethodDecl *MD = *i; |
| 1938 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 1939 | if (!C) |
| 1940 | return GetOrEmitProtocolRef(PD); |
| 1941 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1942 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1943 | OptInstanceMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1944 | OptMethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1945 | } else { |
| 1946 | InstanceMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1947 | MethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1948 | } |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1949 | } |
| 1950 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1951 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1952 | i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1953 | ObjCMethodDecl *MD = *i; |
| 1954 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 1955 | if (!C) |
| 1956 | return GetOrEmitProtocolRef(PD); |
| 1957 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1958 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1959 | OptClassMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1960 | OptMethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1961 | } else { |
| 1962 | ClassMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1963 | MethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1964 | } |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1965 | } |
| 1966 | |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1967 | MethodTypesExt.insert(MethodTypesExt.end(), |
| 1968 | OptMethodTypesExt.begin(), OptMethodTypesExt.end()); |
| 1969 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1970 | llvm::Constant *Values[] = { |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1971 | EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods, |
| 1972 | MethodTypesExt), |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1973 | GetClassName(PD->getIdentifier()), |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1974 | EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1975 | PD->protocol_begin(), |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1976 | PD->protocol_end()), |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1977 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1978 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1979 | InstanceMethods), |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1980 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1981 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1982 | ClassMethods) |
| 1983 | }; |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 1984 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1985 | Values); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1986 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1987 | if (Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1988 | // Already created, fix the linkage and update the initializer. |
| 1989 | Entry->setLinkage(llvm::GlobalValue::InternalLinkage); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1990 | Entry->setInitializer(Init); |
| 1991 | } else { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1992 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1993 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1994 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1995 | Init, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1996 | "\01L_OBJC_PROTOCOL_" + PD->getName()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1997 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1998 | // FIXME: Is this necessary? Why only for protocol? |
| 1999 | Entry->setAlignment(4); |
John McCall | 50651b9 | 2012-03-30 21:29:05 +0000 | [diff] [blame] | 2000 | |
| 2001 | Protocols[PD->getIdentifier()] = Entry; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2002 | } |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2003 | CGM.AddUsedGlobal(Entry); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2004 | |
| 2005 | return Entry; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2006 | } |
| 2007 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2008 | llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2009 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 2010 | |
| 2011 | if (!Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2012 | // We use the initializer as a marker of whether this is a forward |
| 2013 | // reference or not. At module finalization we add the empty |
| 2014 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2015 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2016 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2017 | llvm::GlobalValue::ExternalLinkage, |
| 2018 | 0, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2019 | "\01L_OBJC_PROTOCOL_" + PD->getName()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2020 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2021 | // FIXME: Is this necessary? Why only for protocol? |
| 2022 | Entry->setAlignment(4); |
| 2023 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2024 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2025 | return Entry; |
| 2026 | } |
| 2027 | |
| 2028 | /* |
| 2029 | struct _objc_protocol_extension { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2030 | uint32_t size; |
| 2031 | struct objc_method_description_list *optional_instance_methods; |
| 2032 | struct objc_method_description_list *optional_class_methods; |
| 2033 | struct objc_property_list *instance_properties; |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2034 | const char ** extendedMethodTypes; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2035 | }; |
| 2036 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2037 | llvm::Constant * |
| 2038 | CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD, |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2039 | ArrayRef<llvm::Constant*> OptInstanceMethods, |
| 2040 | ArrayRef<llvm::Constant*> OptClassMethods, |
| 2041 | ArrayRef<llvm::Constant*> MethodTypesExt) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2042 | uint64_t Size = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2043 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy); |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2044 | llvm::Constant *Values[] = { |
| 2045 | llvm::ConstantInt::get(ObjCTypes.IntTy, Size), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2046 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2047 | + PD->getName(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2048 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2049 | OptInstanceMethods), |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2050 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2051 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2052 | OptClassMethods), |
| 2053 | EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2054 | ObjCTypes), |
| 2055 | EmitProtocolMethodTypes("\01L_OBJC_PROTOCOL_METHOD_TYPES_" + PD->getName(), |
| 2056 | MethodTypesExt, ObjCTypes) |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2057 | }; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2058 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2059 | // Return null if no extension bits are used. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2060 | if (Values[1]->isNullValue() && Values[2]->isNullValue() && |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2061 | Values[3]->isNullValue() && Values[4]->isNullValue()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2062 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2063 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2064 | llvm::Constant *Init = |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2065 | llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2066 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2067 | // No special section, but goes in llvm.used |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2068 | return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2069 | Init, |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2070 | 0, 0, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2071 | } |
| 2072 | |
| 2073 | /* |
| 2074 | struct objc_protocol_list { |
Bill Wendling | 3964e62 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2075 | struct objc_protocol_list *next; |
| 2076 | long count; |
| 2077 | Protocol *list[]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2078 | }; |
| 2079 | */ |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 2080 | llvm::Constant * |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2081 | CGObjCMac::EmitProtocolList(Twine Name, |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 2082 | ObjCProtocolDecl::protocol_iterator begin, |
| 2083 | ObjCProtocolDecl::protocol_iterator end) { |
Bill Wendling | 3964e62 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2084 | llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2085 | |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 2086 | for (; begin != end; ++begin) |
| 2087 | ProtocolRefs.push_back(GetProtocolRef(*begin)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2088 | |
| 2089 | // Just return null for empty protocol lists |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2090 | if (ProtocolRefs.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2091 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2092 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2093 | // This list is null terminated. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2094 | ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2095 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2096 | llvm::Constant *Values[3]; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2097 | // This field is only used by the runtime. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2098 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2099 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2100 | ProtocolRefs.size() - 1); |
| 2101 | Values[2] = |
| 2102 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy, |
| 2103 | ProtocolRefs.size()), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2104 | ProtocolRefs); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2105 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2106 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2107 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2108 | CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2109 | 4, false); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2110 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2111 | } |
| 2112 | |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2113 | void CGObjCCommonMac:: |
| 2114 | PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*,16> &PropertySet, |
Bill Wendling | 3964e62 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2115 | llvm::SmallVectorImpl<llvm::Constant*> &Properties, |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2116 | const Decl *Container, |
| 2117 | const ObjCProtocolDecl *PROTO, |
| 2118 | const ObjCCommonTypesHelper &ObjCTypes) { |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2119 | for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(), |
| 2120 | E = PROTO->protocol_end(); P != E; ++P) |
| 2121 | PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes); |
| 2122 | for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(), |
| 2123 | E = PROTO->prop_end(); I != E; ++I) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2124 | const ObjCPropertyDecl *PD = *I; |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2125 | if (!PropertySet.insert(PD->getIdentifier())) |
| 2126 | continue; |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2127 | llvm::Constant *Prop[] = { |
| 2128 | GetPropertyName(PD->getIdentifier()), |
| 2129 | GetPropertyTypeString(PD, Container) |
| 2130 | }; |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2131 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop)); |
| 2132 | } |
| 2133 | } |
| 2134 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2135 | /* |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2136 | struct _objc_property { |
Bill Wendling | 3964e62 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2137 | const char * const name; |
| 2138 | const char * const attributes; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2139 | }; |
| 2140 | |
| 2141 | struct _objc_property_list { |
Bill Wendling | 3964e62 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2142 | uint32_t entsize; // sizeof (struct _objc_property) |
| 2143 | uint32_t prop_count; |
| 2144 | struct _objc_property[prop_count]; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2145 | }; |
| 2146 | */ |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2147 | llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2148 | const Decl *Container, |
| 2149 | const ObjCContainerDecl *OCD, |
| 2150 | const ObjCCommonTypesHelper &ObjCTypes) { |
Bill Wendling | 3964e62 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2151 | llvm::SmallVector<llvm::Constant*, 16> Properties; |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2152 | llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2153 | for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(), |
| 2154 | E = OCD->prop_end(); I != E; ++I) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2155 | const ObjCPropertyDecl *PD = *I; |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2156 | PropertySet.insert(PD->getIdentifier()); |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2157 | llvm::Constant *Prop[] = { |
| 2158 | GetPropertyName(PD->getIdentifier()), |
| 2159 | GetPropertyTypeString(PD, Container) |
| 2160 | }; |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2161 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2162 | Prop)); |
| 2163 | } |
Fariborz Jahanian | 6afbdf5 | 2010-06-22 16:33:55 +0000 | [diff] [blame] | 2164 | if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 2165 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 2166 | P = OID->all_referenced_protocol_begin(), |
| 2167 | E = OID->all_referenced_protocol_end(); P != E; ++P) |
Fariborz Jahanian | 6afbdf5 | 2010-06-22 16:33:55 +0000 | [diff] [blame] | 2168 | PushProtocolProperties(PropertySet, Properties, Container, (*P), |
| 2169 | ObjCTypes); |
| 2170 | } |
| 2171 | else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) { |
| 2172 | for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(), |
| 2173 | E = CD->protocol_end(); P != E; ++P) |
| 2174 | PushProtocolProperties(PropertySet, Properties, Container, (*P), |
| 2175 | ObjCTypes); |
| 2176 | } |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2177 | |
| 2178 | // Return null for empty list. |
| 2179 | if (Properties.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2180 | return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2181 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2182 | unsigned PropertySize = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2183 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2184 | llvm::Constant *Values[3]; |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2185 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize); |
| 2186 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2187 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy, |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2188 | Properties.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2189 | Values[2] = llvm::ConstantArray::get(AT, Properties); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2190 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2191 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2192 | llvm::GlobalVariable *GV = |
| 2193 | CreateMetadataVar(Name, Init, |
| 2194 | (ObjCABI == 2) ? "__DATA, __objc_const" : |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2195 | "__OBJC,__property,regular,no_dead_strip", |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2196 | (ObjCABI == 2) ? 8 : 4, |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2197 | true); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2198 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2199 | } |
| 2200 | |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2201 | llvm::Constant * |
| 2202 | CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name, |
| 2203 | ArrayRef<llvm::Constant*> MethodTypes, |
| 2204 | const ObjCCommonTypesHelper &ObjCTypes) { |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2205 | // Return null for empty list. |
| 2206 | if (MethodTypes.empty()) |
| 2207 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy); |
| 2208 | |
| 2209 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 2210 | MethodTypes.size()); |
| 2211 | llvm::Constant *Init = llvm::ConstantArray::get(AT, MethodTypes); |
| 2212 | |
| 2213 | llvm::GlobalVariable *GV = |
| 2214 | CreateMetadataVar(Name, Init, |
| 2215 | (ObjCABI == 2) ? "__DATA, __objc_const" : 0, |
| 2216 | (ObjCABI == 2) ? 8 : 4, |
| 2217 | true); |
| 2218 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.Int8PtrPtrTy); |
| 2219 | } |
| 2220 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2221 | /* |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2222 | struct objc_method_description_list { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2223 | int count; |
| 2224 | struct objc_method_description list[]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2225 | }; |
| 2226 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2227 | llvm::Constant * |
| 2228 | CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2229 | llvm::Constant *Desc[] = { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2230 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2231 | ObjCTypes.SelectorPtrTy), |
| 2232 | GetMethodVarType(MD) |
| 2233 | }; |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 2234 | if (!Desc[1]) |
| 2235 | return 0; |
| 2236 | |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2237 | return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2238 | Desc); |
| 2239 | } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2240 | |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2241 | llvm::Constant * |
| 2242 | CGObjCMac::EmitMethodDescList(Twine Name, const char *Section, |
| 2243 | ArrayRef<llvm::Constant*> Methods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2244 | // Return null for empty list. |
| 2245 | if (Methods.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2246 | return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2247 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2248 | llvm::Constant *Values[2]; |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2249 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2250 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2251 | Methods.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2252 | Values[1] = llvm::ConstantArray::get(AT, Methods); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2253 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2254 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2255 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2256 | return llvm::ConstantExpr::getBitCast(GV, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2257 | ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2258 | } |
| 2259 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2260 | /* |
| 2261 | struct _objc_category { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2262 | char *category_name; |
| 2263 | char *class_name; |
| 2264 | struct _objc_method_list *instance_methods; |
| 2265 | struct _objc_method_list *class_methods; |
| 2266 | struct _objc_protocol_list *protocols; |
| 2267 | uint32_t size; // <rdar://4585769> |
| 2268 | struct _objc_property_list *instance_properties; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2269 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2270 | */ |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2271 | void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2272 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2273 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2274 | // FIXME: This is poor design, the OCD should have a pointer to the category |
| 2275 | // decl. Additionally, note that Category can be null for the @implementation |
| 2276 | // w/o an @interface case. Sema should just create one for us as it does for |
| 2277 | // @implementation so everyone else can live life under a clear blue sky. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2278 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2279 | const ObjCCategoryDecl *Category = |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2280 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2281 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2282 | SmallString<256> ExtName; |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2283 | llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_' |
| 2284 | << OCD->getName(); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2285 | |
Bill Wendling | 3964e62 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2286 | llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2287 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2288 | i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2289 | // Instance methods should always be defined. |
| 2290 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 2291 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2292 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2293 | i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2294 | // Class methods should always be defined. |
| 2295 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 2296 | } |
| 2297 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2298 | llvm::Constant *Values[7]; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2299 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 2300 | Values[1] = GetClassName(Interface->getIdentifier()); |
Fariborz Jahanian | 679cd7f | 2009-04-29 20:40:05 +0000 | [diff] [blame] | 2301 | LazySymbols.insert(Interface->getIdentifier()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2302 | Values[2] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2303 | EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(), |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2304 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2305 | InstanceMethods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2306 | Values[3] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2307 | EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2308 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2309 | ClassMethods); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2310 | if (Category) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2311 | Values[4] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2312 | EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2313 | Category->protocol_begin(), |
| 2314 | Category->protocol_end()); |
| 2315 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2316 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2317 | } |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2318 | Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2319 | |
| 2320 | // If there is no category @interface then there can be no properties. |
| 2321 | if (Category) { |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2322 | Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 2323 | OCD, Category, ObjCTypes); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2324 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2325 | Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2326 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2327 | |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2328 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2329 | Values); |
| 2330 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2331 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2332 | CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init, |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2333 | "__OBJC,__category,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2334 | 4, true); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2335 | DefinedCategories.push_back(GV); |
Fariborz Jahanian | b9c5b3d | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 2336 | DefinedCategoryNames.insert(ExtName.str()); |
Fariborz Jahanian | 64089ce | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 2337 | // method definition entries must be clear for next implementation. |
| 2338 | MethodDefinitions.clear(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2339 | } |
| 2340 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2341 | // FIXME: Get from somewhere? |
| 2342 | enum ClassFlags { |
| 2343 | eClassFlags_Factory = 0x00001, |
| 2344 | eClassFlags_Meta = 0x00002, |
| 2345 | // <rdr://5142207> |
| 2346 | eClassFlags_HasCXXStructors = 0x02000, |
| 2347 | eClassFlags_Hidden = 0x20000, |
| 2348 | eClassFlags_ABI2_Hidden = 0x00010, |
| 2349 | eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634> |
| 2350 | }; |
| 2351 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2352 | /* |
| 2353 | struct _objc_class { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2354 | Class isa; |
| 2355 | Class super_class; |
| 2356 | const char *name; |
| 2357 | long version; |
| 2358 | long info; |
| 2359 | long instance_size; |
| 2360 | struct _objc_ivar_list *ivars; |
| 2361 | struct _objc_method_list *methods; |
| 2362 | struct _objc_cache *cache; |
| 2363 | struct _objc_protocol_list *protocols; |
| 2364 | // Objective-C 1.0 extensions (<rdr://4585769>) |
| 2365 | const char *ivar_layout; |
| 2366 | struct _objc_class_ext *ext; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2367 | }; |
| 2368 | |
| 2369 | See EmitClassExtension(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2370 | */ |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2371 | void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2372 | DefinedSymbols.insert(ID->getIdentifier()); |
| 2373 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2374 | std::string ClassName = ID->getNameAsString(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2375 | // FIXME: Gross |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2376 | ObjCInterfaceDecl *Interface = |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2377 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2378 | llvm::Constant *Protocols = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2379 | EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(), |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 2380 | Interface->all_referenced_protocol_begin(), |
| 2381 | Interface->all_referenced_protocol_end()); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2382 | unsigned Flags = eClassFlags_Factory; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2383 | if (ID->hasCXXStructors()) |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 2384 | Flags |= eClassFlags_HasCXXStructors; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2385 | unsigned Size = |
Ken Dyck | 5f022d8 | 2011-02-09 01:59:34 +0000 | [diff] [blame] | 2386 | CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2387 | |
| 2388 | // FIXME: Set CXX-structors flag. |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 2389 | if (ID->getClassInterface()->getVisibility() == HiddenVisibility) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2390 | Flags |= eClassFlags_Hidden; |
| 2391 | |
Bill Wendling | 3964e62 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 2392 | llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2393 | for (ObjCImplementationDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2394 | i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2395 | // Instance methods should always be defined. |
| 2396 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 2397 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2398 | for (ObjCImplementationDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2399 | i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2400 | // Class methods should always be defined. |
| 2401 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 2402 | } |
| 2403 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2404 | for (ObjCImplementationDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2405 | i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2406 | ObjCPropertyImplDecl *PID = *i; |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2407 | |
| 2408 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 2409 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 2410 | |
| 2411 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 2412 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 2413 | InstanceMethods.push_back(C); |
| 2414 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 2415 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 2416 | InstanceMethods.push_back(C); |
| 2417 | } |
| 2418 | } |
| 2419 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2420 | llvm::Constant *Values[12]; |
Daniel Dunbar | 5384b09 | 2009-05-03 08:56:52 +0000 | [diff] [blame] | 2421 | Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2422 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2423 | // Record a reference to the super class. |
| 2424 | LazySymbols.insert(Super->getIdentifier()); |
| 2425 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2426 | Values[ 1] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2427 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2428 | ObjCTypes.ClassPtrTy); |
| 2429 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2430 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2431 | } |
| 2432 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 2433 | // Version is always 0. |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2434 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2435 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 2436 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2437 | Values[ 6] = EmitIvarList(ID, false); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2438 | Values[ 7] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2439 | EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(), |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2440 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2441 | InstanceMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2442 | // cache is always NULL. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2443 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2444 | Values[ 9] = Protocols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2445 | Values[10] = BuildIvarLayout(ID, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2446 | Values[11] = EmitClassExtension(ID); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2447 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2448 | Values); |
Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 2449 | std::string Name("\01L_OBJC_CLASS_"); |
| 2450 | Name += ClassName; |
| 2451 | const char *Section = "__OBJC,__class,regular,no_dead_strip"; |
| 2452 | // Check for a forward reference. |
| 2453 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 2454 | if (GV) { |
| 2455 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2456 | "Forward metaclass reference has incorrect type."); |
| 2457 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 2458 | GV->setInitializer(Init); |
| 2459 | GV->setSection(Section); |
| 2460 | GV->setAlignment(4); |
| 2461 | CGM.AddUsedGlobal(GV); |
| 2462 | } |
| 2463 | else |
| 2464 | GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2465 | DefinedClasses.push_back(GV); |
Fariborz Jahanian | 64089ce | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 2466 | // method definition entries must be clear for next implementation. |
| 2467 | MethodDefinitions.clear(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2468 | } |
| 2469 | |
| 2470 | llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID, |
| 2471 | llvm::Constant *Protocols, |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2472 | ArrayRef<llvm::Constant*> Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2473 | unsigned Flags = eClassFlags_Meta; |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2474 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2475 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 2476 | if (ID->getClassInterface()->getVisibility() == HiddenVisibility) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2477 | Flags |= eClassFlags_Hidden; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2478 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2479 | llvm::Constant *Values[12]; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2480 | // The isa for the metaclass is the root of the hierarchy. |
| 2481 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 2482 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 2483 | Root = Super; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2484 | Values[ 0] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2485 | llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2486 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2487 | // The super class for the metaclass is emitted as the name of the |
| 2488 | // super class. The runtime fixes this up to point to the |
| 2489 | // *metaclass* for the super class. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2490 | if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2491 | Values[ 1] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2492 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2493 | ObjCTypes.ClassPtrTy); |
| 2494 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2495 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2496 | } |
| 2497 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 2498 | // Version is always 0. |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2499 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2500 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 2501 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2502 | Values[ 6] = EmitIvarList(ID, true); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2503 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2504 | EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2505 | "__OBJC,__cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2506 | Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2507 | // cache is always NULL. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2508 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2509 | Values[ 9] = Protocols; |
| 2510 | // ivar_layout for metaclass is always NULL. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2511 | Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2512 | // The class extension is always unused for metaclasses. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2513 | Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2514 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2515 | Values); |
| 2516 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2517 | std::string Name("\01L_OBJC_METACLASS_"); |
Benjamin Kramer | 94be8ea | 2012-07-31 11:45:39 +0000 | [diff] [blame] | 2518 | Name += ID->getName(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2519 | |
| 2520 | // Check for a forward reference. |
| 2521 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 2522 | if (GV) { |
| 2523 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2524 | "Forward metaclass reference has incorrect type."); |
| 2525 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 2526 | GV->setInitializer(Init); |
| 2527 | } else { |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2528 | GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2529 | llvm::GlobalValue::InternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2530 | Init, Name); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2531 | } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2532 | GV->setSection("__OBJC,__meta_class,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2533 | GV->setAlignment(4); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2534 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2535 | |
| 2536 | return GV; |
| 2537 | } |
| 2538 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2539 | llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2540 | std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2541 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2542 | // FIXME: Should we look these up somewhere other than the module. Its a bit |
| 2543 | // silly since we only generate these while processing an implementation, so |
| 2544 | // exactly one pointer would work if know when we entered/exitted an |
| 2545 | // implementation block. |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2546 | |
| 2547 | // Check for an existing forward reference. |
Fariborz Jahanian | b0d2794 | 2009-01-07 20:11:22 +0000 | [diff] [blame] | 2548 | // Previously, metaclass with internal linkage may have been defined. |
| 2549 | // pass 'true' as 2nd argument so it is returned. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2550 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, |
| 2551 | true)) { |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2552 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2553 | "Forward metaclass reference has incorrect type."); |
| 2554 | return GV; |
| 2555 | } else { |
| 2556 | // Generate as an external reference to keep a consistent |
| 2557 | // module. This will be patched up when we emit the metaclass. |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2558 | return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2559 | llvm::GlobalValue::ExternalLinkage, |
| 2560 | 0, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2561 | Name); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2562 | } |
| 2563 | } |
| 2564 | |
Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 2565 | llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) { |
| 2566 | std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString(); |
| 2567 | |
| 2568 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, |
| 2569 | true)) { |
| 2570 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2571 | "Forward class metadata reference has incorrect type."); |
| 2572 | return GV; |
| 2573 | } else { |
| 2574 | return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
| 2575 | llvm::GlobalValue::ExternalLinkage, |
| 2576 | 0, |
| 2577 | Name); |
| 2578 | } |
| 2579 | } |
| 2580 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2581 | /* |
| 2582 | struct objc_class_ext { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2583 | uint32_t size; |
| 2584 | const char *weak_ivar_layout; |
| 2585 | struct _objc_property_list *properties; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2586 | }; |
| 2587 | */ |
| 2588 | llvm::Constant * |
| 2589 | CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2590 | uint64_t Size = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2591 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2592 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2593 | llvm::Constant *Values[3]; |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2594 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 2595 | Values[1] = BuildIvarLayout(ID, false); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2596 | Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 2597 | ID, ID->getClassInterface(), ObjCTypes); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2598 | |
| 2599 | // Return null if no extension bits are used. |
| 2600 | if (Values[1]->isNullValue() && Values[2]->isNullValue()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2601 | return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2602 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2603 | llvm::Constant *Init = |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2604 | llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2605 | return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2606 | Init, "__OBJC,__class_ext,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2607 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2608 | } |
| 2609 | |
| 2610 | /* |
| 2611 | struct objc_ivar { |
Bill Wendling | 795b100 | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 2612 | char *ivar_name; |
| 2613 | char *ivar_type; |
| 2614 | int ivar_offset; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2615 | }; |
| 2616 | |
| 2617 | struct objc_ivar_list { |
Bill Wendling | 795b100 | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 2618 | int ivar_count; |
| 2619 | struct objc_ivar list[count]; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2620 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2621 | */ |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2622 | llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2623 | bool ForClass) { |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2624 | std::vector<llvm::Constant*> Ivars; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2625 | |
| 2626 | // When emitting the root class GCC emits ivar entries for the |
| 2627 | // actual class structure. It is not clear if we need to follow this |
| 2628 | // behavior; for now lets try and get away with not doing it. If so, |
| 2629 | // the cleanest solution would be to make up an ObjCInterfaceDecl |
| 2630 | // for the class. |
| 2631 | if (ForClass) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2632 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2633 | |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 2634 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2635 | |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 2636 | for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin(); |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 2637 | IVD; IVD = IVD->getNextIvar()) { |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 2638 | // Ignore unnamed bit-fields. |
| 2639 | if (!IVD->getDeclName()) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2640 | continue; |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2641 | llvm::Constant *Ivar[] = { |
| 2642 | GetMethodVarName(IVD->getIdentifier()), |
| 2643 | GetMethodVarType(IVD), |
| 2644 | llvm::ConstantInt::get(ObjCTypes.IntTy, |
| 2645 | ComputeIvarBaseOffset(CGM, OID, IVD)) |
| 2646 | }; |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2647 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2648 | } |
| 2649 | |
| 2650 | // Return null for empty list. |
| 2651 | if (Ivars.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2652 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2653 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2654 | llvm::Constant *Values[2]; |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2655 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2656 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2657 | Ivars.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2658 | Values[1] = llvm::ConstantArray::get(AT, Ivars); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2659 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2660 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2661 | llvm::GlobalVariable *GV; |
| 2662 | if (ForClass) |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2663 | GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2664 | Init, "__OBJC,__class_vars,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2665 | 4, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2666 | else |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2667 | GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2668 | Init, "__OBJC,__instance_vars,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2669 | 4, true); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2670 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2671 | } |
| 2672 | |
| 2673 | /* |
| 2674 | struct objc_method { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2675 | SEL method_name; |
| 2676 | char *method_types; |
| 2677 | void *method; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2678 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2679 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2680 | struct objc_method_list { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2681 | struct objc_method_list *obsolete; |
| 2682 | int count; |
| 2683 | struct objc_method methods_list[count]; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2684 | }; |
| 2685 | */ |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2686 | |
| 2687 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 2688 | /// given method if it has been defined. The result is null if the |
| 2689 | /// method has not been defined. The return value has type MethodPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2690 | llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) { |
Argyrios Kyrtzidis | 9d50c06 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 2691 | llvm::Function *Fn = GetMethodDefinition(MD); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2692 | if (!Fn) |
| 2693 | return 0; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2694 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2695 | llvm::Constant *Method[] = { |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2696 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2697 | ObjCTypes.SelectorPtrTy), |
| 2698 | GetMethodVarType(MD), |
| 2699 | llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy) |
| 2700 | }; |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2701 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2702 | } |
| 2703 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2704 | llvm::Constant *CGObjCMac::EmitMethodList(Twine Name, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2705 | const char *Section, |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 2706 | ArrayRef<llvm::Constant*> Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2707 | // Return null for empty list. |
| 2708 | if (Methods.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2709 | return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2710 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2711 | llvm::Constant *Values[3]; |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2712 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2713 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2714 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2715 | Methods.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2716 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2717 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2718 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2719 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2720 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2721 | } |
| 2722 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 2723 | llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2724 | const ObjCContainerDecl *CD) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2725 | SmallString<256> Name; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 2726 | GetNameForMethod(OMD, CD, Name); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2727 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2728 | CodeGenTypes &Types = CGM.getTypes(); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2729 | llvm::FunctionType *MethodTy = |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2730 | Types.GetFunctionType(Types.arrangeObjCMethodDeclaration(OMD)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2731 | llvm::Function *Method = |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 2732 | llvm::Function::Create(MethodTy, |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2733 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 2734 | Name.str(), |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2735 | &CGM.getModule()); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2736 | MethodDefinitions.insert(std::make_pair(OMD, Method)); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2737 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2738 | return Method; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2739 | } |
| 2740 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2741 | llvm::GlobalVariable * |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2742 | CGObjCCommonMac::CreateMetadataVar(Twine Name, |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2743 | llvm::Constant *Init, |
| 2744 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2745 | unsigned Align, |
| 2746 | bool AddToUsed) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2747 | llvm::Type *Ty = Init->getType(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2748 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2749 | new llvm::GlobalVariable(CGM.getModule(), Ty, false, |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2750 | llvm::GlobalValue::InternalLinkage, Init, Name); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2751 | if (Section) |
| 2752 | GV->setSection(Section); |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2753 | if (Align) |
| 2754 | GV->setAlignment(Align); |
| 2755 | if (AddToUsed) |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2756 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2757 | return GV; |
| 2758 | } |
| 2759 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2760 | llvm::Function *CGObjCMac::ModuleInitFunction() { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2761 | // Abuse this interface function as a place to finalize. |
| 2762 | FinishModule(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2763 | return NULL; |
| 2764 | } |
| 2765 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2766 | llvm::Constant *CGObjCMac::GetPropertyGetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2767 | return ObjCTypes.getGetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2768 | } |
| 2769 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2770 | llvm::Constant *CGObjCMac::GetPropertySetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2771 | return ObjCTypes.getSetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2772 | } |
| 2773 | |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2774 | llvm::Constant *CGObjCMac::GetOptimizedPropertySetFunction(bool atomic, |
| 2775 | bool copy) { |
| 2776 | return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy); |
| 2777 | } |
| 2778 | |
David Chisnall | 8fac25d | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 2779 | llvm::Constant *CGObjCMac::GetGetStructFunction() { |
| 2780 | return ObjCTypes.getCopyStructFn(); |
| 2781 | } |
| 2782 | llvm::Constant *CGObjCMac::GetSetStructFunction() { |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 2783 | return ObjCTypes.getCopyStructFn(); |
| 2784 | } |
| 2785 | |
Fariborz Jahanian | e317302 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 2786 | llvm::Constant *CGObjCMac::GetCppAtomicObjectFunction() { |
| 2787 | return ObjCTypes.getCppAtomicObjectFunction(); |
| 2788 | } |
| 2789 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2790 | llvm::Constant *CGObjCMac::EnumerationMutationFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2791 | return ObjCTypes.getEnumerationMutationFn(); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 2792 | } |
| 2793 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2794 | void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) { |
| 2795 | return EmitTryOrSynchronizedStmt(CGF, S); |
| 2796 | } |
| 2797 | |
| 2798 | void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF, |
| 2799 | const ObjCAtSynchronizedStmt &S) { |
| 2800 | return EmitTryOrSynchronizedStmt(CGF, S); |
| 2801 | } |
| 2802 | |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2803 | namespace { |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 2804 | struct PerformFragileFinally : EHScopeStack::Cleanup { |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2805 | const Stmt &S; |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2806 | llvm::Value *SyncArgSlot; |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2807 | llvm::Value *CallTryExitVar; |
| 2808 | llvm::Value *ExceptionData; |
| 2809 | ObjCTypesHelper &ObjCTypes; |
| 2810 | PerformFragileFinally(const Stmt *S, |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2811 | llvm::Value *SyncArgSlot, |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2812 | llvm::Value *CallTryExitVar, |
| 2813 | llvm::Value *ExceptionData, |
| 2814 | ObjCTypesHelper *ObjCTypes) |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2815 | : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar), |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2816 | ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {} |
| 2817 | |
John McCall | ad346f4 | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 2818 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2819 | // Check whether we need to call objc_exception_try_exit. |
| 2820 | // In optimized code, this branch will always be folded. |
| 2821 | llvm::BasicBlock *FinallyCallExit = |
| 2822 | CGF.createBasicBlock("finally.call_exit"); |
| 2823 | llvm::BasicBlock *FinallyNoCallExit = |
| 2824 | CGF.createBasicBlock("finally.no_call_exit"); |
| 2825 | CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar), |
| 2826 | FinallyCallExit, FinallyNoCallExit); |
| 2827 | |
| 2828 | CGF.EmitBlock(FinallyCallExit); |
| 2829 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData) |
| 2830 | ->setDoesNotThrow(); |
| 2831 | |
| 2832 | CGF.EmitBlock(FinallyNoCallExit); |
| 2833 | |
| 2834 | if (isa<ObjCAtTryStmt>(S)) { |
| 2835 | if (const ObjCAtFinallyStmt* FinallyStmt = |
John McCall | d96a8e7 | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 2836 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) { |
| 2837 | // Save the current cleanup destination in case there's |
| 2838 | // control flow inside the finally statement. |
| 2839 | llvm::Value *CurCleanupDest = |
| 2840 | CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot()); |
| 2841 | |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2842 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
| 2843 | |
John McCall | d96a8e7 | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 2844 | if (CGF.HaveInsertPoint()) { |
| 2845 | CGF.Builder.CreateStore(CurCleanupDest, |
| 2846 | CGF.getNormalCleanupDestSlot()); |
| 2847 | } else { |
| 2848 | // Currently, the end of the cleanup must always exist. |
| 2849 | CGF.EnsureInsertPoint(); |
| 2850 | } |
| 2851 | } |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2852 | } else { |
| 2853 | // Emit objc_sync_exit(expr); as finally's sole statement for |
| 2854 | // @synchronized. |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2855 | llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot); |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2856 | CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg) |
| 2857 | ->setDoesNotThrow(); |
| 2858 | } |
| 2859 | } |
| 2860 | }; |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2861 | |
| 2862 | class FragileHazards { |
| 2863 | CodeGenFunction &CGF; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2864 | SmallVector<llvm::Value*, 20> Locals; |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2865 | llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry; |
| 2866 | |
| 2867 | llvm::InlineAsm *ReadHazard; |
| 2868 | llvm::InlineAsm *WriteHazard; |
| 2869 | |
| 2870 | llvm::FunctionType *GetAsmFnType(); |
| 2871 | |
| 2872 | void collectLocals(); |
| 2873 | void emitReadHazard(CGBuilderTy &Builder); |
| 2874 | |
| 2875 | public: |
| 2876 | FragileHazards(CodeGenFunction &CGF); |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2877 | |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2878 | void emitWriteHazard(); |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2879 | void emitHazardsInNewBlocks(); |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2880 | }; |
| 2881 | } |
| 2882 | |
| 2883 | /// Create the fragile-ABI read and write hazards based on the current |
| 2884 | /// state of the function, which is presumed to be immediately prior |
| 2885 | /// to a @try block. These hazards are used to maintain correct |
| 2886 | /// semantics in the face of optimization and the fragile ABI's |
| 2887 | /// cavalier use of setjmp/longjmp. |
| 2888 | FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) { |
| 2889 | collectLocals(); |
| 2890 | |
| 2891 | if (Locals.empty()) return; |
| 2892 | |
| 2893 | // Collect all the blocks in the function. |
| 2894 | for (llvm::Function::iterator |
| 2895 | I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I) |
| 2896 | BlocksBeforeTry.insert(&*I); |
| 2897 | |
| 2898 | llvm::FunctionType *AsmFnTy = GetAsmFnType(); |
| 2899 | |
| 2900 | // Create a read hazard for the allocas. This inhibits dead-store |
| 2901 | // optimizations and forces the values to memory. This hazard is |
| 2902 | // inserted before any 'throwing' calls in the protected scope to |
| 2903 | // reflect the possibility that the variables might be read from the |
| 2904 | // catch block if the call throws. |
| 2905 | { |
| 2906 | std::string Constraint; |
| 2907 | for (unsigned I = 0, E = Locals.size(); I != E; ++I) { |
| 2908 | if (I) Constraint += ','; |
| 2909 | Constraint += "*m"; |
| 2910 | } |
| 2911 | |
| 2912 | ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false); |
| 2913 | } |
| 2914 | |
| 2915 | // Create a write hazard for the allocas. This inhibits folding |
| 2916 | // loads across the hazard. This hazard is inserted at the |
| 2917 | // beginning of the catch path to reflect the possibility that the |
| 2918 | // variables might have been written within the protected scope. |
| 2919 | { |
| 2920 | std::string Constraint; |
| 2921 | for (unsigned I = 0, E = Locals.size(); I != E; ++I) { |
| 2922 | if (I) Constraint += ','; |
| 2923 | Constraint += "=*m"; |
| 2924 | } |
| 2925 | |
| 2926 | WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false); |
| 2927 | } |
| 2928 | } |
| 2929 | |
| 2930 | /// Emit a write hazard at the current location. |
| 2931 | void FragileHazards::emitWriteHazard() { |
| 2932 | if (Locals.empty()) return; |
| 2933 | |
Jay Foad | 4c7d9f1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 2934 | CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow(); |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2935 | } |
| 2936 | |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2937 | void FragileHazards::emitReadHazard(CGBuilderTy &Builder) { |
| 2938 | assert(!Locals.empty()); |
Jay Foad | 4c7d9f1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 2939 | Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow(); |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2940 | } |
| 2941 | |
| 2942 | /// Emit read hazards in all the protected blocks, i.e. all the blocks |
| 2943 | /// which have been inserted since the beginning of the try. |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2944 | void FragileHazards::emitHazardsInNewBlocks() { |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2945 | if (Locals.empty()) return; |
| 2946 | |
| 2947 | CGBuilderTy Builder(CGF.getLLVMContext()); |
| 2948 | |
| 2949 | // Iterate through all blocks, skipping those prior to the try. |
| 2950 | for (llvm::Function::iterator |
| 2951 | FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) { |
| 2952 | llvm::BasicBlock &BB = *FI; |
| 2953 | if (BlocksBeforeTry.count(&BB)) continue; |
| 2954 | |
| 2955 | // Walk through all the calls in the block. |
| 2956 | for (llvm::BasicBlock::iterator |
| 2957 | BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) { |
| 2958 | llvm::Instruction &I = *BI; |
| 2959 | |
| 2960 | // Ignore instructions that aren't non-intrinsic calls. |
| 2961 | // These are the only calls that can possibly call longjmp. |
| 2962 | if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue; |
| 2963 | if (isa<llvm::IntrinsicInst>(I)) |
| 2964 | continue; |
| 2965 | |
| 2966 | // Ignore call sites marked nounwind. This may be questionable, |
| 2967 | // since 'nounwind' doesn't necessarily mean 'does not call longjmp'. |
| 2968 | llvm::CallSite CS(&I); |
| 2969 | if (CS.doesNotThrow()) continue; |
| 2970 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2971 | // Insert a read hazard before the call. This will ensure that |
| 2972 | // any writes to the locals are performed before making the |
| 2973 | // call. If the call throws, then this is sufficient to |
| 2974 | // guarantee correctness as long as it doesn't also write to any |
| 2975 | // locals. |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2976 | Builder.SetInsertPoint(&BB, BI); |
| 2977 | emitReadHazard(Builder); |
| 2978 | } |
| 2979 | } |
| 2980 | } |
| 2981 | |
| 2982 | static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) { |
| 2983 | if (V) S.insert(V); |
| 2984 | } |
| 2985 | |
| 2986 | void FragileHazards::collectLocals() { |
| 2987 | // Compute a set of allocas to ignore. |
| 2988 | llvm::DenseSet<llvm::Value*> AllocasToIgnore; |
| 2989 | addIfPresent(AllocasToIgnore, CGF.ReturnValue); |
| 2990 | addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest); |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2991 | |
| 2992 | // Collect all the allocas currently in the function. This is |
| 2993 | // probably way too aggressive. |
| 2994 | llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock(); |
| 2995 | for (llvm::BasicBlock::iterator |
| 2996 | I = Entry.begin(), E = Entry.end(); I != E; ++I) |
| 2997 | if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I)) |
| 2998 | Locals.push_back(&*I); |
| 2999 | } |
| 3000 | |
| 3001 | llvm::FunctionType *FragileHazards::GetAsmFnType() { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3002 | SmallVector<llvm::Type *, 16> tys(Locals.size()); |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 3003 | for (unsigned i = 0, e = Locals.size(); i != e; ++i) |
| 3004 | tys[i] = Locals[i]->getType(); |
| 3005 | return llvm::FunctionType::get(CGF.VoidTy, tys, false); |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 3006 | } |
| 3007 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3008 | /* |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3009 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3010 | Objective-C setjmp-longjmp (sjlj) Exception Handling |
| 3011 | -- |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3012 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3013 | A catch buffer is a setjmp buffer plus: |
| 3014 | - a pointer to the exception that was caught |
| 3015 | - a pointer to the previous exception data buffer |
| 3016 | - two pointers of reserved storage |
| 3017 | Therefore catch buffers form a stack, with a pointer to the top |
| 3018 | of the stack kept in thread-local storage. |
| 3019 | |
| 3020 | objc_exception_try_enter pushes a catch buffer onto the EH stack. |
| 3021 | objc_exception_try_exit pops the given catch buffer, which is |
| 3022 | required to be the top of the EH stack. |
| 3023 | objc_exception_throw pops the top of the EH stack, writes the |
| 3024 | thrown exception into the appropriate field, and longjmps |
| 3025 | to the setjmp buffer. It crashes the process (with a printf |
| 3026 | and an abort()) if there are no catch buffers on the stack. |
| 3027 | objc_exception_extract just reads the exception pointer out of the |
| 3028 | catch buffer. |
| 3029 | |
| 3030 | There's no reason an implementation couldn't use a light-weight |
| 3031 | setjmp here --- something like __builtin_setjmp, but API-compatible |
| 3032 | with the heavyweight setjmp. This will be more important if we ever |
| 3033 | want to implement correct ObjC/C++ exception interactions for the |
| 3034 | fragile ABI. |
| 3035 | |
| 3036 | Note that for this use of setjmp/longjmp to be correct, we may need |
| 3037 | to mark some local variables volatile: if a non-volatile local |
| 3038 | variable is modified between the setjmp and the longjmp, it has |
| 3039 | indeterminate value. For the purposes of LLVM IR, it may be |
| 3040 | sufficient to make loads and stores within the @try (to variables |
| 3041 | declared outside the @try) volatile. This is necessary for |
| 3042 | optimized correctness, but is not currently being done; this is |
| 3043 | being tracked as rdar://problem/8160285 |
| 3044 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3045 | The basic framework for a @try-catch-finally is as follows: |
| 3046 | { |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3047 | objc_exception_data d; |
| 3048 | id _rethrow = null; |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 3049 | bool _call_try_exit = true; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3050 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3051 | objc_exception_try_enter(&d); |
| 3052 | if (!setjmp(d.jmp_buf)) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3053 | ... try body ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3054 | } else { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3055 | // exception path |
| 3056 | id _caught = objc_exception_extract(&d); |
| 3057 | |
| 3058 | // enter new try scope for handlers |
| 3059 | if (!setjmp(d.jmp_buf)) { |
| 3060 | ... match exception and execute catch blocks ... |
| 3061 | |
| 3062 | // fell off end, rethrow. |
| 3063 | _rethrow = _caught; |
| 3064 | ... jump-through-finally to finally_rethrow ... |
| 3065 | } else { |
| 3066 | // exception in catch block |
| 3067 | _rethrow = objc_exception_extract(&d); |
| 3068 | _call_try_exit = false; |
| 3069 | ... jump-through-finally to finally_rethrow ... |
| 3070 | } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3071 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3072 | ... jump-through-finally to finally_end ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3073 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3074 | finally: |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 3075 | if (_call_try_exit) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3076 | objc_exception_try_exit(&d); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 3077 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3078 | ... finally block .... |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3079 | ... dispatch to finally destination ... |
| 3080 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3081 | finally_rethrow: |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3082 | objc_exception_throw(_rethrow); |
| 3083 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3084 | finally_end: |
| 3085 | } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3086 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3087 | This framework differs slightly from the one gcc uses, in that gcc |
| 3088 | uses _rethrow to determine if objc_exception_try_exit should be called |
| 3089 | and if the object should be rethrown. This breaks in the face of |
| 3090 | throwing nil and introduces unnecessary branches. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3091 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3092 | We specialize this framework for a few particular circumstances: |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3093 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3094 | - If there are no catch blocks, then we avoid emitting the second |
| 3095 | exception handling context. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3096 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3097 | - If there is a catch-all catch block (i.e. @catch(...) or @catch(id |
| 3098 | e)) we avoid emitting the code to rethrow an uncaught exception. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3099 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3100 | - FIXME: If there is no @finally block we can do a few more |
| 3101 | simplifications. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3102 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3103 | Rethrows and Jumps-Through-Finally |
| 3104 | -- |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3105 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3106 | '@throw;' is supported by pushing the currently-caught exception |
| 3107 | onto ObjCEHStack while the @catch blocks are emitted. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3108 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3109 | Branches through the @finally block are handled with an ordinary |
| 3110 | normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC |
| 3111 | exceptions are not compatible with C++ exceptions, and this is |
| 3112 | hardly the only place where this will go wrong. |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3113 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3114 | @synchronized(expr) { stmt; } is emitted as if it were: |
| 3115 | id synch_value = expr; |
| 3116 | objc_sync_enter(synch_value); |
| 3117 | @try { stmt; } @finally { objc_sync_exit(synch_value); } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3118 | */ |
| 3119 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 3120 | void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 3121 | const Stmt &S) { |
| 3122 | bool isTry = isa<ObjCAtTryStmt>(S); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3123 | |
| 3124 | // A destination for the fall-through edges of the catch handlers to |
| 3125 | // jump to. |
| 3126 | CodeGenFunction::JumpDest FinallyEnd = |
| 3127 | CGF.getJumpDestInCurrentScope("finally.end"); |
| 3128 | |
| 3129 | // A destination for the rethrow edge of the catch handlers to jump |
| 3130 | // to. |
| 3131 | CodeGenFunction::JumpDest FinallyRethrow = |
| 3132 | CGF.getJumpDestInCurrentScope("finally.rethrow"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3133 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3134 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 3135 | // evaluation of the expression must occur before we enter the |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3136 | // @synchronized. We can't avoid a temp here because we need the |
| 3137 | // value to be preserved. If the backend ever does liveness |
| 3138 | // correctly after setjmp, this will be unnecessary. |
| 3139 | llvm::Value *SyncArgSlot = 0; |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3140 | if (!isTry) { |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3141 | llvm::Value *SyncArg = |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3142 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 3143 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3144 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg) |
| 3145 | ->setDoesNotThrow(); |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3146 | |
| 3147 | SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg"); |
| 3148 | CGF.Builder.CreateStore(SyncArg, SyncArgSlot); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3149 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3150 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3151 | // Allocate memory for the setjmp buffer. This needs to be kept |
| 3152 | // live throughout the try and catch blocks. |
| 3153 | llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy, |
| 3154 | "exceptiondata.ptr"); |
| 3155 | |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3156 | // Create the fragile hazards. Note that this will not capture any |
| 3157 | // of the allocas required for exception processing, but will |
| 3158 | // capture the current basic block (which extends all the way to the |
| 3159 | // setjmp call) as "before the @try". |
| 3160 | FragileHazards Hazards(CGF); |
| 3161 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3162 | // Create a flag indicating whether the cleanup needs to call |
| 3163 | // objc_exception_try_exit. This is true except when |
| 3164 | // - no catches match and we're branching through the cleanup |
| 3165 | // just to rethrow the exception, or |
| 3166 | // - a catch matched and we're falling out of the catch handler. |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3167 | // The setjmp-safety rule here is that we should always store to this |
| 3168 | // variable in a place that dominates the branch through the cleanup |
| 3169 | // without passing through any setjmps. |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3170 | llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(), |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 3171 | "_call_try_exit"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3172 | |
John McCall | 9e2213d | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3173 | // A slot containing the exception to rethrow. Only needed when we |
| 3174 | // have both a @catch and a @finally. |
| 3175 | llvm::Value *PropagatingExnVar = 0; |
| 3176 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3177 | // Push a normal cleanup to leave the try scope. |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 3178 | CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S, |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3179 | SyncArgSlot, |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 3180 | CallTryExitVar, |
| 3181 | ExceptionData, |
| 3182 | &ObjCTypes); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3183 | |
| 3184 | // Enter a try block: |
| 3185 | // - Call objc_exception_try_enter to push ExceptionData on top of |
| 3186 | // the EH stack. |
| 3187 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData) |
| 3188 | ->setDoesNotThrow(); |
| 3189 | |
| 3190 | // - Call setjmp on the exception data buffer. |
| 3191 | llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0); |
| 3192 | llvm::Value *GEPIndexes[] = { Zero, Zero, Zero }; |
| 3193 | llvm::Value *SetJmpBuffer = |
Jay Foad | 0f6ac7c | 2011-07-22 08:16:57 +0000 | [diff] [blame] | 3194 | CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer"); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3195 | llvm::CallInst *SetJmpResult = |
| 3196 | CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result"); |
| 3197 | SetJmpResult->setDoesNotThrow(); |
Bill Wendling | 6446c3e | 2011-12-19 23:53:28 +0000 | [diff] [blame] | 3198 | SetJmpResult->setCanReturnTwice(); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3199 | |
| 3200 | // If setjmp returned 0, enter the protected block; otherwise, |
| 3201 | // branch to the handler. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 3202 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 3203 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3204 | llvm::Value *DidCatch = |
John McCall | d96a8e7 | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 3205 | CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception"); |
| 3206 | CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3207 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3208 | // Emit the protected block. |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3209 | CGF.EmitBlock(TryBlock); |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3210 | CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3211 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3212 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3213 | |
| 3214 | CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3215 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3216 | // Emit the exception handler block. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 3217 | CGF.EmitBlock(TryHandler); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3218 | |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3219 | // Don't optimize loads of the in-scope locals across this point. |
| 3220 | Hazards.emitWriteHazard(); |
| 3221 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3222 | // For a @synchronized (or a @try with no catches), just branch |
| 3223 | // through the cleanup to the rethrow block. |
| 3224 | if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) { |
| 3225 | // Tell the cleanup not to re-pop the exit. |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3226 | CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3227 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3228 | |
| 3229 | // Otherwise, we have to match against the caught exceptions. |
| 3230 | } else { |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3231 | // Retrieve the exception object. We may emit multiple blocks but |
| 3232 | // nothing can cross this so the value is already in SSA form. |
| 3233 | llvm::CallInst *Caught = |
| 3234 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 3235 | ExceptionData, "caught"); |
| 3236 | Caught->setDoesNotThrow(); |
| 3237 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3238 | // Push the exception to rethrow onto the EH value stack for the |
| 3239 | // benefit of any @throws in the handlers. |
| 3240 | CGF.ObjCEHValueStack.push_back(Caught); |
| 3241 | |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 3242 | const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3243 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3244 | bool HasFinally = (AtTryStmt->getFinallyStmt() != 0); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3245 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3246 | llvm::BasicBlock *CatchBlock = 0; |
| 3247 | llvm::BasicBlock *CatchHandler = 0; |
| 3248 | if (HasFinally) { |
John McCall | 9e2213d | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3249 | // Save the currently-propagating exception before |
| 3250 | // objc_exception_try_enter clears the exception slot. |
| 3251 | PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(), |
| 3252 | "propagating_exception"); |
| 3253 | CGF.Builder.CreateStore(Caught, PropagatingExnVar); |
| 3254 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3255 | // Enter a new exception try block (in case a @catch block |
| 3256 | // throws an exception). |
| 3257 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData) |
| 3258 | ->setDoesNotThrow(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3259 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3260 | llvm::CallInst *SetJmpResult = |
| 3261 | CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, |
| 3262 | "setjmp.result"); |
| 3263 | SetJmpResult->setDoesNotThrow(); |
Bill Wendling | 6446c3e | 2011-12-19 23:53:28 +0000 | [diff] [blame] | 3264 | SetJmpResult->setCanReturnTwice(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3265 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3266 | llvm::Value *Threw = |
| 3267 | CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception"); |
| 3268 | |
| 3269 | CatchBlock = CGF.createBasicBlock("catch"); |
| 3270 | CatchHandler = CGF.createBasicBlock("catch_for_catch"); |
| 3271 | CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock); |
| 3272 | |
| 3273 | CGF.EmitBlock(CatchBlock); |
| 3274 | } |
| 3275 | |
| 3276 | CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3277 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3278 | // Handle catch list. As a special case we check if everything is |
| 3279 | // matched and avoid generating code for falling off the end if |
| 3280 | // so. |
| 3281 | bool AllMatched = false; |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 3282 | for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) { |
| 3283 | const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3284 | |
Douglas Gregor | c00d8e1 | 2010-04-26 16:46:50 +0000 | [diff] [blame] | 3285 | const VarDecl *CatchParam = CatchStmt->getCatchParamDecl(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3286 | const ObjCObjectPointerType *OPT = 0; |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 3287 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3288 | // catch(...) always matches. |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3289 | if (!CatchParam) { |
| 3290 | AllMatched = true; |
| 3291 | } else { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3292 | OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3293 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3294 | // catch(id e) always matches under this ABI, since only |
| 3295 | // ObjC exceptions end up here in the first place. |
Daniel Dunbar | 97f61d1 | 2008-09-27 22:21:14 +0000 | [diff] [blame] | 3296 | // FIXME: For the time being we also match id<X>; this should |
| 3297 | // be rejected by Sema instead. |
Eli Friedman | 818e96f | 2009-07-11 00:57:02 +0000 | [diff] [blame] | 3298 | if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType())) |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3299 | AllMatched = true; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3300 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3301 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3302 | // 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] | 3303 | if (AllMatched) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3304 | CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF); |
| 3305 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3306 | if (CatchParam) { |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 3307 | CGF.EmitAutoVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 3308 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3309 | |
| 3310 | // These types work out because ConvertType(id) == i8*. |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 3311 | CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3312 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3313 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3314 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3315 | |
| 3316 | // The scope of the catch variable ends right here. |
| 3317 | CatchVarCleanups.ForceCleanup(); |
| 3318 | |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3319 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3320 | break; |
| 3321 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3322 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3323 | assert(OPT && "Unexpected non-object pointer type in @catch"); |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 3324 | const ObjCObjectType *ObjTy = OPT->getObjectType(); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3325 | |
| 3326 | // FIXME: @catch (Class c) ? |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 3327 | ObjCInterfaceDecl *IDecl = ObjTy->getInterface(); |
| 3328 | assert(IDecl && "Catch parameter must have Objective-C type!"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3329 | |
| 3330 | // Check if the @catch block matches the exception object. |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 3331 | llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3332 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3333 | llvm::CallInst *Match = |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 3334 | CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(), |
| 3335 | Class, Caught, "match"); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3336 | Match->setDoesNotThrow(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3337 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3338 | llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match"); |
| 3339 | llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3340 | |
| 3341 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 3342 | MatchedBlock, NextCatchBlock); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3343 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3344 | // Emit the @catch block. |
| 3345 | CGF.EmitBlock(MatchedBlock); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3346 | |
| 3347 | // Collect any cleanups for the catch variable. The scope lasts until |
| 3348 | // the end of the catch body. |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3349 | CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3350 | |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 3351 | CGF.EmitAutoVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 3352 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3353 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3354 | // Initialize the catch variable. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3355 | llvm::Value *Tmp = |
| 3356 | CGF.Builder.CreateBitCast(Caught, |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 3357 | CGF.ConvertType(CatchParam->getType())); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 3358 | CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3359 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3360 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3361 | |
| 3362 | // We're done with the catch variable. |
| 3363 | CatchVarCleanups.ForceCleanup(); |
| 3364 | |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3365 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3366 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3367 | CGF.EmitBlock(NextCatchBlock); |
| 3368 | } |
| 3369 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3370 | CGF.ObjCEHValueStack.pop_back(); |
| 3371 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3372 | // If nothing wanted anything to do with the caught exception, |
| 3373 | // kill the extract call. |
| 3374 | if (Caught->use_empty()) |
| 3375 | Caught->eraseFromParent(); |
| 3376 | |
| 3377 | if (!AllMatched) |
| 3378 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 3379 | |
| 3380 | if (HasFinally) { |
| 3381 | // Emit the exception handler for the @catch blocks. |
| 3382 | CGF.EmitBlock(CatchHandler); |
| 3383 | |
| 3384 | // In theory we might now need a write hazard, but actually it's |
| 3385 | // unnecessary because there's no local-accessing code between |
| 3386 | // the try's write hazard and here. |
| 3387 | //Hazards.emitWriteHazard(); |
| 3388 | |
John McCall | 9e2213d | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3389 | // Extract the new exception and save it to the |
| 3390 | // propagating-exception slot. |
| 3391 | assert(PropagatingExnVar); |
| 3392 | llvm::CallInst *NewCaught = |
| 3393 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 3394 | ExceptionData, "caught"); |
| 3395 | NewCaught->setDoesNotThrow(); |
| 3396 | CGF.Builder.CreateStore(NewCaught, PropagatingExnVar); |
| 3397 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3398 | // Don't pop the catch handler; the throw already did. |
| 3399 | CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3400 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3401 | } |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3402 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3403 | |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3404 | // Insert read hazards as required in the new blocks. |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3405 | Hazards.emitHazardsInNewBlocks(); |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3406 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3407 | // Pop the cleanup. |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3408 | CGF.Builder.restoreIP(TryFallthroughIP); |
| 3409 | if (CGF.HaveInsertPoint()) |
| 3410 | CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3411 | CGF.PopCleanupBlock(); |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3412 | CGF.EmitBlock(FinallyEnd.getBlock(), true); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3413 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3414 | // Emit the rethrow block. |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3415 | CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP(); |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 3416 | CGF.EmitBlock(FinallyRethrow.getBlock(), true); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3417 | if (CGF.HaveInsertPoint()) { |
John McCall | 9e2213d | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3418 | // If we have a propagating-exception variable, check it. |
| 3419 | llvm::Value *PropagatingExn; |
| 3420 | if (PropagatingExnVar) { |
| 3421 | PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar); |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3422 | |
John McCall | 9e2213d | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3423 | // Otherwise, just look in the buffer for the exception to throw. |
| 3424 | } else { |
| 3425 | llvm::CallInst *Caught = |
| 3426 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 3427 | ExceptionData); |
| 3428 | Caught->setDoesNotThrow(); |
| 3429 | PropagatingExn = Caught; |
| 3430 | } |
| 3431 | |
| 3432 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn) |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3433 | ->setDoesNotThrow(); |
| 3434 | CGF.Builder.CreateUnreachable(); |
Fariborz Jahanian | f2878e5 | 2008-11-21 19:21:53 +0000 | [diff] [blame] | 3435 | } |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3436 | |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3437 | CGF.Builder.restoreIP(SavedIP); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 3438 | } |
| 3439 | |
| 3440 | void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3441 | const ObjCAtThrowStmt &S) { |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 3442 | llvm::Value *ExceptionAsObject; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3443 | |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 3444 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
John McCall | 2b014d6 | 2011-10-01 10:32:24 +0000 | [diff] [blame] | 3445 | llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3446 | ExceptionAsObject = |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 3447 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy); |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 3448 | } else { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3449 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3450 | "Unexpected rethrow outside @catch block."); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 3451 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 3452 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3453 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3454 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject) |
| 3455 | ->setDoesNotReturn(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3456 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 3457 | |
| 3458 | // Clear the insertion point to indicate we are in unreachable code. |
| 3459 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 3460 | } |
| 3461 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3462 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 3463 | /// object: objc_read_weak (id *src) |
| 3464 | /// |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3465 | llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3466 | llvm::Value *AddrWeakObj) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3467 | llvm::Type* DestTy = |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3468 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
| 3469 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, |
| 3470 | ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 3471 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3472 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 3473 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 3474 | return read_weak; |
| 3475 | } |
| 3476 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3477 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 3478 | /// objc_assign_weak (id src, id *dst) |
| 3479 | /// |
| 3480 | void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3481 | llvm::Value *src, llvm::Value *dst) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3482 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3483 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3484 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3485 | assert(Size <= 8 && "does not support size > 8"); |
| 3486 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3487 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 3488 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 3489 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 3490 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 3491 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 3492 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3493 | src, dst, "weakassign"); |
| 3494 | return; |
| 3495 | } |
| 3496 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 3497 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 3498 | /// objc_assign_global (id src, id *dst) |
| 3499 | /// |
| 3500 | void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 3501 | llvm::Value *src, llvm::Value *dst, |
| 3502 | bool threadlocal) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3503 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3504 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3505 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3506 | assert(Size <= 8 && "does not support size > 8"); |
| 3507 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3508 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 3509 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 3510 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 3511 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 3512 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 3513 | if (!threadlocal) |
| 3514 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
| 3515 | src, dst, "globalassign"); |
| 3516 | else |
| 3517 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(), |
| 3518 | src, dst, "threadlocalassign"); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 3519 | return; |
| 3520 | } |
| 3521 | |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 3522 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 3523 | /// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset) |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 3524 | /// |
| 3525 | void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 3526 | llvm::Value *src, llvm::Value *dst, |
| 3527 | llvm::Value *ivarOffset) { |
| 3528 | assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL"); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3529 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3530 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3531 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3532 | assert(Size <= 8 && "does not support size > 8"); |
| 3533 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3534 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 3535 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 3536 | } |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 3537 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 3538 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 3539 | CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(), |
| 3540 | src, dst, ivarOffset); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 3541 | return; |
| 3542 | } |
| 3543 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 3544 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 3545 | /// objc_assign_strongCast (id src, id *dst) |
| 3546 | /// |
| 3547 | void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3548 | llvm::Value *src, llvm::Value *dst) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3549 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3550 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3551 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3552 | assert(Size <= 8 && "does not support size > 8"); |
| 3553 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3554 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 3555 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 3556 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 3557 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 3558 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 3559 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 3560 | src, dst, "weakassign"); |
| 3561 | return; |
| 3562 | } |
| 3563 | |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 3564 | void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3565 | llvm::Value *DestPtr, |
| 3566 | llvm::Value *SrcPtr, |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 3567 | llvm::Value *size) { |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 3568 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy); |
| 3569 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 3570 | CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(), |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 3571 | DestPtr, SrcPtr, size); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 3572 | return; |
| 3573 | } |
| 3574 | |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 3575 | /// EmitObjCValueForIvar - Code Gen for ivar reference. |
| 3576 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 3577 | LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 3578 | QualType ObjectTy, |
| 3579 | llvm::Value *BaseValue, |
| 3580 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 3581 | unsigned CVRQualifiers) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3582 | const ObjCInterfaceDecl *ID = |
| 3583 | ObjectTy->getAs<ObjCObjectType>()->getInterface(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 3584 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 3585 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 3586 | } |
| 3587 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 3588 | llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 3589 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 3590 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 3591 | uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3592 | return llvm::ConstantInt::get( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3593 | CGM.getTypes().ConvertType(CGM.getContext().LongTy), |
| 3594 | Offset); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 3595 | } |
| 3596 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3597 | /* *** Private Interface *** */ |
| 3598 | |
| 3599 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 3600 | /// level information. |
| 3601 | /// |
| 3602 | /// See: <rdr://4810609&4810587&4810587> |
| 3603 | /// struct IMAGE_INFO { |
| 3604 | /// unsigned version; |
| 3605 | /// unsigned flags; |
| 3606 | /// }; |
| 3607 | enum ImageInfoFlags { |
Daniel Dunbar | fce176b | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 3608 | eImageInfo_FixAndContinue = (1 << 0), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3609 | eImageInfo_GarbageCollected = (1 << 1), |
| 3610 | eImageInfo_GCOnly = (1 << 2), |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 3611 | eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set. |
| 3612 | |
Daniel Dunbar | fce176b | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 3613 | // A flag indicating that the module has no instances of a @synthesize of a |
| 3614 | // superclass variable. <rdar://problem/6803242> |
Bill Wendling | 0982251 | 2012-04-24 11:04:57 +0000 | [diff] [blame] | 3615 | eImageInfo_CorrectedSynthesize = (1 << 4), |
| 3616 | eImageInfo_ImageIsSimulated = (1 << 5) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3617 | }; |
| 3618 | |
Daniel Dunbar | fce176b | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 3619 | void CGObjCCommonMac::EmitImageInfo() { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3620 | unsigned version = 0; // Version is unused? |
Bill Wendling | 3973acc | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 3621 | const char *Section = (ObjCABI == 1) ? |
| 3622 | "__OBJC, __image_info,regular" : |
| 3623 | "__DATA, __objc_imageinfo, regular, no_dead_strip"; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3624 | |
Bill Wendling | 3973acc | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 3625 | // Generate module-level named metadata to convey this information to the |
| 3626 | // linker and code-gen. |
| 3627 | llvm::Module &Mod = CGM.getModule(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3628 | |
Bill Wendling | 3973acc | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 3629 | // Add the ObjC ABI version to the module flags. |
| 3630 | Mod.addModuleFlag(llvm::Module::Error, "Objective-C Version", ObjCABI); |
| 3631 | Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Version", |
| 3632 | version); |
| 3633 | Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Section", |
| 3634 | llvm::MDString::get(VMContext,Section)); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3635 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3636 | if (CGM.getLangOpts().getGC() == LangOptions::NonGC) { |
Bill Wendling | 3973acc | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 3637 | // Non-GC overrides those files which specify GC. |
| 3638 | Mod.addModuleFlag(llvm::Module::Override, |
| 3639 | "Objective-C Garbage Collection", (uint32_t)0); |
| 3640 | } else { |
| 3641 | // Add the ObjC garbage collection value. |
| 3642 | Mod.addModuleFlag(llvm::Module::Error, |
| 3643 | "Objective-C Garbage Collection", |
| 3644 | eImageInfo_GarbageCollected); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3645 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3646 | if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) { |
Bill Wendling | 3973acc | 2012-02-16 01:13:30 +0000 | [diff] [blame] | 3647 | // Add the ObjC GC Only value. |
| 3648 | Mod.addModuleFlag(llvm::Module::Error, "Objective-C GC Only", |
| 3649 | eImageInfo_GCOnly); |
| 3650 | |
| 3651 | // Require that GC be specified and set to eImageInfo_GarbageCollected. |
| 3652 | llvm::Value *Ops[2] = { |
| 3653 | llvm::MDString::get(VMContext, "Objective-C Garbage Collection"), |
| 3654 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), |
| 3655 | eImageInfo_GarbageCollected) |
| 3656 | }; |
| 3657 | Mod.addModuleFlag(llvm::Module::Require, "Objective-C GC Only", |
| 3658 | llvm::MDNode::get(VMContext, Ops)); |
| 3659 | } |
| 3660 | } |
Bill Wendling | 0982251 | 2012-04-24 11:04:57 +0000 | [diff] [blame] | 3661 | |
| 3662 | // Indicate whether we're compiling this to run on a simulator. |
| 3663 | const llvm::Triple &Triple = CGM.getTarget().getTriple(); |
| 3664 | if (Triple.getOS() == llvm::Triple::IOS && |
| 3665 | (Triple.getArch() == llvm::Triple::x86 || |
| 3666 | Triple.getArch() == llvm::Triple::x86_64)) |
| 3667 | Mod.addModuleFlag(llvm::Module::Error, "Objective-C Is Simulated", |
| 3668 | eImageInfo_ImageIsSimulated); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3669 | } |
| 3670 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3671 | // struct objc_module { |
| 3672 | // unsigned long version; |
| 3673 | // unsigned long size; |
| 3674 | // const char *name; |
| 3675 | // Symtab symtab; |
| 3676 | // }; |
| 3677 | |
| 3678 | // FIXME: Get from somewhere |
| 3679 | static const int ModuleVersion = 7; |
| 3680 | |
| 3681 | void CGObjCMac::EmitModuleInfo() { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3682 | uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3683 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 3684 | llvm::Constant *Values[] = { |
| 3685 | llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion), |
| 3686 | llvm::ConstantInt::get(ObjCTypes.LongTy, Size), |
| 3687 | // This used to be the filename, now it is unused. <rdr://4327263> |
| 3688 | GetClassName(&CGM.getContext().Idents.get("")), |
| 3689 | EmitModuleSymbols() |
| 3690 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3691 | CreateMetadataVar("\01L_OBJC_MODULES", |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 3692 | llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3693 | "__OBJC,__module_info,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 3694 | 4, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3695 | } |
| 3696 | |
| 3697 | llvm::Constant *CGObjCMac::EmitModuleSymbols() { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3698 | unsigned NumClasses = DefinedClasses.size(); |
| 3699 | unsigned NumCategories = DefinedCategories.size(); |
| 3700 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3701 | // Return null if no symbols were defined. |
| 3702 | if (!NumClasses && !NumCategories) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3703 | return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3704 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3705 | llvm::Constant *Values[5]; |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3706 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3707 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3708 | Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses); |
| 3709 | Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3710 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3711 | // The runtime expects exactly the list of defined classes followed |
| 3712 | // by the list of defined categories, in a single array. |
Chris Lattner | 0b23971 | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 3713 | SmallVector<llvm::Constant*, 8> Symbols(NumClasses + NumCategories); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3714 | for (unsigned i=0; i<NumClasses; i++) |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3715 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3716 | ObjCTypes.Int8PtrTy); |
| 3717 | for (unsigned i=0; i<NumCategories; i++) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3718 | Symbols[NumClasses + i] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3719 | llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3720 | ObjCTypes.Int8PtrTy); |
| 3721 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3722 | Values[4] = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3723 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Chris Lattner | 0b23971 | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 3724 | Symbols.size()), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3725 | Symbols); |
| 3726 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3727 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3728 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3729 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3730 | CreateMetadataVar("\01L_OBJC_SYMBOLS", Init, |
| 3731 | "__OBJC,__symbols,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 3732 | 4, true); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3733 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3734 | } |
| 3735 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3736 | llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder, |
| 3737 | IdentifierInfo *II) { |
| 3738 | LazySymbols.insert(II); |
| 3739 | |
| 3740 | llvm::GlobalVariable *&Entry = ClassReferences[II]; |
| 3741 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3742 | if (!Entry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3743 | llvm::Constant *Casted = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3744 | llvm::ConstantExpr::getBitCast(GetClassName(II), |
| 3745 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3746 | Entry = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3747 | CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted, |
| 3748 | "__OBJC,__cls_refs,literal_pointers,no_dead_strip", |
| 3749 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3750 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3751 | |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 3752 | return Builder.CreateLoad(Entry); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3753 | } |
| 3754 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3755 | llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder, |
| 3756 | const ObjCInterfaceDecl *ID) { |
| 3757 | return EmitClassRefFromId(Builder, ID->getIdentifier()); |
| 3758 | } |
| 3759 | |
| 3760 | llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) { |
| 3761 | IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool"); |
| 3762 | return EmitClassRefFromId(Builder, II); |
| 3763 | } |
| 3764 | |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 3765 | llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel, |
| 3766 | bool lvalue) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3767 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3768 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3769 | if (!Entry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3770 | llvm::Constant *Casted = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3771 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3772 | ObjCTypes.SelectorPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3773 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3774 | CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted, |
| 3775 | "__OBJC,__message_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 3776 | 4, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3777 | } |
| 3778 | |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 3779 | if (lvalue) |
| 3780 | return Entry; |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 3781 | return Builder.CreateLoad(Entry); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3782 | } |
| 3783 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3784 | llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3785 | llvm::GlobalVariable *&Entry = ClassNames[Ident]; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3786 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3787 | if (!Entry) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3788 | Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
Chris Lattner | 9401069 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 3789 | llvm::ConstantDataArray::getString(VMContext, |
| 3790 | Ident->getNameStart()), |
Daniel Dunbar | af19ac4 | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 3791 | ((ObjCABI == 2) ? |
| 3792 | "__TEXT,__objc_classname,cstring_literals" : |
| 3793 | "__TEXT,__cstring,cstring_literals"), |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3794 | 1, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3795 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3796 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3797 | } |
| 3798 | |
Argyrios Kyrtzidis | 9d50c06 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 3799 | llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) { |
| 3800 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator |
| 3801 | I = MethodDefinitions.find(MD); |
| 3802 | if (I != MethodDefinitions.end()) |
| 3803 | return I->second; |
| 3804 | |
Argyrios Kyrtzidis | 9d50c06 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 3805 | return NULL; |
| 3806 | } |
| 3807 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 3808 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 3809 | /// ivar layout bitmap. |
| 3810 | llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3811 | const ObjCCommonTypesHelper &ObjCTypes) { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3812 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 3813 | } |
| 3814 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3815 | void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3816 | unsigned int BytePos, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3817 | bool ForStrongLayout, |
| 3818 | bool &HasUnion) { |
| 3819 | const RecordDecl *RD = RT->getDecl(); |
| 3820 | // FIXME - Use iterator. |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 3821 | SmallVector<const FieldDecl*, 16> Fields; |
| 3822 | for (RecordDecl::field_iterator i = RD->field_begin(), |
| 3823 | e = RD->field_end(); i != e; ++i) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3824 | Fields.push_back(*i); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3825 | llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3826 | const llvm::StructLayout *RecLayout = |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3827 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3828 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3829 | BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos, |
| 3830 | ForStrongLayout, HasUnion); |
| 3831 | } |
| 3832 | |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 3833 | void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3834 | const llvm::StructLayout *Layout, |
| 3835 | const RecordDecl *RD, |
Bill Wendling | 795b100 | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 3836 | ArrayRef<const FieldDecl*> RecFields, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3837 | unsigned int BytePos, bool ForStrongLayout, |
| 3838 | bool &HasUnion) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3839 | bool IsUnion = (RD && RD->isUnion()); |
| 3840 | uint64_t MaxUnionIvarSize = 0; |
| 3841 | uint64_t MaxSkippedUnionIvarSize = 0; |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3842 | const FieldDecl *MaxField = 0; |
| 3843 | const FieldDecl *MaxSkippedField = 0; |
| 3844 | const FieldDecl *LastFieldBitfieldOrUnnamed = 0; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3845 | uint64_t MaxFieldOffset = 0; |
| 3846 | uint64_t MaxSkippedFieldOffset = 0; |
Argyrios Kyrtzidis | 0dc7509 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 3847 | uint64_t LastBitfieldOrUnnamedOffset = 0; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3848 | uint64_t FirstFieldDelta = 0; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3849 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3850 | if (RecFields.empty()) |
| 3851 | return; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3852 | unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0); |
| 3853 | unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3854 | if (!RD && CGM.getLangOpts().ObjCAutoRefCount) { |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3855 | const FieldDecl *FirstField = RecFields[0]; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3856 | FirstFieldDelta = |
| 3857 | ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField)); |
| 3858 | } |
| 3859 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3860 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3861 | const FieldDecl *Field = RecFields[i]; |
Daniel Dunbar | e05cc98 | 2009-05-03 23:35:23 +0000 | [diff] [blame] | 3862 | uint64_t FieldOffset; |
Anders Carlsson | fc514ab | 2009-07-24 17:23:54 +0000 | [diff] [blame] | 3863 | if (RD) { |
Daniel Dunbar | f8f8eba | 2010-04-14 17:02:21 +0000 | [diff] [blame] | 3864 | // Note that 'i' here is actually the field index inside RD of Field, |
| 3865 | // although this dependency is hidden. |
| 3866 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3867 | FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta; |
Anders Carlsson | fc514ab | 2009-07-24 17:23:54 +0000 | [diff] [blame] | 3868 | } else |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3869 | FieldOffset = |
| 3870 | ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta; |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3871 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3872 | // Skip over unnamed or bitfields |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3873 | if (!Field->getIdentifier() || Field->isBitField()) { |
Argyrios Kyrtzidis | 0dc7509 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 3874 | LastFieldBitfieldOrUnnamed = Field; |
| 3875 | LastBitfieldOrUnnamedOffset = FieldOffset; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3876 | continue; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3877 | } |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3878 | |
Argyrios Kyrtzidis | 0dc7509 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 3879 | LastFieldBitfieldOrUnnamed = 0; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3880 | QualType FQT = Field->getType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3881 | if (FQT->isRecordType() || FQT->isUnionType()) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3882 | if (FQT->isUnionType()) |
| 3883 | HasUnion = true; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3884 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3885 | BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(), |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3886 | BytePos + FieldOffset, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3887 | ForStrongLayout, HasUnion); |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3888 | continue; |
| 3889 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3890 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3891 | if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3892 | const ConstantArrayType *CArray = |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3893 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3894 | uint64_t ElCount = CArray->getSize().getZExtValue(); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3895 | assert(CArray && "only array with known element size is supported"); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3896 | FQT = CArray->getElementType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3897 | while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 3898 | const ConstantArrayType *CArray = |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3899 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3900 | ElCount *= CArray->getSize().getZExtValue(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3901 | FQT = CArray->getElementType(); |
| 3902 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3903 | |
| 3904 | assert(!FQT->isUnionType() && |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3905 | "layout for array of unions not supported"); |
Fariborz Jahanian | f96bdf4 | 2011-01-03 19:23:18 +0000 | [diff] [blame] | 3906 | if (FQT->isRecordType() && ElCount) { |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3907 | int OldIndex = IvarsInfo.size() - 1; |
| 3908 | int OldSkIndex = SkipIvars.size() -1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3909 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3910 | const RecordType *RT = FQT->getAs<RecordType>(); |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3911 | BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3912 | ForStrongLayout, HasUnion); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3913 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3914 | // Replicate layout information for each array element. Note that |
| 3915 | // one element is already done. |
| 3916 | uint64_t ElIx = 1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3917 | for (int FirstIndex = IvarsInfo.size() - 1, |
| 3918 | FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3919 | uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits; |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3920 | for (int i = OldIndex+1; i <= FirstIndex; ++i) |
| 3921 | IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx, |
| 3922 | IvarsInfo[i].ivar_size)); |
| 3923 | for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i) |
| 3924 | SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx, |
| 3925 | SkipIvars[i].ivar_size)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3926 | } |
| 3927 | continue; |
| 3928 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3929 | } |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3930 | // At this point, we are done with Record/Union and array there of. |
| 3931 | // For other arrays we are down to its element type. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3932 | Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3933 | |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3934 | unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3935 | if ((ForStrongLayout && GCAttr == Qualifiers::Strong) |
| 3936 | || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) { |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3937 | if (IsUnion) { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3938 | uint64_t UnionIvarSize = FieldSize / WordSizeInBits; |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3939 | if (UnionIvarSize > MaxUnionIvarSize) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3940 | MaxUnionIvarSize = UnionIvarSize; |
| 3941 | MaxField = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3942 | MaxFieldOffset = FieldOffset; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3943 | } |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3944 | } else { |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3945 | IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3946 | FieldSize / WordSizeInBits)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3947 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3948 | } else if ((ForStrongLayout && |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3949 | (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)) |
| 3950 | || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) { |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3951 | if (IsUnion) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 3952 | // FIXME: Why the asymmetry? We divide by word size in bits on other |
| 3953 | // side. |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3954 | uint64_t UnionIvarSize = FieldSize; |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3955 | if (UnionIvarSize > MaxSkippedUnionIvarSize) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3956 | MaxSkippedUnionIvarSize = UnionIvarSize; |
| 3957 | MaxSkippedField = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3958 | MaxSkippedFieldOffset = FieldOffset; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3959 | } |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3960 | } else { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3961 | // FIXME: Why the asymmetry, we divide by byte size in bits here? |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3962 | SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3963 | FieldSize / ByteSizeInBits)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3964 | } |
| 3965 | } |
| 3966 | } |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3967 | |
Argyrios Kyrtzidis | 0dc7509 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 3968 | if (LastFieldBitfieldOrUnnamed) { |
| 3969 | if (LastFieldBitfieldOrUnnamed->isBitField()) { |
| 3970 | // Last field was a bitfield. Must update skip info. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3971 | uint64_t BitFieldSize |
| 3972 | = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext()); |
Argyrios Kyrtzidis | 0dc7509 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 3973 | GC_IVAR skivar; |
| 3974 | skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset; |
| 3975 | skivar.ivar_size = (BitFieldSize / ByteSizeInBits) |
| 3976 | + ((BitFieldSize % ByteSizeInBits) != 0); |
| 3977 | SkipIvars.push_back(skivar); |
| 3978 | } else { |
| 3979 | assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed"); |
| 3980 | // Last field was unnamed. Must update skip info. |
| 3981 | unsigned FieldSize |
| 3982 | = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType()); |
| 3983 | SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset, |
| 3984 | FieldSize / ByteSizeInBits)); |
| 3985 | } |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3986 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3987 | |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3988 | if (MaxField) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3989 | IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3990 | MaxUnionIvarSize)); |
| 3991 | if (MaxSkippedField) |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3992 | SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3993 | MaxSkippedUnionIvarSize)); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3994 | } |
| 3995 | |
Fariborz Jahanian | b8fd2eb | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 3996 | /// BuildIvarLayoutBitmap - This routine is the horsework for doing all |
| 3997 | /// the computations and returning the layout bitmap (for ivar or blocks) in |
| 3998 | /// the given argument BitMap string container. Routine reads |
| 3999 | /// two containers, IvarsInfo and SkipIvars which are assumed to be |
| 4000 | /// filled already by the caller. |
Chris Lattner | 9401069 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4001 | llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string &BitMap) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4002 | unsigned int WordsToScan, WordsToSkip; |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4003 | llvm::Type *PtrTy = CGM.Int8PtrTy; |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4004 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4005 | // Build the string of skip/scan nibbles |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4006 | SmallVector<SKIP_SCAN, 32> SkipScanIvars; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4007 | unsigned int WordSize = |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4008 | CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4009 | if (IvarsInfo[0].ivar_bytepos == 0) { |
| 4010 | WordsToSkip = 0; |
| 4011 | WordsToScan = IvarsInfo[0].ivar_size; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4012 | } else { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4013 | WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize; |
| 4014 | WordsToScan = IvarsInfo[0].ivar_size; |
| 4015 | } |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4016 | for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4017 | unsigned int TailPrevGCObjC = |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4018 | IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4019 | if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4020 | // consecutive 'scanned' object pointers. |
| 4021 | WordsToScan += IvarsInfo[i].ivar_size; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4022 | } else { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4023 | // Skip over 'gc'able object pointer which lay over each other. |
| 4024 | if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos) |
| 4025 | continue; |
| 4026 | // Must skip over 1 or more words. We save current skip/scan values |
| 4027 | // and start a new pair. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 4028 | SKIP_SCAN SkScan; |
| 4029 | SkScan.skip = WordsToSkip; |
| 4030 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4031 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4032 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4033 | // Skip the hole. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 4034 | SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize; |
| 4035 | SkScan.scan = 0; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4036 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4037 | WordsToSkip = 0; |
| 4038 | WordsToScan = IvarsInfo[i].ivar_size; |
| 4039 | } |
| 4040 | } |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4041 | if (WordsToScan > 0) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 4042 | SKIP_SCAN SkScan; |
| 4043 | SkScan.skip = WordsToSkip; |
| 4044 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4045 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4046 | } |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4047 | |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4048 | if (!SkipIvars.empty()) { |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4049 | unsigned int LastIndex = SkipIvars.size()-1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4050 | int LastByteSkipped = |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4051 | SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4052 | LastIndex = IvarsInfo.size()-1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4053 | int LastByteScanned = |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4054 | IvarsInfo[LastIndex].ivar_bytepos + |
| 4055 | IvarsInfo[LastIndex].ivar_size * WordSize; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4056 | // 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] | 4057 | if (LastByteSkipped > LastByteScanned) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4058 | unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 4059 | SKIP_SCAN SkScan; |
| 4060 | SkScan.skip = TotalWords - (LastByteScanned/WordSize); |
| 4061 | SkScan.scan = 0; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4062 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4063 | } |
| 4064 | } |
| 4065 | // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced |
| 4066 | // as 0xMN. |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 4067 | int SkipScan = SkipScanIvars.size()-1; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4068 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4069 | if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0 |
| 4070 | && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) { |
| 4071 | // 0xM0 followed by 0x0N detected. |
| 4072 | SkipScanIvars[i].scan = SkipScanIvars[i+1].scan; |
| 4073 | for (int j = i+1; j < SkipScan; j++) |
| 4074 | SkipScanIvars[j] = SkipScanIvars[j+1]; |
| 4075 | --SkipScan; |
| 4076 | } |
| 4077 | } |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4078 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4079 | // Generate the string. |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4080 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4081 | unsigned char byte; |
| 4082 | unsigned int skip_small = SkipScanIvars[i].skip % 0xf; |
| 4083 | unsigned int scan_small = SkipScanIvars[i].scan % 0xf; |
| 4084 | unsigned int skip_big = SkipScanIvars[i].skip / 0xf; |
| 4085 | unsigned int scan_big = SkipScanIvars[i].scan / 0xf; |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4086 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4087 | // first skip big. |
| 4088 | for (unsigned int ix = 0; ix < skip_big; ix++) |
| 4089 | BitMap += (unsigned char)(0xf0); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4090 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4091 | // next (skip small, scan) |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4092 | if (skip_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4093 | byte = skip_small << 4; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4094 | if (scan_big > 0) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4095 | byte |= 0xf; |
| 4096 | --scan_big; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4097 | } else if (scan_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4098 | byte |= scan_small; |
| 4099 | scan_small = 0; |
| 4100 | } |
| 4101 | BitMap += byte; |
| 4102 | } |
| 4103 | // next scan big |
| 4104 | for (unsigned int ix = 0; ix < scan_big; ix++) |
| 4105 | BitMap += (unsigned char)(0x0f); |
| 4106 | // last scan small |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4107 | if (scan_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4108 | byte = scan_small; |
| 4109 | BitMap += byte; |
| 4110 | } |
| 4111 | } |
| 4112 | // null terminate string. |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 4113 | unsigned char zero = 0; |
| 4114 | BitMap += zero; |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4115 | |
| 4116 | llvm::GlobalVariable * Entry = |
| 4117 | CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
Chris Lattner | 9401069 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4118 | llvm::ConstantDataArray::getString(VMContext, BitMap,false), |
Daniel Dunbar | af19ac4 | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4119 | ((ObjCABI == 2) ? |
| 4120 | "__TEXT,__objc_classname,cstring_literals" : |
| 4121 | "__TEXT,__cstring,cstring_literals"), |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4122 | 1, true); |
| 4123 | return getConstantGEP(VMContext, Entry, 0, 0); |
| 4124 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4125 | |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4126 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 4127 | /// implementation for the __strong or __weak case. |
| 4128 | /// The layout map displays which words in ivar list must be skipped |
| 4129 | /// and which must be scanned by GC (see below). String is built of bytes. |
| 4130 | /// Each byte is divided up in two nibbles (4-bit each). Left nibble is count |
| 4131 | /// of words to skip and right nibble is count of words to scan. So, each |
| 4132 | /// nibble represents up to 15 workds to skip or scan. Skipping the rest is |
| 4133 | /// represented by a 0x00 byte which also ends the string. |
| 4134 | /// 1. when ForStrongLayout is true, following ivars are scanned: |
| 4135 | /// - id, Class |
| 4136 | /// - object * |
| 4137 | /// - __strong anything |
| 4138 | /// |
| 4139 | /// 2. When ForStrongLayout is false, following ivars are scanned: |
| 4140 | /// - __weak anything |
| 4141 | /// |
| 4142 | llvm::Constant *CGObjCCommonMac::BuildIvarLayout( |
| 4143 | const ObjCImplementationDecl *OMD, |
| 4144 | bool ForStrongLayout) { |
| 4145 | bool hasUnion = false; |
| 4146 | |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4147 | llvm::Type *PtrTy = CGM.Int8PtrTy; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4148 | if (CGM.getLangOpts().getGC() == LangOptions::NonGC && |
| 4149 | !CGM.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4150 | return llvm::Constant::getNullValue(PtrTy); |
| 4151 | |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4152 | const ObjCInterfaceDecl *OI = OMD->getClassInterface(); |
| 4153 | SmallVector<const FieldDecl*, 32> RecFields; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4154 | if (CGM.getLangOpts().ObjCAutoRefCount) { |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4155 | for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin(); |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 4156 | IVD; IVD = IVD->getNextIvar()) |
| 4157 | RecFields.push_back(cast<FieldDecl>(IVD)); |
| 4158 | } |
| 4159 | else { |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4160 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4161 | CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4162 | |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4163 | // FIXME: This is not ideal; we shouldn't have to do this copy. |
| 4164 | RecFields.append(Ivars.begin(), Ivars.end()); |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 4165 | } |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4166 | |
| 4167 | if (RecFields.empty()) |
| 4168 | return llvm::Constant::getNullValue(PtrTy); |
| 4169 | |
| 4170 | SkipIvars.clear(); |
| 4171 | IvarsInfo.clear(); |
| 4172 | |
| 4173 | BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion); |
| 4174 | if (IvarsInfo.empty()) |
| 4175 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | b8fd2eb | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 4176 | // Sort on byte position in case we encounterred a union nested in |
| 4177 | // the ivar list. |
| 4178 | if (hasUnion && !IvarsInfo.empty()) |
| 4179 | std::sort(IvarsInfo.begin(), IvarsInfo.end()); |
| 4180 | if (hasUnion && !SkipIvars.empty()) |
| 4181 | std::sort(SkipIvars.begin(), SkipIvars.end()); |
| 4182 | |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4183 | std::string BitMap; |
Fariborz Jahanian | b8fd2eb | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 4184 | llvm::Constant *C = BuildIvarLayoutBitmap(BitMap); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4185 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4186 | if (CGM.getLangOpts().ObjCGCBitmapPrint) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4187 | printf("\n%s ivar layout for class '%s': ", |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 4188 | ForStrongLayout ? "strong" : "weak", |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4189 | OMD->getClassInterface()->getName().data()); |
Roman Divacky | 31ba613 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 4190 | const unsigned char *s = (const unsigned char*)BitMap.c_str(); |
Bill Wendling | 13562a1 | 2012-02-07 09:06:01 +0000 | [diff] [blame] | 4191 | for (unsigned i = 0, e = BitMap.size(); i < e; i++) |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 4192 | if (!(s[i] & 0xf0)) |
| 4193 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 4194 | else |
| 4195 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 4196 | printf("\n"); |
| 4197 | } |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4198 | return C; |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 4199 | } |
| 4200 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4201 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4202 | llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; |
| 4203 | |
Chris Lattner | 0b23971 | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 4204 | // FIXME: Avoid std::string in "Sel.getAsString()" |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4205 | if (!Entry) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4206 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_", |
Chris Lattner | 9401069 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4207 | llvm::ConstantDataArray::getString(VMContext, Sel.getAsString()), |
Daniel Dunbar | af19ac4 | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4208 | ((ObjCABI == 2) ? |
| 4209 | "__TEXT,__objc_methname,cstring_literals" : |
| 4210 | "__TEXT,__cstring,cstring_literals"), |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4211 | 1, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4212 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4213 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4214 | } |
| 4215 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4216 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4217 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4218 | return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); |
| 4219 | } |
| 4220 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4221 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) { |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4222 | std::string TypeStr; |
| 4223 | CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); |
| 4224 | |
| 4225 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4226 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4227 | if (!Entry) |
| 4228 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
Chris Lattner | 9401069 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4229 | llvm::ConstantDataArray::getString(VMContext, TypeStr), |
Daniel Dunbar | af19ac4 | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4230 | ((ObjCABI == 2) ? |
| 4231 | "__TEXT,__objc_methtype,cstring_literals" : |
| 4232 | "__TEXT,__cstring,cstring_literals"), |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4233 | 1, true); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4234 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4235 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4236 | } |
| 4237 | |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4238 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D, |
| 4239 | bool Extended) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4240 | std::string TypeStr; |
Bill Wendling | a4dc693 | 2012-02-09 22:45:21 +0000 | [diff] [blame] | 4241 | if (CGM.getContext().getObjCEncodingForMethodDecl(D, TypeStr, Extended)) |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4242 | return 0; |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4243 | |
| 4244 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
| 4245 | |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4246 | if (!Entry) |
| 4247 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
Chris Lattner | 9401069 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4248 | llvm::ConstantDataArray::getString(VMContext, TypeStr), |
Daniel Dunbar | af19ac4 | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4249 | ((ObjCABI == 2) ? |
| 4250 | "__TEXT,__objc_methtype,cstring_literals" : |
| 4251 | "__TEXT,__cstring,cstring_literals"), |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4252 | 1, true); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4253 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4254 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4255 | } |
| 4256 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4257 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4258 | llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4259 | llvm::GlobalVariable *&Entry = PropertyNames[Ident]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4260 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4261 | if (!Entry) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4262 | Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_", |
Chris Lattner | 9401069 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4263 | llvm::ConstantDataArray::getString(VMContext, |
| 4264 | Ident->getNameStart()), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4265 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4266 | 1, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4267 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4268 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4269 | } |
| 4270 | |
| 4271 | // FIXME: Merge into a single cstring creation function. |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4272 | // FIXME: This Decl should be more precise. |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4273 | llvm::Constant * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4274 | CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 4275 | const Decl *Container) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4276 | std::string TypeStr; |
| 4277 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4278 | return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); |
| 4279 | } |
| 4280 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4281 | void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D, |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4282 | const ObjCContainerDecl *CD, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4283 | SmallVectorImpl<char> &Name) { |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4284 | llvm::raw_svector_ostream OS(Name); |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 4285 | assert (CD && "Missing container decl in GetNameForMethod"); |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4286 | OS << '\01' << (D->isInstanceMethod() ? '-' : '+') |
| 4287 | << '[' << CD->getName(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4288 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4289 | dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) |
Benjamin Kramer | f978059 | 2012-02-07 11:57:45 +0000 | [diff] [blame] | 4290 | OS << '(' << *CID << ')'; |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4291 | OS << ' ' << D->getSelector().getAsString() << ']'; |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 4292 | } |
| 4293 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4294 | void CGObjCMac::FinishModule() { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4295 | EmitModuleInfo(); |
| 4296 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4297 | // Emit the dummy bodies for any protocols which were referenced but |
| 4298 | // never defined. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4299 | for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4300 | I = Protocols.begin(), e = Protocols.end(); I != e; ++I) { |
| 4301 | if (I->second->hasInitializer()) |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4302 | continue; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4303 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 4304 | llvm::Constant *Values[5]; |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4305 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4306 | Values[1] = GetClassName(I->first); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4307 | Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4308 | Values[3] = Values[4] = |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4309 | llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4310 | I->second->setLinkage(llvm::GlobalValue::InternalLinkage); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 4311 | I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4312 | Values)); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4313 | CGM.AddUsedGlobal(I->second); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4314 | } |
| 4315 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4316 | // Add assembler directives to add lazy undefined symbol references |
| 4317 | // for classes which are referenced but not defined. This is |
| 4318 | // important for correct linker interaction. |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4319 | // |
| 4320 | // FIXME: It would be nice if we had an LLVM construct for this. |
| 4321 | if (!LazySymbols.empty() || !DefinedSymbols.empty()) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 4322 | SmallString<256> Asm; |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4323 | Asm += CGM.getModule().getModuleInlineAsm(); |
| 4324 | if (!Asm.empty() && Asm.back() != '\n') |
| 4325 | Asm += '\n'; |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4326 | |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4327 | llvm::raw_svector_ostream OS(Asm); |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4328 | for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(), |
| 4329 | e = DefinedSymbols.end(); I != e; ++I) |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 4330 | OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n" |
| 4331 | << "\t.globl .objc_class_name_" << (*I)->getName() << "\n"; |
Chris Lattner | afd5eda | 2010-04-17 18:26:20 +0000 | [diff] [blame] | 4332 | for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(), |
Fariborz Jahanian | b9c5b3d | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 4333 | e = LazySymbols.end(); I != e; ++I) { |
Chris Lattner | afd5eda | 2010-04-17 18:26:20 +0000 | [diff] [blame] | 4334 | OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n"; |
Fariborz Jahanian | b9c5b3d | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 4335 | } |
| 4336 | |
Bill Wendling | 13562a1 | 2012-02-07 09:06:01 +0000 | [diff] [blame] | 4337 | for (size_t i = 0, e = DefinedCategoryNames.size(); i < e; ++i) { |
Fariborz Jahanian | b9c5b3d | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 4338 | OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n" |
| 4339 | << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n"; |
| 4340 | } |
Chris Lattner | afd5eda | 2010-04-17 18:26:20 +0000 | [diff] [blame] | 4341 | |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4342 | CGM.getModule().setModuleInlineAsm(OS.str()); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4343 | } |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4344 | } |
| 4345 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4346 | CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4347 | : CGObjCCommonMac(cgm), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4348 | ObjCTypes(cgm) { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4349 | ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4350 | ObjCABI = 2; |
| 4351 | } |
| 4352 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4353 | /* *** */ |
| 4354 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4355 | ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm) |
Douglas Gregor | 65a1e67 | 2012-01-17 23:38:32 +0000 | [diff] [blame] | 4356 | : VMContext(cgm.getLLVMContext()), CGM(cgm), ExternalProtocolPtrTy(0) |
| 4357 | { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4358 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 4359 | ASTContext &Ctx = CGM.getContext(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4360 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4361 | ShortTy = Types.ConvertType(Ctx.ShortTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4362 | IntTy = Types.ConvertType(Ctx.IntTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4363 | LongTy = Types.ConvertType(Ctx.LongTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4364 | LongLongTy = Types.ConvertType(Ctx.LongLongTy); |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4365 | Int8PtrTy = CGM.Int8PtrTy; |
| 4366 | Int8PtrPtrTy = CGM.Int8PtrPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4367 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4368 | ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4369 | PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4370 | SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4371 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4372 | // I'm not sure I like this. The implicit coordination is a bit |
| 4373 | // gross. We should solve this in a reasonable fashion because this |
| 4374 | // is a pretty common task (match some runtime data structure with |
| 4375 | // an LLVM data structure). |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4376 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4377 | // FIXME: This is leaked. |
| 4378 | // FIXME: Merge with rewriter code? |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4379 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4380 | // struct _objc_super { |
| 4381 | // id self; |
| 4382 | // Class cls; |
| 4383 | // } |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4384 | RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct, |
Daniel Dunbar | daa3ac5 | 2010-04-29 16:29:11 +0000 | [diff] [blame] | 4385 | Ctx.getTranslationUnitDecl(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4386 | SourceLocation(), SourceLocation(), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4387 | &Ctx.Idents.get("_objc_super")); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4388 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4389 | Ctx.getObjCIdType(), 0, 0, false, ICIS_NoInit)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4390 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4391 | Ctx.getObjCClassType(), 0, 0, false, |
| 4392 | ICIS_NoInit)); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4393 | RD->completeDefinition(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4394 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4395 | SuperCTy = Ctx.getTagDeclType(RD); |
| 4396 | SuperPtrCTy = Ctx.getPointerType(SuperCTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4397 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4398 | SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4399 | SuperPtrTy = llvm::PointerType::getUnqual(SuperTy); |
| 4400 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4401 | // struct _prop_t { |
| 4402 | // char *name; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4403 | // char *attributes; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4404 | // } |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4405 | PropertyTy = llvm::StructType::create("struct._prop_t", |
| 4406 | Int8PtrTy, Int8PtrTy, NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4407 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4408 | // struct _prop_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4409 | // uint32_t entsize; // sizeof(struct _prop_t) |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4410 | // uint32_t count_of_properties; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4411 | // struct _prop_t prop_list[count_of_properties]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4412 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4413 | PropertyListTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4414 | llvm::StructType::create("struct._prop_list_t", IntTy, IntTy, |
| 4415 | llvm::ArrayType::get(PropertyTy, 0), NULL); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4416 | // struct _prop_list_t * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4417 | PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4418 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4419 | // struct _objc_method { |
| 4420 | // SEL _cmd; |
| 4421 | // char *method_type; |
| 4422 | // char *_imp; |
| 4423 | // } |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4424 | MethodTy = llvm::StructType::create("struct._objc_method", |
| 4425 | SelectorPtrTy, Int8PtrTy, Int8PtrTy, |
| 4426 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4427 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4428 | // struct _objc_cache * |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4429 | CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache"); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4430 | CachePtrTy = llvm::PointerType::getUnqual(CacheTy); |
Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 4431 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4432 | } |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4433 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4434 | ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4435 | : ObjCCommonTypesHelper(cgm) { |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4436 | // struct _objc_method_description { |
| 4437 | // SEL name; |
| 4438 | // char *types; |
| 4439 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4440 | MethodDescriptionTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4441 | llvm::StructType::create("struct._objc_method_description", |
| 4442 | SelectorPtrTy, Int8PtrTy, NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4443 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4444 | // struct _objc_method_description_list { |
| 4445 | // int count; |
| 4446 | // struct _objc_method_description[1]; |
| 4447 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4448 | MethodDescriptionListTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4449 | llvm::StructType::create("struct._objc_method_description_list", |
| 4450 | IntTy, |
| 4451 | llvm::ArrayType::get(MethodDescriptionTy, 0),NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4452 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4453 | // struct _objc_method_description_list * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4454 | MethodDescriptionListPtrTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4455 | llvm::PointerType::getUnqual(MethodDescriptionListTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4456 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4457 | // Protocol description structures |
| 4458 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4459 | // struct _objc_protocol_extension { |
| 4460 | // uint32_t size; // sizeof(struct _objc_protocol_extension) |
| 4461 | // struct _objc_method_description_list *optional_instance_methods; |
| 4462 | // struct _objc_method_description_list *optional_class_methods; |
| 4463 | // struct _objc_property_list *instance_properties; |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4464 | // const char ** extendedMethodTypes; |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4465 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4466 | ProtocolExtensionTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4467 | llvm::StructType::create("struct._objc_protocol_extension", |
| 4468 | IntTy, MethodDescriptionListPtrTy, |
| 4469 | MethodDescriptionListPtrTy, PropertyListPtrTy, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4470 | Int8PtrPtrTy, NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4471 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4472 | // struct _objc_protocol_extension * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4473 | ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4474 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4475 | // Handle recursive construction of Protocol and ProtocolList types |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4476 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4477 | ProtocolTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4478 | llvm::StructType::create(VMContext, "struct._objc_protocol"); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4479 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4480 | ProtocolListTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4481 | llvm::StructType::create(VMContext, "struct._objc_protocol_list"); |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4482 | ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy), |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4483 | LongTy, |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4484 | llvm::ArrayType::get(ProtocolTy, 0), |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4485 | NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4486 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4487 | // struct _objc_protocol { |
| 4488 | // struct _objc_protocol_extension *isa; |
| 4489 | // char *protocol_name; |
| 4490 | // struct _objc_protocol **_objc_protocol_list; |
| 4491 | // struct _objc_method_description_list *instance_methods; |
| 4492 | // struct _objc_method_description_list *class_methods; |
| 4493 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4494 | ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy, |
| 4495 | llvm::PointerType::getUnqual(ProtocolListTy), |
| 4496 | MethodDescriptionListPtrTy, |
| 4497 | MethodDescriptionListPtrTy, |
| 4498 | NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4499 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4500 | // struct _objc_protocol_list * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4501 | ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4502 | |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4503 | ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4504 | |
| 4505 | // Class description structures |
| 4506 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4507 | // struct _objc_ivar { |
| 4508 | // char *ivar_name; |
| 4509 | // char *ivar_type; |
| 4510 | // int ivar_offset; |
| 4511 | // } |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4512 | IvarTy = llvm::StructType::create("struct._objc_ivar", |
| 4513 | Int8PtrTy, Int8PtrTy, IntTy, NULL); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4514 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4515 | // struct _objc_ivar_list * |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4516 | IvarListTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4517 | llvm::StructType::create(VMContext, "struct._objc_ivar_list"); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4518 | IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4519 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4520 | // struct _objc_method_list * |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4521 | MethodListTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4522 | llvm::StructType::create(VMContext, "struct._objc_method_list"); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4523 | MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4524 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4525 | // struct _objc_class_extension * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4526 | ClassExtensionTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4527 | llvm::StructType::create("struct._objc_class_extension", |
| 4528 | IntTy, Int8PtrTy, PropertyListPtrTy, NULL); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4529 | ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4530 | |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4531 | ClassTy = llvm::StructType::create(VMContext, "struct._objc_class"); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4532 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4533 | // struct _objc_class { |
| 4534 | // Class isa; |
| 4535 | // Class super_class; |
| 4536 | // char *name; |
| 4537 | // long version; |
| 4538 | // long info; |
| 4539 | // long instance_size; |
| 4540 | // struct _objc_ivar_list *ivars; |
| 4541 | // struct _objc_method_list *methods; |
| 4542 | // struct _objc_cache *cache; |
| 4543 | // struct _objc_protocol_list *protocols; |
| 4544 | // char *ivar_layout; |
| 4545 | // struct _objc_class_ext *ext; |
| 4546 | // }; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4547 | ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy), |
| 4548 | llvm::PointerType::getUnqual(ClassTy), |
| 4549 | Int8PtrTy, |
| 4550 | LongTy, |
| 4551 | LongTy, |
| 4552 | LongTy, |
| 4553 | IvarListPtrTy, |
| 4554 | MethodListPtrTy, |
| 4555 | CachePtrTy, |
| 4556 | ProtocolListPtrTy, |
| 4557 | Int8PtrTy, |
| 4558 | ClassExtensionPtrTy, |
| 4559 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4560 | |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4561 | ClassPtrTy = llvm::PointerType::getUnqual(ClassTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4562 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4563 | // struct _objc_category { |
| 4564 | // char *category_name; |
| 4565 | // char *class_name; |
| 4566 | // struct _objc_method_list *instance_method; |
| 4567 | // struct _objc_method_list *class_method; |
| 4568 | // uint32_t size; // sizeof(struct _objc_category) |
| 4569 | // struct _objc_property_list *instance_properties;// category's @property |
| 4570 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4571 | CategoryTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4572 | llvm::StructType::create("struct._objc_category", |
| 4573 | Int8PtrTy, Int8PtrTy, MethodListPtrTy, |
| 4574 | MethodListPtrTy, ProtocolListPtrTy, |
| 4575 | IntTy, PropertyListPtrTy, NULL); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 4576 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4577 | // Global metadata structures |
| 4578 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4579 | // struct _objc_symtab { |
| 4580 | // long sel_ref_cnt; |
| 4581 | // SEL *refs; |
| 4582 | // short cls_def_cnt; |
| 4583 | // short cat_def_cnt; |
| 4584 | // char *defs[cls_def_cnt + cat_def_cnt]; |
| 4585 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4586 | SymtabTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4587 | llvm::StructType::create("struct._objc_symtab", |
| 4588 | LongTy, SelectorPtrTy, ShortTy, ShortTy, |
| 4589 | llvm::ArrayType::get(Int8PtrTy, 0), NULL); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4590 | SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4591 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 4592 | // struct _objc_module { |
| 4593 | // long version; |
| 4594 | // long size; // sizeof(struct _objc_module) |
| 4595 | // char *name; |
| 4596 | // struct _objc_symtab* symtab; |
| 4597 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4598 | ModuleTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4599 | llvm::StructType::create("struct._objc_module", |
| 4600 | LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 4601 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4602 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 4603 | // FIXME: This is the size of the setjmp buffer and should be target |
| 4604 | // specific. 18 is what's used on 32-bit X86. |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 4605 | uint64_t SetJmpBufferSize = 18; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4606 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 4607 | // Exceptions |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4608 | llvm::Type *StackPtrTy = llvm::ArrayType::get(CGM.Int8PtrTy, 4); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4609 | |
| 4610 | ExceptionDataTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4611 | llvm::StructType::create("struct._objc_exception_data", |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 4612 | llvm::ArrayType::get(CGM.Int32Ty,SetJmpBufferSize), |
| 4613 | StackPtrTy, NULL); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 4614 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 4615 | } |
| 4616 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4617 | ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4618 | : ObjCCommonTypesHelper(cgm) { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4619 | // struct _method_list_t { |
| 4620 | // uint32_t entsize; // sizeof(struct _objc_method) |
| 4621 | // uint32_t method_count; |
| 4622 | // struct _objc_method method_list[method_count]; |
| 4623 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4624 | MethodListnfABITy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4625 | llvm::StructType::create("struct.__method_list_t", IntTy, IntTy, |
| 4626 | llvm::ArrayType::get(MethodTy, 0), NULL); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4627 | // struct method_list_t * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4628 | MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4629 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4630 | // struct _protocol_t { |
| 4631 | // id isa; // NULL |
| 4632 | // const char * const protocol_name; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4633 | // const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4634 | // const struct method_list_t * const instance_methods; |
| 4635 | // const struct method_list_t * const class_methods; |
| 4636 | // const struct method_list_t *optionalInstanceMethods; |
| 4637 | // const struct method_list_t *optionalClassMethods; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4638 | // const struct _prop_list_t * properties; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4639 | // const uint32_t size; // sizeof(struct _protocol_t) |
| 4640 | // const uint32_t flags; // = 0 |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4641 | // const char ** extendedMethodTypes; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4642 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4643 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4644 | // Holder for struct _protocol_list_t * |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4645 | ProtocolListnfABITy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4646 | llvm::StructType::create(VMContext, "struct._objc_protocol_list"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4647 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4648 | ProtocolnfABITy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4649 | llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy, |
| 4650 | llvm::PointerType::getUnqual(ProtocolListnfABITy), |
| 4651 | MethodListnfABIPtrTy, MethodListnfABIPtrTy, |
| 4652 | MethodListnfABIPtrTy, MethodListnfABIPtrTy, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4653 | PropertyListPtrTy, IntTy, IntTy, Int8PtrPtrTy, |
| 4654 | NULL); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4655 | |
| 4656 | // struct _protocol_t* |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4657 | ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4658 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4659 | // struct _protocol_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4660 | // long protocol_count; // Note, this is 32/64 bit |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4661 | // struct _protocol_t *[protocol_count]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4662 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4663 | ProtocolListnfABITy->setBody(LongTy, |
| 4664 | llvm::ArrayType::get(ProtocolnfABIPtrTy, 0), |
| 4665 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4666 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4667 | // struct _objc_protocol_list* |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4668 | ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4669 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4670 | // struct _ivar_t { |
| 4671 | // unsigned long int *offset; // pointer to ivar offset location |
| 4672 | // char *name; |
| 4673 | // char *type; |
| 4674 | // uint32_t alignment; |
| 4675 | // uint32_t size; |
| 4676 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4677 | IvarnfABITy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4678 | llvm::StructType::create("struct._ivar_t", |
| 4679 | llvm::PointerType::getUnqual(LongTy), |
| 4680 | Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4681 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4682 | // struct _ivar_list_t { |
| 4683 | // uint32 entsize; // sizeof(struct _ivar_t) |
| 4684 | // uint32 count; |
| 4685 | // struct _iver_t list[count]; |
| 4686 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4687 | IvarListnfABITy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4688 | llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy, |
| 4689 | llvm::ArrayType::get(IvarnfABITy, 0), NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4690 | |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4691 | IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4692 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4693 | // struct _class_ro_t { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4694 | // uint32_t const flags; |
| 4695 | // uint32_t const instanceStart; |
| 4696 | // uint32_t const instanceSize; |
| 4697 | // uint32_t const reserved; // only when building for 64bit targets |
| 4698 | // const uint8_t * const ivarLayout; |
| 4699 | // const char *const name; |
| 4700 | // const struct _method_list_t * const baseMethods; |
| 4701 | // const struct _objc_protocol_list *const baseProtocols; |
| 4702 | // const struct _ivar_list_t *const ivars; |
| 4703 | // const uint8_t * const weakIvarLayout; |
| 4704 | // const struct _prop_list_t * const properties; |
| 4705 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4706 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4707 | // FIXME. Add 'reserved' field in 64bit abi mode! |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4708 | ClassRonfABITy = llvm::StructType::create("struct._class_ro_t", |
| 4709 | IntTy, IntTy, IntTy, Int8PtrTy, |
| 4710 | Int8PtrTy, MethodListnfABIPtrTy, |
| 4711 | ProtocolListnfABIPtrTy, |
| 4712 | IvarListnfABIPtrTy, |
| 4713 | Int8PtrTy, PropertyListPtrTy, NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4714 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4715 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4716 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 4717 | ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false) |
| 4718 | ->getPointerTo(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4719 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4720 | // struct _class_t { |
| 4721 | // struct _class_t *isa; |
| 4722 | // struct _class_t * const superclass; |
| 4723 | // void *cache; |
| 4724 | // IMP *vtable; |
| 4725 | // struct class_ro_t *ro; |
| 4726 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4727 | |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4728 | ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t"); |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4729 | ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy), |
| 4730 | llvm::PointerType::getUnqual(ClassnfABITy), |
| 4731 | CachePtrTy, |
| 4732 | llvm::PointerType::getUnqual(ImpnfABITy), |
| 4733 | llvm::PointerType::getUnqual(ClassRonfABITy), |
| 4734 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4735 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4736 | // LLVM for struct _class_t * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4737 | ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4738 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4739 | // struct _category_t { |
| 4740 | // const char * const name; |
| 4741 | // struct _class_t *const cls; |
| 4742 | // const struct _method_list_t * const instance_methods; |
| 4743 | // const struct _method_list_t * const class_methods; |
| 4744 | // const struct _protocol_list_t * const protocols; |
| 4745 | // const struct _prop_list_t * const properties; |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 4746 | // } |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4747 | CategorynfABITy = llvm::StructType::create("struct._category_t", |
| 4748 | Int8PtrTy, ClassnfABIPtrTy, |
| 4749 | MethodListnfABIPtrTy, |
| 4750 | MethodListnfABIPtrTy, |
| 4751 | ProtocolListnfABIPtrTy, |
| 4752 | PropertyListPtrTy, |
| 4753 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4754 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4755 | // New types for nonfragile abi messaging. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4756 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 4757 | ASTContext &Ctx = CGM.getContext(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4758 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4759 | // MessageRefTy - LLVM for: |
| 4760 | // struct _message_ref_t { |
| 4761 | // IMP messenger; |
| 4762 | // SEL name; |
| 4763 | // }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4764 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4765 | // First the clang type for struct _message_ref_t |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4766 | RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct, |
Daniel Dunbar | daa3ac5 | 2010-04-29 16:29:11 +0000 | [diff] [blame] | 4767 | Ctx.getTranslationUnitDecl(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4768 | SourceLocation(), SourceLocation(), |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4769 | &Ctx.Idents.get("_message_ref_t")); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4770 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4771 | Ctx.VoidPtrTy, 0, 0, false, ICIS_NoInit)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4772 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4773 | Ctx.getObjCSelType(), 0, 0, false, |
| 4774 | ICIS_NoInit)); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4775 | RD->completeDefinition(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4776 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4777 | MessageRefCTy = Ctx.getTagDeclType(RD); |
| 4778 | MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy); |
| 4779 | MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4780 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4781 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4782 | MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4783 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4784 | // SuperMessageRefTy - LLVM for: |
| 4785 | // struct _super_message_ref_t { |
| 4786 | // SUPER_IMP messenger; |
| 4787 | // SEL name; |
| 4788 | // }; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4789 | SuperMessageRefTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4790 | llvm::StructType::create("struct._super_message_ref_t", |
| 4791 | ImpnfABITy, SelectorPtrTy, NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4792 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4793 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4794 | SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy); |
Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 4795 | |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 4796 | |
| 4797 | // struct objc_typeinfo { |
| 4798 | // const void** vtable; // objc_ehtype_vtable + 2 |
| 4799 | // const char* name; // c++ typeinfo string |
| 4800 | // Class cls; |
| 4801 | // }; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4802 | EHTypeTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4803 | llvm::StructType::create("struct._objc_typeinfo", |
| 4804 | llvm::PointerType::getUnqual(Int8PtrTy), |
| 4805 | Int8PtrTy, ClassnfABIPtrTy, NULL); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4806 | EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 4807 | } |
| 4808 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4809 | llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4810 | FinishNonFragileABIModule(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4811 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4812 | return NULL; |
| 4813 | } |
| 4814 | |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 4815 | void CGObjCNonFragileABIMac:: |
| 4816 | AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container, |
| 4817 | const char *SymbolName, |
| 4818 | const char *SectionName) { |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4819 | unsigned NumClasses = Container.size(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4820 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4821 | if (!NumClasses) |
| 4822 | return; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4823 | |
Chris Lattner | 0b23971 | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 4824 | SmallVector<llvm::Constant*, 8> Symbols(NumClasses); |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4825 | for (unsigned i=0; i<NumClasses; i++) |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4826 | Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i], |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4827 | ObjCTypes.Int8PtrTy); |
Chris Lattner | 0b23971 | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 4828 | llvm::Constant *Init = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4829 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Chris Lattner | 0b23971 | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 4830 | Symbols.size()), |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4831 | Symbols); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4832 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4833 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4834 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4835 | llvm::GlobalValue::InternalLinkage, |
| 4836 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4837 | SymbolName); |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 4838 | GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType())); |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4839 | GV->setSection(SectionName); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4840 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4841 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4842 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4843 | void CGObjCNonFragileABIMac::FinishNonFragileABIModule() { |
| 4844 | // nonfragile abi has no module definition. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4845 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4846 | // Build list of all implemented class addresses in array |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4847 | // L_OBJC_LABEL_CLASS_$. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4848 | AddModuleClassList(DefinedClasses, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4849 | "\01L_OBJC_LABEL_CLASS_$", |
| 4850 | "__DATA, __objc_classlist, regular, no_dead_strip"); |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4851 | |
Bill Wendling | 13562a1 | 2012-02-07 09:06:01 +0000 | [diff] [blame] | 4852 | for (unsigned i = 0, e = DefinedClasses.size(); i < e; i++) { |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4853 | llvm::GlobalValue *IMPLGV = DefinedClasses[i]; |
| 4854 | if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage) |
| 4855 | continue; |
| 4856 | IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4857 | } |
| 4858 | |
Bill Wendling | 13562a1 | 2012-02-07 09:06:01 +0000 | [diff] [blame] | 4859 | for (unsigned i = 0, e = DefinedMetaClasses.size(); i < e; i++) { |
Fariborz Jahanian | 0e93d25 | 2009-12-01 18:25:24 +0000 | [diff] [blame] | 4860 | llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i]; |
| 4861 | if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage) |
| 4862 | continue; |
| 4863 | IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 4864 | } |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4865 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4866 | AddModuleClassList(DefinedNonLazyClasses, |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4867 | "\01L_OBJC_LABEL_NONLAZY_CLASS_$", |
| 4868 | "__DATA, __objc_nlclslist, regular, no_dead_strip"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4869 | |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4870 | // Build list of all implemented category addresses in array |
| 4871 | // L_OBJC_LABEL_CATEGORY_$. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4872 | AddModuleClassList(DefinedCategories, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4873 | "\01L_OBJC_LABEL_CATEGORY_$", |
| 4874 | "__DATA, __objc_catlist, regular, no_dead_strip"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4875 | AddModuleClassList(DefinedNonLazyCategories, |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4876 | "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$", |
| 4877 | "__DATA, __objc_nlcatlist, regular, no_dead_strip"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4878 | |
Daniel Dunbar | fce176b | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 4879 | EmitImageInfo(); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4880 | } |
| 4881 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4882 | /// isVTableDispatchedSelector - Returns true if SEL is not in the list of |
| 4883 | /// VTableDispatchMethods; false otherwise. What this means is that |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4884 | /// except for the 19 selectors in the list, we generate 32bit-style |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4885 | /// message dispatch call for all the rest. |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4886 | bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) { |
| 4887 | // At various points we've experimented with using vtable-based |
| 4888 | // dispatch for all methods. |
Daniel Dunbar | f643b9b | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 4889 | switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) { |
Daniel Dunbar | f643b9b | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 4890 | case CodeGenOptions::Legacy: |
Fariborz Jahanian | 776dbf9 | 2010-04-19 17:53:30 +0000 | [diff] [blame] | 4891 | return false; |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4892 | case CodeGenOptions::NonLegacy: |
| 4893 | return true; |
Daniel Dunbar | f643b9b | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 4894 | case CodeGenOptions::Mixed: |
| 4895 | break; |
| 4896 | } |
| 4897 | |
| 4898 | // If so, see whether this selector is in the white-list of things which must |
| 4899 | // 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] | 4900 | if (VTableDispatchMethods.empty()) { |
| 4901 | VTableDispatchMethods.insert(GetNullarySelector("alloc")); |
| 4902 | VTableDispatchMethods.insert(GetNullarySelector("class")); |
| 4903 | VTableDispatchMethods.insert(GetNullarySelector("self")); |
| 4904 | VTableDispatchMethods.insert(GetNullarySelector("isFlipped")); |
| 4905 | VTableDispatchMethods.insert(GetNullarySelector("length")); |
| 4906 | VTableDispatchMethods.insert(GetNullarySelector("count")); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4907 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4908 | // These are vtable-based if GC is disabled. |
| 4909 | // Optimistically use vtable dispatch for hybrid compiles. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4910 | if (CGM.getLangOpts().getGC() != LangOptions::GCOnly) { |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4911 | VTableDispatchMethods.insert(GetNullarySelector("retain")); |
| 4912 | VTableDispatchMethods.insert(GetNullarySelector("release")); |
| 4913 | VTableDispatchMethods.insert(GetNullarySelector("autorelease")); |
| 4914 | } |
| 4915 | |
| 4916 | VTableDispatchMethods.insert(GetUnarySelector("allocWithZone")); |
| 4917 | VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass")); |
| 4918 | VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector")); |
| 4919 | VTableDispatchMethods.insert(GetUnarySelector("objectForKey")); |
| 4920 | VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex")); |
| 4921 | VTableDispatchMethods.insert(GetUnarySelector("isEqualToString")); |
| 4922 | VTableDispatchMethods.insert(GetUnarySelector("isEqual")); |
| 4923 | |
| 4924 | // These are vtable-based if GC is enabled. |
| 4925 | // Optimistically use vtable dispatch for hybrid compiles. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4926 | if (CGM.getLangOpts().getGC() != LangOptions::NonGC) { |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4927 | VTableDispatchMethods.insert(GetNullarySelector("hash")); |
| 4928 | VTableDispatchMethods.insert(GetUnarySelector("addObject")); |
| 4929 | |
| 4930 | // "countByEnumeratingWithState:objects:count" |
| 4931 | IdentifierInfo *KeyIdents[] = { |
| 4932 | &CGM.getContext().Idents.get("countByEnumeratingWithState"), |
| 4933 | &CGM.getContext().Idents.get("objects"), |
| 4934 | &CGM.getContext().Idents.get("count") |
| 4935 | }; |
| 4936 | VTableDispatchMethods.insert( |
| 4937 | CGM.getContext().Selectors.getSelector(3, KeyIdents)); |
| 4938 | } |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4939 | } |
Daniel Dunbar | f643b9b | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 4940 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4941 | return VTableDispatchMethods.count(Sel); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4942 | } |
| 4943 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4944 | // Metadata flags |
| 4945 | enum MetaDataDlags { |
| 4946 | CLS = 0x0, |
| 4947 | CLS_META = 0x1, |
| 4948 | CLS_ROOT = 0x2, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4949 | OBJC2_CLS_HIDDEN = 0x10, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4950 | CLS_EXCEPTION = 0x20, |
| 4951 | |
| 4952 | /// (Obsolete) ARC-specific: this class has a .release_ivars method |
| 4953 | CLS_HAS_IVAR_RELEASER = 0x40, |
| 4954 | /// class was compiled with -fobjc-arr |
| 4955 | CLS_COMPILED_BY_ARC = 0x80 // (1<<7) |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4956 | }; |
| 4957 | /// BuildClassRoTInitializer - generate meta-data for: |
| 4958 | /// struct _class_ro_t { |
| 4959 | /// uint32_t const flags; |
| 4960 | /// uint32_t const instanceStart; |
| 4961 | /// uint32_t const instanceSize; |
| 4962 | /// uint32_t const reserved; // only when building for 64bit targets |
| 4963 | /// const uint8_t * const ivarLayout; |
| 4964 | /// const char *const name; |
| 4965 | /// const struct _method_list_t * const baseMethods; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4966 | /// const struct _protocol_list_t *const baseProtocols; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4967 | /// const struct _ivar_list_t *const ivars; |
| 4968 | /// const uint8_t * const weakIvarLayout; |
| 4969 | /// const struct _prop_list_t * const properties; |
| 4970 | /// } |
| 4971 | /// |
| 4972 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4973 | unsigned flags, |
| 4974 | unsigned InstanceStart, |
| 4975 | unsigned InstanceSize, |
| 4976 | const ObjCImplementationDecl *ID) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4977 | std::string ClassName = ID->getNameAsString(); |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 4978 | llvm::Constant *Values[10]; // 11 for 64bit targets! |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4979 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4980 | if (CGM.getLangOpts().ObjCAutoRefCount) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4981 | flags |= CLS_COMPILED_BY_ARC; |
| 4982 | |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4983 | Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
| 4984 | Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart); |
| 4985 | Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4986 | // FIXME. For 64bit targets add 0 here. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4987 | Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4988 | : BuildIvarLayout(ID, true); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4989 | Values[ 4] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4990 | // const struct _method_list_t * const baseMethods; |
| 4991 | std::vector<llvm::Constant*> Methods; |
| 4992 | std::string MethodListName("\01l_OBJC_$_"); |
| 4993 | if (flags & CLS_META) { |
| 4994 | MethodListName += "CLASS_METHODS_" + ID->getNameAsString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4995 | for (ObjCImplementationDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4996 | i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4997 | // Class methods should always be defined. |
| 4998 | Methods.push_back(GetMethodConstant(*i)); |
| 4999 | } |
| 5000 | } else { |
| 5001 | MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5002 | for (ObjCImplementationDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5003 | i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5004 | // Instance methods should always be defined. |
| 5005 | Methods.push_back(GetMethodConstant(*i)); |
| 5006 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5007 | for (ObjCImplementationDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5008 | i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5009 | ObjCPropertyImplDecl *PID = *i; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5010 | |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 5011 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){ |
| 5012 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5013 | |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 5014 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 5015 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 5016 | Methods.push_back(C); |
| 5017 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 5018 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 5019 | Methods.push_back(C); |
| 5020 | } |
| 5021 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5022 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5023 | Values[ 5] = EmitMethodList(MethodListName, |
| 5024 | "__DATA, __objc_const", Methods); |
| 5025 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5026 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 5027 | assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5028 | Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5029 | + OID->getName(), |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 5030 | OID->all_referenced_protocol_begin(), |
| 5031 | OID->all_referenced_protocol_end()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5032 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5033 | if (flags & CLS_META) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5034 | Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5035 | else |
| 5036 | Values[ 7] = EmitIvarList(ID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5037 | Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 5038 | : BuildIvarLayout(ID, false); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 5039 | if (flags & CLS_META) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5040 | Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 5041 | else |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5042 | Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(), |
| 5043 | ID, ID->getClassInterface(), ObjCTypes); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5044 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5045 | Values); |
| 5046 | llvm::GlobalVariable *CLASS_RO_GV = |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5047 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false, |
| 5048 | llvm::GlobalValue::InternalLinkage, |
| 5049 | Init, |
| 5050 | (flags & CLS_META) ? |
| 5051 | std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName : |
| 5052 | std::string("\01l_OBJC_CLASS_RO_$_")+ClassName); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5053 | CLASS_RO_GV->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5054 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5055 | CLASS_RO_GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5056 | return CLASS_RO_GV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5057 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5058 | } |
| 5059 | |
| 5060 | /// BuildClassMetaData - This routine defines that to-level meta-data |
| 5061 | /// for the given ClassName for: |
| 5062 | /// struct _class_t { |
| 5063 | /// struct _class_t *isa; |
| 5064 | /// struct _class_t * const superclass; |
| 5065 | /// void *cache; |
| 5066 | /// IMP *vtable; |
| 5067 | /// struct class_ro_t *ro; |
| 5068 | /// } |
| 5069 | /// |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5070 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5071 | std::string &ClassName, |
| 5072 | llvm::Constant *IsAGV, |
| 5073 | llvm::Constant *SuperClassGV, |
| 5074 | llvm::Constant *ClassRoGV, |
| 5075 | bool HiddenVisibility) { |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5076 | llvm::Constant *Values[] = { |
| 5077 | IsAGV, |
| 5078 | SuperClassGV, |
| 5079 | ObjCEmptyCacheVar, // &ObjCEmptyCacheVar |
| 5080 | ObjCEmptyVtableVar, // &ObjCEmptyVtableVar |
| 5081 | ClassRoGV // &CLASS_RO_GV |
| 5082 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5083 | if (!Values[1]) |
| 5084 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5085 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5086 | Values); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5087 | llvm::GlobalVariable *GV = GetClassGlobal(ClassName); |
| 5088 | GV->setInitializer(Init); |
Fariborz Jahanian | dd0db2a | 2009-01-31 01:07:39 +0000 | [diff] [blame] | 5089 | GV->setSection("__DATA, __objc_data"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5090 | GV->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5091 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy)); |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5092 | if (HiddenVisibility) |
| 5093 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5094 | return GV; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5095 | } |
| 5096 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5097 | bool |
Fariborz Jahanian | ecfbdcb | 2009-05-21 01:03:45 +0000 | [diff] [blame] | 5098 | CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5099 | return OD->getClassMethod(GetNullarySelector("load")) != 0; |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5100 | } |
| 5101 | |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 5102 | void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 5103 | uint32_t &InstanceStart, |
| 5104 | uint32_t &InstanceSize) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5105 | const ASTRecordLayout &RL = |
Daniel Dunbar | b4c79e0 | 2009-05-04 21:26:30 +0000 | [diff] [blame] | 5106 | CGM.getContext().getASTObjCImplementationLayout(OID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5107 | |
Daniel Dunbar | 6e8575b | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 5108 | // InstanceSize is really instance end. |
Ken Dyck | ec29903 | 2011-02-11 02:20:09 +0000 | [diff] [blame] | 5109 | InstanceSize = RL.getDataSize().getQuantity(); |
Daniel Dunbar | 6e8575b | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 5110 | |
| 5111 | // If there are no fields, the start is the same as the end. |
| 5112 | if (!RL.getFieldCount()) |
| 5113 | InstanceStart = InstanceSize; |
| 5114 | else |
Ken Dyck | fb67ccd | 2011-04-14 00:43:09 +0000 | [diff] [blame] | 5115 | InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth(); |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 5116 | } |
| 5117 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5118 | void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) { |
| 5119 | std::string ClassName = ID->getNameAsString(); |
| 5120 | if (!ObjCEmptyCacheVar) { |
| 5121 | ObjCEmptyCacheVar = new llvm::GlobalVariable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5122 | CGM.getModule(), |
| 5123 | ObjCTypes.CacheTy, |
| 5124 | false, |
| 5125 | llvm::GlobalValue::ExternalLinkage, |
| 5126 | 0, |
| 5127 | "_objc_empty_cache"); |
| 5128 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5129 | ObjCEmptyVtableVar = new llvm::GlobalVariable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5130 | CGM.getModule(), |
| 5131 | ObjCTypes.ImpnfABITy, |
| 5132 | false, |
| 5133 | llvm::GlobalValue::ExternalLinkage, |
| 5134 | 0, |
| 5135 | "_objc_empty_vtable"); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5136 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5137 | assert(ID->getClassInterface() && |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5138 | "CGObjCNonFragileABIMac::GenerateClass - class is 0"); |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 5139 | // FIXME: Is this correct (that meta class size is never computed)? |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5140 | uint32_t InstanceStart = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5141 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5142 | uint32_t InstanceSize = InstanceStart; |
| 5143 | uint32_t flags = CLS_META; |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5144 | std::string ObjCMetaClassName(getMetaclassSymbolPrefix()); |
| 5145 | std::string ObjCClassName(getClassSymbolPrefix()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5146 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5147 | llvm::GlobalVariable *SuperClassGV, *IsAGV; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5148 | |
| 5149 | bool classIsHidden = |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 5150 | ID->getClassInterface()->getVisibility() == HiddenVisibility; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5151 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5152 | flags |= OBJC2_CLS_HIDDEN; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5153 | if (ID->hasCXXStructors()) |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 5154 | flags |= eClassFlags_ABI2_HasCXXStructors; |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5155 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5156 | // class is root |
| 5157 | flags |= CLS_ROOT; |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5158 | SuperClassGV = GetClassGlobal(ObjCClassName + ClassName); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5159 | IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5160 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5161 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 5162 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 5163 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 5164 | Root = Super; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5165 | IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString()); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5166 | if (Root->isWeakImported()) |
Fariborz Jahanian | 0e93d25 | 2009-12-01 18:25:24 +0000 | [diff] [blame] | 5167 | IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 5168 | // work on super class metadata symbol. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5169 | std::string SuperClassName = |
Fariborz Jahanian | 0e93d25 | 2009-12-01 18:25:24 +0000 | [diff] [blame] | 5170 | ObjCMetaClassName + |
| 5171 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5172 | SuperClassGV = GetClassGlobal(SuperClassName); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5173 | if (ID->getClassInterface()->getSuperClass()->isWeakImported()) |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 5174 | SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5175 | } |
| 5176 | llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags, |
| 5177 | InstanceStart, |
| 5178 | InstanceSize,ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5179 | std::string TClassName = ObjCMetaClassName + ClassName; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5180 | llvm::GlobalVariable *MetaTClass = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5181 | BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV, |
| 5182 | classIsHidden); |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 5183 | DefinedMetaClasses.push_back(MetaTClass); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5184 | |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5185 | // Metadata for the class |
| 5186 | flags = CLS; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5187 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5188 | flags |= OBJC2_CLS_HIDDEN; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5189 | if (ID->hasCXXStructors()) |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 5190 | flags |= eClassFlags_ABI2_HasCXXStructors; |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5191 | |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 5192 | if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface())) |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5193 | flags |= CLS_EXCEPTION; |
| 5194 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5195 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5196 | flags |= CLS_ROOT; |
| 5197 | SuperClassGV = 0; |
Chris Lattner | b7b58b1 | 2009-04-19 06:02:28 +0000 | [diff] [blame] | 5198 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5199 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 5200 | std::string RootClassName = |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5201 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5202 | SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5203 | if (ID->getClassInterface()->getSuperClass()->isWeakImported()) |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 5204 | SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5205 | } |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 5206 | GetClassSizeInfo(ID, InstanceStart, InstanceSize); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5207 | CLASS_RO_GV = BuildClassRoTInitializer(flags, |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 5208 | InstanceStart, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5209 | InstanceSize, |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 5210 | ID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5211 | |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5212 | TClassName = ObjCClassName + ClassName; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5213 | llvm::GlobalVariable *ClassMD = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5214 | BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV, |
| 5215 | classIsHidden); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 5216 | DefinedClasses.push_back(ClassMD); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5217 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5218 | // Determine if this class is also "non-lazy". |
| 5219 | if (ImplementationIsNonLazy(ID)) |
| 5220 | DefinedNonLazyClasses.push_back(ClassMD); |
| 5221 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5222 | // Force the definition of the EHType if necessary. |
| 5223 | if (flags & CLS_EXCEPTION) |
| 5224 | GetInterfaceEHType(ID->getClassInterface(), true); |
Fariborz Jahanian | 64089ce | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 5225 | // Make sure method definition entries are all clear for next implementation. |
| 5226 | MethodDefinitions.clear(); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5227 | } |
| 5228 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5229 | /// GenerateProtocolRef - This routine is called to generate code for |
| 5230 | /// a protocol reference expression; as in: |
| 5231 | /// @code |
| 5232 | /// @protocol(Proto1); |
| 5233 | /// @endcode |
| 5234 | /// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1 |
| 5235 | /// which will hold address of the protocol meta-data. |
| 5236 | /// |
| 5237 | llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5238 | const ObjCProtocolDecl *PD) { |
| 5239 | |
Fariborz Jahanian | 960cd06 | 2009-04-10 18:47:34 +0000 | [diff] [blame] | 5240 | // This routine is called for @protocol only. So, we must build definition |
| 5241 | // of protocol's meta-data (not a reference to it!) |
| 5242 | // |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5243 | llvm::Constant *Init = |
| 5244 | llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD), |
Douglas Gregor | 4c86fdb | 2012-01-17 18:36:30 +0000 | [diff] [blame] | 5245 | ObjCTypes.getExternalProtocolPtrTy()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5246 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5247 | std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_"); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5248 | ProtocolName += PD->getName(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5249 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5250 | llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName); |
| 5251 | if (PTGV) |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 5252 | return Builder.CreateLoad(PTGV); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5253 | PTGV = new llvm::GlobalVariable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5254 | CGM.getModule(), |
| 5255 | Init->getType(), false, |
| 5256 | llvm::GlobalValue::WeakAnyLinkage, |
| 5257 | Init, |
| 5258 | ProtocolName); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5259 | PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip"); |
| 5260 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5261 | CGM.AddUsedGlobal(PTGV); |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 5262 | return Builder.CreateLoad(PTGV); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5263 | } |
| 5264 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5265 | /// GenerateCategory - Build metadata for a category implementation. |
| 5266 | /// struct _category_t { |
| 5267 | /// const char * const name; |
| 5268 | /// struct _class_t *const cls; |
| 5269 | /// const struct _method_list_t * const instance_methods; |
| 5270 | /// const struct _method_list_t * const class_methods; |
| 5271 | /// const struct _protocol_list_t * const protocols; |
| 5272 | /// const struct _prop_list_t * const properties; |
| 5273 | /// } |
| 5274 | /// |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5275 | void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5276 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5277 | const char *Prefix = "\01l_OBJC_$_CATEGORY_"; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5278 | std::string ExtCatName(Prefix + Interface->getNameAsString()+ |
| 5279 | "_$_" + OCD->getNameAsString()); |
| 5280 | std::string ExtClassName(getClassSymbolPrefix() + |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5281 | Interface->getNameAsString()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5282 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5283 | llvm::Constant *Values[6]; |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5284 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 5285 | // meta-class entry symbol |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5286 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5287 | if (Interface->isWeakImported()) |
Fariborz Jahanian | 2cdcc4c | 2009-11-17 22:02:21 +0000 | [diff] [blame] | 5288 | ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
| 5289 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5290 | Values[1] = ClassGV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5291 | std::vector<llvm::Constant*> Methods; |
| 5292 | std::string MethodListName(Prefix); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5293 | MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() + |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5294 | "_$_" + OCD->getNameAsString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5295 | |
| 5296 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5297 | i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5298 | // Instance methods should always be defined. |
| 5299 | Methods.push_back(GetMethodConstant(*i)); |
| 5300 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5301 | |
| 5302 | Values[2] = EmitMethodList(MethodListName, |
| 5303 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5304 | Methods); |
| 5305 | |
| 5306 | MethodListName = Prefix; |
| 5307 | MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" + |
| 5308 | OCD->getNameAsString(); |
| 5309 | Methods.clear(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5310 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5311 | i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5312 | // Class methods should always be defined. |
| 5313 | Methods.push_back(GetMethodConstant(*i)); |
| 5314 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5315 | |
| 5316 | Values[3] = EmitMethodList(MethodListName, |
| 5317 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5318 | Methods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5319 | const ObjCCategoryDecl *Category = |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 5320 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5321 | if (Category) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 5322 | SmallString<256> ExtName; |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5323 | llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_" |
| 5324 | << OCD->getName(); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5325 | Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5326 | + Interface->getName() + "_$_" |
| 5327 | + Category->getName(), |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5328 | Category->protocol_begin(), |
| 5329 | Category->protocol_end()); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5330 | Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(), |
| 5331 | OCD, Category, ObjCTypes); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5332 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5333 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 5334 | Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5335 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5336 | |
| 5337 | llvm::Constant *Init = |
| 5338 | llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy, |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5339 | Values); |
| 5340 | llvm::GlobalVariable *GCATV |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5341 | = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy, |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5342 | false, |
| 5343 | llvm::GlobalValue::InternalLinkage, |
| 5344 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5345 | ExtCatName); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5346 | GCATV->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5347 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5348 | GCATV->setSection("__DATA, __objc_const"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5349 | CGM.AddUsedGlobal(GCATV); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5350 | DefinedCategories.push_back(GCATV); |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5351 | |
| 5352 | // Determine if this category is also "non-lazy". |
| 5353 | if (ImplementationIsNonLazy(OCD)) |
| 5354 | DefinedNonLazyCategories.push_back(GCATV); |
Fariborz Jahanian | 64089ce | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 5355 | // method definition entries must be clear for next implementation. |
| 5356 | MethodDefinitions.clear(); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5357 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5358 | |
| 5359 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 5360 | /// given method if it has been defined. The result is null if the |
| 5361 | /// method has not been defined. The return value has type MethodPtrTy. |
| 5362 | llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5363 | const ObjCMethodDecl *MD) { |
Argyrios Kyrtzidis | 9d50c06 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 5364 | llvm::Function *Fn = GetMethodDefinition(MD); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5365 | if (!Fn) |
| 5366 | return 0; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5367 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5368 | llvm::Constant *Method[] = { |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 5369 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5370 | ObjCTypes.SelectorPtrTy), |
| 5371 | GetMethodVarType(MD), |
| 5372 | llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy) |
| 5373 | }; |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5374 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5375 | } |
| 5376 | |
| 5377 | /// EmitMethodList - Build meta-data for method declarations |
| 5378 | /// struct _method_list_t { |
| 5379 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 5380 | /// uint32_t method_count; |
| 5381 | /// struct _objc_method method_list[method_count]; |
| 5382 | /// } |
| 5383 | /// |
Bill Wendling | bb02855 | 2012-02-07 09:25:09 +0000 | [diff] [blame] | 5384 | llvm::Constant * |
| 5385 | CGObjCNonFragileABIMac::EmitMethodList(Twine Name, |
| 5386 | const char *Section, |
| 5387 | ArrayRef<llvm::Constant*> Methods) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5388 | // Return null for empty list. |
| 5389 | if (Methods.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5390 | return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5391 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5392 | llvm::Constant *Values[3]; |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5393 | // sizeof(struct _objc_method) |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5394 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5395 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5396 | // method_count |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5397 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5398 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5399 | Methods.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 5400 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5401 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5402 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5403 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5404 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5405 | llvm::GlobalValue::InternalLinkage, Init, Name); |
| 5406 | GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType())); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5407 | GV->setSection(Section); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5408 | CGM.AddUsedGlobal(GV); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5409 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5410 | } |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5411 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 5412 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 5413 | /// the given ivar. |
Daniel Dunbar | e83be12 | 2010-04-02 21:14:02 +0000 | [diff] [blame] | 5414 | llvm::GlobalVariable * |
| 5415 | CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID, |
| 5416 | const ObjCIvarDecl *Ivar) { |
| 5417 | const ObjCInterfaceDecl *Container = Ivar->getContainingInterface(); |
Daniel Dunbar | a81419d | 2009-05-05 00:36:57 +0000 | [diff] [blame] | 5418 | std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() + |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 5419 | '.' + Ivar->getNameAsString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5420 | llvm::GlobalVariable *IvarOffsetGV = |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 5421 | CGM.getModule().getGlobalVariable(Name); |
| 5422 | if (!IvarOffsetGV) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5423 | IvarOffsetGV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5424 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 5425 | false, |
| 5426 | llvm::GlobalValue::ExternalLinkage, |
| 5427 | 0, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5428 | Name); |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 5429 | return IvarOffsetGV; |
| 5430 | } |
| 5431 | |
Daniel Dunbar | e83be12 | 2010-04-02 21:14:02 +0000 | [diff] [blame] | 5432 | llvm::Constant * |
| 5433 | CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
| 5434 | const ObjCIvarDecl *Ivar, |
| 5435 | unsigned long int Offset) { |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 5436 | llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5437 | IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy, |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 5438 | Offset)); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5439 | IvarOffsetGV->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5440 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy)); |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 5441 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 5442 | // FIXME: This matches gcc, but shouldn't the visibility be set on the use as |
| 5443 | // well (i.e., in ObjCIvarOffsetVariable). |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 5444 | if (Ivar->getAccessControl() == ObjCIvarDecl::Private || |
| 5445 | Ivar->getAccessControl() == ObjCIvarDecl::Package || |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 5446 | ID->getVisibility() == HiddenVisibility) |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 5447 | IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 5448 | else |
Fariborz Jahanian | 77c9fd2 | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 5449 | IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Bill Wendling | 1f38251 | 2011-05-04 21:37:25 +0000 | [diff] [blame] | 5450 | IvarOffsetGV->setSection("__DATA, __objc_ivar"); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 5451 | return IvarOffsetGV; |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5452 | } |
| 5453 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5454 | /// EmitIvarList - Emit the ivar list for the given |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5455 | /// implementation. The return value has type |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5456 | /// IvarListnfABIPtrTy. |
| 5457 | /// struct _ivar_t { |
| 5458 | /// unsigned long int *offset; // pointer to ivar offset location |
| 5459 | /// char *name; |
| 5460 | /// char *type; |
| 5461 | /// uint32_t alignment; |
| 5462 | /// uint32_t size; |
| 5463 | /// } |
| 5464 | /// struct _ivar_list_t { |
| 5465 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 5466 | /// uint32 count; |
| 5467 | /// struct _iver_t list[count]; |
| 5468 | /// } |
| 5469 | /// |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 5470 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5471 | llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5472 | const ObjCImplementationDecl *ID) { |
| 5473 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5474 | std::vector<llvm::Constant*> Ivars; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5475 | |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 5476 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5477 | assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5478 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5479 | // FIXME. Consolidate this with similar code in GenerateClass. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5480 | |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 5481 | for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin(); |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 5482 | IVD; IVD = IVD->getNextIvar()) { |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 5483 | // Ignore unnamed bit-fields. |
| 5484 | if (!IVD->getDeclName()) |
| 5485 | continue; |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5486 | llvm::Constant *Ivar[5]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5487 | Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD, |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 5488 | ComputeIvarBaseOffset(CGM, ID, IVD)); |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 5489 | Ivar[1] = GetMethodVarName(IVD->getIdentifier()); |
| 5490 | Ivar[2] = GetMethodVarType(IVD); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5491 | llvm::Type *FieldTy = |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 5492 | CGM.getTypes().ConvertTypeForMem(IVD->getType()); |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5493 | unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5494 | unsigned Align = CGM.getContext().getPreferredTypeAlign( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5495 | IVD->getType().getTypePtr()) >> 3; |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5496 | Align = llvm::Log2_32(Align); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5497 | Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align); |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 5498 | // NOTE. Size of a bitfield does not match gcc's, because of the |
| 5499 | // way bitfields are treated special in each. But I am told that |
| 5500 | // 'size' for bitfield ivars is ignored by the runtime so it does |
| 5501 | // not matter. If it matters, there is enough info to get the |
| 5502 | // bitfield right! |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5503 | Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5504 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar)); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5505 | } |
| 5506 | // Return null for empty list. |
| 5507 | if (Ivars.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5508 | return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5509 | |
| 5510 | llvm::Constant *Values[3]; |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5511 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5512 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 5513 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5514 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5515 | Ivars.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 5516 | Values[2] = llvm::ConstantArray::get(AT, Ivars); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5517 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5518 | const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_"; |
| 5519 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5520 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5521 | llvm::GlobalValue::InternalLinkage, |
| 5522 | Init, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5523 | Prefix + OID->getName()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5524 | GV->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5525 | CGM.getTargetData().getABITypeAlignment(Init->getType())); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5526 | GV->setSection("__DATA, __objc_const"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5527 | |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5528 | CGM.AddUsedGlobal(GV); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 5529 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5530 | } |
| 5531 | |
| 5532 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5533 | const ObjCProtocolDecl *PD) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5534 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5535 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5536 | if (!Entry) { |
| 5537 | // We use the initializer as a marker of whether this is a forward |
| 5538 | // reference or not. At module finalization we add the empty |
| 5539 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5540 | Entry = |
| 5541 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false, |
| 5542 | llvm::GlobalValue::ExternalLinkage, |
| 5543 | 0, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5544 | "\01l_OBJC_PROTOCOL_$_" + PD->getName()); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5545 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5546 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5547 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5548 | return Entry; |
| 5549 | } |
| 5550 | |
| 5551 | /// GetOrEmitProtocol - Generate the protocol meta-data: |
| 5552 | /// @code |
| 5553 | /// struct _protocol_t { |
| 5554 | /// id isa; // NULL |
| 5555 | /// const char * const protocol_name; |
| 5556 | /// const struct _protocol_list_t * protocol_list; // super protocols |
| 5557 | /// const struct method_list_t * const instance_methods; |
| 5558 | /// const struct method_list_t * const class_methods; |
| 5559 | /// const struct method_list_t *optionalInstanceMethods; |
| 5560 | /// const struct method_list_t *optionalClassMethods; |
| 5561 | /// const struct _prop_list_t * properties; |
| 5562 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 5563 | /// const uint32_t flags; // = 0 |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5564 | /// const char ** extendedMethodTypes; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5565 | /// } |
| 5566 | /// @endcode |
| 5567 | /// |
| 5568 | |
| 5569 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5570 | const ObjCProtocolDecl *PD) { |
John McCall | 50651b9 | 2012-03-30 21:29:05 +0000 | [diff] [blame] | 5571 | llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5572 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5573 | // Early exit if a defining object has already been generated. |
| 5574 | if (Entry && Entry->hasInitializer()) |
| 5575 | return Entry; |
| 5576 | |
Douglas Gregor | 1d784b2 | 2012-01-01 19:51:50 +0000 | [diff] [blame] | 5577 | // Use the protocol definition, if there is one. |
| 5578 | if (const ObjCProtocolDecl *Def = PD->getDefinition()) |
| 5579 | PD = Def; |
| 5580 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5581 | // Construct method lists. |
| 5582 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 5583 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5584 | std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5585 | for (ObjCProtocolDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5586 | i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5587 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5588 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5589 | if (!C) |
| 5590 | return GetOrEmitProtocolRef(PD); |
| 5591 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5592 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 5593 | OptInstanceMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5594 | OptMethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5595 | } else { |
| 5596 | InstanceMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5597 | MethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5598 | } |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5599 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5600 | |
| 5601 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5602 | i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5603 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5604 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5605 | if (!C) |
| 5606 | return GetOrEmitProtocolRef(PD); |
| 5607 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5608 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 5609 | OptClassMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5610 | OptMethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5611 | } else { |
| 5612 | ClassMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5613 | MethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5614 | } |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5615 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5616 | |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5617 | MethodTypesExt.insert(MethodTypesExt.end(), |
| 5618 | OptMethodTypesExt.begin(), OptMethodTypesExt.end()); |
| 5619 | |
| 5620 | llvm::Constant *Values[11]; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5621 | // isa is NULL |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5622 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5623 | Values[1] = GetClassName(PD->getIdentifier()); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5624 | Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(), |
| 5625 | PD->protocol_begin(), |
| 5626 | PD->protocol_end()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5627 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5628 | Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5629 | + PD->getName(), |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5630 | "__DATA, __objc_const", |
| 5631 | InstanceMethods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5632 | Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5633 | + PD->getName(), |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5634 | "__DATA, __objc_const", |
| 5635 | ClassMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5636 | Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5637 | + PD->getName(), |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5638 | "__DATA, __objc_const", |
| 5639 | OptInstanceMethods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5640 | Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5641 | + PD->getName(), |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5642 | "__DATA, __objc_const", |
| 5643 | OptClassMethods); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5644 | Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(), |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5645 | 0, PD, ObjCTypes); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5646 | uint32_t Size = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5647 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5648 | Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5649 | Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5650 | Values[10] = EmitProtocolMethodTypes("\01l_OBJC_$_PROTOCOL_METHOD_TYPES_" |
| 5651 | + PD->getName(), |
| 5652 | MethodTypesExt, ObjCTypes); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5653 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5654 | Values); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5655 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5656 | if (Entry) { |
| 5657 | // Already created, fix the linkage and update the initializer. |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 5658 | Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5659 | Entry->setInitializer(Init); |
| 5660 | } else { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5661 | Entry = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5662 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, |
| 5663 | false, llvm::GlobalValue::WeakAnyLinkage, Init, |
| 5664 | "\01l_OBJC_PROTOCOL_$_" + PD->getName()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5665 | Entry->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5666 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5667 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
John McCall | 50651b9 | 2012-03-30 21:29:05 +0000 | [diff] [blame] | 5668 | |
| 5669 | Protocols[PD->getIdentifier()] = Entry; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5670 | } |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 5671 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5672 | CGM.AddUsedGlobal(Entry); |
| 5673 | |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 5674 | // Use this protocol meta-data to build protocol list table in section |
| 5675 | // __DATA, __objc_protolist |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5676 | llvm::GlobalVariable *PTGV = |
| 5677 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy, |
| 5678 | false, llvm::GlobalValue::WeakAnyLinkage, Entry, |
| 5679 | "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5680 | PTGV->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5681 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy)); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 5682 | PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip"); |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 5683 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5684 | CGM.AddUsedGlobal(PTGV); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5685 | return Entry; |
| 5686 | } |
| 5687 | |
| 5688 | /// EmitProtocolList - Generate protocol list meta-data: |
| 5689 | /// @code |
| 5690 | /// struct _protocol_list_t { |
| 5691 | /// long protocol_count; // Note, this is 32/64 bit |
| 5692 | /// struct _protocol_t[protocol_count]; |
| 5693 | /// } |
| 5694 | /// @endcode |
| 5695 | /// |
| 5696 | llvm::Constant * |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5697 | CGObjCNonFragileABIMac::EmitProtocolList(Twine Name, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5698 | ObjCProtocolDecl::protocol_iterator begin, |
| 5699 | ObjCProtocolDecl::protocol_iterator end) { |
Bill Wendling | 3964e62 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 5700 | llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5701 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5702 | // Just return null for empty protocol lists |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5703 | if (begin == end) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5704 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5705 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5706 | // FIXME: We shouldn't need to do this lookup here, should we? |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 5707 | SmallString<256> TmpName; |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5708 | Name.toVector(TmpName); |
| 5709 | llvm::GlobalVariable *GV = |
| 5710 | CGM.getModule().getGlobalVariable(TmpName.str(), true); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5711 | if (GV) |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5712 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5713 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5714 | for (; begin != end; ++begin) |
| 5715 | ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented??? |
| 5716 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5717 | // This list is null terminated. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5718 | ProtocolRefs.push_back(llvm::Constant::getNullValue( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5719 | ObjCTypes.ProtocolnfABIPtrTy)); |
| 5720 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5721 | llvm::Constant *Values[2]; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 5722 | Values[0] = |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5723 | llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5724 | Values[1] = |
Bill Wendling | 3964e62 | 2012-02-09 22:16:49 +0000 | [diff] [blame] | 5725 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy, |
| 5726 | ProtocolRefs.size()), |
| 5727 | ProtocolRefs); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5728 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5729 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5730 | GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5731 | llvm::GlobalValue::InternalLinkage, |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5732 | Init, Name); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5733 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5734 | GV->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5735 | CGM.getTargetData().getABITypeAlignment(Init->getType())); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5736 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5737 | return llvm::ConstantExpr::getBitCast(GV, |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5738 | ObjCTypes.ProtocolListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5739 | } |
| 5740 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5741 | /// GetMethodDescriptionConstant - This routine build following meta-data: |
| 5742 | /// struct _objc_method { |
| 5743 | /// SEL _cmd; |
| 5744 | /// char *method_type; |
| 5745 | /// char *_imp; |
| 5746 | /// } |
| 5747 | |
| 5748 | llvm::Constant * |
| 5749 | CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5750 | llvm::Constant *Desc[3]; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 5751 | Desc[0] = |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5752 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 5753 | ObjCTypes.SelectorPtrTy); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5754 | Desc[1] = GetMethodVarType(MD); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5755 | if (!Desc[1]) |
| 5756 | return 0; |
| 5757 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5758 | // Protocol methods have no implementation. So, this entry is always NULL. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5759 | Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5760 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5761 | } |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 5762 | |
| 5763 | /// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference. |
| 5764 | /// This code gen. amounts to generating code for: |
| 5765 | /// @code |
| 5766 | /// (type *)((char *)base + _OBJC_IVAR_$_.ivar; |
| 5767 | /// @encode |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5768 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 5769 | LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar( |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 5770 | CodeGen::CodeGenFunction &CGF, |
| 5771 | QualType ObjectTy, |
| 5772 | llvm::Value *BaseValue, |
| 5773 | const ObjCIvarDecl *Ivar, |
| 5774 | unsigned CVRQualifiers) { |
| 5775 | ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface(); |
Fariborz Jahanian | cd285d0 | 2012-02-20 22:42:22 +0000 | [diff] [blame] | 5776 | llvm::Value *Offset = EmitIvarOffset(CGF, ID, Ivar); |
| 5777 | if (llvm::LoadInst *LI = dyn_cast<llvm::LoadInst>(Offset)) |
| 5778 | LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"), |
| 5779 | llvm::MDNode::get(VMContext, |
| 5780 | ArrayRef<llvm::Value*>())); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 5781 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
Fariborz Jahanian | cd285d0 | 2012-02-20 22:42:22 +0000 | [diff] [blame] | 5782 | Offset); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 5783 | } |
| 5784 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 5785 | llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5786 | CodeGen::CodeGenFunction &CGF, |
| 5787 | const ObjCInterfaceDecl *Interface, |
| 5788 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 2da84ff | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 5789 | return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar"); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 5790 | } |
| 5791 | |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5792 | static void appendSelectorForMessageRefTable(std::string &buffer, |
| 5793 | Selector selector) { |
| 5794 | if (selector.isUnarySelector()) { |
| 5795 | buffer += selector.getNameForSlot(0); |
| 5796 | return; |
| 5797 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5798 | |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5799 | for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) { |
| 5800 | buffer += selector.getNameForSlot(i); |
| 5801 | buffer += '_'; |
| 5802 | } |
| 5803 | } |
| 5804 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5805 | /// Emit a "v-table" message send. We emit a weak hidden-visibility |
| 5806 | /// struct, initially containing the selector pointer and a pointer to |
| 5807 | /// a "fixup" variant of the appropriate objc_msgSend. To call, we |
| 5808 | /// load and call the function pointer, passing the address of the |
| 5809 | /// struct as the second parameter. The runtime determines whether |
| 5810 | /// the selector is currently emitted using vtable dispatch; if so, it |
| 5811 | /// substitutes a stub function which simply tail-calls through the |
| 5812 | /// appropriate vtable slot, and if not, it substitues a stub function |
| 5813 | /// which tail-calls objc_msgSend. Both stubs adjust the selector |
| 5814 | /// argument to correctly point to the selector. |
| 5815 | RValue |
| 5816 | CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF, |
| 5817 | ReturnValueSlot returnSlot, |
| 5818 | QualType resultType, |
| 5819 | Selector selector, |
| 5820 | llvm::Value *arg0, |
| 5821 | QualType arg0Type, |
| 5822 | bool isSuper, |
| 5823 | const CallArgList &formalArgs, |
| 5824 | const ObjCMethodDecl *method) { |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5825 | // Compute the actual arguments. |
| 5826 | CallArgList args; |
| 5827 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5828 | // First argument: the receiver / super-call structure. |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5829 | if (!isSuper) |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5830 | arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy); |
| 5831 | args.add(RValue::get(arg0), arg0Type); |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5832 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5833 | // Second argument: a pointer to the message ref structure. Leave |
| 5834 | // the actual argument value blank for now. |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5835 | args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy); |
| 5836 | |
| 5837 | args.insert(args.end(), formalArgs.begin(), formalArgs.end()); |
| 5838 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 5839 | MessageSendInfo MSI = getMessageSendInfo(method, resultType, args); |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5840 | |
John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 5841 | NullReturnState nullReturn; |
| 5842 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5843 | // Find the function to call and the mangled name for the message |
| 5844 | // ref structure. Using a different mangled name wouldn't actually |
| 5845 | // be a problem; it would just be a waste. |
| 5846 | // |
| 5847 | // The runtime currently never uses vtable dispatch for anything |
| 5848 | // except normal, non-super message-sends. |
| 5849 | // FIXME: don't use this for that. |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5850 | llvm::Constant *fn = 0; |
| 5851 | std::string messageRefName("\01l_"); |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 5852 | if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) { |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5853 | if (isSuper) { |
| 5854 | fn = ObjCTypes.getMessageSendSuper2StretFixupFn(); |
| 5855 | messageRefName += "objc_msgSendSuper2_stret_fixup"; |
Chris Lattner | 9b7d670 | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 5856 | } else { |
John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 5857 | nullReturn.init(CGF, arg0); |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5858 | fn = ObjCTypes.getMessageSendStretFixupFn(); |
| 5859 | messageRefName += "objc_msgSend_stret_fixup"; |
Chris Lattner | 9b7d670 | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 5860 | } |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5861 | } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) { |
| 5862 | fn = ObjCTypes.getMessageSendFpretFixupFn(); |
| 5863 | messageRefName += "objc_msgSend_fpret_fixup"; |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5864 | } else { |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5865 | if (isSuper) { |
| 5866 | fn = ObjCTypes.getMessageSendSuper2FixupFn(); |
| 5867 | messageRefName += "objc_msgSendSuper2_fixup"; |
Chris Lattner | 9b7d670 | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 5868 | } else { |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5869 | fn = ObjCTypes.getMessageSendFixupFn(); |
| 5870 | messageRefName += "objc_msgSend_fixup"; |
Chris Lattner | 9b7d670 | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 5871 | } |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5872 | } |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5873 | assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend"); |
| 5874 | messageRefName += '_'; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5875 | |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5876 | // Append the selector name, except use underscores anywhere we |
| 5877 | // would have used colons. |
| 5878 | appendSelectorForMessageRefTable(messageRefName, selector); |
| 5879 | |
| 5880 | llvm::GlobalVariable *messageRef |
| 5881 | = CGM.getModule().getGlobalVariable(messageRefName); |
| 5882 | if (!messageRef) { |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5883 | // Build the message ref structure. |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5884 | llvm::Constant *values[] = { fn, GetMethodVarName(selector) }; |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5885 | llvm::Constant *init = llvm::ConstantStruct::getAnon(values); |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5886 | messageRef = new llvm::GlobalVariable(CGM.getModule(), |
| 5887 | init->getType(), |
| 5888 | /*constant*/ false, |
| 5889 | llvm::GlobalValue::WeakAnyLinkage, |
| 5890 | init, |
| 5891 | messageRefName); |
| 5892 | messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 5893 | messageRef->setAlignment(16); |
| 5894 | messageRef->setSection("__DATA, __objc_msgrefs, coalesced"); |
| 5895 | } |
Fariborz Jahanian | 3c52d36 | 2012-01-30 23:39:30 +0000 | [diff] [blame] | 5896 | |
| 5897 | bool requiresnullCheck = false; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5898 | if (CGM.getLangOpts().ObjCAutoRefCount && method) |
Fariborz Jahanian | 3c52d36 | 2012-01-30 23:39:30 +0000 | [diff] [blame] | 5899 | for (ObjCMethodDecl::param_const_iterator i = method->param_begin(), |
| 5900 | e = method->param_end(); i != e; ++i) { |
| 5901 | const ParmVarDecl *ParamDecl = (*i); |
| 5902 | if (ParamDecl->hasAttr<NSConsumedAttr>()) { |
| 5903 | if (!nullReturn.NullBB) |
| 5904 | nullReturn.init(CGF, arg0); |
| 5905 | requiresnullCheck = true; |
| 5906 | break; |
| 5907 | } |
| 5908 | } |
| 5909 | |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5910 | llvm::Value *mref = |
| 5911 | CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy); |
| 5912 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5913 | // Update the message ref argument. |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5914 | args[1].RV = RValue::get(mref); |
| 5915 | |
| 5916 | // Load the function to call from the message ref table. |
| 5917 | llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0); |
| 5918 | callee = CGF.Builder.CreateLoad(callee, "msgSend_fn"); |
| 5919 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 5920 | callee = CGF.Builder.CreateBitCast(callee, MSI.MessengerType); |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5921 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 5922 | RValue result = CGF.EmitCall(MSI.CallInfo, callee, returnSlot, args); |
Fariborz Jahanian | 3c52d36 | 2012-01-30 23:39:30 +0000 | [diff] [blame] | 5923 | return nullReturn.complete(CGF, result, resultType, formalArgs, |
| 5924 | requiresnullCheck ? method : 0); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5925 | } |
| 5926 | |
| 5927 | /// Generate code for a message send expression in the nonfragile abi. |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5928 | CodeGen::RValue |
| 5929 | CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 5930 | ReturnValueSlot Return, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5931 | QualType ResultType, |
| 5932 | Selector Sel, |
| 5933 | llvm::Value *Receiver, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5934 | const CallArgList &CallArgs, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 5935 | const ObjCInterfaceDecl *Class, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5936 | const ObjCMethodDecl *Method) { |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5937 | return isVTableDispatchedSelector(Sel) |
| 5938 | ? EmitVTableMessageSend(CGF, Return, ResultType, Sel, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5939 | Receiver, CGF.getContext().getObjCIdType(), |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5940 | false, CallArgs, Method) |
| 5941 | : EmitMessageSend(CGF, Return, ResultType, |
| 5942 | EmitSelector(CGF.Builder, Sel), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5943 | Receiver, CGF.getContext().getObjCIdType(), |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5944 | false, CallArgs, Method, ObjCTypes); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5945 | } |
| 5946 | |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5947 | llvm::GlobalVariable * |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5948 | CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5949 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5950 | |
Daniel Dunbar | dfff230 | 2009-03-02 05:18:14 +0000 | [diff] [blame] | 5951 | if (!GV) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5952 | GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5953 | false, llvm::GlobalValue::ExternalLinkage, |
| 5954 | 0, Name); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5955 | } |
| 5956 | |
| 5957 | return GV; |
| 5958 | } |
| 5959 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5960 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder, |
| 5961 | IdentifierInfo *II) { |
| 5962 | llvm::GlobalVariable *&Entry = ClassReferences[II]; |
| 5963 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5964 | if (!Entry) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5965 | std::string ClassName(getClassSymbolPrefix() + II->getName().str()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5966 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5967 | Entry = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5968 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, |
| 5969 | false, llvm::GlobalValue::InternalLinkage, |
| 5970 | ClassGV, |
| 5971 | "\01L_OBJC_CLASSLIST_REFERENCES_$_"); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5972 | Entry->setAlignment( |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5973 | CGM.getTargetData().getABITypeAlignment( |
| 5974 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5975 | Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5976 | CGM.AddUsedGlobal(Entry); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5977 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5978 | |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 5979 | return Builder.CreateLoad(Entry); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5980 | } |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5981 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5982 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder, |
| 5983 | const ObjCInterfaceDecl *ID) { |
| 5984 | return EmitClassRefFromId(Builder, ID->getIdentifier()); |
| 5985 | } |
| 5986 | |
| 5987 | llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef( |
| 5988 | CGBuilderTy &Builder) { |
| 5989 | IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool"); |
| 5990 | return EmitClassRefFromId(Builder, II); |
| 5991 | } |
| 5992 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5993 | llvm::Value * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5994 | CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5995 | const ObjCInterfaceDecl *ID) { |
| 5996 | llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5997 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5998 | if (!Entry) { |
| 5999 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
| 6000 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6001 | Entry = |
| 6002 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6003 | false, llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6004 | ClassGV, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6005 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_"); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6006 | Entry->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 6007 | CGM.getTargetData().getABITypeAlignment( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6008 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6009 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 6010 | CGM.AddUsedGlobal(Entry); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 6011 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6012 | |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 6013 | return Builder.CreateLoad(Entry); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 6014 | } |
| 6015 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6016 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 6017 | /// meta-data |
| 6018 | /// |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6019 | llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder, |
| 6020 | const ObjCInterfaceDecl *ID) { |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6021 | llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()]; |
| 6022 | if (Entry) |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 6023 | return Builder.CreateLoad(Entry); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6024 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 6025 | std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 6026 | llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6027 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6028 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6029 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6030 | MetaClassGV, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6031 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_"); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6032 | Entry->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 6033 | CGM.getTargetData().getABITypeAlignment( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6034 | ObjCTypes.ClassnfABIPtrTy)); |
| 6035 | |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 6036 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 6037 | CGM.AddUsedGlobal(Entry); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6038 | |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 6039 | return Builder.CreateLoad(Entry); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6040 | } |
| 6041 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 6042 | /// GetClass - Return a reference to the class for the given interface |
| 6043 | /// decl. |
| 6044 | llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder, |
| 6045 | const ObjCInterfaceDecl *ID) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 6046 | if (ID->isWeakImported()) { |
Fariborz Jahanian | 269f8bc | 2009-11-17 22:42:00 +0000 | [diff] [blame] | 6047 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
| 6048 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
| 6049 | ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
| 6050 | } |
| 6051 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 6052 | return EmitClassRef(Builder, ID); |
| 6053 | } |
| 6054 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6055 | /// Generates a message send where the super is the receiver. This is |
| 6056 | /// a message send to self with special delivery semantics indicating |
| 6057 | /// which class's method should be called. |
| 6058 | CodeGen::RValue |
| 6059 | CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 6060 | ReturnValueSlot Return, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6061 | QualType ResultType, |
| 6062 | Selector Sel, |
| 6063 | const ObjCInterfaceDecl *Class, |
| 6064 | bool isCategoryImpl, |
| 6065 | llvm::Value *Receiver, |
| 6066 | bool IsClassMessage, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 6067 | const CodeGen::CallArgList &CallArgs, |
| 6068 | const ObjCMethodDecl *Method) { |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6069 | // ... |
| 6070 | // Create and init a super structure; this is a (receiver, class) |
| 6071 | // pair we will pass to objc_msgSendSuper. |
| 6072 | llvm::Value *ObjCSuper = |
John McCall | 604da29 | 2011-03-04 08:00:29 +0000 | [diff] [blame] | 6073 | CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6074 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6075 | llvm::Value *ReceiverAsObject = |
| 6076 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 6077 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 6078 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6079 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6080 | // 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] | 6081 | llvm::Value *Target; |
| 6082 | if (IsClassMessage) { |
| 6083 | if (isCategoryImpl) { |
| 6084 | // Message sent to "super' in a class method defined in |
| 6085 | // a category implementation. |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6086 | Target = EmitClassRef(CGF.Builder, Class); |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 6087 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 6088 | Target = CGF.Builder.CreateLoad(Target); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 6089 | } else |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 6090 | Target = EmitMetaClassRef(CGF.Builder, Class); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 6091 | } else |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6092 | Target = EmitSuperClassRef(CGF.Builder, Class); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6093 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 6094 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext and |
| 6095 | // ObjCTypes types. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6096 | llvm::Type *ClassTy = |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6097 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
| 6098 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
| 6099 | CGF.Builder.CreateStore(Target, |
| 6100 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6101 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6102 | return (isVTableDispatchedSelector(Sel)) |
| 6103 | ? EmitVTableMessageSend(CGF, Return, ResultType, Sel, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6104 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6105 | true, CallArgs, Method) |
| 6106 | : EmitMessageSend(CGF, Return, ResultType, |
| 6107 | EmitSelector(CGF.Builder, Sel), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6108 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6109 | true, CallArgs, Method, ObjCTypes); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6110 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6111 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6112 | llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder, |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 6113 | Selector Sel, bool lval) { |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6114 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6115 | |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6116 | if (!Entry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6117 | llvm::Constant *Casted = |
| 6118 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 6119 | ObjCTypes.SelectorPtrTy); |
| 6120 | Entry = |
| 6121 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false, |
| 6122 | llvm::GlobalValue::InternalLinkage, |
| 6123 | Casted, "\01L_OBJC_SELECTOR_REFERENCES_"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 6124 | Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 6125 | CGM.AddUsedGlobal(Entry); |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6126 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6127 | |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 6128 | if (lval) |
| 6129 | return Entry; |
Pete Cooper | b6d7114 | 2011-11-10 21:45:06 +0000 | [diff] [blame] | 6130 | llvm::LoadInst* LI = Builder.CreateLoad(Entry); |
| 6131 | |
| 6132 | LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"), |
| 6133 | llvm::MDNode::get(VMContext, |
| 6134 | ArrayRef<llvm::Value*>())); |
| 6135 | return LI; |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6136 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6137 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 6138 | /// objc_assign_ivar (id src, id *dst, ptrdiff_t) |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6139 | /// |
| 6140 | void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6141 | llvm::Value *src, |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 6142 | llvm::Value *dst, |
| 6143 | llvm::Value *ivarOffset) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6144 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6145 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 6146 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6147 | assert(Size <= 8 && "does not support size > 8"); |
| 6148 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 6149 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 6150 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 6151 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6152 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 6153 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 6154 | CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(), |
| 6155 | src, dst, ivarOffset); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6156 | return; |
| 6157 | } |
| 6158 | |
| 6159 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 6160 | /// objc_assign_strongCast (id src, id *dst) |
| 6161 | /// |
| 6162 | void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6163 | CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6164 | llvm::Value *src, llvm::Value *dst) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6165 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6166 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 6167 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6168 | assert(Size <= 8 && "does not support size > 8"); |
| 6169 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6170 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 6171 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 6172 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6173 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 6174 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 6175 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6176 | src, dst, "weakassign"); |
| 6177 | return; |
| 6178 | } |
| 6179 | |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 6180 | void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6181 | CodeGen::CodeGenFunction &CGF, |
| 6182 | llvm::Value *DestPtr, |
| 6183 | llvm::Value *SrcPtr, |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 6184 | llvm::Value *Size) { |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 6185 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy); |
| 6186 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 6187 | CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(), |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 6188 | DestPtr, SrcPtr, Size); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 6189 | return; |
| 6190 | } |
| 6191 | |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6192 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
| 6193 | /// object: objc_read_weak (id *src) |
| 6194 | /// |
| 6195 | llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6196 | CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6197 | llvm::Value *AddrWeakObj) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6198 | llvm::Type* DestTy = |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6199 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
| 6200 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 6201 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6202 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 6203 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6204 | return read_weak; |
| 6205 | } |
| 6206 | |
| 6207 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 6208 | /// objc_assign_weak (id src, id *dst) |
| 6209 | /// |
| 6210 | void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6211 | llvm::Value *src, llvm::Value *dst) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6212 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6213 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 6214 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6215 | assert(Size <= 8 && "does not support size > 8"); |
| 6216 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 6217 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 6218 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 6219 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6220 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 6221 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 6222 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6223 | src, dst, "weakassign"); |
| 6224 | return; |
| 6225 | } |
| 6226 | |
| 6227 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 6228 | /// objc_assign_global (id src, id *dst) |
| 6229 | /// |
| 6230 | void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 6231 | llvm::Value *src, llvm::Value *dst, |
| 6232 | bool threadlocal) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6233 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6234 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 6235 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6236 | assert(Size <= 8 && "does not support size > 8"); |
| 6237 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 6238 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 6239 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 6240 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6241 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 6242 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 6243 | if (!threadlocal) |
| 6244 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
| 6245 | src, dst, "globalassign"); |
| 6246 | else |
| 6247 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(), |
| 6248 | src, dst, "threadlocalassign"); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6249 | return; |
| 6250 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6251 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6252 | void |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6253 | CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 6254 | const ObjCAtSynchronizedStmt &S) { |
David Chisnall | 05dc91b | 2011-03-25 17:46:35 +0000 | [diff] [blame] | 6255 | EmitAtSynchronizedStmt(CGF, S, |
| 6256 | cast<llvm::Function>(ObjCTypes.getSyncEnterFn()), |
| 6257 | cast<llvm::Function>(ObjCTypes.getSyncExitFn())); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6258 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6259 | |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6260 | llvm::Constant * |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 6261 | CGObjCNonFragileABIMac::GetEHType(QualType T) { |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6262 | // There's a particular fixed type info for 'id'. |
| 6263 | if (T->isObjCIdType() || |
| 6264 | T->isObjCQualifiedIdType()) { |
| 6265 | llvm::Constant *IDEHType = |
| 6266 | CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id"); |
| 6267 | if (!IDEHType) |
| 6268 | IDEHType = |
| 6269 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, |
| 6270 | false, |
| 6271 | llvm::GlobalValue::ExternalLinkage, |
| 6272 | 0, "OBJC_EHTYPE_id"); |
| 6273 | return IDEHType; |
| 6274 | } |
| 6275 | |
| 6276 | // All other types should be Objective-C interface pointer types. |
| 6277 | const ObjCObjectPointerType *PT = |
| 6278 | T->getAs<ObjCObjectPointerType>(); |
| 6279 | assert(PT && "Invalid @catch type."); |
| 6280 | const ObjCInterfaceType *IT = PT->getInterfaceType(); |
| 6281 | assert(IT && "Invalid @catch type."); |
| 6282 | return GetInterfaceEHType(IT->getDecl(), false); |
| 6283 | } |
| 6284 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6285 | void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
| 6286 | const ObjCAtTryStmt &S) { |
David Chisnall | 05dc91b | 2011-03-25 17:46:35 +0000 | [diff] [blame] | 6287 | EmitTryCatchStmt(CGF, S, |
| 6288 | cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()), |
| 6289 | cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()), |
| 6290 | cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn())); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 6291 | } |
| 6292 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6293 | /// EmitThrowStmt - Generate code for a throw statement. |
| 6294 | void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 6295 | const ObjCAtThrowStmt &S) { |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6296 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
John McCall | 2b014d6 | 2011-10-01 10:32:24 +0000 | [diff] [blame] | 6297 | llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr); |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 6298 | Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy); |
Jay Foad | 4c7d9f1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 6299 | CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception) |
John McCall | 7ec404c | 2010-10-16 08:21:07 +0000 | [diff] [blame] | 6300 | .setDoesNotReturn(); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6301 | } else { |
Jay Foad | 4c7d9f1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 6302 | CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn()) |
John McCall | 7ec404c | 2010-10-16 08:21:07 +0000 | [diff] [blame] | 6303 | .setDoesNotReturn(); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6304 | } |
| 6305 | |
John McCall | 7ec404c | 2010-10-16 08:21:07 +0000 | [diff] [blame] | 6306 | CGF.Builder.CreateUnreachable(); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6307 | CGF.Builder.ClearInsertionPoint(); |
| 6308 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6309 | |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6310 | llvm::Constant * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6311 | CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6312 | bool ForDefinition) { |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6313 | llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()]; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6314 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6315 | // If we don't need a definition, return the entry if found or check |
| 6316 | // if we use an external reference. |
| 6317 | if (!ForDefinition) { |
| 6318 | if (Entry) |
| 6319 | return Entry; |
Daniel Dunbar | 7e075cb | 2009-04-07 06:43:45 +0000 | [diff] [blame] | 6320 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6321 | // If this type (or a super class) has the __objc_exception__ |
| 6322 | // attribute, emit an external reference. |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 6323 | if (hasObjCExceptionAttribute(CGM.getContext(), ID)) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6324 | return Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6325 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false, |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6326 | llvm::GlobalValue::ExternalLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6327 | 0, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6328 | ("OBJC_EHTYPE_$_" + |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 6329 | ID->getIdentifier()->getName())); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6330 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6331 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6332 | // Otherwise we need to either make a new entry or fill in the |
| 6333 | // initializer. |
| 6334 | assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition"); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 6335 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6336 | std::string VTableName = "objc_ehtype_vtable"; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6337 | llvm::GlobalVariable *VTableGV = |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6338 | CGM.getModule().getGlobalVariable(VTableName); |
| 6339 | if (!VTableGV) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6340 | VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy, |
| 6341 | false, |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6342 | llvm::GlobalValue::ExternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6343 | 0, VTableName); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6344 | |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame] | 6345 | llvm::Value *VTableIdx = llvm::ConstantInt::get(CGM.Int32Ty, 2); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6346 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 6347 | llvm::Constant *Values[] = { |
| 6348 | llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx), |
| 6349 | GetClassName(ID->getIdentifier()), |
| 6350 | GetClassGlobal(ClassName) |
| 6351 | }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 6352 | llvm::Constant *Init = |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 6353 | llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6354 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6355 | if (Entry) { |
| 6356 | Entry->setInitializer(Init); |
| 6357 | } else { |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6358 | Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false, |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6359 | llvm::GlobalValue::WeakAnyLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6360 | Init, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6361 | ("OBJC_EHTYPE_$_" + |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 6362 | ID->getIdentifier()->getName())); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6363 | } |
| 6364 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6365 | if (CGM.getLangOpts().getVisibilityMode() == HiddenVisibility) |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 6366 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 6367 | Entry->setAlignment(CGM.getTargetData().getABITypeAlignment( |
| 6368 | ObjCTypes.EHTypeTy)); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6369 | |
| 6370 | if (ForDefinition) { |
| 6371 | Entry->setSection("__DATA,__objc_const"); |
| 6372 | Entry->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 6373 | } else { |
| 6374 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 6375 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6376 | |
| 6377 | return Entry; |
| 6378 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6379 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 6380 | /* *** */ |
| 6381 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 6382 | CodeGen::CGObjCRuntime * |
| 6383 | CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 6384 | switch (CGM.getLangOpts().ObjCRuntime.getKind()) { |
| 6385 | case ObjCRuntime::FragileMacOSX: |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 6386 | return new CGObjCMac(CGM); |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 6387 | |
| 6388 | case ObjCRuntime::MacOSX: |
| 6389 | case ObjCRuntime::iOS: |
| 6390 | return new CGObjCNonFragileABIMac(CGM); |
| 6391 | |
David Chisnall | 11d3f4c | 2012-07-03 20:49:52 +0000 | [diff] [blame] | 6392 | case ObjCRuntime::GNUstep: |
| 6393 | case ObjCRuntime::GCC: |
John McCall | f7226fb | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 6394 | case ObjCRuntime::ObjFW: |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 6395 | llvm_unreachable("these runtimes are not Mac runtimes"); |
| 6396 | } |
| 6397 | llvm_unreachable("bad runtime"); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 6398 | } |