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 | |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 45 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 46 | namespace { |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 47 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 48 | typedef std::vector<llvm::Constant*> ConstantVector; |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 49 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 50 | // FIXME: We should find a nicer way to make the labels for metadata, string |
| 51 | // concatenation is lame. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 52 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 53 | class ObjCCommonTypesHelper { |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 54 | protected: |
| 55 | llvm::LLVMContext &VMContext; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 56 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 57 | private: |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 58 | // The types of these functions don't really matter because we |
| 59 | // should always bitcast before calling them. |
| 60 | |
| 61 | /// id objc_msgSend (id, SEL, ...) |
| 62 | /// |
| 63 | /// The default messenger, used for sends whose ABI is unchanged from |
| 64 | /// the all-integer/pointer case. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 65 | llvm::Constant *getMessageSendFn() const { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 66 | // Add the non-lazy-bind attribute, since objc_msgSend is likely to |
| 67 | // be called a lot. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 68 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 69 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 70 | params, true), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 71 | "objc_msgSend", |
| 72 | llvm::Attribute::NonLazyBind); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 73 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 74 | |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 75 | /// void objc_msgSend_stret (id, SEL, ...) |
| 76 | /// |
| 77 | /// The messenger used when the return value is an aggregate returned |
| 78 | /// by indirect reference in the first argument, and therefore the |
| 79 | /// self and selector parameters are shifted over by one. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 80 | llvm::Constant *getMessageSendStretFn() const { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 81 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 82 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, |
| 83 | params, true), |
| 84 | "objc_msgSend_stret"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 85 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 86 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 87 | |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 88 | /// [double | long double] objc_msgSend_fpret(id self, SEL op, ...) |
| 89 | /// |
| 90 | /// The messenger used when the return value is returned on the x87 |
| 91 | /// floating-point stack; without a special entrypoint, the nil case |
| 92 | /// would be unbalanced. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 93 | llvm::Constant *getMessageSendFpretFn() const { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 94 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame^] | 95 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.DoubleTy, |
| 96 | params, true), |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 97 | "objc_msgSend_fpret"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 98 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 99 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 100 | |
Anders Carlsson | eea6480 | 2011-10-31 16:27:11 +0000 | [diff] [blame] | 101 | /// _Complex long double objc_msgSend_fp2ret(id self, SEL op, ...) |
| 102 | /// |
| 103 | /// The messenger used when the return value is returned in two values on the |
| 104 | /// x87 floating point stack; without a special entrypoint, the nil case |
| 105 | /// would be unbalanced. Only used on 64-bit X86. |
| 106 | llvm::Constant *getMessageSendFp2retFn() const { |
| 107 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
| 108 | llvm::Type *longDoubleType = llvm::Type::getX86_FP80Ty(VMContext); |
| 109 | llvm::Type *resultType = |
| 110 | llvm::StructType::get(longDoubleType, longDoubleType, NULL); |
| 111 | |
| 112 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(resultType, |
| 113 | params, true), |
| 114 | "objc_msgSend_fp2ret"); |
| 115 | } |
| 116 | |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 117 | /// id objc_msgSendSuper(struct objc_super *super, SEL op, ...) |
| 118 | /// |
| 119 | /// The messenger used for super calls, which have different dispatch |
| 120 | /// semantics. The class passed is the superclass of the current |
| 121 | /// class. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 122 | llvm::Constant *getMessageSendSuperFn() const { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 123 | llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 124 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 125 | params, true), |
| 126 | "objc_msgSendSuper"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 127 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 128 | |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 129 | /// id objc_msgSendSuper2(struct objc_super *super, SEL op, ...) |
| 130 | /// |
| 131 | /// A slightly different messenger used for super calls. The class |
| 132 | /// passed is the current class. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 133 | llvm::Constant *getMessageSendSuperFn2() const { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 134 | llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 135 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 136 | params, true), |
| 137 | "objc_msgSendSuper2"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 138 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 139 | |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 140 | /// void objc_msgSendSuper_stret(void *stretAddr, struct objc_super *super, |
| 141 | /// SEL op, ...) |
| 142 | /// |
| 143 | /// The messenger used for super calls which return an aggregate indirectly. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 144 | llvm::Constant *getMessageSendSuperStretFn() const { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 145 | llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 146 | return CGM.CreateRuntimeFunction( |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 147 | llvm::FunctionType::get(CGM.VoidTy, params, true), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 148 | "objc_msgSendSuper_stret"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 149 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 150 | |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 151 | /// void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super, |
| 152 | /// SEL op, ...) |
| 153 | /// |
| 154 | /// objc_msgSendSuper_stret with the super2 semantics. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 155 | llvm::Constant *getMessageSendSuperStretFn2() const { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 156 | llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 157 | return CGM.CreateRuntimeFunction( |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 158 | llvm::FunctionType::get(CGM.VoidTy, params, true), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 159 | "objc_msgSendSuper2_stret"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 160 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 161 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 162 | llvm::Constant *getMessageSendSuperFpretFn() const { |
| 163 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 164 | return getMessageSendSuperFn(); |
| 165 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 166 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 167 | llvm::Constant *getMessageSendSuperFpretFn2() const { |
| 168 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 169 | return getMessageSendSuperFn2(); |
| 170 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 171 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 172 | protected: |
| 173 | CodeGen::CodeGenModule &CGM; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 174 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 175 | public: |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 176 | llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy; |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 177 | llvm::Type *Int8PtrTy, *Int8PtrPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 178 | |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 179 | /// ObjectPtrTy - LLVM type for object handles (typeof(id)) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 180 | llvm::Type *ObjectPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 181 | |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 182 | /// PtrObjectPtrTy - LLVM type for id * |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 183 | llvm::Type *PtrObjectPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 184 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 185 | /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL)) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 186 | llvm::Type *SelectorPtrTy; |
Douglas Gregor | 4c86fdb | 2012-01-17 18:36:30 +0000 | [diff] [blame] | 187 | |
| 188 | private: |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 189 | /// ProtocolPtrTy - LLVM type for external protocol handles |
| 190 | /// (typeof(Protocol)) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 191 | llvm::Type *ExternalProtocolPtrTy; |
Douglas Gregor | 4c86fdb | 2012-01-17 18:36:30 +0000 | [diff] [blame] | 192 | |
| 193 | public: |
| 194 | llvm::Type *getExternalProtocolPtrTy() { |
| 195 | if (!ExternalProtocolPtrTy) { |
| 196 | // FIXME: It would be nice to unify this with the opaque type, so that the |
| 197 | // IR comes out a bit cleaner. |
| 198 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 199 | ASTContext &Ctx = CGM.getContext(); |
| 200 | llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); |
| 201 | ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T); |
| 202 | } |
| 203 | |
| 204 | return ExternalProtocolPtrTy; |
| 205 | } |
| 206 | |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 207 | // SuperCTy - clang type for struct objc_super. |
| 208 | QualType SuperCTy; |
| 209 | // SuperPtrCTy - clang type for struct objc_super *. |
| 210 | QualType SuperPtrCTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 211 | |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 212 | /// SuperTy - LLVM type for struct objc_super. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 213 | llvm::StructType *SuperTy; |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 214 | /// SuperPtrTy - LLVM type for struct objc_super *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 215 | llvm::Type *SuperPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 216 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 217 | /// PropertyTy - LLVM type for struct objc_property (struct _prop_t |
| 218 | /// in GCC parlance). |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 219 | llvm::StructType *PropertyTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 220 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 221 | /// PropertyListTy - LLVM type for struct objc_property_list |
| 222 | /// (_prop_list_t in GCC parlance). |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 223 | llvm::StructType *PropertyListTy; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 224 | /// PropertyListPtrTy - LLVM type for struct objc_property_list*. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 225 | llvm::Type *PropertyListPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 226 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 227 | // MethodTy - LLVM type for struct objc_method. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 228 | llvm::StructType *MethodTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 229 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 230 | /// CacheTy - LLVM type for struct objc_cache. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 231 | llvm::Type *CacheTy; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 232 | /// CachePtrTy - LLVM type for struct objc_cache *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 233 | llvm::Type *CachePtrTy; |
Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 234 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 235 | llvm::Constant *getGetPropertyFn() { |
| 236 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 237 | ASTContext &Ctx = CGM.getContext(); |
| 238 | // id objc_getProperty (id, SEL, ptrdiff_t, bool) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 239 | SmallVector<CanQualType,4> Params; |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 240 | CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType()); |
| 241 | CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType()); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 242 | Params.push_back(IdType); |
| 243 | Params.push_back(SelType); |
David Chisnall | ab5824e | 2011-03-22 20:03:13 +0000 | [diff] [blame] | 244 | Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified()); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 245 | Params.push_back(Ctx.BoolTy); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 246 | llvm::FunctionType *FTy = |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 247 | Types.GetFunctionType(Types.getFunctionInfo(IdType, Params, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 248 | FunctionType::ExtInfo()), |
| 249 | false); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 250 | return CGM.CreateRuntimeFunction(FTy, "objc_getProperty"); |
| 251 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 252 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 253 | llvm::Constant *getSetPropertyFn() { |
| 254 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 255 | ASTContext &Ctx = CGM.getContext(); |
| 256 | // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 257 | SmallVector<CanQualType,6> Params; |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 258 | CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType()); |
| 259 | CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType()); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 260 | Params.push_back(IdType); |
| 261 | Params.push_back(SelType); |
David Chisnall | ab5824e | 2011-03-22 20:03:13 +0000 | [diff] [blame] | 262 | Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified()); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 263 | Params.push_back(IdType); |
| 264 | Params.push_back(Ctx.BoolTy); |
| 265 | Params.push_back(Ctx.BoolTy); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 266 | llvm::FunctionType *FTy = |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 267 | Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 268 | FunctionType::ExtInfo()), |
| 269 | false); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 270 | return CGM.CreateRuntimeFunction(FTy, "objc_setProperty"); |
| 271 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 272 | |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 273 | |
| 274 | llvm::Constant *getCopyStructFn() { |
| 275 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 276 | ASTContext &Ctx = CGM.getContext(); |
| 277 | // void objc_copyStruct (void *, const void *, size_t, bool, bool) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 278 | SmallVector<CanQualType,5> Params; |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 279 | Params.push_back(Ctx.VoidPtrTy); |
| 280 | Params.push_back(Ctx.VoidPtrTy); |
| 281 | Params.push_back(Ctx.LongTy); |
| 282 | Params.push_back(Ctx.BoolTy); |
| 283 | Params.push_back(Ctx.BoolTy); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 284 | llvm::FunctionType *FTy = |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 285 | Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params, |
| 286 | FunctionType::ExtInfo()), |
| 287 | false); |
| 288 | return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct"); |
| 289 | } |
| 290 | |
Fariborz Jahanian | e317302 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 291 | /// This routine declares and returns address of: |
| 292 | /// void objc_copyCppObjectAtomic( |
| 293 | /// void *dest, const void *src, |
| 294 | /// void (*copyHelper) (void *dest, const void *source)); |
| 295 | llvm::Constant *getCppAtomicObjectFunction() { |
| 296 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 297 | ASTContext &Ctx = CGM.getContext(); |
| 298 | /// void objc_copyCppObjectAtomic(void *dest, const void *src, void *helper); |
| 299 | SmallVector<CanQualType,3> Params; |
| 300 | Params.push_back(Ctx.VoidPtrTy); |
| 301 | Params.push_back(Ctx.VoidPtrTy); |
| 302 | Params.push_back(Ctx.VoidPtrTy); |
| 303 | llvm::FunctionType *FTy = |
| 304 | Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params, |
| 305 | FunctionType::ExtInfo()), |
| 306 | false); |
| 307 | return CGM.CreateRuntimeFunction(FTy, "objc_copyCppObjectAtomic"); |
| 308 | } |
| 309 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 310 | llvm::Constant *getEnumerationMutationFn() { |
Daniel Dunbar | c1ab900 | 2009-07-11 20:32:50 +0000 | [diff] [blame] | 311 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 312 | ASTContext &Ctx = CGM.getContext(); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 313 | // void objc_enumerationMutation (id) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 314 | SmallVector<CanQualType,1> Params; |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 315 | Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType())); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 316 | llvm::FunctionType *FTy = |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 317 | Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 318 | FunctionType::ExtInfo()), |
| 319 | false); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 320 | return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation"); |
| 321 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 322 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 323 | /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function. |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 324 | llvm::Constant *getGcReadWeakFn() { |
| 325 | // id objc_read_weak (id *) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 326 | llvm::Type *args[] = { ObjectPtrTy->getPointerTo() }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 327 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 328 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 329 | return CGM.CreateRuntimeFunction(FTy, "objc_read_weak"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 332 | /// GcAssignWeakFn -- LLVM objc_assign_weak function. |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 333 | llvm::Constant *getGcAssignWeakFn() { |
| 334 | // id objc_assign_weak (id, id *) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 335 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() }; |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 336 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 337 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 338 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak"); |
| 339 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 340 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 341 | /// GcAssignGlobalFn -- LLVM objc_assign_global function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 342 | llvm::Constant *getGcAssignGlobalFn() { |
| 343 | // id objc_assign_global(id, id *) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 344 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 345 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 346 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 347 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_global"); |
| 348 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 349 | |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 350 | /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function. |
| 351 | llvm::Constant *getGcAssignThreadLocalFn() { |
| 352 | // id objc_assign_threadlocal(id src, id * dest) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 353 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() }; |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 354 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 355 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 356 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal"); |
| 357 | } |
| 358 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 359 | /// GcAssignIvarFn -- LLVM objc_assign_ivar function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 360 | llvm::Constant *getGcAssignIvarFn() { |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 361 | // id objc_assign_ivar(id, id *, ptrdiff_t) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 362 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo(), |
| 363 | CGM.PtrDiffTy }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 364 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 365 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 366 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar"); |
| 367 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 368 | |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 369 | /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function. |
| 370 | llvm::Constant *GcMemmoveCollectableFn() { |
| 371 | // void *objc_memmove_collectable(void *dst, const void *src, size_t size) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 372 | llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, LongTy }; |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 373 | llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, args, false); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 374 | return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable"); |
| 375 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 376 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 377 | /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 378 | llvm::Constant *getGcAssignStrongCastFn() { |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 379 | // id objc_assign_strongCast(id, id *) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 380 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 381 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 382 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 383 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast"); |
| 384 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 385 | |
| 386 | /// ExceptionThrowFn - LLVM objc_exception_throw function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 387 | llvm::Constant *getExceptionThrowFn() { |
| 388 | // void objc_exception_throw(id) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 389 | llvm::Type *args[] = { ObjectPtrTy }; |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 390 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 391 | llvm::FunctionType::get(CGM.VoidTy, args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 392 | return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw"); |
| 393 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 394 | |
Fariborz Jahanian | 69677ea | 2010-05-28 17:34:43 +0000 | [diff] [blame] | 395 | /// ExceptionRethrowFn - LLVM objc_exception_rethrow function. |
| 396 | llvm::Constant *getExceptionRethrowFn() { |
| 397 | // void objc_exception_rethrow(void) |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 398 | llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, false); |
Fariborz Jahanian | 69677ea | 2010-05-28 17:34:43 +0000 | [diff] [blame] | 399 | return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow"); |
| 400 | } |
| 401 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 402 | /// SyncEnterFn - LLVM object_sync_enter function. |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 403 | llvm::Constant *getSyncEnterFn() { |
| 404 | // void objc_sync_enter (id) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 405 | llvm::Type *args[] = { ObjectPtrTy }; |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 406 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 407 | llvm::FunctionType::get(CGM.VoidTy, args, false); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 408 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter"); |
| 409 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 410 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 411 | /// SyncExitFn - LLVM object_sync_exit function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 412 | llvm::Constant *getSyncExitFn() { |
| 413 | // void objc_sync_exit (id) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 414 | llvm::Type *args[] = { ObjectPtrTy }; |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 415 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 416 | llvm::FunctionType::get(CGM.VoidTy, args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 417 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit"); |
| 418 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 419 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 420 | llvm::Constant *getSendFn(bool IsSuper) const { |
| 421 | return IsSuper ? getMessageSendSuperFn() : getMessageSendFn(); |
| 422 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 423 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 424 | llvm::Constant *getSendFn2(bool IsSuper) const { |
| 425 | return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn(); |
| 426 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 427 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 428 | llvm::Constant *getSendStretFn(bool IsSuper) const { |
| 429 | return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn(); |
| 430 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 431 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 432 | llvm::Constant *getSendStretFn2(bool IsSuper) const { |
| 433 | return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn(); |
| 434 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 435 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 436 | llvm::Constant *getSendFpretFn(bool IsSuper) const { |
| 437 | return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn(); |
| 438 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 439 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 440 | llvm::Constant *getSendFpretFn2(bool IsSuper) const { |
| 441 | return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn(); |
| 442 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 443 | |
Anders Carlsson | eea6480 | 2011-10-31 16:27:11 +0000 | [diff] [blame] | 444 | llvm::Constant *getSendFp2retFn(bool IsSuper) const { |
| 445 | return IsSuper ? getMessageSendSuperFn() : getMessageSendFp2retFn(); |
| 446 | } |
| 447 | |
| 448 | llvm::Constant *getSendFp2RetFn2(bool IsSuper) const { |
| 449 | return IsSuper ? getMessageSendSuperFn2() : getMessageSendFp2retFn(); |
| 450 | } |
| 451 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 452 | ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm); |
| 453 | ~ObjCCommonTypesHelper(){} |
| 454 | }; |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 455 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 456 | /// ObjCTypesHelper - Helper class that encapsulates lazy |
| 457 | /// construction of varies types used during ObjC generation. |
| 458 | class ObjCTypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 459 | public: |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 460 | /// SymtabTy - LLVM type for struct objc_symtab. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 461 | llvm::StructType *SymtabTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 462 | /// SymtabPtrTy - LLVM type for struct objc_symtab *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 463 | llvm::Type *SymtabPtrTy; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 464 | /// ModuleTy - LLVM type for struct objc_module. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 465 | llvm::StructType *ModuleTy; |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 466 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 467 | /// ProtocolTy - LLVM type for struct objc_protocol. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 468 | llvm::StructType *ProtocolTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 469 | /// ProtocolPtrTy - LLVM type for struct objc_protocol *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 470 | llvm::Type *ProtocolPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 471 | /// ProtocolExtensionTy - LLVM type for struct |
| 472 | /// objc_protocol_extension. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 473 | llvm::StructType *ProtocolExtensionTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 474 | /// ProtocolExtensionTy - LLVM type for struct |
| 475 | /// objc_protocol_extension *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 476 | llvm::Type *ProtocolExtensionPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 477 | /// MethodDescriptionTy - LLVM type for struct |
| 478 | /// objc_method_description. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 479 | llvm::StructType *MethodDescriptionTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 480 | /// MethodDescriptionListTy - LLVM type for struct |
| 481 | /// objc_method_description_list. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 482 | llvm::StructType *MethodDescriptionListTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 483 | /// MethodDescriptionListPtrTy - LLVM type for struct |
| 484 | /// objc_method_description_list *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 485 | llvm::Type *MethodDescriptionListPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 486 | /// ProtocolListTy - LLVM type for struct objc_property_list. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 487 | llvm::StructType *ProtocolListTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 488 | /// ProtocolListPtrTy - LLVM type for struct objc_property_list*. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 489 | llvm::Type *ProtocolListPtrTy; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 490 | /// CategoryTy - LLVM type for struct objc_category. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 491 | llvm::StructType *CategoryTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 492 | /// ClassTy - LLVM type for struct objc_class. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 493 | llvm::StructType *ClassTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 494 | /// ClassPtrTy - LLVM type for struct objc_class *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 495 | llvm::Type *ClassPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 496 | /// ClassExtensionTy - LLVM type for struct objc_class_ext. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 497 | llvm::StructType *ClassExtensionTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 498 | /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 499 | llvm::Type *ClassExtensionPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 500 | // IvarTy - LLVM type for struct objc_ivar. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 501 | llvm::StructType *IvarTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 502 | /// IvarListTy - LLVM type for struct objc_ivar_list. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 503 | llvm::Type *IvarListTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 504 | /// IvarListPtrTy - LLVM type for struct objc_ivar_list *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 505 | llvm::Type *IvarListPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 506 | /// MethodListTy - LLVM type for struct objc_method_list. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 507 | llvm::Type *MethodListTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 508 | /// MethodListPtrTy - LLVM type for struct objc_method_list *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 509 | llvm::Type *MethodListPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 510 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 511 | /// ExceptionDataTy - LLVM type for struct _objc_exception_data. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 512 | llvm::Type *ExceptionDataTy; |
Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 513 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 514 | /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 515 | llvm::Constant *getExceptionTryEnterFn() { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 516 | llvm::Type *params[] = { ExceptionDataTy->getPointerTo() }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 517 | return CGM.CreateRuntimeFunction( |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 518 | llvm::FunctionType::get(CGM.VoidTy, params, false), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 519 | "objc_exception_try_enter"); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 520 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 521 | |
| 522 | /// ExceptionTryExitFn - LLVM objc_exception_try_exit function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 523 | llvm::Constant *getExceptionTryExitFn() { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 524 | llvm::Type *params[] = { ExceptionDataTy->getPointerTo() }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 525 | return CGM.CreateRuntimeFunction( |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 526 | llvm::FunctionType::get(CGM.VoidTy, params, false), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 527 | "objc_exception_try_exit"); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 528 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 529 | |
| 530 | /// ExceptionExtractFn - LLVM objc_exception_extract function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 531 | llvm::Constant *getExceptionExtractFn() { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 532 | llvm::Type *params[] = { ExceptionDataTy->getPointerTo() }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 533 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 534 | params, false), |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 535 | "objc_exception_extract"); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 536 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 537 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 538 | /// ExceptionMatchFn - LLVM objc_exception_match function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 539 | llvm::Constant *getExceptionMatchFn() { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 540 | llvm::Type *params[] = { ClassPtrTy, ObjectPtrTy }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 541 | return CGM.CreateRuntimeFunction( |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 542 | llvm::FunctionType::get(CGM.Int32Ty, params, false), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 543 | "objc_exception_match"); |
| 544 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 545 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 546 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 547 | /// SetJmpFn - LLVM _setjmp function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 548 | llvm::Constant *getSetJmpFn() { |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 549 | // This is specifically the prototype for x86. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 550 | llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() }; |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 551 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty, |
| 552 | params, false), |
Bill Wendling | a9e269e | 2011-11-29 00:10:10 +0000 | [diff] [blame] | 553 | "_setjmp", |
| 554 | llvm::Attribute::ReturnsTwice); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 555 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 556 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 557 | public: |
| 558 | ObjCTypesHelper(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 559 | ~ObjCTypesHelper() {} |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 560 | }; |
| 561 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 562 | /// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 563 | /// modern abi |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 564 | class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 565 | public: |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 566 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 567 | // MethodListnfABITy - LLVM for struct _method_list_t |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 568 | llvm::StructType *MethodListnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 569 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 570 | // MethodListnfABIPtrTy - LLVM for struct _method_list_t* |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 571 | llvm::Type *MethodListnfABIPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 572 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 573 | // ProtocolnfABITy = LLVM for struct _protocol_t |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 574 | llvm::StructType *ProtocolnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 575 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 576 | // ProtocolnfABIPtrTy = LLVM for struct _protocol_t* |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 577 | llvm::Type *ProtocolnfABIPtrTy; |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 578 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 579 | // ProtocolListnfABITy - LLVM for struct _objc_protocol_list |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 580 | llvm::StructType *ProtocolListnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 581 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 582 | // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list* |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 583 | llvm::Type *ProtocolListnfABIPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 584 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 585 | // ClassnfABITy - LLVM for struct _class_t |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 586 | llvm::StructType *ClassnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 587 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 588 | // ClassnfABIPtrTy - LLVM for struct _class_t* |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 589 | llvm::Type *ClassnfABIPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 590 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 591 | // IvarnfABITy - LLVM for struct _ivar_t |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 592 | llvm::StructType *IvarnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 593 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 594 | // IvarListnfABITy - LLVM for struct _ivar_list_t |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 595 | llvm::StructType *IvarListnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 596 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 597 | // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t* |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 598 | llvm::Type *IvarListnfABIPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 599 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 600 | // ClassRonfABITy - LLVM for struct _class_ro_t |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 601 | llvm::StructType *ClassRonfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 602 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 603 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 604 | llvm::Type *ImpnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 605 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 606 | // CategorynfABITy - LLVM for struct _category_t |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 607 | llvm::StructType *CategorynfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 608 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 609 | // New types for nonfragile abi messaging. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 610 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 611 | // MessageRefTy - LLVM for: |
| 612 | // struct _message_ref_t { |
| 613 | // IMP messenger; |
| 614 | // SEL name; |
| 615 | // }; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 616 | llvm::StructType *MessageRefTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 617 | // MessageRefCTy - clang type for struct _message_ref_t |
| 618 | QualType MessageRefCTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 619 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 620 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 621 | llvm::Type *MessageRefPtrTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 622 | // MessageRefCPtrTy - clang type for struct _message_ref_t* |
| 623 | QualType MessageRefCPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 624 | |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 625 | // MessengerTy - Type of the messenger (shown as IMP above) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 626 | llvm::FunctionType *MessengerTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 627 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 628 | // SuperMessageRefTy - LLVM for: |
| 629 | // struct _super_message_ref_t { |
| 630 | // SUPER_IMP messenger; |
| 631 | // SEL name; |
| 632 | // }; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 633 | llvm::StructType *SuperMessageRefTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 634 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 635 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 636 | llvm::Type *SuperMessageRefPtrTy; |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 637 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 638 | llvm::Constant *getMessageSendFixupFn() { |
| 639 | // id objc_msgSend_fixup(id, struct message_ref_t*, ...) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 640 | llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 641 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 642 | params, true), |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 643 | "objc_msgSend_fixup"); |
| 644 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 645 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 646 | llvm::Constant *getMessageSendFpretFixupFn() { |
| 647 | // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 648 | llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 649 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 650 | params, true), |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 651 | "objc_msgSend_fpret_fixup"); |
| 652 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 653 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 654 | llvm::Constant *getMessageSendStretFixupFn() { |
| 655 | // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 656 | llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 657 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 658 | params, true), |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 659 | "objc_msgSend_stret_fixup"); |
| 660 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 661 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 662 | llvm::Constant *getMessageSendSuper2FixupFn() { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 663 | // id objc_msgSendSuper2_fixup (struct objc_super *, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 664 | // struct _super_message_ref_t*, ...) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 665 | llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 666 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 667 | params, true), |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 668 | "objc_msgSendSuper2_fixup"); |
| 669 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 670 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 671 | llvm::Constant *getMessageSendSuper2StretFixupFn() { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 672 | // id objc_msgSendSuper2_stret_fixup(struct objc_super *, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 673 | // struct _super_message_ref_t*, ...) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 674 | llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 675 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 676 | params, true), |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 677 | "objc_msgSendSuper2_stret_fixup"); |
| 678 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 679 | |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 680 | llvm::Constant *getObjCEndCatchFn() { |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 681 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false), |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 682 | "objc_end_catch"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 683 | |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 684 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 685 | |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 686 | llvm::Constant *getObjCBeginCatchFn() { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 687 | llvm::Type *params[] = { Int8PtrTy }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 688 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 689 | params, false), |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 690 | "objc_begin_catch"); |
| 691 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 692 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 693 | llvm::StructType *EHTypeTy; |
| 694 | llvm::Type *EHTypePtrTy; |
Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 695 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 696 | ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm); |
| 697 | ~ObjCNonFragileABITypesHelper(){} |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 698 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 699 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 700 | class CGObjCCommonMac : public CodeGen::CGObjCRuntime { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 701 | public: |
| 702 | // FIXME - accessibility |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 703 | class GC_IVAR { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 704 | public: |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 705 | unsigned ivar_bytepos; |
| 706 | unsigned ivar_size; |
| 707 | GC_IVAR(unsigned bytepos = 0, unsigned size = 0) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 708 | : ivar_bytepos(bytepos), ivar_size(size) {} |
Daniel Dunbar | 0941b49 | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 709 | |
| 710 | // Allow sorting based on byte pos. |
| 711 | bool operator<(const GC_IVAR &b) const { |
| 712 | return ivar_bytepos < b.ivar_bytepos; |
| 713 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 714 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 715 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 716 | class SKIP_SCAN { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 717 | public: |
| 718 | unsigned skip; |
| 719 | unsigned scan; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 720 | SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0) |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 721 | : skip(_skip), scan(_scan) {} |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 722 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 723 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 724 | protected: |
| 725 | CodeGen::CodeGenModule &CGM; |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 726 | llvm::LLVMContext &VMContext; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 727 | // FIXME! May not be needing this after all. |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 728 | unsigned ObjCABI; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 729 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 730 | // gc ivar layout bitmap calculation helper caches. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 731 | SmallVector<GC_IVAR, 16> SkipIvars; |
| 732 | SmallVector<GC_IVAR, 16> IvarsInfo; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 733 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 734 | /// LazySymbols - Symbols to generate a lazy reference for. See |
| 735 | /// DefinedSymbols and FinishModule(). |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 736 | llvm::SetVector<IdentifierInfo*> LazySymbols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 737 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 738 | /// DefinedSymbols - External symbols which are defined by this |
| 739 | /// module. The symbols in this list and LazySymbols are used to add |
| 740 | /// special linker symbols which ensure that Objective-C modules are |
| 741 | /// linked properly. |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 742 | llvm::SetVector<IdentifierInfo*> DefinedSymbols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 743 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 744 | /// ClassNames - uniqued class names. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 745 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 746 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 747 | /// MethodVarNames - uniqued method variable names. |
| 748 | llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 749 | |
Fariborz Jahanian | b9c5b3d | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 750 | /// DefinedCategoryNames - list of category names in form Class_Category. |
| 751 | llvm::SetVector<std::string> DefinedCategoryNames; |
| 752 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 753 | /// MethodVarTypes - uniqued method type signatures. We have to use |
| 754 | /// a StringMap here because have no other unique reference. |
| 755 | llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 756 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 757 | /// MethodDefinitions - map of methods which have been defined in |
| 758 | /// this translation unit. |
| 759 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 760 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 761 | /// PropertyNames - uniqued method variable names. |
| 762 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 763 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 764 | /// ClassReferences - uniqued class references. |
| 765 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 766 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 767 | /// SelectorReferences - uniqued selector references. |
| 768 | llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 769 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 770 | /// Protocols - Protocols for which an objc_protocol structure has |
| 771 | /// been emitted. Forward declarations are handled by creating an |
| 772 | /// empty structure whose initializer is filled in when/if defined. |
| 773 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 774 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 775 | /// DefinedProtocols - Protocols which have actually been |
| 776 | /// defined. We should not need this, see FIXME in GenerateProtocol. |
| 777 | llvm::DenseSet<IdentifierInfo*> DefinedProtocols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 778 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 779 | /// DefinedClasses - List of defined classes. |
| 780 | std::vector<llvm::GlobalValue*> DefinedClasses; |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 781 | |
| 782 | /// DefinedNonLazyClasses - List of defined "non-lazy" classes. |
| 783 | std::vector<llvm::GlobalValue*> DefinedNonLazyClasses; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 784 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 785 | /// DefinedCategories - List of defined categories. |
| 786 | std::vector<llvm::GlobalValue*> DefinedCategories; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 787 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 788 | /// DefinedNonLazyCategories - List of defined "non-lazy" categories. |
| 789 | std::vector<llvm::GlobalValue*> DefinedNonLazyCategories; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 790 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 791 | /// GetNameForMethod - Return a name for the given method. |
| 792 | /// \param[out] NameOut - The return value. |
| 793 | void GetNameForMethod(const ObjCMethodDecl *OMD, |
| 794 | const ObjCContainerDecl *CD, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 795 | SmallVectorImpl<char> &NameOut); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 796 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 797 | /// GetMethodVarName - Return a unique constant for the given |
| 798 | /// selector's name. The return value has type char *. |
| 799 | llvm::Constant *GetMethodVarName(Selector Sel); |
| 800 | llvm::Constant *GetMethodVarName(IdentifierInfo *Ident); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 801 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 802 | /// GetMethodVarType - Return a unique constant for the given |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 803 | /// method's type encoding string. The return value has type char *. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 804 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 805 | // FIXME: This is a horrible name. |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 806 | llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D, |
| 807 | bool Extended = false); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 808 | llvm::Constant *GetMethodVarType(const FieldDecl *D); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 809 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 810 | /// GetPropertyName - Return a unique constant for the given |
| 811 | /// name. The return value has type char *. |
| 812 | llvm::Constant *GetPropertyName(IdentifierInfo *Ident); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 813 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 814 | // FIXME: This can be dropped once string functions are unified. |
| 815 | llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 816 | const Decl *Container); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 817 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 818 | /// GetClassName - Return a unique constant for the given selector's |
| 819 | /// name. The return value has type char *. |
| 820 | llvm::Constant *GetClassName(IdentifierInfo *Ident); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 821 | |
Argyrios Kyrtzidis | 9d50c06 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 822 | llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD); |
| 823 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 824 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 825 | /// implementation for the __strong or __weak case. |
| 826 | /// |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 827 | llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI, |
| 828 | bool ForStrongLayout); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 829 | |
Fariborz Jahanian | b8fd2eb | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 830 | llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 831 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 832 | void BuildAggrIvarRecordLayout(const RecordType *RT, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 833 | unsigned int BytePos, bool ForStrongLayout, |
| 834 | bool &HasUnion); |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 835 | void BuildAggrIvarLayout(const ObjCImplementationDecl *OI, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 836 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 837 | const RecordDecl *RD, |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 838 | const SmallVectorImpl<const FieldDecl*> &RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 839 | unsigned int BytePos, bool ForStrongLayout, |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 840 | bool &HasUnion); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 841 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 842 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 843 | /// ivar layout bitmap. |
| 844 | llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident, |
| 845 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 846 | |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 847 | /// EmitPropertyList - Emit the given property list. The return |
| 848 | /// value has type PropertyListPtrTy. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 849 | llvm::Constant *EmitPropertyList(Twine Name, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 850 | const Decl *Container, |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 851 | const ObjCContainerDecl *OCD, |
| 852 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 853 | |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 854 | /// EmitProtocolMethodTypes - Generate the array of extended method type |
| 855 | /// strings. The return value has type Int8PtrPtrTy. |
| 856 | llvm::Constant *EmitProtocolMethodTypes(Twine Name, |
| 857 | const ConstantVector &MethodTypes, |
| 858 | const ObjCCommonTypesHelper &ObjCTypes); |
| 859 | |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 860 | /// PushProtocolProperties - Push protocol's property on the input stack. |
| 861 | void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet, |
| 862 | std::vector<llvm::Constant*> &Properties, |
| 863 | const Decl *Container, |
| 864 | const ObjCProtocolDecl *PROTO, |
| 865 | const ObjCCommonTypesHelper &ObjCTypes); |
| 866 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 867 | /// GetProtocolRef - Return a reference to the internal protocol |
| 868 | /// description, creating an empty one if it has not been |
| 869 | /// defined. The return value has type ProtocolPtrTy. |
| 870 | llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD); |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 871 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 872 | /// CreateMetadataVar - Create a global variable with internal |
| 873 | /// linkage for use by the Objective-C runtime. |
| 874 | /// |
| 875 | /// This is a convenience wrapper which not only creates the |
| 876 | /// variable, but also sets the section and alignment and adds the |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 877 | /// global to the "llvm.used" list. |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 878 | /// |
| 879 | /// \param Name - The variable name. |
| 880 | /// \param Init - The variable initializer; this is also used to |
| 881 | /// define the type of the variable. |
| 882 | /// \param Section - The section the variable should go into, or 0. |
| 883 | /// \param Align - The alignment for the variable, or 0. |
| 884 | /// \param AddToUsed - Whether the variable should be added to |
Daniel Dunbar | c158306 | 2009-04-14 17:42:51 +0000 | [diff] [blame] | 885 | /// "llvm.used". |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 886 | llvm::GlobalVariable *CreateMetadataVar(Twine Name, |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 887 | llvm::Constant *Init, |
| 888 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 889 | unsigned Align, |
| 890 | bool AddToUsed); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 891 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 892 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 893 | ReturnValueSlot Return, |
| 894 | QualType ResultType, |
| 895 | llvm::Value *Sel, |
| 896 | llvm::Value *Arg0, |
| 897 | QualType Arg0Ty, |
| 898 | bool IsSuper, |
| 899 | const CallArgList &CallArgs, |
| 900 | const ObjCMethodDecl *OMD, |
| 901 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 902 | |
Daniel Dunbar | fce176b | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 903 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 904 | /// level information. |
| 905 | void EmitImageInfo(); |
| 906 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 907 | public: |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 908 | CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 909 | CGM(cgm), VMContext(cgm.getLLVMContext()) { } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 910 | |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 911 | virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 912 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 913 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
| 914 | const ObjCContainerDecl *CD=0); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 915 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 916 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 917 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 918 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 919 | /// declaration, emitting it if necessary. The return value has type |
| 920 | /// ProtocolPtrTy. |
| 921 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 922 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 923 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 924 | /// object for the given declaration, emitting it if needed. These |
| 925 | /// forward references will be filled in with empty bodies if no |
| 926 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 927 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0; |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 928 | virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM, |
| 929 | const CGBlockInfo &blockInfo); |
Fariborz Jahanian | 89ecd41 | 2010-08-04 16:57:49 +0000 | [diff] [blame] | 930 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 931 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 932 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 933 | class CGObjCMac : public CGObjCCommonMac { |
| 934 | private: |
| 935 | ObjCTypesHelper ObjCTypes; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 936 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 937 | /// EmitModuleInfo - Another marker encoding module level |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 938 | /// information. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 939 | void EmitModuleInfo(); |
| 940 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 941 | /// EmitModuleSymols - Emit module symbols, the list of defined |
| 942 | /// classes and categories. The result has type SymtabPtrTy. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 943 | llvm::Constant *EmitModuleSymbols(); |
| 944 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 945 | /// FinishModule - Write out global data structures at the end of |
| 946 | /// processing a translation unit. |
| 947 | void FinishModule(); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 948 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 949 | /// EmitClassExtension - Generate the class extension structure used |
| 950 | /// to store the weak ivar layout and properties. The return value |
| 951 | /// has type ClassExtensionPtrTy. |
| 952 | llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID); |
| 953 | |
| 954 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 955 | /// for the given class. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 956 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 957 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 958 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 959 | llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder, |
| 960 | IdentifierInfo *II); |
| 961 | |
| 962 | llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder); |
| 963 | |
Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 964 | /// EmitSuperClassRef - Emits reference to class's main metadata class. |
| 965 | llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 966 | |
| 967 | /// EmitIvarList - Emit the ivar list for the given |
| 968 | /// implementation. If ForClass is true the list of class ivars |
| 969 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 970 | /// interface ivars will be emitted. The return value has type |
| 971 | /// IvarListPtrTy. |
| 972 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 973 | bool ForClass); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 974 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 975 | /// EmitMetaClass - Emit a forward reference to the class structure |
| 976 | /// for the metaclass of the given interface. The return value has |
| 977 | /// type ClassPtrTy. |
| 978 | llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID); |
| 979 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 980 | /// EmitMetaClass - Emit a class structure for the metaclass of the |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 981 | /// given implementation. The return value has type ClassPtrTy. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 982 | llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID, |
| 983 | llvm::Constant *Protocols, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 984 | const ConstantVector &Methods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 985 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 986 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 987 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 988 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 989 | |
| 990 | /// EmitMethodList - Emit the method list for the given |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 991 | /// implementation. The return value has type MethodListPtrTy. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 992 | llvm::Constant *EmitMethodList(Twine Name, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 993 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 994 | const ConstantVector &Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 995 | |
| 996 | /// EmitMethodDescList - Emit a method description list for a list of |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 997 | /// method declarations. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 998 | /// - TypeName: The name for the type containing the methods. |
| 999 | /// - IsProtocol: True iff these methods are for a protocol. |
| 1000 | /// - ClassMethds: True iff these are class methods. |
| 1001 | /// - Required: When true, only "required" methods are |
| 1002 | /// listed. Similarly, when false only "optional" methods are |
| 1003 | /// listed. For classes this should always be true. |
| 1004 | /// - begin, end: The method list to output. |
| 1005 | /// |
| 1006 | /// The return value has type MethodDescriptionListPtrTy. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1007 | llvm::Constant *EmitMethodDescList(Twine Name, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1008 | const char *Section, |
| 1009 | const ConstantVector &Methods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1010 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1011 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1012 | /// declaration, emitting it if necessary. The return value has type |
| 1013 | /// ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1014 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1015 | |
| 1016 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1017 | /// object for the given declaration, emitting it if needed. These |
| 1018 | /// forward references will be filled in with empty bodies if no |
| 1019 | /// definition is seen. The return value has type ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1020 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1021 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1022 | /// EmitProtocolExtension - Generate the protocol extension |
| 1023 | /// structure used to store optional instance and class methods, and |
| 1024 | /// protocol properties. The return value has type |
| 1025 | /// ProtocolExtensionPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1026 | llvm::Constant * |
| 1027 | EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 1028 | const ConstantVector &OptInstanceMethods, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1029 | const ConstantVector &OptClassMethods, |
| 1030 | const ConstantVector &MethodTypesExt); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1031 | |
| 1032 | /// EmitProtocolList - Generate the list of referenced |
| 1033 | /// protocols. The return value has type ProtocolListPtrTy. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1034 | llvm::Constant *EmitProtocolList(Twine Name, |
Daniel Dunbar | dbc93370 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1035 | ObjCProtocolDecl::protocol_iterator begin, |
| 1036 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1037 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1038 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1039 | /// for the given selector. |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1040 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel, |
| 1041 | bool lval=false); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1042 | |
| 1043 | public: |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1044 | CGObjCMac(CodeGen::CodeGenModule &cgm); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1045 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1046 | virtual llvm::Function *ModuleInitFunction(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1047 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1048 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1049 | ReturnValueSlot Return, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1050 | QualType ResultType, |
| 1051 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1052 | llvm::Value *Receiver, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1053 | const CallArgList &CallArgs, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1054 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1055 | const ObjCMethodDecl *Method); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1056 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1057 | virtual CodeGen::RValue |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1058 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1059 | ReturnValueSlot Return, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1060 | QualType ResultType, |
| 1061 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1062 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1063 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1064 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1065 | bool IsClassMessage, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1066 | const CallArgList &CallArgs, |
| 1067 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1068 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1069 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1070 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1071 | |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1072 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel, |
| 1073 | bool lval = false); |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1074 | |
| 1075 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an |
| 1076 | /// untyped one. |
| 1077 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, |
| 1078 | const ObjCMethodDecl *Method); |
| 1079 | |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1080 | virtual llvm::Constant *GetEHType(QualType T); |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1081 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1082 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1083 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1084 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1085 | |
David Chisnall | 29254f4 | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 1086 | virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {}; |
| 1087 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1088 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1089 | const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1090 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1091 | virtual llvm::Constant *GetPropertyGetFunction(); |
| 1092 | virtual llvm::Constant *GetPropertySetFunction(); |
David Chisnall | 8fac25d | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 1093 | virtual llvm::Constant *GetGetStructFunction(); |
| 1094 | virtual llvm::Constant *GetSetStructFunction(); |
Fariborz Jahanian | e317302 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 1095 | virtual llvm::Constant *GetCppAtomicObjectFunction(); |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1096 | virtual llvm::Constant *EnumerationMutationFunction(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1097 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1098 | virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
| 1099 | const ObjCAtTryStmt &S); |
| 1100 | virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1101 | const ObjCAtSynchronizedStmt &S); |
| 1102 | void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1103 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 1104 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1105 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1106 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1107 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1108 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1109 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 1110 | llvm::Value *src, llvm::Value *dest, |
| 1111 | bool threadlocal = false); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 1112 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1113 | llvm::Value *src, llvm::Value *dest, |
| 1114 | llvm::Value *ivarOffset); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1115 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 1116 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 1117 | virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
| 1118 | llvm::Value *dest, llvm::Value *src, |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 1119 | llvm::Value *size); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1120 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1121 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1122 | QualType ObjectTy, |
| 1123 | llvm::Value *BaseValue, |
| 1124 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1125 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1126 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1127 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1128 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | 6f40e22 | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1129 | |
| 1130 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 1131 | /// class of the given name. |
| 1132 | virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1133 | llvm_unreachable("CGObjCMac::GetClassGlobal"); |
Fariborz Jahanian | 6f40e22 | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1134 | } |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1135 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1136 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1137 | class CGObjCNonFragileABIMac : public CGObjCCommonMac { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1138 | private: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1139 | ObjCNonFragileABITypesHelper ObjCTypes; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1140 | llvm::GlobalVariable* ObjCEmptyCacheVar; |
| 1141 | llvm::GlobalVariable* ObjCEmptyVtableVar; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1142 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1143 | /// SuperClassReferences - uniqued super class references. |
| 1144 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1145 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1146 | /// MetaClassReferences - uniqued meta class references. |
| 1147 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1148 | |
| 1149 | /// EHTypeReferences - uniqued class ehtype references. |
| 1150 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1151 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1152 | /// VTableDispatchMethods - List of methods for which we generate |
| 1153 | /// vtable-based message dispatch. |
| 1154 | llvm::DenseSet<Selector> VTableDispatchMethods; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1155 | |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 1156 | /// DefinedMetaClasses - List of defined meta-classes. |
| 1157 | std::vector<llvm::GlobalValue*> DefinedMetaClasses; |
| 1158 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1159 | /// isVTableDispatchedSelector - Returns true if SEL is a |
| 1160 | /// vtable-based selector. |
| 1161 | bool isVTableDispatchedSelector(Selector Sel); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1162 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1163 | /// FinishNonFragileABIModule - Write out global data structures at the end of |
| 1164 | /// processing a translation unit. |
| 1165 | void FinishNonFragileABIModule(); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1166 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 1167 | /// AddModuleClassList - Add the given list of class pointers to the |
| 1168 | /// module with the provided symbol and section names. |
| 1169 | void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container, |
| 1170 | const char *SymbolName, |
| 1171 | const char *SectionName); |
| 1172 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1173 | llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags, |
| 1174 | unsigned InstanceStart, |
| 1175 | unsigned InstanceSize, |
| 1176 | const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1177 | llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1178 | llvm::Constant *IsAGV, |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1179 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 1180 | llvm::Constant *ClassRoGV, |
| 1181 | bool HiddenVisibility); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1182 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1183 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1184 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 1185 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1186 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1187 | /// EmitMethodList - Emit the method list for the given |
| 1188 | /// implementation. The return value has type MethodListnfABITy. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1189 | llvm::Constant *EmitMethodList(Twine Name, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1190 | const char *Section, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 1191 | const ConstantVector &Methods); |
| 1192 | /// EmitIvarList - Emit the ivar list for the given |
| 1193 | /// implementation. If ForClass is true the list of class ivars |
| 1194 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 1195 | /// interface ivars will be emitted. The return value has type |
| 1196 | /// IvarListnfABIPtrTy. |
| 1197 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1198 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1199 | llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 1200 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 1201 | unsigned long int offset); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1202 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1203 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1204 | /// declaration, emitting it if necessary. The return value has type |
| 1205 | /// ProtocolPtrTy. |
| 1206 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1207 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1208 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1209 | /// object for the given declaration, emitting it if needed. These |
| 1210 | /// forward references will be filled in with empty bodies if no |
| 1211 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 1212 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1213 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1214 | /// EmitProtocolList - Generate the list of referenced |
| 1215 | /// protocols. The return value has type ProtocolListPtrTy. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1216 | llvm::Constant *EmitProtocolList(Twine Name, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1217 | ObjCProtocolDecl::protocol_iterator begin, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1218 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1219 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1220 | CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1221 | ReturnValueSlot Return, |
| 1222 | QualType ResultType, |
| 1223 | Selector Sel, |
| 1224 | llvm::Value *Receiver, |
| 1225 | QualType Arg0Ty, |
| 1226 | bool IsSuper, |
| 1227 | const CallArgList &CallArgs, |
| 1228 | const ObjCMethodDecl *Method); |
Fariborz Jahanian | 6f40e22 | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1229 | |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 1230 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 1231 | /// class of the given name. |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 1232 | llvm::GlobalVariable *GetClassGlobal(const std::string &Name); |
Fariborz Jahanian | 6f40e22 | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1233 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1234 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1235 | /// for the given class reference. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1236 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1237 | const ObjCInterfaceDecl *ID); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1238 | |
| 1239 | llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder, |
| 1240 | IdentifierInfo *II); |
| 1241 | |
| 1242 | llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1243 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1244 | /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 1245 | /// for the given super class reference. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1246 | llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder, |
| 1247 | const ObjCInterfaceDecl *ID); |
| 1248 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1249 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 1250 | /// meta-data |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1251 | llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1252 | const ObjCInterfaceDecl *ID); |
| 1253 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1254 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 1255 | /// the given ivar. |
| 1256 | /// |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 1257 | llvm::GlobalVariable * ObjCIvarOffsetVariable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1258 | const ObjCInterfaceDecl *ID, |
| 1259 | const ObjCIvarDecl *Ivar); |
| 1260 | |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 1261 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1262 | /// for the given selector. |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1263 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel, |
| 1264 | bool lval=false); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1265 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1266 | /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1267 | /// interface. The return value has type EHTypePtrTy. |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1268 | llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1269 | bool ForDefinition); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1270 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1271 | const char *getMetaclassSymbolPrefix() const { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1272 | return "OBJC_METACLASS_$_"; |
| 1273 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1274 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1275 | const char *getClassSymbolPrefix() const { |
| 1276 | return "OBJC_CLASS_$_"; |
| 1277 | } |
| 1278 | |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 1279 | void GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1280 | uint32_t &InstanceStart, |
| 1281 | uint32_t &InstanceSize); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1282 | |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1283 | // Shamelessly stolen from Analysis/CFRefCount.cpp |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1284 | Selector GetNullarySelector(const char* name) const { |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1285 | IdentifierInfo* II = &CGM.getContext().Idents.get(name); |
| 1286 | return CGM.getContext().Selectors.getSelector(0, &II); |
| 1287 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1288 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1289 | Selector GetUnarySelector(const char* name) const { |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1290 | IdentifierInfo* II = &CGM.getContext().Idents.get(name); |
| 1291 | return CGM.getContext().Selectors.getSelector(1, &II); |
| 1292 | } |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1293 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1294 | /// ImplementationIsNonLazy - Check whether the given category or |
| 1295 | /// class implementation is "non-lazy". |
Fariborz Jahanian | ecfbdcb | 2009-05-21 01:03:45 +0000 | [diff] [blame] | 1296 | bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const; |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1297 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1298 | public: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1299 | CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1300 | // FIXME. All stubs for now! |
| 1301 | virtual llvm::Function *ModuleInitFunction(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1302 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1303 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1304 | ReturnValueSlot Return, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1305 | QualType ResultType, |
| 1306 | Selector Sel, |
| 1307 | llvm::Value *Receiver, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1308 | const CallArgList &CallArgs, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1309 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1310 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1311 | |
| 1312 | virtual CodeGen::RValue |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1313 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1314 | ReturnValueSlot Return, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1315 | QualType ResultType, |
| 1316 | Selector Sel, |
| 1317 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1318 | bool isCategoryImpl, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1319 | llvm::Value *Receiver, |
| 1320 | bool IsClassMessage, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1321 | const CallArgList &CallArgs, |
| 1322 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1323 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1324 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1325 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1326 | |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1327 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel, |
| 1328 | bool lvalue = false) |
| 1329 | { return EmitSelector(Builder, Sel, lvalue); } |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1330 | |
| 1331 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an |
| 1332 | /// untyped one. |
| 1333 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, |
| 1334 | const ObjCMethodDecl *Method) |
| 1335 | { return EmitSelector(Builder, Method->getSelector()); } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1336 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 1337 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1338 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1339 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
David Chisnall | 29254f4 | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 1340 | |
| 1341 | virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {}; |
| 1342 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1343 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 1344 | const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1345 | |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1346 | virtual llvm::Constant *GetEHType(QualType T); |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1347 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1348 | virtual llvm::Constant *GetPropertyGetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1349 | return ObjCTypes.getGetPropertyFn(); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1350 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1351 | virtual llvm::Constant *GetPropertySetFunction() { |
| 1352 | return ObjCTypes.getSetPropertyFn(); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1353 | } |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 1354 | |
David Chisnall | 8fac25d | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 1355 | virtual llvm::Constant *GetSetStructFunction() { |
| 1356 | return ObjCTypes.getCopyStructFn(); |
| 1357 | } |
| 1358 | virtual llvm::Constant *GetGetStructFunction() { |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 1359 | return ObjCTypes.getCopyStructFn(); |
| 1360 | } |
Fariborz Jahanian | e317302 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 1361 | virtual llvm::Constant *GetCppAtomicObjectFunction() { |
| 1362 | return ObjCTypes.getCppAtomicObjectFunction(); |
| 1363 | } |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 1364 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1365 | virtual llvm::Constant *EnumerationMutationFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1366 | return ObjCTypes.getEnumerationMutationFn(); |
Daniel Dunbar | 28ed084 | 2009-02-16 18:48:45 +0000 | [diff] [blame] | 1367 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1368 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1369 | virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
| 1370 | const ObjCAtTryStmt &S); |
| 1371 | virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1372 | const ObjCAtSynchronizedStmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1373 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 1374 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1375 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1376 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1377 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1378 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1379 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 1380 | llvm::Value *src, llvm::Value *dest, |
| 1381 | bool threadlocal = false); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1382 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1383 | llvm::Value *src, llvm::Value *dest, |
| 1384 | llvm::Value *ivarOffset); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1385 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1386 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 1387 | virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
| 1388 | llvm::Value *dest, llvm::Value *src, |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 1389 | llvm::Value *size); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1390 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1391 | QualType ObjectTy, |
| 1392 | llvm::Value *BaseValue, |
| 1393 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1394 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1395 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1396 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1397 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1398 | }; |
Fariborz Jahanian | 4e1524b | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1399 | |
| 1400 | /// A helper class for performing the null-initialization of a return |
| 1401 | /// value. |
| 1402 | struct NullReturnState { |
| 1403 | llvm::BasicBlock *NullBB; |
| 1404 | llvm::BasicBlock *callBB; |
| 1405 | NullReturnState() : NullBB(0), callBB(0) {} |
| 1406 | |
| 1407 | void init(CodeGenFunction &CGF, llvm::Value *receiver) { |
| 1408 | // Make blocks for the null-init and call edges. |
| 1409 | NullBB = CGF.createBasicBlock("msgSend.nullinit"); |
| 1410 | callBB = CGF.createBasicBlock("msgSend.call"); |
| 1411 | |
| 1412 | // Check for a null receiver and, if there is one, jump to the |
| 1413 | // null-init test. |
| 1414 | llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver); |
| 1415 | CGF.Builder.CreateCondBr(isNull, NullBB, callBB); |
| 1416 | |
| 1417 | // Otherwise, start performing the call. |
| 1418 | CGF.EmitBlock(callBB); |
| 1419 | } |
| 1420 | |
Fariborz Jahanian | 3c267e6 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1421 | RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType, |
| 1422 | const CallArgList &CallArgs, |
| 1423 | const ObjCMethodDecl *Method) { |
Fariborz Jahanian | 4e1524b | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1424 | if (!NullBB) return result; |
Fariborz Jahanian | 3c267e6 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1425 | |
| 1426 | llvm::Value *NullInitPtr = 0; |
| 1427 | if (result.isScalar() && !resultType->isVoidType()) { |
| 1428 | NullInitPtr = CGF.CreateTempAlloca(result.getScalarVal()->getType()); |
| 1429 | CGF.Builder.CreateStore(result.getScalarVal(), NullInitPtr); |
| 1430 | } |
Fariborz Jahanian | 4e1524b | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1431 | |
| 1432 | // Finish the call path. |
| 1433 | llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont"); |
| 1434 | if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB); |
| 1435 | |
| 1436 | // Emit the null-init block and perform the null-initialization there. |
| 1437 | CGF.EmitBlock(NullBB); |
Fariborz Jahanian | 3c267e6 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1438 | |
| 1439 | // Release consumed arguments along the null-receiver path. |
| 1440 | if (Method) { |
| 1441 | CallArgList::const_iterator I = CallArgs.begin(); |
| 1442 | for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(), |
| 1443 | e = Method->param_end(); i != e; ++i, ++I) { |
| 1444 | const ParmVarDecl *ParamDecl = (*i); |
| 1445 | if (ParamDecl->hasAttr<NSConsumedAttr>()) { |
| 1446 | RValue RV = I->RV; |
| 1447 | assert(RV.isScalar() && |
| 1448 | "NullReturnState::complete - arg not on object"); |
| 1449 | CGF.EmitARCRelease(RV.getScalarVal(), true); |
| 1450 | } |
| 1451 | } |
| 1452 | } |
| 1453 | |
| 1454 | if (result.isScalar()) { |
| 1455 | if (NullInitPtr) |
| 1456 | CGF.EmitNullInitialization(NullInitPtr, resultType); |
| 1457 | // Jump to the continuation block. |
| 1458 | CGF.EmitBlock(contBB); |
| 1459 | return NullInitPtr ? RValue::get(CGF.Builder.CreateLoad(NullInitPtr)) |
| 1460 | : result; |
| 1461 | } |
| 1462 | |
Fariborz Jahanian | 4e1524b | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1463 | if (!resultType->isAnyComplexType()) { |
| 1464 | assert(result.isAggregate() && "null init of non-aggregate result?"); |
| 1465 | CGF.EmitNullInitialization(result.getAggregateAddr(), resultType); |
| 1466 | // Jump to the continuation block. |
| 1467 | CGF.EmitBlock(contBB); |
| 1468 | return result; |
| 1469 | } |
| 1470 | |
| 1471 | // _Complex type |
| 1472 | // FIXME. Now easy to handle any other scalar type whose result is returned |
| 1473 | // in memory due to ABI limitations. |
| 1474 | CGF.EmitBlock(contBB); |
| 1475 | CodeGenFunction::ComplexPairTy CallCV = result.getComplexVal(); |
| 1476 | llvm::Type *MemberType = CallCV.first->getType(); |
| 1477 | llvm::Constant *ZeroCV = llvm::Constant::getNullValue(MemberType); |
| 1478 | // Create phi instruction for scalar complex value. |
| 1479 | llvm::PHINode *PHIReal = CGF.Builder.CreatePHI(MemberType, 2); |
| 1480 | PHIReal->addIncoming(ZeroCV, NullBB); |
| 1481 | PHIReal->addIncoming(CallCV.first, callBB); |
| 1482 | llvm::PHINode *PHIImag = CGF.Builder.CreatePHI(MemberType, 2); |
| 1483 | PHIImag->addIncoming(ZeroCV, NullBB); |
| 1484 | PHIImag->addIncoming(CallCV.second, callBB); |
| 1485 | return RValue::getComplex(PHIReal, PHIImag); |
| 1486 | } |
| 1487 | }; |
| 1488 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1489 | } // end anonymous namespace |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1490 | |
| 1491 | /* *** Helper Functions *** */ |
| 1492 | |
| 1493 | /// getConstantGEP() - Help routine to construct simple GEPs. |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1494 | static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1495 | llvm::Constant *C, |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1496 | unsigned idx0, |
| 1497 | unsigned idx1) { |
| 1498 | llvm::Value *Idxs[] = { |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1499 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0), |
| 1500 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1501 | }; |
Jay Foad | a5c0434 | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 1502 | return llvm::ConstantExpr::getGetElementPtr(C, Idxs); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1505 | /// hasObjCExceptionAttribute - Return true if this class or any super |
| 1506 | /// class has the __objc_exception__ attribute. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1507 | static bool hasObjCExceptionAttribute(ASTContext &Context, |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 1508 | const ObjCInterfaceDecl *OID) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1509 | if (OID->hasAttr<ObjCExceptionAttr>()) |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1510 | return true; |
| 1511 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 1512 | return hasObjCExceptionAttribute(Context, Super); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1513 | return false; |
| 1514 | } |
| 1515 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1516 | /* *** CGObjCMac Public Interface *** */ |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1517 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1518 | CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1519 | ObjCTypes(cgm) { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1520 | ObjCABI = 1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1521 | EmitImageInfo(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 1524 | /// GetClass - Return a reference to the class for the given interface |
| 1525 | /// decl. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1526 | llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1527 | const ObjCInterfaceDecl *ID) { |
| 1528 | return EmitClassRef(Builder, ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | /// GetSelector - Return the pointer to the unique'd string for this selector. |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1532 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel, |
| 1533 | bool lval) { |
| 1534 | return EmitSelector(Builder, Sel, lval); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1535 | } |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1536 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1537 | *Method) { |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1538 | return EmitSelector(Builder, Method->getSelector()); |
| 1539 | } |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1540 | |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1541 | llvm::Constant *CGObjCMac::GetEHType(QualType T) { |
Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 1542 | if (T->isObjCIdType() || |
| 1543 | T->isObjCQualifiedIdType()) { |
| 1544 | return CGM.GetAddrOfRTTIDescriptor( |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 1545 | CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true); |
Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 1546 | } |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1547 | if (T->isObjCClassType() || |
| 1548 | T->isObjCQualifiedClassType()) { |
| 1549 | return CGM.GetAddrOfRTTIDescriptor( |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 1550 | CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true); |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1551 | } |
| 1552 | if (T->isObjCObjectPointerType()) |
| 1553 | return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true); |
| 1554 | |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1555 | llvm_unreachable("asking for catch type for ObjC type in fragile runtime"); |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1556 | } |
| 1557 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1558 | /// Generate a constant CFString object. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1559 | /* |
| 1560 | struct __builtin_CFString { |
| 1561 | const int *isa; // point to __CFConstantStringClassReference |
| 1562 | int flags; |
| 1563 | const char *str; |
| 1564 | long length; |
| 1565 | }; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1566 | */ |
| 1567 | |
Fariborz Jahanian | 33e982b | 2010-04-22 20:26:39 +0000 | [diff] [blame] | 1568 | /// or Generate a constant NSString object. |
| 1569 | /* |
| 1570 | struct __builtin_NSString { |
| 1571 | const int *isa; // point to __NSConstantStringClassReference |
| 1572 | const char *str; |
| 1573 | unsigned int length; |
| 1574 | }; |
| 1575 | */ |
| 1576 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1577 | llvm::Constant *CGObjCCommonMac::GenerateConstantString( |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 1578 | const StringLiteral *SL) { |
Fariborz Jahanian | 33e982b | 2010-04-22 20:26:39 +0000 | [diff] [blame] | 1579 | return (CGM.getLangOptions().NoConstantCFStrings == 0 ? |
| 1580 | CGM.GetAddrOfConstantCFString(SL) : |
Fariborz Jahanian | 4c73307 | 2010-10-19 17:19:29 +0000 | [diff] [blame] | 1581 | CGM.GetAddrOfConstantString(SL)); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | /// Generates a message send where the super is the receiver. This is |
| 1585 | /// a message send to self with special delivery semantics indicating |
| 1586 | /// which class's method should be called. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1587 | CodeGen::RValue |
| 1588 | CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1589 | ReturnValueSlot Return, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1590 | QualType ResultType, |
| 1591 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1592 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1593 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1594 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1595 | bool IsClassMessage, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1596 | const CodeGen::CallArgList &CallArgs, |
| 1597 | const ObjCMethodDecl *Method) { |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1598 | // Create and init a super structure; this is a (receiver, class) |
| 1599 | // pair we will pass to objc_msgSendSuper. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1600 | llvm::Value *ObjCSuper = |
John McCall | 604da29 | 2011-03-04 08:00:29 +0000 | [diff] [blame] | 1601 | CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1602 | llvm::Value *ReceiverAsObject = |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1603 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1604 | CGF.Builder.CreateStore(ReceiverAsObject, |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1605 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1606 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1607 | // If this is a class message the metaclass is passed as the target. |
| 1608 | llvm::Value *Target; |
| 1609 | if (IsClassMessage) { |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1610 | if (isCategoryImpl) { |
| 1611 | // Message sent to 'super' in a class method defined in a category |
| 1612 | // implementation requires an odd treatment. |
| 1613 | // If we are in a class method, we must retrieve the |
| 1614 | // _metaclass_ for the current class, pointed at by |
| 1615 | // the class's "isa" pointer. The following assumes that |
| 1616 | // isa" is the first ivar in a class (which it must be). |
| 1617 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1618 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 1619 | Target = CGF.Builder.CreateLoad(Target); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 1620 | } else { |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1621 | llvm::Value *MetaClassPtr = EmitMetaClassRef(Class); |
| 1622 | llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1); |
| 1623 | llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr); |
| 1624 | Target = Super; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1625 | } |
Fariborz Jahanian | 182f268 | 2009-11-14 02:18:31 +0000 | [diff] [blame] | 1626 | } |
| 1627 | else if (isCategoryImpl) |
| 1628 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1629 | else { |
Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 1630 | llvm::Value *ClassPtr = EmitSuperClassRef(Class); |
| 1631 | ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1); |
| 1632 | Target = CGF.Builder.CreateLoad(ClassPtr); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1633 | } |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1634 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext and |
| 1635 | // ObjCTypes types. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1636 | llvm::Type *ClassTy = |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1637 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1638 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1639 | CGF.Builder.CreateStore(Target, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1640 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1641 | return EmitMessageSend(CGF, Return, ResultType, |
| 1642 | EmitSelector(CGF.Builder, Sel), |
| 1643 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 1644 | true, CallArgs, Method, ObjCTypes); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1645 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1646 | |
| 1647 | /// Generate code for a message send expression. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1648 | CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1649 | ReturnValueSlot Return, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1650 | QualType ResultType, |
| 1651 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1652 | llvm::Value *Receiver, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1653 | const CallArgList &CallArgs, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1654 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1655 | const ObjCMethodDecl *Method) { |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1656 | return EmitMessageSend(CGF, Return, ResultType, |
| 1657 | EmitSelector(CGF.Builder, Sel), |
| 1658 | Receiver, CGF.getContext().getObjCIdType(), |
| 1659 | false, CallArgs, Method, ObjCTypes); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1662 | CodeGen::RValue |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1663 | CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1664 | ReturnValueSlot Return, |
| 1665 | QualType ResultType, |
| 1666 | llvm::Value *Sel, |
| 1667 | llvm::Value *Arg0, |
| 1668 | QualType Arg0Ty, |
| 1669 | bool IsSuper, |
| 1670 | const CallArgList &CallArgs, |
| 1671 | const ObjCMethodDecl *Method, |
| 1672 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1673 | CallArgList ActualArgs; |
Fariborz Jahanian | d019d96 | 2009-04-24 21:07:43 +0000 | [diff] [blame] | 1674 | if (!IsSuper) |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 1675 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy); |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1676 | ActualArgs.add(RValue::get(Arg0), Arg0Ty); |
| 1677 | ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1678 | ActualArgs.addFrom(CallArgs); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1679 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1680 | CodeGenTypes &Types = CGM.getTypes(); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1681 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1682 | FunctionType::ExtInfo()); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1683 | llvm::FunctionType *FTy = |
Daniel Dunbar | 6793966 | 2009-09-17 04:01:40 +0000 | [diff] [blame] | 1684 | Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1685 | |
Anders Carlsson | 7e70fb2 | 2010-06-21 20:59:55 +0000 | [diff] [blame] | 1686 | if (Method) |
| 1687 | assert(CGM.getContext().getCanonicalType(Method->getResultType()) == |
| 1688 | CGM.getContext().getCanonicalType(ResultType) && |
| 1689 | "Result type mismatch!"); |
| 1690 | |
John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 1691 | NullReturnState nullReturn; |
| 1692 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1693 | llvm::Constant *Fn = NULL; |
Daniel Dunbar | dacf9dd | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 1694 | if (CGM.ReturnTypeUsesSRet(FnInfo)) { |
Fariborz Jahanian | 4e1524b | 2012-01-29 20:27:13 +0000 | [diff] [blame] | 1695 | if (!IsSuper) nullReturn.init(CGF, Arg0); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1696 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1697 | : ObjCTypes.getSendStretFn(IsSuper); |
Daniel Dunbar | dacf9dd | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 1698 | } else if (CGM.ReturnTypeUsesFPRet(ResultType)) { |
| 1699 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper) |
| 1700 | : ObjCTypes.getSendFpretFn(IsSuper); |
Anders Carlsson | eea6480 | 2011-10-31 16:27:11 +0000 | [diff] [blame] | 1701 | } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) { |
| 1702 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper) |
| 1703 | : ObjCTypes.getSendFp2retFn(IsSuper); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1704 | } else { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1705 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1706 | : ObjCTypes.getSendFn(IsSuper); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1707 | } |
Fariborz Jahanian | 3c267e6 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1708 | |
| 1709 | bool requiresnullCheck = false; |
| 1710 | if (CGM.getLangOptions().ObjCAutoRefCount && Method) |
| 1711 | for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(), |
| 1712 | e = Method->param_end(); i != e; ++i) { |
| 1713 | const ParmVarDecl *ParamDecl = (*i); |
| 1714 | if (ParamDecl->hasAttr<NSConsumedAttr>()) { |
| 1715 | if (!nullReturn.NullBB) |
| 1716 | nullReturn.init(CGF, Arg0); |
| 1717 | requiresnullCheck = true; |
| 1718 | break; |
| 1719 | } |
| 1720 | } |
| 1721 | |
Daniel Dunbar | dacf9dd | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 1722 | Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy)); |
John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 1723 | RValue rvalue = CGF.EmitCall(FnInfo, Fn, Return, ActualArgs); |
Fariborz Jahanian | 3c267e6 | 2012-01-30 21:40:37 +0000 | [diff] [blame] | 1724 | return nullReturn.complete(CGF, rvalue, ResultType, CallArgs, |
| 1725 | requiresnullCheck ? Method : 0); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1728 | static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) { |
| 1729 | if (FQT.isObjCGCStrong()) |
| 1730 | return Qualifiers::Strong; |
| 1731 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1732 | if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak) |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1733 | return Qualifiers::Weak; |
| 1734 | |
| 1735 | if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType()) |
| 1736 | return Qualifiers::Strong; |
| 1737 | |
| 1738 | if (const PointerType *PT = FQT->getAs<PointerType>()) |
| 1739 | return GetGCAttrTypeForType(Ctx, PT->getPointeeType()); |
| 1740 | |
| 1741 | return Qualifiers::GCNone; |
| 1742 | } |
| 1743 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1744 | llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM, |
| 1745 | const CGBlockInfo &blockInfo) { |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame^] | 1746 | llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1747 | |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 1748 | if (CGM.getLangOptions().getGC() == LangOptions::NonGC && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1749 | !CGM.getLangOptions().ObjCAutoRefCount) |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1750 | return nullPtr; |
| 1751 | |
Fariborz Jahanian | a1f024c | 2010-08-06 16:28:55 +0000 | [diff] [blame] | 1752 | bool hasUnion = false; |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1753 | SkipIvars.clear(); |
| 1754 | IvarsInfo.clear(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1755 | unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0); |
| 1756 | unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth(); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1757 | |
Fariborz Jahanian | 8197982 | 2010-09-09 00:21:45 +0000 | [diff] [blame] | 1758 | // __isa is the first field in block descriptor and must assume by runtime's |
| 1759 | // convention that it is GC'able. |
| 1760 | IvarsInfo.push_back(GC_IVAR(0, 1)); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1761 | |
| 1762 | const BlockDecl *blockDecl = blockInfo.getBlockDecl(); |
| 1763 | |
| 1764 | // Calculate the basic layout of the block structure. |
| 1765 | const llvm::StructLayout *layout = |
| 1766 | CGM.getTargetData().getStructLayout(blockInfo.StructureType); |
| 1767 | |
| 1768 | // Ignore the optional 'this' capture: C++ objects are not assumed |
| 1769 | // to be GC'ed. |
| 1770 | |
| 1771 | // Walk the captured variables. |
| 1772 | for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(), |
| 1773 | ce = blockDecl->capture_end(); ci != ce; ++ci) { |
| 1774 | const VarDecl *variable = ci->getVariable(); |
| 1775 | QualType type = variable->getType(); |
| 1776 | |
| 1777 | const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); |
| 1778 | |
| 1779 | // Ignore constant captures. |
| 1780 | if (capture.isConstant()) continue; |
| 1781 | |
| 1782 | uint64_t fieldOffset = layout->getElementOffset(capture.getIndex()); |
| 1783 | |
| 1784 | // __block variables are passed by their descriptor address. |
| 1785 | if (ci->isByRef()) { |
| 1786 | IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1)); |
Fariborz Jahanian | c5904b4 | 2010-09-11 01:27:29 +0000 | [diff] [blame] | 1787 | continue; |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1788 | } |
| 1789 | |
| 1790 | assert(!type->isArrayType() && "array variable should not be caught"); |
| 1791 | if (const RecordType *record = type->getAs<RecordType>()) { |
| 1792 | BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion); |
Fariborz Jahanian | e1a4898 | 2010-08-05 21:00:25 +0000 | [diff] [blame] | 1793 | continue; |
| 1794 | } |
Fariborz Jahanian | a1f024c | 2010-08-06 16:28:55 +0000 | [diff] [blame] | 1795 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1796 | Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type); |
| 1797 | unsigned fieldSize = CGM.getContext().getTypeSize(type); |
| 1798 | |
| 1799 | if (GCAttr == Qualifiers::Strong) |
| 1800 | IvarsInfo.push_back(GC_IVAR(fieldOffset, |
| 1801 | fieldSize / WordSizeInBits)); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1802 | else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak) |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1803 | SkipIvars.push_back(GC_IVAR(fieldOffset, |
| 1804 | fieldSize / ByteSizeInBits)); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1805 | } |
| 1806 | |
| 1807 | if (IvarsInfo.empty()) |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1808 | return nullPtr; |
| 1809 | |
| 1810 | // Sort on byte position; captures might not be allocated in order, |
| 1811 | // and unions can do funny things. |
| 1812 | llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end()); |
| 1813 | llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end()); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1814 | |
| 1815 | std::string BitMap; |
Fariborz Jahanian | b8fd2eb | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 1816 | llvm::Constant *C = BuildIvarLayoutBitmap(BitMap); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1817 | if (CGM.getLangOptions().ObjCGCBitmapPrint) { |
| 1818 | printf("\n block variable layout for block: "); |
| 1819 | const unsigned char *s = (unsigned char*)BitMap.c_str(); |
| 1820 | for (unsigned i = 0; i < BitMap.size(); i++) |
| 1821 | if (!(s[i] & 0xf0)) |
| 1822 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 1823 | else |
| 1824 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 1825 | printf("\n"); |
| 1826 | } |
| 1827 | |
| 1828 | return C; |
Fariborz Jahanian | 89ecd41 | 2010-08-04 16:57:49 +0000 | [diff] [blame] | 1829 | } |
| 1830 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1831 | llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1832 | const ObjCProtocolDecl *PD) { |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1833 | // 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] | 1834 | // resolved. Investigate. Its also wasteful to look this up over and over. |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1835 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1836 | |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1837 | return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), |
Douglas Gregor | 4c86fdb | 2012-01-17 18:36:30 +0000 | [diff] [blame] | 1838 | ObjCTypes.getExternalProtocolPtrTy()); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1839 | } |
| 1840 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1841 | void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1842 | // FIXME: We shouldn't need this, the protocol decl should contain enough |
| 1843 | // information to tell us whether this was a declaration or a definition. |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1844 | DefinedProtocols.insert(PD->getIdentifier()); |
| 1845 | |
| 1846 | // If we have generated a forward reference to this protocol, emit |
| 1847 | // it now. Otherwise do nothing, the protocol objects are lazily |
| 1848 | // emitted. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1849 | if (Protocols.count(PD->getIdentifier())) |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1850 | GetOrEmitProtocol(PD); |
| 1851 | } |
| 1852 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1853 | llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1854 | if (DefinedProtocols.count(PD->getIdentifier())) |
| 1855 | return GetOrEmitProtocol(PD); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 1856 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1857 | return GetOrEmitProtocolRef(PD); |
| 1858 | } |
| 1859 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1860 | /* |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1861 | // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions |
| 1862 | struct _objc_protocol { |
| 1863 | struct _objc_protocol_extension *isa; |
| 1864 | char *protocol_name; |
| 1865 | struct _objc_protocol_list *protocol_list; |
| 1866 | struct _objc__method_prototype_list *instance_methods; |
| 1867 | struct _objc__method_prototype_list *class_methods |
| 1868 | }; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1869 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1870 | See EmitProtocolExtension(). |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1871 | */ |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1872 | llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) { |
| 1873 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1874 | |
| 1875 | // Early exit if a defining object has already been generated. |
| 1876 | if (Entry && Entry->hasInitializer()) |
| 1877 | return Entry; |
| 1878 | |
Douglas Gregor | 1d784b2 | 2012-01-01 19:51:50 +0000 | [diff] [blame] | 1879 | // Use the protocol definition, if there is one. |
| 1880 | if (const ObjCProtocolDecl *Def = PD->getDefinition()) |
| 1881 | PD = Def; |
| 1882 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1883 | // 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] | 1884 | // resolved. Investigate. Its also wasteful to look this up over and over. |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1885 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1886 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1887 | // Construct method lists. |
| 1888 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1889 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1890 | std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1891 | for (ObjCProtocolDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1892 | i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1893 | ObjCMethodDecl *MD = *i; |
| 1894 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 1895 | if (!C) |
| 1896 | return GetOrEmitProtocolRef(PD); |
| 1897 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1898 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1899 | OptInstanceMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1900 | OptMethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1901 | } else { |
| 1902 | InstanceMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1903 | MethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1904 | } |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1905 | } |
| 1906 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1907 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1908 | i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1909 | ObjCMethodDecl *MD = *i; |
| 1910 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 1911 | if (!C) |
| 1912 | return GetOrEmitProtocolRef(PD); |
| 1913 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1914 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1915 | OptClassMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1916 | OptMethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1917 | } else { |
| 1918 | ClassMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1919 | MethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1920 | } |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1921 | } |
| 1922 | |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1923 | MethodTypesExt.insert(MethodTypesExt.end(), |
| 1924 | OptMethodTypesExt.begin(), OptMethodTypesExt.end()); |
| 1925 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1926 | llvm::Constant *Values[] = { |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1927 | EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods, |
| 1928 | MethodTypesExt), |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1929 | GetClassName(PD->getIdentifier()), |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1930 | EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(), |
Daniel Dunbar | dbc93370 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1931 | PD->protocol_begin(), |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1932 | PD->protocol_end()), |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1933 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1934 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1935 | InstanceMethods), |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1936 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1937 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1938 | ClassMethods) |
| 1939 | }; |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 1940 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1941 | Values); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1942 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1943 | if (Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1944 | // Already created, fix the linkage and update the initializer. |
| 1945 | Entry->setLinkage(llvm::GlobalValue::InternalLinkage); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1946 | Entry->setInitializer(Init); |
| 1947 | } else { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1948 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1949 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1950 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1951 | Init, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1952 | "\01L_OBJC_PROTOCOL_" + PD->getName()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1953 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1954 | // FIXME: Is this necessary? Why only for protocol? |
| 1955 | Entry->setAlignment(4); |
| 1956 | } |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 1957 | CGM.AddUsedGlobal(Entry); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1958 | |
| 1959 | return Entry; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1962 | llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1963 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1964 | |
| 1965 | if (!Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1966 | // We use the initializer as a marker of whether this is a forward |
| 1967 | // reference or not. At module finalization we add the empty |
| 1968 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1969 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1970 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1971 | llvm::GlobalValue::ExternalLinkage, |
| 1972 | 0, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1973 | "\01L_OBJC_PROTOCOL_" + PD->getName()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1974 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1975 | // FIXME: Is this necessary? Why only for protocol? |
| 1976 | Entry->setAlignment(4); |
| 1977 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1978 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1979 | return Entry; |
| 1980 | } |
| 1981 | |
| 1982 | /* |
| 1983 | struct _objc_protocol_extension { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1984 | uint32_t size; |
| 1985 | struct objc_method_description_list *optional_instance_methods; |
| 1986 | struct objc_method_description_list *optional_class_methods; |
| 1987 | struct objc_property_list *instance_properties; |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1988 | const char ** extendedMethodTypes; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1989 | }; |
| 1990 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1991 | llvm::Constant * |
| 1992 | CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 1993 | const ConstantVector &OptInstanceMethods, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1994 | const ConstantVector &OptClassMethods, |
| 1995 | const ConstantVector &MethodTypesExt) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1996 | uint64_t Size = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 1997 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy); |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1998 | llvm::Constant *Values[] = { |
| 1999 | llvm::ConstantInt::get(ObjCTypes.IntTy, Size), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2000 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2001 | + PD->getName(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2002 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2003 | OptInstanceMethods), |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2004 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2005 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2006 | OptClassMethods), |
| 2007 | EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2008 | ObjCTypes), |
| 2009 | EmitProtocolMethodTypes("\01L_OBJC_PROTOCOL_METHOD_TYPES_" + PD->getName(), |
| 2010 | MethodTypesExt, ObjCTypes) |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2011 | }; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2012 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2013 | // Return null if no extension bits are used. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2014 | if (Values[1]->isNullValue() && Values[2]->isNullValue() && |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2015 | Values[3]->isNullValue() && Values[4]->isNullValue()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2016 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2017 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2018 | llvm::Constant *Init = |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2019 | llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2020 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2021 | // No special section, but goes in llvm.used |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2022 | return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2023 | Init, |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2024 | 0, 0, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2025 | } |
| 2026 | |
| 2027 | /* |
| 2028 | struct objc_protocol_list { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2029 | struct objc_protocol_list *next; |
| 2030 | long count; |
| 2031 | Protocol *list[]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2032 | }; |
| 2033 | */ |
Daniel Dunbar | dbc93370 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 2034 | llvm::Constant * |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2035 | CGObjCMac::EmitProtocolList(Twine Name, |
Daniel Dunbar | dbc93370 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 2036 | ObjCProtocolDecl::protocol_iterator begin, |
| 2037 | ObjCProtocolDecl::protocol_iterator end) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2038 | std::vector<llvm::Constant*> ProtocolRefs; |
| 2039 | |
Daniel Dunbar | dbc93370 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 2040 | for (; begin != end; ++begin) |
| 2041 | ProtocolRefs.push_back(GetProtocolRef(*begin)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2042 | |
| 2043 | // Just return null for empty protocol lists |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2044 | if (ProtocolRefs.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2045 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2046 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2047 | // This list is null terminated. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2048 | ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2049 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2050 | llvm::Constant *Values[3]; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2051 | // This field is only used by the runtime. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2052 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2053 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2054 | ProtocolRefs.size() - 1); |
| 2055 | Values[2] = |
| 2056 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy, |
| 2057 | ProtocolRefs.size()), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2058 | ProtocolRefs); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2059 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2060 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2061 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2062 | CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2063 | 4, false); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2064 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2067 | void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet, |
| 2068 | std::vector<llvm::Constant*> &Properties, |
| 2069 | const Decl *Container, |
| 2070 | const ObjCProtocolDecl *PROTO, |
| 2071 | const ObjCCommonTypesHelper &ObjCTypes) { |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2072 | for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(), |
| 2073 | E = PROTO->protocol_end(); P != E; ++P) |
| 2074 | PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes); |
| 2075 | for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(), |
| 2076 | E = PROTO->prop_end(); I != E; ++I) { |
| 2077 | const ObjCPropertyDecl *PD = *I; |
| 2078 | if (!PropertySet.insert(PD->getIdentifier())) |
| 2079 | continue; |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2080 | llvm::Constant *Prop[] = { |
| 2081 | GetPropertyName(PD->getIdentifier()), |
| 2082 | GetPropertyTypeString(PD, Container) |
| 2083 | }; |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2084 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop)); |
| 2085 | } |
| 2086 | } |
| 2087 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2088 | /* |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2089 | struct _objc_property { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2090 | const char * const name; |
| 2091 | const char * const attributes; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2092 | }; |
| 2093 | |
| 2094 | struct _objc_property_list { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2095 | uint32_t entsize; // sizeof (struct _objc_property) |
| 2096 | uint32_t prop_count; |
| 2097 | struct _objc_property[prop_count]; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2098 | }; |
| 2099 | */ |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2100 | llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2101 | const Decl *Container, |
| 2102 | const ObjCContainerDecl *OCD, |
| 2103 | const ObjCCommonTypesHelper &ObjCTypes) { |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2104 | std::vector<llvm::Constant*> Properties; |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2105 | llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2106 | for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(), |
| 2107 | E = OCD->prop_end(); I != E; ++I) { |
Steve Naroff | 93983f8 | 2009-01-11 12:47:58 +0000 | [diff] [blame] | 2108 | const ObjCPropertyDecl *PD = *I; |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2109 | PropertySet.insert(PD->getIdentifier()); |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2110 | llvm::Constant *Prop[] = { |
| 2111 | GetPropertyName(PD->getIdentifier()), |
| 2112 | GetPropertyTypeString(PD, Container) |
| 2113 | }; |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2114 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2115 | Prop)); |
| 2116 | } |
Fariborz Jahanian | 6afbdf5 | 2010-06-22 16:33:55 +0000 | [diff] [blame] | 2117 | if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 2118 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 2119 | P = OID->all_referenced_protocol_begin(), |
| 2120 | E = OID->all_referenced_protocol_end(); P != E; ++P) |
Fariborz Jahanian | 6afbdf5 | 2010-06-22 16:33:55 +0000 | [diff] [blame] | 2121 | PushProtocolProperties(PropertySet, Properties, Container, (*P), |
| 2122 | ObjCTypes); |
| 2123 | } |
| 2124 | else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) { |
| 2125 | for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(), |
| 2126 | E = CD->protocol_end(); P != E; ++P) |
| 2127 | PushProtocolProperties(PropertySet, Properties, Container, (*P), |
| 2128 | ObjCTypes); |
| 2129 | } |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2130 | |
| 2131 | // Return null for empty list. |
| 2132 | if (Properties.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2133 | return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2134 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2135 | unsigned PropertySize = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2136 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2137 | llvm::Constant *Values[3]; |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2138 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize); |
| 2139 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2140 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy, |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2141 | Properties.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2142 | Values[2] = llvm::ConstantArray::get(AT, Properties); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2143 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2144 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2145 | llvm::GlobalVariable *GV = |
| 2146 | CreateMetadataVar(Name, Init, |
| 2147 | (ObjCABI == 2) ? "__DATA, __objc_const" : |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2148 | "__OBJC,__property,regular,no_dead_strip", |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2149 | (ObjCABI == 2) ? 8 : 4, |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2150 | true); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2151 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2152 | } |
| 2153 | |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2154 | llvm::Constant *CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name, |
| 2155 | const ConstantVector &MethodTypes, |
| 2156 | const ObjCCommonTypesHelper &ObjCTypes) { |
| 2157 | // Return null for empty list. |
| 2158 | if (MethodTypes.empty()) |
| 2159 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy); |
| 2160 | |
| 2161 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 2162 | MethodTypes.size()); |
| 2163 | llvm::Constant *Init = llvm::ConstantArray::get(AT, MethodTypes); |
| 2164 | |
| 2165 | llvm::GlobalVariable *GV = |
| 2166 | CreateMetadataVar(Name, Init, |
| 2167 | (ObjCABI == 2) ? "__DATA, __objc_const" : 0, |
| 2168 | (ObjCABI == 2) ? 8 : 4, |
| 2169 | true); |
| 2170 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.Int8PtrPtrTy); |
| 2171 | } |
| 2172 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2173 | /* |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2174 | struct objc_method_description_list { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2175 | int count; |
| 2176 | struct objc_method_description list[]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2177 | }; |
| 2178 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2179 | llvm::Constant * |
| 2180 | CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2181 | llvm::Constant *Desc[] = { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2182 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2183 | ObjCTypes.SelectorPtrTy), |
| 2184 | GetMethodVarType(MD) |
| 2185 | }; |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 2186 | if (!Desc[1]) |
| 2187 | return 0; |
| 2188 | |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2189 | return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2190 | Desc); |
| 2191 | } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2192 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2193 | llvm::Constant *CGObjCMac::EmitMethodDescList(Twine Name, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2194 | const char *Section, |
| 2195 | const ConstantVector &Methods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2196 | // Return null for empty list. |
| 2197 | if (Methods.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2198 | return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2199 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2200 | llvm::Constant *Values[2]; |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2201 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2202 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2203 | Methods.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2204 | Values[1] = llvm::ConstantArray::get(AT, Methods); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2205 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2206 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2207 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2208 | return llvm::ConstantExpr::getBitCast(GV, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2209 | ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2210 | } |
| 2211 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2212 | /* |
| 2213 | struct _objc_category { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2214 | char *category_name; |
| 2215 | char *class_name; |
| 2216 | struct _objc_method_list *instance_methods; |
| 2217 | struct _objc_method_list *class_methods; |
| 2218 | struct _objc_protocol_list *protocols; |
| 2219 | uint32_t size; // <rdar://4585769> |
| 2220 | struct _objc_property_list *instance_properties; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2221 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2222 | */ |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2223 | void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2224 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2225 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2226 | // FIXME: This is poor design, the OCD should have a pointer to the category |
| 2227 | // decl. Additionally, note that Category can be null for the @implementation |
| 2228 | // w/o an @interface case. Sema should just create one for us as it does for |
| 2229 | // @implementation so everyone else can live life under a clear blue sky. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2230 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2231 | const ObjCCategoryDecl *Category = |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2232 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2233 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2234 | SmallString<256> ExtName; |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2235 | llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_' |
| 2236 | << OCD->getName(); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2237 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2238 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2239 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2240 | i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2241 | // Instance methods should always be defined. |
| 2242 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 2243 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2244 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2245 | i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2246 | // Class methods should always be defined. |
| 2247 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 2248 | } |
| 2249 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2250 | llvm::Constant *Values[7]; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2251 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 2252 | Values[1] = GetClassName(Interface->getIdentifier()); |
Fariborz Jahanian | 679cd7f | 2009-04-29 20:40:05 +0000 | [diff] [blame] | 2253 | LazySymbols.insert(Interface->getIdentifier()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2254 | Values[2] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2255 | EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(), |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2256 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2257 | InstanceMethods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2258 | Values[3] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2259 | EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2260 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2261 | ClassMethods); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2262 | if (Category) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2263 | Values[4] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2264 | EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2265 | Category->protocol_begin(), |
| 2266 | Category->protocol_end()); |
| 2267 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2268 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2269 | } |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2270 | Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2271 | |
| 2272 | // If there is no category @interface then there can be no properties. |
| 2273 | if (Category) { |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2274 | Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 2275 | OCD, Category, ObjCTypes); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2276 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2277 | Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2278 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2279 | |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2280 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2281 | Values); |
| 2282 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2283 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2284 | CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init, |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2285 | "__OBJC,__category,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2286 | 4, true); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2287 | DefinedCategories.push_back(GV); |
Fariborz Jahanian | b9c5b3d | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 2288 | DefinedCategoryNames.insert(ExtName.str()); |
Fariborz Jahanian | 64089ce | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 2289 | // method definition entries must be clear for next implementation. |
| 2290 | MethodDefinitions.clear(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2291 | } |
| 2292 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2293 | // FIXME: Get from somewhere? |
| 2294 | enum ClassFlags { |
| 2295 | eClassFlags_Factory = 0x00001, |
| 2296 | eClassFlags_Meta = 0x00002, |
| 2297 | // <rdr://5142207> |
| 2298 | eClassFlags_HasCXXStructors = 0x02000, |
| 2299 | eClassFlags_Hidden = 0x20000, |
| 2300 | eClassFlags_ABI2_Hidden = 0x00010, |
| 2301 | eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634> |
| 2302 | }; |
| 2303 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2304 | /* |
| 2305 | struct _objc_class { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2306 | Class isa; |
| 2307 | Class super_class; |
| 2308 | const char *name; |
| 2309 | long version; |
| 2310 | long info; |
| 2311 | long instance_size; |
| 2312 | struct _objc_ivar_list *ivars; |
| 2313 | struct _objc_method_list *methods; |
| 2314 | struct _objc_cache *cache; |
| 2315 | struct _objc_protocol_list *protocols; |
| 2316 | // Objective-C 1.0 extensions (<rdr://4585769>) |
| 2317 | const char *ivar_layout; |
| 2318 | struct _objc_class_ext *ext; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2319 | }; |
| 2320 | |
| 2321 | See EmitClassExtension(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2322 | */ |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2323 | void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2324 | DefinedSymbols.insert(ID->getIdentifier()); |
| 2325 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2326 | std::string ClassName = ID->getNameAsString(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2327 | // FIXME: Gross |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2328 | ObjCInterfaceDecl *Interface = |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2329 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2330 | llvm::Constant *Protocols = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2331 | EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(), |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 2332 | Interface->all_referenced_protocol_begin(), |
| 2333 | Interface->all_referenced_protocol_end()); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2334 | unsigned Flags = eClassFlags_Factory; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2335 | if (ID->hasCXXStructors()) |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 2336 | Flags |= eClassFlags_HasCXXStructors; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2337 | unsigned Size = |
Ken Dyck | 5f022d8 | 2011-02-09 01:59:34 +0000 | [diff] [blame] | 2338 | CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2339 | |
| 2340 | // FIXME: Set CXX-structors flag. |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 2341 | if (ID->getClassInterface()->getVisibility() == HiddenVisibility) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2342 | Flags |= eClassFlags_Hidden; |
| 2343 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2344 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2345 | for (ObjCImplementationDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2346 | i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2347 | // Instance methods should always be defined. |
| 2348 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 2349 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2350 | for (ObjCImplementationDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2351 | i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2352 | // Class methods should always be defined. |
| 2353 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 2354 | } |
| 2355 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2356 | for (ObjCImplementationDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2357 | i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2358 | ObjCPropertyImplDecl *PID = *i; |
| 2359 | |
| 2360 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 2361 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 2362 | |
| 2363 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 2364 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 2365 | InstanceMethods.push_back(C); |
| 2366 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 2367 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 2368 | InstanceMethods.push_back(C); |
| 2369 | } |
| 2370 | } |
| 2371 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2372 | llvm::Constant *Values[12]; |
Daniel Dunbar | 5384b09 | 2009-05-03 08:56:52 +0000 | [diff] [blame] | 2373 | Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2374 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2375 | // Record a reference to the super class. |
| 2376 | LazySymbols.insert(Super->getIdentifier()); |
| 2377 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2378 | Values[ 1] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2379 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2380 | ObjCTypes.ClassPtrTy); |
| 2381 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2382 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2383 | } |
| 2384 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 2385 | // Version is always 0. |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2386 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2387 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 2388 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2389 | Values[ 6] = EmitIvarList(ID, false); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2390 | Values[ 7] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2391 | EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(), |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2392 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2393 | InstanceMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2394 | // cache is always NULL. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2395 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2396 | Values[ 9] = Protocols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2397 | Values[10] = BuildIvarLayout(ID, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2398 | Values[11] = EmitClassExtension(ID); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2399 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2400 | Values); |
Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 2401 | std::string Name("\01L_OBJC_CLASS_"); |
| 2402 | Name += ClassName; |
| 2403 | const char *Section = "__OBJC,__class,regular,no_dead_strip"; |
| 2404 | // Check for a forward reference. |
| 2405 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 2406 | if (GV) { |
| 2407 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2408 | "Forward metaclass reference has incorrect type."); |
| 2409 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 2410 | GV->setInitializer(Init); |
| 2411 | GV->setSection(Section); |
| 2412 | GV->setAlignment(4); |
| 2413 | CGM.AddUsedGlobal(GV); |
| 2414 | } |
| 2415 | else |
| 2416 | GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2417 | DefinedClasses.push_back(GV); |
Fariborz Jahanian | 64089ce | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 2418 | // method definition entries must be clear for next implementation. |
| 2419 | MethodDefinitions.clear(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2420 | } |
| 2421 | |
| 2422 | llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID, |
| 2423 | llvm::Constant *Protocols, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2424 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2425 | unsigned Flags = eClassFlags_Meta; |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2426 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2427 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 2428 | if (ID->getClassInterface()->getVisibility() == HiddenVisibility) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2429 | Flags |= eClassFlags_Hidden; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2430 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2431 | llvm::Constant *Values[12]; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2432 | // The isa for the metaclass is the root of the hierarchy. |
| 2433 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 2434 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 2435 | Root = Super; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2436 | Values[ 0] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2437 | llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2438 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2439 | // The super class for the metaclass is emitted as the name of the |
| 2440 | // super class. The runtime fixes this up to point to the |
| 2441 | // *metaclass* for the super class. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2442 | if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2443 | Values[ 1] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2444 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2445 | ObjCTypes.ClassPtrTy); |
| 2446 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2447 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2448 | } |
| 2449 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 2450 | // Version is always 0. |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2451 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2452 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 2453 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2454 | Values[ 6] = EmitIvarList(ID, true); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2455 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2456 | EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2457 | "__OBJC,__cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2458 | Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2459 | // cache is always NULL. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2460 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2461 | Values[ 9] = Protocols; |
| 2462 | // ivar_layout for metaclass is always NULL. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2463 | Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2464 | // The class extension is always unused for metaclasses. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2465 | Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2466 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2467 | Values); |
| 2468 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2469 | std::string Name("\01L_OBJC_METACLASS_"); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2470 | Name += ID->getNameAsCString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2471 | |
| 2472 | // Check for a forward reference. |
| 2473 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 2474 | if (GV) { |
| 2475 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2476 | "Forward metaclass reference has incorrect type."); |
| 2477 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 2478 | GV->setInitializer(Init); |
| 2479 | } else { |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2480 | GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2481 | llvm::GlobalValue::InternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2482 | Init, Name); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2483 | } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2484 | GV->setSection("__OBJC,__meta_class,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2485 | GV->setAlignment(4); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2486 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2487 | |
| 2488 | return GV; |
| 2489 | } |
| 2490 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2491 | llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2492 | std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2493 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2494 | // FIXME: Should we look these up somewhere other than the module. Its a bit |
| 2495 | // silly since we only generate these while processing an implementation, so |
| 2496 | // exactly one pointer would work if know when we entered/exitted an |
| 2497 | // implementation block. |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2498 | |
| 2499 | // Check for an existing forward reference. |
Fariborz Jahanian | b0d2794 | 2009-01-07 20:11:22 +0000 | [diff] [blame] | 2500 | // Previously, metaclass with internal linkage may have been defined. |
| 2501 | // pass 'true' as 2nd argument so it is returned. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2502 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, |
| 2503 | true)) { |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2504 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2505 | "Forward metaclass reference has incorrect type."); |
| 2506 | return GV; |
| 2507 | } else { |
| 2508 | // Generate as an external reference to keep a consistent |
| 2509 | // module. This will be patched up when we emit the metaclass. |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2510 | return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2511 | llvm::GlobalValue::ExternalLinkage, |
| 2512 | 0, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2513 | Name); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2514 | } |
| 2515 | } |
| 2516 | |
Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 2517 | llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) { |
| 2518 | std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString(); |
| 2519 | |
| 2520 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, |
| 2521 | true)) { |
| 2522 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2523 | "Forward class metadata reference has incorrect type."); |
| 2524 | return GV; |
| 2525 | } else { |
| 2526 | return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
| 2527 | llvm::GlobalValue::ExternalLinkage, |
| 2528 | 0, |
| 2529 | Name); |
| 2530 | } |
| 2531 | } |
| 2532 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2533 | /* |
| 2534 | struct objc_class_ext { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2535 | uint32_t size; |
| 2536 | const char *weak_ivar_layout; |
| 2537 | struct _objc_property_list *properties; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2538 | }; |
| 2539 | */ |
| 2540 | llvm::Constant * |
| 2541 | CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2542 | uint64_t Size = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2543 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2544 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2545 | llvm::Constant *Values[3]; |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2546 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 2547 | Values[1] = BuildIvarLayout(ID, false); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2548 | Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 2549 | ID, ID->getClassInterface(), ObjCTypes); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2550 | |
| 2551 | // Return null if no extension bits are used. |
| 2552 | if (Values[1]->isNullValue() && Values[2]->isNullValue()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2553 | return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2554 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2555 | llvm::Constant *Init = |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2556 | llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2557 | return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2558 | Init, "__OBJC,__class_ext,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2559 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2560 | } |
| 2561 | |
| 2562 | /* |
| 2563 | struct objc_ivar { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2564 | char *ivar_name; |
| 2565 | char *ivar_type; |
| 2566 | int ivar_offset; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2567 | }; |
| 2568 | |
| 2569 | struct objc_ivar_list { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2570 | int ivar_count; |
| 2571 | struct objc_ivar list[count]; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2572 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2573 | */ |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2574 | llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2575 | bool ForClass) { |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2576 | std::vector<llvm::Constant*> Ivars; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2577 | |
| 2578 | // When emitting the root class GCC emits ivar entries for the |
| 2579 | // actual class structure. It is not clear if we need to follow this |
| 2580 | // behavior; for now lets try and get away with not doing it. If so, |
| 2581 | // the cleanest solution would be to make up an ObjCInterfaceDecl |
| 2582 | // for the class. |
| 2583 | if (ForClass) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2584 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2585 | |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 2586 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2587 | |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 2588 | for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin(); |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 2589 | IVD; IVD = IVD->getNextIvar()) { |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 2590 | // Ignore unnamed bit-fields. |
| 2591 | if (!IVD->getDeclName()) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2592 | continue; |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2593 | llvm::Constant *Ivar[] = { |
| 2594 | GetMethodVarName(IVD->getIdentifier()), |
| 2595 | GetMethodVarType(IVD), |
| 2596 | llvm::ConstantInt::get(ObjCTypes.IntTy, |
| 2597 | ComputeIvarBaseOffset(CGM, OID, IVD)) |
| 2598 | }; |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2599 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2600 | } |
| 2601 | |
| 2602 | // Return null for empty list. |
| 2603 | if (Ivars.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2604 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2605 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2606 | llvm::Constant *Values[2]; |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2607 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2608 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2609 | Ivars.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2610 | Values[1] = llvm::ConstantArray::get(AT, Ivars); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2611 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2612 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2613 | llvm::GlobalVariable *GV; |
| 2614 | if (ForClass) |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2615 | GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2616 | Init, "__OBJC,__class_vars,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2617 | 4, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2618 | else |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2619 | GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2620 | Init, "__OBJC,__instance_vars,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2621 | 4, true); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2622 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2623 | } |
| 2624 | |
| 2625 | /* |
| 2626 | struct objc_method { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2627 | SEL method_name; |
| 2628 | char *method_types; |
| 2629 | void *method; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2630 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2631 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2632 | struct objc_method_list { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2633 | struct objc_method_list *obsolete; |
| 2634 | int count; |
| 2635 | struct objc_method methods_list[count]; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2636 | }; |
| 2637 | */ |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2638 | |
| 2639 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 2640 | /// given method if it has been defined. The result is null if the |
| 2641 | /// method has not been defined. The return value has type MethodPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2642 | llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) { |
Argyrios Kyrtzidis | 9d50c06 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 2643 | llvm::Function *Fn = GetMethodDefinition(MD); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2644 | if (!Fn) |
| 2645 | return 0; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2646 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2647 | llvm::Constant *Method[] = { |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2648 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2649 | ObjCTypes.SelectorPtrTy), |
| 2650 | GetMethodVarType(MD), |
| 2651 | llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy) |
| 2652 | }; |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2653 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2654 | } |
| 2655 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2656 | llvm::Constant *CGObjCMac::EmitMethodList(Twine Name, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2657 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2658 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2659 | // Return null for empty list. |
| 2660 | if (Methods.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2661 | return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2662 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2663 | llvm::Constant *Values[3]; |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2664 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2665 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2666 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2667 | Methods.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2668 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2669 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2670 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2671 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2672 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2673 | } |
| 2674 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 2675 | llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2676 | const ObjCContainerDecl *CD) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2677 | SmallString<256> Name; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 2678 | GetNameForMethod(OMD, CD, Name); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2679 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2680 | CodeGenTypes &Types = CGM.getTypes(); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2681 | llvm::FunctionType *MethodTy = |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2682 | Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2683 | llvm::Function *Method = |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 2684 | llvm::Function::Create(MethodTy, |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2685 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 2686 | Name.str(), |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2687 | &CGM.getModule()); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2688 | MethodDefinitions.insert(std::make_pair(OMD, Method)); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2689 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2690 | return Method; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2691 | } |
| 2692 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2693 | llvm::GlobalVariable * |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2694 | CGObjCCommonMac::CreateMetadataVar(Twine Name, |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2695 | llvm::Constant *Init, |
| 2696 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2697 | unsigned Align, |
| 2698 | bool AddToUsed) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2699 | llvm::Type *Ty = Init->getType(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2700 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2701 | new llvm::GlobalVariable(CGM.getModule(), Ty, false, |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2702 | llvm::GlobalValue::InternalLinkage, Init, Name); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2703 | if (Section) |
| 2704 | GV->setSection(Section); |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2705 | if (Align) |
| 2706 | GV->setAlignment(Align); |
| 2707 | if (AddToUsed) |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2708 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2709 | return GV; |
| 2710 | } |
| 2711 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2712 | llvm::Function *CGObjCMac::ModuleInitFunction() { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2713 | // Abuse this interface function as a place to finalize. |
| 2714 | FinishModule(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2715 | return NULL; |
| 2716 | } |
| 2717 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2718 | llvm::Constant *CGObjCMac::GetPropertyGetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2719 | return ObjCTypes.getGetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2720 | } |
| 2721 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2722 | llvm::Constant *CGObjCMac::GetPropertySetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2723 | return ObjCTypes.getSetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2724 | } |
| 2725 | |
David Chisnall | 8fac25d | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 2726 | llvm::Constant *CGObjCMac::GetGetStructFunction() { |
| 2727 | return ObjCTypes.getCopyStructFn(); |
| 2728 | } |
| 2729 | llvm::Constant *CGObjCMac::GetSetStructFunction() { |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 2730 | return ObjCTypes.getCopyStructFn(); |
| 2731 | } |
| 2732 | |
Fariborz Jahanian | e317302 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 2733 | llvm::Constant *CGObjCMac::GetCppAtomicObjectFunction() { |
| 2734 | return ObjCTypes.getCppAtomicObjectFunction(); |
| 2735 | } |
| 2736 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2737 | llvm::Constant *CGObjCMac::EnumerationMutationFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2738 | return ObjCTypes.getEnumerationMutationFn(); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 2739 | } |
| 2740 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2741 | void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) { |
| 2742 | return EmitTryOrSynchronizedStmt(CGF, S); |
| 2743 | } |
| 2744 | |
| 2745 | void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF, |
| 2746 | const ObjCAtSynchronizedStmt &S) { |
| 2747 | return EmitTryOrSynchronizedStmt(CGF, S); |
| 2748 | } |
| 2749 | |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2750 | namespace { |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 2751 | struct PerformFragileFinally : EHScopeStack::Cleanup { |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2752 | const Stmt &S; |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2753 | llvm::Value *SyncArgSlot; |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2754 | llvm::Value *CallTryExitVar; |
| 2755 | llvm::Value *ExceptionData; |
| 2756 | ObjCTypesHelper &ObjCTypes; |
| 2757 | PerformFragileFinally(const Stmt *S, |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2758 | llvm::Value *SyncArgSlot, |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2759 | llvm::Value *CallTryExitVar, |
| 2760 | llvm::Value *ExceptionData, |
| 2761 | ObjCTypesHelper *ObjCTypes) |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2762 | : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar), |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2763 | ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {} |
| 2764 | |
John McCall | ad346f4 | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 2765 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2766 | // Check whether we need to call objc_exception_try_exit. |
| 2767 | // In optimized code, this branch will always be folded. |
| 2768 | llvm::BasicBlock *FinallyCallExit = |
| 2769 | CGF.createBasicBlock("finally.call_exit"); |
| 2770 | llvm::BasicBlock *FinallyNoCallExit = |
| 2771 | CGF.createBasicBlock("finally.no_call_exit"); |
| 2772 | CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar), |
| 2773 | FinallyCallExit, FinallyNoCallExit); |
| 2774 | |
| 2775 | CGF.EmitBlock(FinallyCallExit); |
| 2776 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData) |
| 2777 | ->setDoesNotThrow(); |
| 2778 | |
| 2779 | CGF.EmitBlock(FinallyNoCallExit); |
| 2780 | |
| 2781 | if (isa<ObjCAtTryStmt>(S)) { |
| 2782 | if (const ObjCAtFinallyStmt* FinallyStmt = |
John McCall | d96a8e7 | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 2783 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) { |
| 2784 | // Save the current cleanup destination in case there's |
| 2785 | // control flow inside the finally statement. |
| 2786 | llvm::Value *CurCleanupDest = |
| 2787 | CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot()); |
| 2788 | |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2789 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
| 2790 | |
John McCall | d96a8e7 | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 2791 | if (CGF.HaveInsertPoint()) { |
| 2792 | CGF.Builder.CreateStore(CurCleanupDest, |
| 2793 | CGF.getNormalCleanupDestSlot()); |
| 2794 | } else { |
| 2795 | // Currently, the end of the cleanup must always exist. |
| 2796 | CGF.EnsureInsertPoint(); |
| 2797 | } |
| 2798 | } |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2799 | } else { |
| 2800 | // Emit objc_sync_exit(expr); as finally's sole statement for |
| 2801 | // @synchronized. |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2802 | llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot); |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2803 | CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg) |
| 2804 | ->setDoesNotThrow(); |
| 2805 | } |
| 2806 | } |
| 2807 | }; |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2808 | |
| 2809 | class FragileHazards { |
| 2810 | CodeGenFunction &CGF; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2811 | SmallVector<llvm::Value*, 20> Locals; |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2812 | llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry; |
| 2813 | |
| 2814 | llvm::InlineAsm *ReadHazard; |
| 2815 | llvm::InlineAsm *WriteHazard; |
| 2816 | |
| 2817 | llvm::FunctionType *GetAsmFnType(); |
| 2818 | |
| 2819 | void collectLocals(); |
| 2820 | void emitReadHazard(CGBuilderTy &Builder); |
| 2821 | |
| 2822 | public: |
| 2823 | FragileHazards(CodeGenFunction &CGF); |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2824 | |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2825 | void emitWriteHazard(); |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2826 | void emitHazardsInNewBlocks(); |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2827 | }; |
| 2828 | } |
| 2829 | |
| 2830 | /// Create the fragile-ABI read and write hazards based on the current |
| 2831 | /// state of the function, which is presumed to be immediately prior |
| 2832 | /// to a @try block. These hazards are used to maintain correct |
| 2833 | /// semantics in the face of optimization and the fragile ABI's |
| 2834 | /// cavalier use of setjmp/longjmp. |
| 2835 | FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) { |
| 2836 | collectLocals(); |
| 2837 | |
| 2838 | if (Locals.empty()) return; |
| 2839 | |
| 2840 | // Collect all the blocks in the function. |
| 2841 | for (llvm::Function::iterator |
| 2842 | I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I) |
| 2843 | BlocksBeforeTry.insert(&*I); |
| 2844 | |
| 2845 | llvm::FunctionType *AsmFnTy = GetAsmFnType(); |
| 2846 | |
| 2847 | // Create a read hazard for the allocas. This inhibits dead-store |
| 2848 | // optimizations and forces the values to memory. This hazard is |
| 2849 | // inserted before any 'throwing' calls in the protected scope to |
| 2850 | // reflect the possibility that the variables might be read from the |
| 2851 | // catch block if the call throws. |
| 2852 | { |
| 2853 | std::string Constraint; |
| 2854 | for (unsigned I = 0, E = Locals.size(); I != E; ++I) { |
| 2855 | if (I) Constraint += ','; |
| 2856 | Constraint += "*m"; |
| 2857 | } |
| 2858 | |
| 2859 | ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false); |
| 2860 | } |
| 2861 | |
| 2862 | // Create a write hazard for the allocas. This inhibits folding |
| 2863 | // loads across the hazard. This hazard is inserted at the |
| 2864 | // beginning of the catch path to reflect the possibility that the |
| 2865 | // variables might have been written within the protected scope. |
| 2866 | { |
| 2867 | std::string Constraint; |
| 2868 | for (unsigned I = 0, E = Locals.size(); I != E; ++I) { |
| 2869 | if (I) Constraint += ','; |
| 2870 | Constraint += "=*m"; |
| 2871 | } |
| 2872 | |
| 2873 | WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false); |
| 2874 | } |
| 2875 | } |
| 2876 | |
| 2877 | /// Emit a write hazard at the current location. |
| 2878 | void FragileHazards::emitWriteHazard() { |
| 2879 | if (Locals.empty()) return; |
| 2880 | |
Jay Foad | 4c7d9f1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 2881 | CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow(); |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2882 | } |
| 2883 | |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2884 | void FragileHazards::emitReadHazard(CGBuilderTy &Builder) { |
| 2885 | assert(!Locals.empty()); |
Jay Foad | 4c7d9f1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 2886 | Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow(); |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2887 | } |
| 2888 | |
| 2889 | /// Emit read hazards in all the protected blocks, i.e. all the blocks |
| 2890 | /// which have been inserted since the beginning of the try. |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2891 | void FragileHazards::emitHazardsInNewBlocks() { |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2892 | if (Locals.empty()) return; |
| 2893 | |
| 2894 | CGBuilderTy Builder(CGF.getLLVMContext()); |
| 2895 | |
| 2896 | // Iterate through all blocks, skipping those prior to the try. |
| 2897 | for (llvm::Function::iterator |
| 2898 | FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) { |
| 2899 | llvm::BasicBlock &BB = *FI; |
| 2900 | if (BlocksBeforeTry.count(&BB)) continue; |
| 2901 | |
| 2902 | // Walk through all the calls in the block. |
| 2903 | for (llvm::BasicBlock::iterator |
| 2904 | BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) { |
| 2905 | llvm::Instruction &I = *BI; |
| 2906 | |
| 2907 | // Ignore instructions that aren't non-intrinsic calls. |
| 2908 | // These are the only calls that can possibly call longjmp. |
| 2909 | if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue; |
| 2910 | if (isa<llvm::IntrinsicInst>(I)) |
| 2911 | continue; |
| 2912 | |
| 2913 | // Ignore call sites marked nounwind. This may be questionable, |
| 2914 | // since 'nounwind' doesn't necessarily mean 'does not call longjmp'. |
| 2915 | llvm::CallSite CS(&I); |
| 2916 | if (CS.doesNotThrow()) continue; |
| 2917 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2918 | // Insert a read hazard before the call. This will ensure that |
| 2919 | // any writes to the locals are performed before making the |
| 2920 | // call. If the call throws, then this is sufficient to |
| 2921 | // guarantee correctness as long as it doesn't also write to any |
| 2922 | // locals. |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2923 | Builder.SetInsertPoint(&BB, BI); |
| 2924 | emitReadHazard(Builder); |
| 2925 | } |
| 2926 | } |
| 2927 | } |
| 2928 | |
| 2929 | static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) { |
| 2930 | if (V) S.insert(V); |
| 2931 | } |
| 2932 | |
| 2933 | void FragileHazards::collectLocals() { |
| 2934 | // Compute a set of allocas to ignore. |
| 2935 | llvm::DenseSet<llvm::Value*> AllocasToIgnore; |
| 2936 | addIfPresent(AllocasToIgnore, CGF.ReturnValue); |
| 2937 | addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest); |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2938 | |
| 2939 | // Collect all the allocas currently in the function. This is |
| 2940 | // probably way too aggressive. |
| 2941 | llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock(); |
| 2942 | for (llvm::BasicBlock::iterator |
| 2943 | I = Entry.begin(), E = Entry.end(); I != E; ++I) |
| 2944 | if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I)) |
| 2945 | Locals.push_back(&*I); |
| 2946 | } |
| 2947 | |
| 2948 | llvm::FunctionType *FragileHazards::GetAsmFnType() { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2949 | SmallVector<llvm::Type *, 16> tys(Locals.size()); |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 2950 | for (unsigned i = 0, e = Locals.size(); i != e; ++i) |
| 2951 | tys[i] = Locals[i]->getType(); |
| 2952 | return llvm::FunctionType::get(CGF.VoidTy, tys, false); |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2953 | } |
| 2954 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2955 | /* |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2956 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2957 | Objective-C setjmp-longjmp (sjlj) Exception Handling |
| 2958 | -- |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2959 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2960 | A catch buffer is a setjmp buffer plus: |
| 2961 | - a pointer to the exception that was caught |
| 2962 | - a pointer to the previous exception data buffer |
| 2963 | - two pointers of reserved storage |
| 2964 | Therefore catch buffers form a stack, with a pointer to the top |
| 2965 | of the stack kept in thread-local storage. |
| 2966 | |
| 2967 | objc_exception_try_enter pushes a catch buffer onto the EH stack. |
| 2968 | objc_exception_try_exit pops the given catch buffer, which is |
| 2969 | required to be the top of the EH stack. |
| 2970 | objc_exception_throw pops the top of the EH stack, writes the |
| 2971 | thrown exception into the appropriate field, and longjmps |
| 2972 | to the setjmp buffer. It crashes the process (with a printf |
| 2973 | and an abort()) if there are no catch buffers on the stack. |
| 2974 | objc_exception_extract just reads the exception pointer out of the |
| 2975 | catch buffer. |
| 2976 | |
| 2977 | There's no reason an implementation couldn't use a light-weight |
| 2978 | setjmp here --- something like __builtin_setjmp, but API-compatible |
| 2979 | with the heavyweight setjmp. This will be more important if we ever |
| 2980 | want to implement correct ObjC/C++ exception interactions for the |
| 2981 | fragile ABI. |
| 2982 | |
| 2983 | Note that for this use of setjmp/longjmp to be correct, we may need |
| 2984 | to mark some local variables volatile: if a non-volatile local |
| 2985 | variable is modified between the setjmp and the longjmp, it has |
| 2986 | indeterminate value. For the purposes of LLVM IR, it may be |
| 2987 | sufficient to make loads and stores within the @try (to variables |
| 2988 | declared outside the @try) volatile. This is necessary for |
| 2989 | optimized correctness, but is not currently being done; this is |
| 2990 | being tracked as rdar://problem/8160285 |
| 2991 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2992 | The basic framework for a @try-catch-finally is as follows: |
| 2993 | { |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2994 | objc_exception_data d; |
| 2995 | id _rethrow = null; |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2996 | bool _call_try_exit = true; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2997 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2998 | objc_exception_try_enter(&d); |
| 2999 | if (!setjmp(d.jmp_buf)) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3000 | ... try body ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3001 | } else { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3002 | // exception path |
| 3003 | id _caught = objc_exception_extract(&d); |
| 3004 | |
| 3005 | // enter new try scope for handlers |
| 3006 | if (!setjmp(d.jmp_buf)) { |
| 3007 | ... match exception and execute catch blocks ... |
| 3008 | |
| 3009 | // fell off end, rethrow. |
| 3010 | _rethrow = _caught; |
| 3011 | ... jump-through-finally to finally_rethrow ... |
| 3012 | } else { |
| 3013 | // exception in catch block |
| 3014 | _rethrow = objc_exception_extract(&d); |
| 3015 | _call_try_exit = false; |
| 3016 | ... jump-through-finally to finally_rethrow ... |
| 3017 | } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3018 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3019 | ... jump-through-finally to finally_end ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3020 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3021 | finally: |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 3022 | if (_call_try_exit) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3023 | objc_exception_try_exit(&d); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 3024 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3025 | ... finally block .... |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3026 | ... dispatch to finally destination ... |
| 3027 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3028 | finally_rethrow: |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3029 | objc_exception_throw(_rethrow); |
| 3030 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3031 | finally_end: |
| 3032 | } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3033 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3034 | This framework differs slightly from the one gcc uses, in that gcc |
| 3035 | uses _rethrow to determine if objc_exception_try_exit should be called |
| 3036 | and if the object should be rethrown. This breaks in the face of |
| 3037 | throwing nil and introduces unnecessary branches. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3038 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3039 | We specialize this framework for a few particular circumstances: |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3040 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3041 | - If there are no catch blocks, then we avoid emitting the second |
| 3042 | exception handling context. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3043 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3044 | - If there is a catch-all catch block (i.e. @catch(...) or @catch(id |
| 3045 | e)) we avoid emitting the code to rethrow an uncaught exception. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3046 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3047 | - FIXME: If there is no @finally block we can do a few more |
| 3048 | simplifications. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3049 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3050 | Rethrows and Jumps-Through-Finally |
| 3051 | -- |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3052 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3053 | '@throw;' is supported by pushing the currently-caught exception |
| 3054 | onto ObjCEHStack while the @catch blocks are emitted. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3055 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3056 | Branches through the @finally block are handled with an ordinary |
| 3057 | normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC |
| 3058 | exceptions are not compatible with C++ exceptions, and this is |
| 3059 | hardly the only place where this will go wrong. |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3060 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3061 | @synchronized(expr) { stmt; } is emitted as if it were: |
| 3062 | id synch_value = expr; |
| 3063 | objc_sync_enter(synch_value); |
| 3064 | @try { stmt; } @finally { objc_sync_exit(synch_value); } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3065 | */ |
| 3066 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 3067 | void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 3068 | const Stmt &S) { |
| 3069 | bool isTry = isa<ObjCAtTryStmt>(S); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3070 | |
| 3071 | // A destination for the fall-through edges of the catch handlers to |
| 3072 | // jump to. |
| 3073 | CodeGenFunction::JumpDest FinallyEnd = |
| 3074 | CGF.getJumpDestInCurrentScope("finally.end"); |
| 3075 | |
| 3076 | // A destination for the rethrow edge of the catch handlers to jump |
| 3077 | // to. |
| 3078 | CodeGenFunction::JumpDest FinallyRethrow = |
| 3079 | CGF.getJumpDestInCurrentScope("finally.rethrow"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3080 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3081 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 3082 | // evaluation of the expression must occur before we enter the |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3083 | // @synchronized. We can't avoid a temp here because we need the |
| 3084 | // value to be preserved. If the backend ever does liveness |
| 3085 | // correctly after setjmp, this will be unnecessary. |
| 3086 | llvm::Value *SyncArgSlot = 0; |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3087 | if (!isTry) { |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3088 | llvm::Value *SyncArg = |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3089 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 3090 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3091 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg) |
| 3092 | ->setDoesNotThrow(); |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3093 | |
| 3094 | SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg"); |
| 3095 | CGF.Builder.CreateStore(SyncArg, SyncArgSlot); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3096 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3097 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3098 | // Allocate memory for the setjmp buffer. This needs to be kept |
| 3099 | // live throughout the try and catch blocks. |
| 3100 | llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy, |
| 3101 | "exceptiondata.ptr"); |
| 3102 | |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3103 | // Create the fragile hazards. Note that this will not capture any |
| 3104 | // of the allocas required for exception processing, but will |
| 3105 | // capture the current basic block (which extends all the way to the |
| 3106 | // setjmp call) as "before the @try". |
| 3107 | FragileHazards Hazards(CGF); |
| 3108 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3109 | // Create a flag indicating whether the cleanup needs to call |
| 3110 | // objc_exception_try_exit. This is true except when |
| 3111 | // - no catches match and we're branching through the cleanup |
| 3112 | // just to rethrow the exception, or |
| 3113 | // - a catch matched and we're falling out of the catch handler. |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3114 | // The setjmp-safety rule here is that we should always store to this |
| 3115 | // variable in a place that dominates the branch through the cleanup |
| 3116 | // without passing through any setjmps. |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3117 | llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(), |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 3118 | "_call_try_exit"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3119 | |
John McCall | 9e2213d | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3120 | // A slot containing the exception to rethrow. Only needed when we |
| 3121 | // have both a @catch and a @finally. |
| 3122 | llvm::Value *PropagatingExnVar = 0; |
| 3123 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3124 | // Push a normal cleanup to leave the try scope. |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 3125 | CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S, |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3126 | SyncArgSlot, |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 3127 | CallTryExitVar, |
| 3128 | ExceptionData, |
| 3129 | &ObjCTypes); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3130 | |
| 3131 | // Enter a try block: |
| 3132 | // - Call objc_exception_try_enter to push ExceptionData on top of |
| 3133 | // the EH stack. |
| 3134 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData) |
| 3135 | ->setDoesNotThrow(); |
| 3136 | |
| 3137 | // - Call setjmp on the exception data buffer. |
| 3138 | llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0); |
| 3139 | llvm::Value *GEPIndexes[] = { Zero, Zero, Zero }; |
| 3140 | llvm::Value *SetJmpBuffer = |
Jay Foad | 0f6ac7c | 2011-07-22 08:16:57 +0000 | [diff] [blame] | 3141 | CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer"); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3142 | llvm::CallInst *SetJmpResult = |
| 3143 | CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result"); |
| 3144 | SetJmpResult->setDoesNotThrow(); |
Bill Wendling | 6446c3e | 2011-12-19 23:53:28 +0000 | [diff] [blame] | 3145 | SetJmpResult->setCanReturnTwice(); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3146 | |
| 3147 | // If setjmp returned 0, enter the protected block; otherwise, |
| 3148 | // branch to the handler. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 3149 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 3150 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3151 | llvm::Value *DidCatch = |
John McCall | d96a8e7 | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 3152 | CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception"); |
| 3153 | CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3154 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3155 | // Emit the protected block. |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3156 | CGF.EmitBlock(TryBlock); |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3157 | CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3158 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3159 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3160 | |
| 3161 | CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3162 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3163 | // Emit the exception handler block. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 3164 | CGF.EmitBlock(TryHandler); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3165 | |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3166 | // Don't optimize loads of the in-scope locals across this point. |
| 3167 | Hazards.emitWriteHazard(); |
| 3168 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3169 | // For a @synchronized (or a @try with no catches), just branch |
| 3170 | // through the cleanup to the rethrow block. |
| 3171 | if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) { |
| 3172 | // Tell the cleanup not to re-pop the exit. |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3173 | CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3174 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3175 | |
| 3176 | // Otherwise, we have to match against the caught exceptions. |
| 3177 | } else { |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3178 | // Retrieve the exception object. We may emit multiple blocks but |
| 3179 | // nothing can cross this so the value is already in SSA form. |
| 3180 | llvm::CallInst *Caught = |
| 3181 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 3182 | ExceptionData, "caught"); |
| 3183 | Caught->setDoesNotThrow(); |
| 3184 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3185 | // Push the exception to rethrow onto the EH value stack for the |
| 3186 | // benefit of any @throws in the handlers. |
| 3187 | CGF.ObjCEHValueStack.push_back(Caught); |
| 3188 | |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 3189 | const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3190 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3191 | bool HasFinally = (AtTryStmt->getFinallyStmt() != 0); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3192 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3193 | llvm::BasicBlock *CatchBlock = 0; |
| 3194 | llvm::BasicBlock *CatchHandler = 0; |
| 3195 | if (HasFinally) { |
John McCall | 9e2213d | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3196 | // Save the currently-propagating exception before |
| 3197 | // objc_exception_try_enter clears the exception slot. |
| 3198 | PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(), |
| 3199 | "propagating_exception"); |
| 3200 | CGF.Builder.CreateStore(Caught, PropagatingExnVar); |
| 3201 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3202 | // Enter a new exception try block (in case a @catch block |
| 3203 | // throws an exception). |
| 3204 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData) |
| 3205 | ->setDoesNotThrow(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3206 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3207 | llvm::CallInst *SetJmpResult = |
| 3208 | CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, |
| 3209 | "setjmp.result"); |
| 3210 | SetJmpResult->setDoesNotThrow(); |
Bill Wendling | 6446c3e | 2011-12-19 23:53:28 +0000 | [diff] [blame] | 3211 | SetJmpResult->setCanReturnTwice(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3212 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3213 | llvm::Value *Threw = |
| 3214 | CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception"); |
| 3215 | |
| 3216 | CatchBlock = CGF.createBasicBlock("catch"); |
| 3217 | CatchHandler = CGF.createBasicBlock("catch_for_catch"); |
| 3218 | CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock); |
| 3219 | |
| 3220 | CGF.EmitBlock(CatchBlock); |
| 3221 | } |
| 3222 | |
| 3223 | CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3224 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3225 | // Handle catch list. As a special case we check if everything is |
| 3226 | // matched and avoid generating code for falling off the end if |
| 3227 | // so. |
| 3228 | bool AllMatched = false; |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 3229 | for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) { |
| 3230 | const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3231 | |
Douglas Gregor | c00d8e1 | 2010-04-26 16:46:50 +0000 | [diff] [blame] | 3232 | const VarDecl *CatchParam = CatchStmt->getCatchParamDecl(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3233 | const ObjCObjectPointerType *OPT = 0; |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 3234 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3235 | // catch(...) always matches. |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3236 | if (!CatchParam) { |
| 3237 | AllMatched = true; |
| 3238 | } else { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3239 | OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3240 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3241 | // catch(id e) always matches under this ABI, since only |
| 3242 | // ObjC exceptions end up here in the first place. |
Daniel Dunbar | 97f61d1 | 2008-09-27 22:21:14 +0000 | [diff] [blame] | 3243 | // FIXME: For the time being we also match id<X>; this should |
| 3244 | // be rejected by Sema instead. |
Eli Friedman | 818e96f | 2009-07-11 00:57:02 +0000 | [diff] [blame] | 3245 | if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType())) |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3246 | AllMatched = true; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3247 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3248 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3249 | // If this is a catch-all, we don't need to test anything. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3250 | if (AllMatched) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3251 | CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF); |
| 3252 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3253 | if (CatchParam) { |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 3254 | CGF.EmitAutoVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 3255 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3256 | |
| 3257 | // These types work out because ConvertType(id) == i8*. |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 3258 | CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3259 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3260 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3261 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3262 | |
| 3263 | // The scope of the catch variable ends right here. |
| 3264 | CatchVarCleanups.ForceCleanup(); |
| 3265 | |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3266 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3267 | break; |
| 3268 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3269 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3270 | assert(OPT && "Unexpected non-object pointer type in @catch"); |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 3271 | const ObjCObjectType *ObjTy = OPT->getObjectType(); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3272 | |
| 3273 | // FIXME: @catch (Class c) ? |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 3274 | ObjCInterfaceDecl *IDecl = ObjTy->getInterface(); |
| 3275 | assert(IDecl && "Catch parameter must have Objective-C type!"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3276 | |
| 3277 | // Check if the @catch block matches the exception object. |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 3278 | llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3279 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3280 | llvm::CallInst *Match = |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 3281 | CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(), |
| 3282 | Class, Caught, "match"); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3283 | Match->setDoesNotThrow(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3284 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3285 | llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match"); |
| 3286 | llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3287 | |
| 3288 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 3289 | MatchedBlock, NextCatchBlock); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3290 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3291 | // Emit the @catch block. |
| 3292 | CGF.EmitBlock(MatchedBlock); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3293 | |
| 3294 | // Collect any cleanups for the catch variable. The scope lasts until |
| 3295 | // the end of the catch body. |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3296 | CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3297 | |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 3298 | CGF.EmitAutoVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 3299 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3300 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3301 | // Initialize the catch variable. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3302 | llvm::Value *Tmp = |
| 3303 | CGF.Builder.CreateBitCast(Caught, |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 3304 | CGF.ConvertType(CatchParam->getType())); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 3305 | CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3306 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3307 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3308 | |
| 3309 | // We're done with the catch variable. |
| 3310 | CatchVarCleanups.ForceCleanup(); |
| 3311 | |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3312 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3313 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3314 | CGF.EmitBlock(NextCatchBlock); |
| 3315 | } |
| 3316 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3317 | CGF.ObjCEHValueStack.pop_back(); |
| 3318 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3319 | // If nothing wanted anything to do with the caught exception, |
| 3320 | // kill the extract call. |
| 3321 | if (Caught->use_empty()) |
| 3322 | Caught->eraseFromParent(); |
| 3323 | |
| 3324 | if (!AllMatched) |
| 3325 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 3326 | |
| 3327 | if (HasFinally) { |
| 3328 | // Emit the exception handler for the @catch blocks. |
| 3329 | CGF.EmitBlock(CatchHandler); |
| 3330 | |
| 3331 | // In theory we might now need a write hazard, but actually it's |
| 3332 | // unnecessary because there's no local-accessing code between |
| 3333 | // the try's write hazard and here. |
| 3334 | //Hazards.emitWriteHazard(); |
| 3335 | |
John McCall | 9e2213d | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3336 | // Extract the new exception and save it to the |
| 3337 | // propagating-exception slot. |
| 3338 | assert(PropagatingExnVar); |
| 3339 | llvm::CallInst *NewCaught = |
| 3340 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 3341 | ExceptionData, "caught"); |
| 3342 | NewCaught->setDoesNotThrow(); |
| 3343 | CGF.Builder.CreateStore(NewCaught, PropagatingExnVar); |
| 3344 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3345 | // Don't pop the catch handler; the throw already did. |
| 3346 | CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3347 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3348 | } |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3349 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3350 | |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3351 | // Insert read hazards as required in the new blocks. |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3352 | Hazards.emitHazardsInNewBlocks(); |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3353 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3354 | // Pop the cleanup. |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3355 | CGF.Builder.restoreIP(TryFallthroughIP); |
| 3356 | if (CGF.HaveInsertPoint()) |
| 3357 | CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3358 | CGF.PopCleanupBlock(); |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3359 | CGF.EmitBlock(FinallyEnd.getBlock(), true); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3360 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3361 | // Emit the rethrow block. |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3362 | CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP(); |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 3363 | CGF.EmitBlock(FinallyRethrow.getBlock(), true); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3364 | if (CGF.HaveInsertPoint()) { |
John McCall | 9e2213d | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3365 | // If we have a propagating-exception variable, check it. |
| 3366 | llvm::Value *PropagatingExn; |
| 3367 | if (PropagatingExnVar) { |
| 3368 | PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar); |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3369 | |
John McCall | 9e2213d | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3370 | // Otherwise, just look in the buffer for the exception to throw. |
| 3371 | } else { |
| 3372 | llvm::CallInst *Caught = |
| 3373 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 3374 | ExceptionData); |
| 3375 | Caught->setDoesNotThrow(); |
| 3376 | PropagatingExn = Caught; |
| 3377 | } |
| 3378 | |
| 3379 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn) |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3380 | ->setDoesNotThrow(); |
| 3381 | CGF.Builder.CreateUnreachable(); |
Fariborz Jahanian | f2878e5 | 2008-11-21 19:21:53 +0000 | [diff] [blame] | 3382 | } |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3383 | |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3384 | CGF.Builder.restoreIP(SavedIP); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 3385 | } |
| 3386 | |
| 3387 | void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3388 | const ObjCAtThrowStmt &S) { |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 3389 | llvm::Value *ExceptionAsObject; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3390 | |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 3391 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
John McCall | 2b014d6 | 2011-10-01 10:32:24 +0000 | [diff] [blame] | 3392 | llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3393 | ExceptionAsObject = |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 3394 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy); |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 3395 | } else { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3396 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3397 | "Unexpected rethrow outside @catch block."); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 3398 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 3399 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3400 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3401 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject) |
| 3402 | ->setDoesNotReturn(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3403 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 3404 | |
| 3405 | // Clear the insertion point to indicate we are in unreachable code. |
| 3406 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 3407 | } |
| 3408 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3409 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 3410 | /// object: objc_read_weak (id *src) |
| 3411 | /// |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3412 | llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3413 | llvm::Value *AddrWeakObj) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3414 | llvm::Type* DestTy = |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3415 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
| 3416 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, |
| 3417 | ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 3418 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3419 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 3420 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 3421 | return read_weak; |
| 3422 | } |
| 3423 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3424 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 3425 | /// objc_assign_weak (id src, id *dst) |
| 3426 | /// |
| 3427 | void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3428 | llvm::Value *src, llvm::Value *dst) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3429 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3430 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3431 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3432 | assert(Size <= 8 && "does not support size > 8"); |
| 3433 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3434 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 3435 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 3436 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 3437 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 3438 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 3439 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3440 | src, dst, "weakassign"); |
| 3441 | return; |
| 3442 | } |
| 3443 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 3444 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 3445 | /// objc_assign_global (id src, id *dst) |
| 3446 | /// |
| 3447 | void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 3448 | llvm::Value *src, llvm::Value *dst, |
| 3449 | bool threadlocal) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3450 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3451 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3452 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3453 | assert(Size <= 8 && "does not support size > 8"); |
| 3454 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3455 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 3456 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 3457 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 3458 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 3459 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 3460 | if (!threadlocal) |
| 3461 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
| 3462 | src, dst, "globalassign"); |
| 3463 | else |
| 3464 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(), |
| 3465 | src, dst, "threadlocalassign"); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 3466 | return; |
| 3467 | } |
| 3468 | |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 3469 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 3470 | /// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset) |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 3471 | /// |
| 3472 | void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 3473 | llvm::Value *src, llvm::Value *dst, |
| 3474 | llvm::Value *ivarOffset) { |
| 3475 | assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL"); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3476 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3477 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3478 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3479 | assert(Size <= 8 && "does not support size > 8"); |
| 3480 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3481 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 3482 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 3483 | } |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 3484 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 3485 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 3486 | CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(), |
| 3487 | src, dst, ivarOffset); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 3488 | return; |
| 3489 | } |
| 3490 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 3491 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 3492 | /// objc_assign_strongCast (id src, id *dst) |
| 3493 | /// |
| 3494 | void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3495 | llvm::Value *src, llvm::Value *dst) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3496 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3497 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3498 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3499 | assert(Size <= 8 && "does not support size > 8"); |
| 3500 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3501 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 3502 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 3503 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 3504 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 3505 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 3506 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 3507 | src, dst, "weakassign"); |
| 3508 | return; |
| 3509 | } |
| 3510 | |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 3511 | void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3512 | llvm::Value *DestPtr, |
| 3513 | llvm::Value *SrcPtr, |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 3514 | llvm::Value *size) { |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 3515 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy); |
| 3516 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 3517 | CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(), |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 3518 | DestPtr, SrcPtr, size); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 3519 | return; |
| 3520 | } |
| 3521 | |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 3522 | /// EmitObjCValueForIvar - Code Gen for ivar reference. |
| 3523 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 3524 | LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 3525 | QualType ObjectTy, |
| 3526 | llvm::Value *BaseValue, |
| 3527 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 3528 | unsigned CVRQualifiers) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3529 | const ObjCInterfaceDecl *ID = |
| 3530 | ObjectTy->getAs<ObjCObjectType>()->getInterface(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 3531 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 3532 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 3533 | } |
| 3534 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 3535 | llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 3536 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 3537 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 3538 | uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3539 | return llvm::ConstantInt::get( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3540 | CGM.getTypes().ConvertType(CGM.getContext().LongTy), |
| 3541 | Offset); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 3542 | } |
| 3543 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3544 | /* *** Private Interface *** */ |
| 3545 | |
| 3546 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 3547 | /// level information. |
| 3548 | /// |
| 3549 | /// See: <rdr://4810609&4810587&4810587> |
| 3550 | /// struct IMAGE_INFO { |
| 3551 | /// unsigned version; |
| 3552 | /// unsigned flags; |
| 3553 | /// }; |
| 3554 | enum ImageInfoFlags { |
Daniel Dunbar | fce176b | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 3555 | eImageInfo_FixAndContinue = (1 << 0), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3556 | eImageInfo_GarbageCollected = (1 << 1), |
| 3557 | eImageInfo_GCOnly = (1 << 2), |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 3558 | eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set. |
| 3559 | |
Daniel Dunbar | fce176b | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 3560 | // A flag indicating that the module has no instances of a @synthesize of a |
| 3561 | // superclass variable. <rdar://problem/6803242> |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 3562 | eImageInfo_CorrectedSynthesize = (1 << 4) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3563 | }; |
| 3564 | |
Daniel Dunbar | fce176b | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 3565 | void CGObjCCommonMac::EmitImageInfo() { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3566 | unsigned version = 0; // Version is unused? |
| 3567 | unsigned flags = 0; |
| 3568 | |
| 3569 | // FIXME: Fix and continue? |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 3570 | if (CGM.getLangOptions().getGC() != LangOptions::NonGC) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3571 | flags |= eImageInfo_GarbageCollected; |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 3572 | if (CGM.getLangOptions().getGC() == LangOptions::GCOnly) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3573 | flags |= eImageInfo_GCOnly; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3574 | |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 3575 | // We never allow @synthesize of a superclass property. |
| 3576 | flags |= eImageInfo_CorrectedSynthesize; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3577 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3578 | // Emitted as int[2]; |
Chris Lattner | 0b23971 | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 3579 | uint32_t Values[2] = { version, flags }; |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3580 | |
| 3581 | const char *Section; |
| 3582 | if (ObjCABI == 1) |
| 3583 | Section = "__OBJC, __image_info,regular"; |
| 3584 | else |
| 3585 | Section = "__DATA, __objc_imageinfo, regular, no_dead_strip"; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3586 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3587 | CreateMetadataVar("\01L_OBJC_IMAGE_INFO", |
Chris Lattner | 0b23971 | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 3588 | llvm::ConstantDataArray::get(VMContext, Values), |
| 3589 | Section, 0, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3590 | GV->setConstant(true); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3591 | } |
| 3592 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3593 | |
| 3594 | // struct objc_module { |
| 3595 | // unsigned long version; |
| 3596 | // unsigned long size; |
| 3597 | // const char *name; |
| 3598 | // Symtab symtab; |
| 3599 | // }; |
| 3600 | |
| 3601 | // FIXME: Get from somewhere |
| 3602 | static const int ModuleVersion = 7; |
| 3603 | |
| 3604 | void CGObjCMac::EmitModuleInfo() { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3605 | uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3606 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 3607 | llvm::Constant *Values[] = { |
| 3608 | llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion), |
| 3609 | llvm::ConstantInt::get(ObjCTypes.LongTy, Size), |
| 3610 | // This used to be the filename, now it is unused. <rdr://4327263> |
| 3611 | GetClassName(&CGM.getContext().Idents.get("")), |
| 3612 | EmitModuleSymbols() |
| 3613 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3614 | CreateMetadataVar("\01L_OBJC_MODULES", |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 3615 | llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3616 | "__OBJC,__module_info,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 3617 | 4, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3618 | } |
| 3619 | |
| 3620 | llvm::Constant *CGObjCMac::EmitModuleSymbols() { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3621 | unsigned NumClasses = DefinedClasses.size(); |
| 3622 | unsigned NumCategories = DefinedCategories.size(); |
| 3623 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3624 | // Return null if no symbols were defined. |
| 3625 | if (!NumClasses && !NumCategories) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3626 | return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3627 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3628 | llvm::Constant *Values[5]; |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3629 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3630 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3631 | Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses); |
| 3632 | Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3633 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3634 | // The runtime expects exactly the list of defined classes followed |
| 3635 | // by the list of defined categories, in a single array. |
Chris Lattner | 0b23971 | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 3636 | SmallVector<llvm::Constant*, 8> Symbols(NumClasses + NumCategories); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3637 | for (unsigned i=0; i<NumClasses; i++) |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3638 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3639 | ObjCTypes.Int8PtrTy); |
| 3640 | for (unsigned i=0; i<NumCategories; i++) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3641 | Symbols[NumClasses + i] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3642 | llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3643 | ObjCTypes.Int8PtrTy); |
| 3644 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3645 | Values[4] = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3646 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Chris Lattner | 0b23971 | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 3647 | Symbols.size()), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3648 | Symbols); |
| 3649 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3650 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3651 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3652 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3653 | CreateMetadataVar("\01L_OBJC_SYMBOLS", Init, |
| 3654 | "__OBJC,__symbols,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 3655 | 4, true); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3656 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3657 | } |
| 3658 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3659 | llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder, |
| 3660 | IdentifierInfo *II) { |
| 3661 | LazySymbols.insert(II); |
| 3662 | |
| 3663 | llvm::GlobalVariable *&Entry = ClassReferences[II]; |
| 3664 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3665 | if (!Entry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3666 | llvm::Constant *Casted = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3667 | llvm::ConstantExpr::getBitCast(GetClassName(II), |
| 3668 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3669 | Entry = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3670 | CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted, |
| 3671 | "__OBJC,__cls_refs,literal_pointers,no_dead_strip", |
| 3672 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3673 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3674 | |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 3675 | return Builder.CreateLoad(Entry); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3676 | } |
| 3677 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3678 | llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder, |
| 3679 | const ObjCInterfaceDecl *ID) { |
| 3680 | return EmitClassRefFromId(Builder, ID->getIdentifier()); |
| 3681 | } |
| 3682 | |
| 3683 | llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) { |
| 3684 | IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool"); |
| 3685 | return EmitClassRefFromId(Builder, II); |
| 3686 | } |
| 3687 | |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 3688 | llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel, |
| 3689 | bool lvalue) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3690 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3691 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3692 | if (!Entry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3693 | llvm::Constant *Casted = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3694 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3695 | ObjCTypes.SelectorPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3696 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3697 | CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted, |
| 3698 | "__OBJC,__message_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 3699 | 4, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3700 | } |
| 3701 | |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 3702 | if (lvalue) |
| 3703 | return Entry; |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 3704 | return Builder.CreateLoad(Entry); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3705 | } |
| 3706 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3707 | llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3708 | llvm::GlobalVariable *&Entry = ClassNames[Ident]; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3709 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3710 | if (!Entry) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3711 | Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
Chris Lattner | 9401069 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 3712 | llvm::ConstantDataArray::getString(VMContext, |
| 3713 | Ident->getNameStart()), |
Daniel Dunbar | af19ac4 | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 3714 | ((ObjCABI == 2) ? |
| 3715 | "__TEXT,__objc_classname,cstring_literals" : |
| 3716 | "__TEXT,__cstring,cstring_literals"), |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3717 | 1, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3718 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3719 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3720 | } |
| 3721 | |
Argyrios Kyrtzidis | 9d50c06 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 3722 | llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) { |
| 3723 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator |
| 3724 | I = MethodDefinitions.find(MD); |
| 3725 | if (I != MethodDefinitions.end()) |
| 3726 | return I->second; |
| 3727 | |
Argyrios Kyrtzidis | 9d50c06 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 3728 | return NULL; |
| 3729 | } |
| 3730 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 3731 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 3732 | /// ivar layout bitmap. |
| 3733 | llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3734 | const ObjCCommonTypesHelper &ObjCTypes) { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3735 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 3736 | } |
| 3737 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3738 | void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3739 | unsigned int BytePos, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3740 | bool ForStrongLayout, |
| 3741 | bool &HasUnion) { |
| 3742 | const RecordDecl *RD = RT->getDecl(); |
| 3743 | // FIXME - Use iterator. |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3744 | SmallVector<const FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end()); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3745 | llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3746 | const llvm::StructLayout *RecLayout = |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3747 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3748 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3749 | BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos, |
| 3750 | ForStrongLayout, HasUnion); |
| 3751 | } |
| 3752 | |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 3753 | void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3754 | const llvm::StructLayout *Layout, |
| 3755 | const RecordDecl *RD, |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3756 | const SmallVectorImpl<const FieldDecl*> &RecFields, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3757 | unsigned int BytePos, bool ForStrongLayout, |
| 3758 | bool &HasUnion) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3759 | bool IsUnion = (RD && RD->isUnion()); |
| 3760 | uint64_t MaxUnionIvarSize = 0; |
| 3761 | uint64_t MaxSkippedUnionIvarSize = 0; |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3762 | const FieldDecl *MaxField = 0; |
| 3763 | const FieldDecl *MaxSkippedField = 0; |
| 3764 | const FieldDecl *LastFieldBitfieldOrUnnamed = 0; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3765 | uint64_t MaxFieldOffset = 0; |
| 3766 | uint64_t MaxSkippedFieldOffset = 0; |
Argyrios Kyrtzidis | 0dc7509 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 3767 | uint64_t LastBitfieldOrUnnamedOffset = 0; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3768 | uint64_t FirstFieldDelta = 0; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3769 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3770 | if (RecFields.empty()) |
| 3771 | return; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3772 | unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0); |
| 3773 | unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3774 | if (!RD && CGM.getLangOptions().ObjCAutoRefCount) { |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3775 | const FieldDecl *FirstField = RecFields[0]; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3776 | FirstFieldDelta = |
| 3777 | ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField)); |
| 3778 | } |
| 3779 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3780 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3781 | const FieldDecl *Field = RecFields[i]; |
Daniel Dunbar | e05cc98 | 2009-05-03 23:35:23 +0000 | [diff] [blame] | 3782 | uint64_t FieldOffset; |
Anders Carlsson | fc514ab | 2009-07-24 17:23:54 +0000 | [diff] [blame] | 3783 | if (RD) { |
Daniel Dunbar | f8f8eba | 2010-04-14 17:02:21 +0000 | [diff] [blame] | 3784 | // Note that 'i' here is actually the field index inside RD of Field, |
| 3785 | // although this dependency is hidden. |
| 3786 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3787 | FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta; |
Anders Carlsson | fc514ab | 2009-07-24 17:23:54 +0000 | [diff] [blame] | 3788 | } else |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3789 | FieldOffset = |
| 3790 | ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta; |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3791 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3792 | // Skip over unnamed or bitfields |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3793 | if (!Field->getIdentifier() || Field->isBitField()) { |
Argyrios Kyrtzidis | 0dc7509 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 3794 | LastFieldBitfieldOrUnnamed = Field; |
| 3795 | LastBitfieldOrUnnamedOffset = FieldOffset; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3796 | continue; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3797 | } |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3798 | |
Argyrios Kyrtzidis | 0dc7509 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 3799 | LastFieldBitfieldOrUnnamed = 0; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3800 | QualType FQT = Field->getType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3801 | if (FQT->isRecordType() || FQT->isUnionType()) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3802 | if (FQT->isUnionType()) |
| 3803 | HasUnion = true; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3804 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3805 | BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(), |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3806 | BytePos + FieldOffset, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3807 | ForStrongLayout, HasUnion); |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3808 | continue; |
| 3809 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3810 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3811 | if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3812 | const ConstantArrayType *CArray = |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3813 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3814 | uint64_t ElCount = CArray->getSize().getZExtValue(); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3815 | assert(CArray && "only array with known element size is supported"); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3816 | FQT = CArray->getElementType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3817 | while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 3818 | const ConstantArrayType *CArray = |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3819 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3820 | ElCount *= CArray->getSize().getZExtValue(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3821 | FQT = CArray->getElementType(); |
| 3822 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3823 | |
| 3824 | assert(!FQT->isUnionType() && |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3825 | "layout for array of unions not supported"); |
Fariborz Jahanian | f96bdf4 | 2011-01-03 19:23:18 +0000 | [diff] [blame] | 3826 | if (FQT->isRecordType() && ElCount) { |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3827 | int OldIndex = IvarsInfo.size() - 1; |
| 3828 | int OldSkIndex = SkipIvars.size() -1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3829 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3830 | const RecordType *RT = FQT->getAs<RecordType>(); |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3831 | BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3832 | ForStrongLayout, HasUnion); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3833 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3834 | // Replicate layout information for each array element. Note that |
| 3835 | // one element is already done. |
| 3836 | uint64_t ElIx = 1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3837 | for (int FirstIndex = IvarsInfo.size() - 1, |
| 3838 | FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3839 | uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits; |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3840 | for (int i = OldIndex+1; i <= FirstIndex; ++i) |
| 3841 | IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx, |
| 3842 | IvarsInfo[i].ivar_size)); |
| 3843 | for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i) |
| 3844 | SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx, |
| 3845 | SkipIvars[i].ivar_size)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3846 | } |
| 3847 | continue; |
| 3848 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3849 | } |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3850 | // At this point, we are done with Record/Union and array there of. |
| 3851 | // For other arrays we are down to its element type. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3852 | Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3853 | |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3854 | unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3855 | if ((ForStrongLayout && GCAttr == Qualifiers::Strong) |
| 3856 | || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) { |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3857 | if (IsUnion) { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3858 | uint64_t UnionIvarSize = FieldSize / WordSizeInBits; |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3859 | if (UnionIvarSize > MaxUnionIvarSize) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3860 | MaxUnionIvarSize = UnionIvarSize; |
| 3861 | MaxField = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3862 | MaxFieldOffset = FieldOffset; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3863 | } |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3864 | } else { |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3865 | IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3866 | FieldSize / WordSizeInBits)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3867 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3868 | } else if ((ForStrongLayout && |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3869 | (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)) |
| 3870 | || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) { |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3871 | if (IsUnion) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 3872 | // FIXME: Why the asymmetry? We divide by word size in bits on other |
| 3873 | // side. |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3874 | uint64_t UnionIvarSize = FieldSize; |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3875 | if (UnionIvarSize > MaxSkippedUnionIvarSize) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3876 | MaxSkippedUnionIvarSize = UnionIvarSize; |
| 3877 | MaxSkippedField = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3878 | MaxSkippedFieldOffset = FieldOffset; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3879 | } |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3880 | } else { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3881 | // FIXME: Why the asymmetry, we divide by byte size in bits here? |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3882 | SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3883 | FieldSize / ByteSizeInBits)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3884 | } |
| 3885 | } |
| 3886 | } |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3887 | |
Argyrios Kyrtzidis | 0dc7509 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 3888 | if (LastFieldBitfieldOrUnnamed) { |
| 3889 | if (LastFieldBitfieldOrUnnamed->isBitField()) { |
| 3890 | // Last field was a bitfield. Must update skip info. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3891 | uint64_t BitFieldSize |
| 3892 | = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext()); |
Argyrios Kyrtzidis | 0dc7509 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 3893 | GC_IVAR skivar; |
| 3894 | skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset; |
| 3895 | skivar.ivar_size = (BitFieldSize / ByteSizeInBits) |
| 3896 | + ((BitFieldSize % ByteSizeInBits) != 0); |
| 3897 | SkipIvars.push_back(skivar); |
| 3898 | } else { |
| 3899 | assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed"); |
| 3900 | // Last field was unnamed. Must update skip info. |
| 3901 | unsigned FieldSize |
| 3902 | = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType()); |
| 3903 | SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset, |
| 3904 | FieldSize / ByteSizeInBits)); |
| 3905 | } |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3906 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3907 | |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3908 | if (MaxField) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3909 | IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3910 | MaxUnionIvarSize)); |
| 3911 | if (MaxSkippedField) |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3912 | SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3913 | MaxSkippedUnionIvarSize)); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3914 | } |
| 3915 | |
Fariborz Jahanian | b8fd2eb | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 3916 | /// BuildIvarLayoutBitmap - This routine is the horsework for doing all |
| 3917 | /// the computations and returning the layout bitmap (for ivar or blocks) in |
| 3918 | /// the given argument BitMap string container. Routine reads |
| 3919 | /// two containers, IvarsInfo and SkipIvars which are assumed to be |
| 3920 | /// filled already by the caller. |
Chris Lattner | 9401069 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 3921 | llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string &BitMap) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3922 | unsigned int WordsToScan, WordsToSkip; |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame^] | 3923 | llvm::Type *PtrTy = CGM.Int8PtrTy; |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3924 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3925 | // Build the string of skip/scan nibbles |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3926 | SmallVector<SKIP_SCAN, 32> SkipScanIvars; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3927 | unsigned int WordSize = |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3928 | CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3929 | if (IvarsInfo[0].ivar_bytepos == 0) { |
| 3930 | WordsToSkip = 0; |
| 3931 | WordsToScan = IvarsInfo[0].ivar_size; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3932 | } else { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3933 | WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize; |
| 3934 | WordsToScan = IvarsInfo[0].ivar_size; |
| 3935 | } |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3936 | for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3937 | unsigned int TailPrevGCObjC = |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3938 | IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3939 | if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3940 | // consecutive 'scanned' object pointers. |
| 3941 | WordsToScan += IvarsInfo[i].ivar_size; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3942 | } else { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3943 | // Skip over 'gc'able object pointer which lay over each other. |
| 3944 | if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos) |
| 3945 | continue; |
| 3946 | // Must skip over 1 or more words. We save current skip/scan values |
| 3947 | // and start a new pair. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3948 | SKIP_SCAN SkScan; |
| 3949 | SkScan.skip = WordsToSkip; |
| 3950 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3951 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3952 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3953 | // Skip the hole. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3954 | SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize; |
| 3955 | SkScan.scan = 0; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3956 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3957 | WordsToSkip = 0; |
| 3958 | WordsToScan = IvarsInfo[i].ivar_size; |
| 3959 | } |
| 3960 | } |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3961 | if (WordsToScan > 0) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3962 | SKIP_SCAN SkScan; |
| 3963 | SkScan.skip = WordsToSkip; |
| 3964 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3965 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3966 | } |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3967 | |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3968 | if (!SkipIvars.empty()) { |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3969 | unsigned int LastIndex = SkipIvars.size()-1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3970 | int LastByteSkipped = |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3971 | SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3972 | LastIndex = IvarsInfo.size()-1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3973 | int LastByteScanned = |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3974 | IvarsInfo[LastIndex].ivar_bytepos + |
| 3975 | IvarsInfo[LastIndex].ivar_size * WordSize; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3976 | // 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] | 3977 | if (LastByteSkipped > LastByteScanned) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3978 | unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3979 | SKIP_SCAN SkScan; |
| 3980 | SkScan.skip = TotalWords - (LastByteScanned/WordSize); |
| 3981 | SkScan.scan = 0; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3982 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3983 | } |
| 3984 | } |
| 3985 | // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced |
| 3986 | // as 0xMN. |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3987 | int SkipScan = SkipScanIvars.size()-1; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3988 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3989 | if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0 |
| 3990 | && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) { |
| 3991 | // 0xM0 followed by 0x0N detected. |
| 3992 | SkipScanIvars[i].scan = SkipScanIvars[i+1].scan; |
| 3993 | for (int j = i+1; j < SkipScan; j++) |
| 3994 | SkipScanIvars[j] = SkipScanIvars[j+1]; |
| 3995 | --SkipScan; |
| 3996 | } |
| 3997 | } |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3998 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3999 | // Generate the string. |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4000 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4001 | unsigned char byte; |
| 4002 | unsigned int skip_small = SkipScanIvars[i].skip % 0xf; |
| 4003 | unsigned int scan_small = SkipScanIvars[i].scan % 0xf; |
| 4004 | unsigned int skip_big = SkipScanIvars[i].skip / 0xf; |
| 4005 | unsigned int scan_big = SkipScanIvars[i].scan / 0xf; |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4006 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4007 | // first skip big. |
| 4008 | for (unsigned int ix = 0; ix < skip_big; ix++) |
| 4009 | BitMap += (unsigned char)(0xf0); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4010 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4011 | // next (skip small, scan) |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4012 | if (skip_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4013 | byte = skip_small << 4; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4014 | if (scan_big > 0) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4015 | byte |= 0xf; |
| 4016 | --scan_big; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4017 | } else if (scan_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4018 | byte |= scan_small; |
| 4019 | scan_small = 0; |
| 4020 | } |
| 4021 | BitMap += byte; |
| 4022 | } |
| 4023 | // next scan big |
| 4024 | for (unsigned int ix = 0; ix < scan_big; ix++) |
| 4025 | BitMap += (unsigned char)(0x0f); |
| 4026 | // last scan small |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 4027 | if (scan_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 4028 | byte = scan_small; |
| 4029 | BitMap += byte; |
| 4030 | } |
| 4031 | } |
| 4032 | // null terminate string. |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 4033 | unsigned char zero = 0; |
| 4034 | BitMap += zero; |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4035 | |
| 4036 | llvm::GlobalVariable * Entry = |
| 4037 | CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
Chris Lattner | 9401069 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4038 | llvm::ConstantDataArray::getString(VMContext, BitMap,false), |
Daniel Dunbar | af19ac4 | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4039 | ((ObjCABI == 2) ? |
| 4040 | "__TEXT,__objc_classname,cstring_literals" : |
| 4041 | "__TEXT,__cstring,cstring_literals"), |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4042 | 1, true); |
| 4043 | return getConstantGEP(VMContext, Entry, 0, 0); |
| 4044 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4045 | |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4046 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 4047 | /// implementation for the __strong or __weak case. |
| 4048 | /// The layout map displays which words in ivar list must be skipped |
| 4049 | /// and which must be scanned by GC (see below). String is built of bytes. |
| 4050 | /// Each byte is divided up in two nibbles (4-bit each). Left nibble is count |
| 4051 | /// of words to skip and right nibble is count of words to scan. So, each |
| 4052 | /// nibble represents up to 15 workds to skip or scan. Skipping the rest is |
| 4053 | /// represented by a 0x00 byte which also ends the string. |
| 4054 | /// 1. when ForStrongLayout is true, following ivars are scanned: |
| 4055 | /// - id, Class |
| 4056 | /// - object * |
| 4057 | /// - __strong anything |
| 4058 | /// |
| 4059 | /// 2. When ForStrongLayout is false, following ivars are scanned: |
| 4060 | /// - __weak anything |
| 4061 | /// |
| 4062 | llvm::Constant *CGObjCCommonMac::BuildIvarLayout( |
| 4063 | const ObjCImplementationDecl *OMD, |
| 4064 | bool ForStrongLayout) { |
| 4065 | bool hasUnion = false; |
| 4066 | |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame^] | 4067 | llvm::Type *PtrTy = CGM.Int8PtrTy; |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 4068 | if (CGM.getLangOptions().getGC() == LangOptions::NonGC && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4069 | !CGM.getLangOptions().ObjCAutoRefCount) |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4070 | return llvm::Constant::getNullValue(PtrTy); |
| 4071 | |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4072 | const ObjCInterfaceDecl *OI = OMD->getClassInterface(); |
| 4073 | SmallVector<const FieldDecl*, 32> RecFields; |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 4074 | if (CGM.getLangOptions().ObjCAutoRefCount) { |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4075 | for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin(); |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 4076 | IVD; IVD = IVD->getNextIvar()) |
| 4077 | RecFields.push_back(cast<FieldDecl>(IVD)); |
| 4078 | } |
| 4079 | else { |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4080 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4081 | CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4082 | |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4083 | // FIXME: This is not ideal; we shouldn't have to do this copy. |
| 4084 | RecFields.append(Ivars.begin(), Ivars.end()); |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 4085 | } |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4086 | |
| 4087 | if (RecFields.empty()) |
| 4088 | return llvm::Constant::getNullValue(PtrTy); |
| 4089 | |
| 4090 | SkipIvars.clear(); |
| 4091 | IvarsInfo.clear(); |
| 4092 | |
| 4093 | BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion); |
| 4094 | if (IvarsInfo.empty()) |
| 4095 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | b8fd2eb | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 4096 | // Sort on byte position in case we encounterred a union nested in |
| 4097 | // the ivar list. |
| 4098 | if (hasUnion && !IvarsInfo.empty()) |
| 4099 | std::sort(IvarsInfo.begin(), IvarsInfo.end()); |
| 4100 | if (hasUnion && !SkipIvars.empty()) |
| 4101 | std::sort(SkipIvars.begin(), SkipIvars.end()); |
| 4102 | |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4103 | std::string BitMap; |
Fariborz Jahanian | b8fd2eb | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 4104 | llvm::Constant *C = BuildIvarLayoutBitmap(BitMap); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4105 | |
| 4106 | if (CGM.getLangOptions().ObjCGCBitmapPrint) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4107 | printf("\n%s ivar layout for class '%s': ", |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 4108 | ForStrongLayout ? "strong" : "weak", |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4109 | OMD->getClassInterface()->getName().data()); |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 4110 | const unsigned char *s = (unsigned char*)BitMap.c_str(); |
| 4111 | for (unsigned i = 0; i < BitMap.size(); i++) |
| 4112 | if (!(s[i] & 0xf0)) |
| 4113 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 4114 | else |
| 4115 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 4116 | printf("\n"); |
| 4117 | } |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4118 | return C; |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 4119 | } |
| 4120 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4121 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4122 | llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; |
| 4123 | |
Chris Lattner | 0b23971 | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 4124 | // FIXME: Avoid std::string in "Sel.getAsString()" |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4125 | if (!Entry) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4126 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_", |
Chris Lattner | 9401069 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4127 | llvm::ConstantDataArray::getString(VMContext, Sel.getAsString()), |
Daniel Dunbar | af19ac4 | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4128 | ((ObjCABI == 2) ? |
| 4129 | "__TEXT,__objc_methname,cstring_literals" : |
| 4130 | "__TEXT,__cstring,cstring_literals"), |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4131 | 1, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4132 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4133 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4134 | } |
| 4135 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4136 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4137 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4138 | return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); |
| 4139 | } |
| 4140 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4141 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) { |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4142 | std::string TypeStr; |
| 4143 | CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); |
| 4144 | |
| 4145 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4146 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4147 | if (!Entry) |
| 4148 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
Chris Lattner | 9401069 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4149 | llvm::ConstantDataArray::getString(VMContext, TypeStr), |
Daniel Dunbar | af19ac4 | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4150 | ((ObjCABI == 2) ? |
| 4151 | "__TEXT,__objc_methtype,cstring_literals" : |
| 4152 | "__TEXT,__cstring,cstring_literals"), |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4153 | 1, true); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4154 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4155 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4156 | } |
| 4157 | |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4158 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D, |
| 4159 | bool Extended) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4160 | std::string TypeStr; |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4161 | if (CGM.getContext().getObjCEncodingForMethodDecl( |
| 4162 | const_cast<ObjCMethodDecl*>(D), |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4163 | TypeStr, Extended)) |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4164 | return 0; |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4165 | |
| 4166 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
| 4167 | |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4168 | if (!Entry) |
| 4169 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
Chris Lattner | 9401069 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4170 | llvm::ConstantDataArray::getString(VMContext, TypeStr), |
Daniel Dunbar | af19ac4 | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4171 | ((ObjCABI == 2) ? |
| 4172 | "__TEXT,__objc_methtype,cstring_literals" : |
| 4173 | "__TEXT,__cstring,cstring_literals"), |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4174 | 1, true); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4175 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4176 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4177 | } |
| 4178 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4179 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4180 | llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4181 | llvm::GlobalVariable *&Entry = PropertyNames[Ident]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4182 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4183 | if (!Entry) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4184 | Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_", |
Chris Lattner | 9401069 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 4185 | llvm::ConstantDataArray::getString(VMContext, |
| 4186 | Ident->getNameStart()), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4187 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4188 | 1, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4189 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4190 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4191 | } |
| 4192 | |
| 4193 | // FIXME: Merge into a single cstring creation function. |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4194 | // FIXME: This Decl should be more precise. |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4195 | llvm::Constant * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4196 | CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 4197 | const Decl *Container) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4198 | std::string TypeStr; |
| 4199 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4200 | return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); |
| 4201 | } |
| 4202 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4203 | void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D, |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4204 | const ObjCContainerDecl *CD, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4205 | SmallVectorImpl<char> &Name) { |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4206 | llvm::raw_svector_ostream OS(Name); |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 4207 | assert (CD && "Missing container decl in GetNameForMethod"); |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4208 | OS << '\01' << (D->isInstanceMethod() ? '-' : '+') |
| 4209 | << '[' << CD->getName(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4210 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4211 | dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 4212 | OS << '(' << CID << ')'; |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4213 | OS << ' ' << D->getSelector().getAsString() << ']'; |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 4214 | } |
| 4215 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4216 | void CGObjCMac::FinishModule() { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4217 | EmitModuleInfo(); |
| 4218 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4219 | // Emit the dummy bodies for any protocols which were referenced but |
| 4220 | // never defined. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4221 | for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4222 | I = Protocols.begin(), e = Protocols.end(); I != e; ++I) { |
| 4223 | if (I->second->hasInitializer()) |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4224 | continue; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4225 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 4226 | llvm::Constant *Values[5]; |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4227 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4228 | Values[1] = GetClassName(I->first); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4229 | Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4230 | Values[3] = Values[4] = |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4231 | llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4232 | I->second->setLinkage(llvm::GlobalValue::InternalLinkage); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 4233 | I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4234 | Values)); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4235 | CGM.AddUsedGlobal(I->second); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4236 | } |
| 4237 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4238 | // Add assembler directives to add lazy undefined symbol references |
| 4239 | // for classes which are referenced but not defined. This is |
| 4240 | // important for correct linker interaction. |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4241 | // |
| 4242 | // FIXME: It would be nice if we had an LLVM construct for this. |
| 4243 | if (!LazySymbols.empty() || !DefinedSymbols.empty()) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 4244 | SmallString<256> Asm; |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4245 | Asm += CGM.getModule().getModuleInlineAsm(); |
| 4246 | if (!Asm.empty() && Asm.back() != '\n') |
| 4247 | Asm += '\n'; |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4248 | |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4249 | llvm::raw_svector_ostream OS(Asm); |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4250 | for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(), |
| 4251 | e = DefinedSymbols.end(); I != e; ++I) |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 4252 | OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n" |
| 4253 | << "\t.globl .objc_class_name_" << (*I)->getName() << "\n"; |
Chris Lattner | afd5eda | 2010-04-17 18:26:20 +0000 | [diff] [blame] | 4254 | for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(), |
Fariborz Jahanian | b9c5b3d | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 4255 | e = LazySymbols.end(); I != e; ++I) { |
Chris Lattner | afd5eda | 2010-04-17 18:26:20 +0000 | [diff] [blame] | 4256 | OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n"; |
Fariborz Jahanian | b9c5b3d | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 4257 | } |
| 4258 | |
| 4259 | for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) { |
| 4260 | OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n" |
| 4261 | << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n"; |
| 4262 | } |
Chris Lattner | afd5eda | 2010-04-17 18:26:20 +0000 | [diff] [blame] | 4263 | |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4264 | CGM.getModule().setModuleInlineAsm(OS.str()); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4265 | } |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4266 | } |
| 4267 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4268 | CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4269 | : CGObjCCommonMac(cgm), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4270 | ObjCTypes(cgm) { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4271 | ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4272 | ObjCABI = 2; |
| 4273 | } |
| 4274 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4275 | /* *** */ |
| 4276 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4277 | ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm) |
Douglas Gregor | 65a1e67 | 2012-01-17 23:38:32 +0000 | [diff] [blame] | 4278 | : VMContext(cgm.getLLVMContext()), CGM(cgm), ExternalProtocolPtrTy(0) |
| 4279 | { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4280 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 4281 | ASTContext &Ctx = CGM.getContext(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4282 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4283 | ShortTy = Types.ConvertType(Ctx.ShortTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4284 | IntTy = Types.ConvertType(Ctx.IntTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4285 | LongTy = Types.ConvertType(Ctx.LongTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4286 | LongLongTy = Types.ConvertType(Ctx.LongLongTy); |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame^] | 4287 | Int8PtrTy = CGM.Int8PtrTy; |
| 4288 | Int8PtrPtrTy = CGM.Int8PtrPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4289 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4290 | ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4291 | PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4292 | SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4293 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4294 | // I'm not sure I like this. The implicit coordination is a bit |
| 4295 | // gross. We should solve this in a reasonable fashion because this |
| 4296 | // is a pretty common task (match some runtime data structure with |
| 4297 | // an LLVM data structure). |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4298 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4299 | // FIXME: This is leaked. |
| 4300 | // FIXME: Merge with rewriter code? |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4301 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4302 | // struct _objc_super { |
| 4303 | // id self; |
| 4304 | // Class cls; |
| 4305 | // } |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4306 | RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct, |
Daniel Dunbar | daa3ac5 | 2010-04-29 16:29:11 +0000 | [diff] [blame] | 4307 | Ctx.getTranslationUnitDecl(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4308 | SourceLocation(), SourceLocation(), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4309 | &Ctx.Idents.get("_objc_super")); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4310 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4311 | Ctx.getObjCIdType(), 0, 0, false, false)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4312 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4313 | Ctx.getObjCClassType(), 0, 0, false, false)); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4314 | RD->completeDefinition(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4315 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4316 | SuperCTy = Ctx.getTagDeclType(RD); |
| 4317 | SuperPtrCTy = Ctx.getPointerType(SuperCTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4318 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4319 | SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4320 | SuperPtrTy = llvm::PointerType::getUnqual(SuperTy); |
| 4321 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4322 | // struct _prop_t { |
| 4323 | // char *name; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4324 | // char *attributes; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4325 | // } |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4326 | PropertyTy = llvm::StructType::create("struct._prop_t", |
| 4327 | Int8PtrTy, Int8PtrTy, NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4328 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4329 | // struct _prop_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4330 | // uint32_t entsize; // sizeof(struct _prop_t) |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4331 | // uint32_t count_of_properties; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4332 | // struct _prop_t prop_list[count_of_properties]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4333 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4334 | PropertyListTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4335 | llvm::StructType::create("struct._prop_list_t", IntTy, IntTy, |
| 4336 | llvm::ArrayType::get(PropertyTy, 0), NULL); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4337 | // struct _prop_list_t * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4338 | PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4339 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4340 | // struct _objc_method { |
| 4341 | // SEL _cmd; |
| 4342 | // char *method_type; |
| 4343 | // char *_imp; |
| 4344 | // } |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4345 | MethodTy = llvm::StructType::create("struct._objc_method", |
| 4346 | SelectorPtrTy, Int8PtrTy, Int8PtrTy, |
| 4347 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4348 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4349 | // struct _objc_cache * |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4350 | CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache"); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4351 | CachePtrTy = llvm::PointerType::getUnqual(CacheTy); |
Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 4352 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4353 | } |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4354 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4355 | ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4356 | : ObjCCommonTypesHelper(cgm) { |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4357 | // struct _objc_method_description { |
| 4358 | // SEL name; |
| 4359 | // char *types; |
| 4360 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4361 | MethodDescriptionTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4362 | llvm::StructType::create("struct._objc_method_description", |
| 4363 | SelectorPtrTy, Int8PtrTy, NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4364 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4365 | // struct _objc_method_description_list { |
| 4366 | // int count; |
| 4367 | // struct _objc_method_description[1]; |
| 4368 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4369 | MethodDescriptionListTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4370 | llvm::StructType::create("struct._objc_method_description_list", |
| 4371 | IntTy, |
| 4372 | llvm::ArrayType::get(MethodDescriptionTy, 0),NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4373 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4374 | // struct _objc_method_description_list * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4375 | MethodDescriptionListPtrTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4376 | llvm::PointerType::getUnqual(MethodDescriptionListTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4377 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4378 | // Protocol description structures |
| 4379 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4380 | // struct _objc_protocol_extension { |
| 4381 | // uint32_t size; // sizeof(struct _objc_protocol_extension) |
| 4382 | // struct _objc_method_description_list *optional_instance_methods; |
| 4383 | // struct _objc_method_description_list *optional_class_methods; |
| 4384 | // struct _objc_property_list *instance_properties; |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4385 | // const char ** extendedMethodTypes; |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4386 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4387 | ProtocolExtensionTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4388 | llvm::StructType::create("struct._objc_protocol_extension", |
| 4389 | IntTy, MethodDescriptionListPtrTy, |
| 4390 | MethodDescriptionListPtrTy, PropertyListPtrTy, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4391 | Int8PtrPtrTy, NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4392 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4393 | // struct _objc_protocol_extension * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4394 | ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4395 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4396 | // Handle recursive construction of Protocol and ProtocolList types |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4397 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4398 | ProtocolTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4399 | llvm::StructType::create(VMContext, "struct._objc_protocol"); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4400 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4401 | ProtocolListTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4402 | llvm::StructType::create(VMContext, "struct._objc_protocol_list"); |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4403 | ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy), |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4404 | LongTy, |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4405 | llvm::ArrayType::get(ProtocolTy, 0), |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4406 | NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4407 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4408 | // struct _objc_protocol { |
| 4409 | // struct _objc_protocol_extension *isa; |
| 4410 | // char *protocol_name; |
| 4411 | // struct _objc_protocol **_objc_protocol_list; |
| 4412 | // struct _objc_method_description_list *instance_methods; |
| 4413 | // struct _objc_method_description_list *class_methods; |
| 4414 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4415 | ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy, |
| 4416 | llvm::PointerType::getUnqual(ProtocolListTy), |
| 4417 | MethodDescriptionListPtrTy, |
| 4418 | MethodDescriptionListPtrTy, |
| 4419 | NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4420 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4421 | // struct _objc_protocol_list * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4422 | ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4423 | |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4424 | ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4425 | |
| 4426 | // Class description structures |
| 4427 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4428 | // struct _objc_ivar { |
| 4429 | // char *ivar_name; |
| 4430 | // char *ivar_type; |
| 4431 | // int ivar_offset; |
| 4432 | // } |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4433 | IvarTy = llvm::StructType::create("struct._objc_ivar", |
| 4434 | Int8PtrTy, Int8PtrTy, IntTy, NULL); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4435 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4436 | // struct _objc_ivar_list * |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4437 | IvarListTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4438 | llvm::StructType::create(VMContext, "struct._objc_ivar_list"); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4439 | IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4440 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4441 | // struct _objc_method_list * |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4442 | MethodListTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4443 | llvm::StructType::create(VMContext, "struct._objc_method_list"); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4444 | MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4445 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4446 | // struct _objc_class_extension * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4447 | ClassExtensionTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4448 | llvm::StructType::create("struct._objc_class_extension", |
| 4449 | IntTy, Int8PtrTy, PropertyListPtrTy, NULL); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4450 | ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4451 | |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4452 | ClassTy = llvm::StructType::create(VMContext, "struct._objc_class"); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4453 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4454 | // struct _objc_class { |
| 4455 | // Class isa; |
| 4456 | // Class super_class; |
| 4457 | // char *name; |
| 4458 | // long version; |
| 4459 | // long info; |
| 4460 | // long instance_size; |
| 4461 | // struct _objc_ivar_list *ivars; |
| 4462 | // struct _objc_method_list *methods; |
| 4463 | // struct _objc_cache *cache; |
| 4464 | // struct _objc_protocol_list *protocols; |
| 4465 | // char *ivar_layout; |
| 4466 | // struct _objc_class_ext *ext; |
| 4467 | // }; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4468 | ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy), |
| 4469 | llvm::PointerType::getUnqual(ClassTy), |
| 4470 | Int8PtrTy, |
| 4471 | LongTy, |
| 4472 | LongTy, |
| 4473 | LongTy, |
| 4474 | IvarListPtrTy, |
| 4475 | MethodListPtrTy, |
| 4476 | CachePtrTy, |
| 4477 | ProtocolListPtrTy, |
| 4478 | Int8PtrTy, |
| 4479 | ClassExtensionPtrTy, |
| 4480 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4481 | |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4482 | ClassPtrTy = llvm::PointerType::getUnqual(ClassTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4483 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4484 | // struct _objc_category { |
| 4485 | // char *category_name; |
| 4486 | // char *class_name; |
| 4487 | // struct _objc_method_list *instance_method; |
| 4488 | // struct _objc_method_list *class_method; |
| 4489 | // uint32_t size; // sizeof(struct _objc_category) |
| 4490 | // struct _objc_property_list *instance_properties;// category's @property |
| 4491 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4492 | CategoryTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4493 | llvm::StructType::create("struct._objc_category", |
| 4494 | Int8PtrTy, Int8PtrTy, MethodListPtrTy, |
| 4495 | MethodListPtrTy, ProtocolListPtrTy, |
| 4496 | IntTy, PropertyListPtrTy, NULL); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 4497 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4498 | // Global metadata structures |
| 4499 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4500 | // struct _objc_symtab { |
| 4501 | // long sel_ref_cnt; |
| 4502 | // SEL *refs; |
| 4503 | // short cls_def_cnt; |
| 4504 | // short cat_def_cnt; |
| 4505 | // char *defs[cls_def_cnt + cat_def_cnt]; |
| 4506 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4507 | SymtabTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4508 | llvm::StructType::create("struct._objc_symtab", |
| 4509 | LongTy, SelectorPtrTy, ShortTy, ShortTy, |
| 4510 | llvm::ArrayType::get(Int8PtrTy, 0), NULL); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4511 | SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4512 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 4513 | // struct _objc_module { |
| 4514 | // long version; |
| 4515 | // long size; // sizeof(struct _objc_module) |
| 4516 | // char *name; |
| 4517 | // struct _objc_symtab* symtab; |
| 4518 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4519 | ModuleTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4520 | llvm::StructType::create("struct._objc_module", |
| 4521 | LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 4522 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4523 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 4524 | // FIXME: This is the size of the setjmp buffer and should be target |
| 4525 | // specific. 18 is what's used on 32-bit X86. |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 4526 | uint64_t SetJmpBufferSize = 18; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4527 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 4528 | // Exceptions |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame^] | 4529 | llvm::Type *StackPtrTy = llvm::ArrayType::get(CGM.Int8PtrTy, 4); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4530 | |
| 4531 | ExceptionDataTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4532 | llvm::StructType::create("struct._objc_exception_data", |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame^] | 4533 | llvm::ArrayType::get(CGM.Int32Ty,SetJmpBufferSize), |
| 4534 | StackPtrTy, NULL); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 4535 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 4536 | } |
| 4537 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4538 | ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4539 | : ObjCCommonTypesHelper(cgm) { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4540 | // struct _method_list_t { |
| 4541 | // uint32_t entsize; // sizeof(struct _objc_method) |
| 4542 | // uint32_t method_count; |
| 4543 | // struct _objc_method method_list[method_count]; |
| 4544 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4545 | MethodListnfABITy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4546 | llvm::StructType::create("struct.__method_list_t", IntTy, IntTy, |
| 4547 | llvm::ArrayType::get(MethodTy, 0), NULL); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4548 | // struct method_list_t * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4549 | MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4550 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4551 | // struct _protocol_t { |
| 4552 | // id isa; // NULL |
| 4553 | // const char * const protocol_name; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4554 | // const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4555 | // const struct method_list_t * const instance_methods; |
| 4556 | // const struct method_list_t * const class_methods; |
| 4557 | // const struct method_list_t *optionalInstanceMethods; |
| 4558 | // const struct method_list_t *optionalClassMethods; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4559 | // const struct _prop_list_t * properties; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4560 | // const uint32_t size; // sizeof(struct _protocol_t) |
| 4561 | // const uint32_t flags; // = 0 |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4562 | // const char ** extendedMethodTypes; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4563 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4564 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4565 | // Holder for struct _protocol_list_t * |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4566 | ProtocolListnfABITy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4567 | llvm::StructType::create(VMContext, "struct._objc_protocol_list"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4568 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4569 | ProtocolnfABITy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4570 | llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy, |
| 4571 | llvm::PointerType::getUnqual(ProtocolListnfABITy), |
| 4572 | MethodListnfABIPtrTy, MethodListnfABIPtrTy, |
| 4573 | MethodListnfABIPtrTy, MethodListnfABIPtrTy, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4574 | PropertyListPtrTy, IntTy, IntTy, Int8PtrPtrTy, |
| 4575 | NULL); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4576 | |
| 4577 | // struct _protocol_t* |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4578 | ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4579 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4580 | // struct _protocol_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4581 | // long protocol_count; // Note, this is 32/64 bit |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4582 | // struct _protocol_t *[protocol_count]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4583 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4584 | ProtocolListnfABITy->setBody(LongTy, |
| 4585 | llvm::ArrayType::get(ProtocolnfABIPtrTy, 0), |
| 4586 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4587 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4588 | // struct _objc_protocol_list* |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4589 | ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4590 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4591 | // struct _ivar_t { |
| 4592 | // unsigned long int *offset; // pointer to ivar offset location |
| 4593 | // char *name; |
| 4594 | // char *type; |
| 4595 | // uint32_t alignment; |
| 4596 | // uint32_t size; |
| 4597 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4598 | IvarnfABITy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4599 | llvm::StructType::create("struct._ivar_t", |
| 4600 | llvm::PointerType::getUnqual(LongTy), |
| 4601 | Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4602 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4603 | // struct _ivar_list_t { |
| 4604 | // uint32 entsize; // sizeof(struct _ivar_t) |
| 4605 | // uint32 count; |
| 4606 | // struct _iver_t list[count]; |
| 4607 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4608 | IvarListnfABITy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4609 | llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy, |
| 4610 | llvm::ArrayType::get(IvarnfABITy, 0), NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4611 | |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4612 | IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4613 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4614 | // struct _class_ro_t { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4615 | // uint32_t const flags; |
| 4616 | // uint32_t const instanceStart; |
| 4617 | // uint32_t const instanceSize; |
| 4618 | // uint32_t const reserved; // only when building for 64bit targets |
| 4619 | // const uint8_t * const ivarLayout; |
| 4620 | // const char *const name; |
| 4621 | // const struct _method_list_t * const baseMethods; |
| 4622 | // const struct _objc_protocol_list *const baseProtocols; |
| 4623 | // const struct _ivar_list_t *const ivars; |
| 4624 | // const uint8_t * const weakIvarLayout; |
| 4625 | // const struct _prop_list_t * const properties; |
| 4626 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4627 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4628 | // FIXME. Add 'reserved' field in 64bit abi mode! |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4629 | ClassRonfABITy = llvm::StructType::create("struct._class_ro_t", |
| 4630 | IntTy, IntTy, IntTy, Int8PtrTy, |
| 4631 | Int8PtrTy, MethodListnfABIPtrTy, |
| 4632 | ProtocolListnfABIPtrTy, |
| 4633 | IvarListnfABIPtrTy, |
| 4634 | Int8PtrTy, PropertyListPtrTy, NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4635 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4636 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4637 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 4638 | ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false) |
| 4639 | ->getPointerTo(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4640 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4641 | // struct _class_t { |
| 4642 | // struct _class_t *isa; |
| 4643 | // struct _class_t * const superclass; |
| 4644 | // void *cache; |
| 4645 | // IMP *vtable; |
| 4646 | // struct class_ro_t *ro; |
| 4647 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4648 | |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4649 | ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t"); |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4650 | ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy), |
| 4651 | llvm::PointerType::getUnqual(ClassnfABITy), |
| 4652 | CachePtrTy, |
| 4653 | llvm::PointerType::getUnqual(ImpnfABITy), |
| 4654 | llvm::PointerType::getUnqual(ClassRonfABITy), |
| 4655 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4656 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4657 | // LLVM for struct _class_t * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4658 | ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4659 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4660 | // struct _category_t { |
| 4661 | // const char * const name; |
| 4662 | // struct _class_t *const cls; |
| 4663 | // const struct _method_list_t * const instance_methods; |
| 4664 | // const struct _method_list_t * const class_methods; |
| 4665 | // const struct _protocol_list_t * const protocols; |
| 4666 | // const struct _prop_list_t * const properties; |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 4667 | // } |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4668 | CategorynfABITy = llvm::StructType::create("struct._category_t", |
| 4669 | Int8PtrTy, ClassnfABIPtrTy, |
| 4670 | MethodListnfABIPtrTy, |
| 4671 | MethodListnfABIPtrTy, |
| 4672 | ProtocolListnfABIPtrTy, |
| 4673 | PropertyListPtrTy, |
| 4674 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4675 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4676 | // New types for nonfragile abi messaging. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4677 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 4678 | ASTContext &Ctx = CGM.getContext(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4679 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4680 | // MessageRefTy - LLVM for: |
| 4681 | // struct _message_ref_t { |
| 4682 | // IMP messenger; |
| 4683 | // SEL name; |
| 4684 | // }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4685 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4686 | // First the clang type for struct _message_ref_t |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4687 | RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct, |
Daniel Dunbar | daa3ac5 | 2010-04-29 16:29:11 +0000 | [diff] [blame] | 4688 | Ctx.getTranslationUnitDecl(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4689 | SourceLocation(), SourceLocation(), |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4690 | &Ctx.Idents.get("_message_ref_t")); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4691 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4692 | Ctx.VoidPtrTy, 0, 0, false, false)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4693 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4694 | Ctx.getObjCSelType(), 0, 0, false, false)); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4695 | RD->completeDefinition(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4696 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4697 | MessageRefCTy = Ctx.getTagDeclType(RD); |
| 4698 | MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy); |
| 4699 | MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4700 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4701 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4702 | MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4703 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4704 | // SuperMessageRefTy - LLVM for: |
| 4705 | // struct _super_message_ref_t { |
| 4706 | // SUPER_IMP messenger; |
| 4707 | // SEL name; |
| 4708 | // }; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4709 | SuperMessageRefTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4710 | llvm::StructType::create("struct._super_message_ref_t", |
| 4711 | ImpnfABITy, SelectorPtrTy, NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4712 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4713 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4714 | SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy); |
Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 4715 | |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 4716 | |
| 4717 | // struct objc_typeinfo { |
| 4718 | // const void** vtable; // objc_ehtype_vtable + 2 |
| 4719 | // const char* name; // c++ typeinfo string |
| 4720 | // Class cls; |
| 4721 | // }; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4722 | EHTypeTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4723 | llvm::StructType::create("struct._objc_typeinfo", |
| 4724 | llvm::PointerType::getUnqual(Int8PtrTy), |
| 4725 | Int8PtrTy, ClassnfABIPtrTy, NULL); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4726 | EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 4727 | } |
| 4728 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4729 | llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4730 | FinishNonFragileABIModule(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4731 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4732 | return NULL; |
| 4733 | } |
| 4734 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4735 | void CGObjCNonFragileABIMac::AddModuleClassList(const |
| 4736 | std::vector<llvm::GlobalValue*> |
| 4737 | &Container, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4738 | const char *SymbolName, |
| 4739 | const char *SectionName) { |
| 4740 | unsigned NumClasses = Container.size(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4741 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4742 | if (!NumClasses) |
| 4743 | return; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4744 | |
Chris Lattner | 0b23971 | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 4745 | SmallVector<llvm::Constant*, 8> Symbols(NumClasses); |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4746 | for (unsigned i=0; i<NumClasses; i++) |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4747 | Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i], |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4748 | ObjCTypes.Int8PtrTy); |
Chris Lattner | 0b23971 | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 4749 | llvm::Constant *Init = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4750 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Chris Lattner | 0b23971 | 2012-02-06 22:16:34 +0000 | [diff] [blame] | 4751 | Symbols.size()), |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4752 | Symbols); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4753 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4754 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4755 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4756 | llvm::GlobalValue::InternalLinkage, |
| 4757 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4758 | SymbolName); |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 4759 | GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType())); |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4760 | GV->setSection(SectionName); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4761 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4762 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4763 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4764 | void CGObjCNonFragileABIMac::FinishNonFragileABIModule() { |
| 4765 | // nonfragile abi has no module definition. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4766 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4767 | // Build list of all implemented class addresses in array |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4768 | // L_OBJC_LABEL_CLASS_$. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4769 | AddModuleClassList(DefinedClasses, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4770 | "\01L_OBJC_LABEL_CLASS_$", |
| 4771 | "__DATA, __objc_classlist, regular, no_dead_strip"); |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4772 | |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4773 | for (unsigned i = 0; i < DefinedClasses.size(); i++) { |
| 4774 | llvm::GlobalValue *IMPLGV = DefinedClasses[i]; |
| 4775 | if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage) |
| 4776 | continue; |
| 4777 | IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4778 | } |
| 4779 | |
Fariborz Jahanian | 0e93d25 | 2009-12-01 18:25:24 +0000 | [diff] [blame] | 4780 | for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) { |
| 4781 | llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i]; |
| 4782 | if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage) |
| 4783 | continue; |
| 4784 | IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 4785 | } |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4786 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4787 | AddModuleClassList(DefinedNonLazyClasses, |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4788 | "\01L_OBJC_LABEL_NONLAZY_CLASS_$", |
| 4789 | "__DATA, __objc_nlclslist, regular, no_dead_strip"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4790 | |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4791 | // Build list of all implemented category addresses in array |
| 4792 | // L_OBJC_LABEL_CATEGORY_$. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4793 | AddModuleClassList(DefinedCategories, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4794 | "\01L_OBJC_LABEL_CATEGORY_$", |
| 4795 | "__DATA, __objc_catlist, regular, no_dead_strip"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4796 | AddModuleClassList(DefinedNonLazyCategories, |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4797 | "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$", |
| 4798 | "__DATA, __objc_nlcatlist, regular, no_dead_strip"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4799 | |
Daniel Dunbar | fce176b | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 4800 | EmitImageInfo(); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4801 | } |
| 4802 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4803 | /// isVTableDispatchedSelector - Returns true if SEL is not in the list of |
| 4804 | /// VTableDispatchMethods; false otherwise. What this means is that |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4805 | /// except for the 19 selectors in the list, we generate 32bit-style |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4806 | /// message dispatch call for all the rest. |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4807 | bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) { |
| 4808 | // At various points we've experimented with using vtable-based |
| 4809 | // dispatch for all methods. |
Daniel Dunbar | f643b9b | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 4810 | switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) { |
Daniel Dunbar | f643b9b | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 4811 | case CodeGenOptions::Legacy: |
Fariborz Jahanian | 776dbf9 | 2010-04-19 17:53:30 +0000 | [diff] [blame] | 4812 | return false; |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4813 | case CodeGenOptions::NonLegacy: |
| 4814 | return true; |
Daniel Dunbar | f643b9b | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 4815 | case CodeGenOptions::Mixed: |
| 4816 | break; |
| 4817 | } |
| 4818 | |
| 4819 | // If so, see whether this selector is in the white-list of things which must |
| 4820 | // 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] | 4821 | if (VTableDispatchMethods.empty()) { |
| 4822 | VTableDispatchMethods.insert(GetNullarySelector("alloc")); |
| 4823 | VTableDispatchMethods.insert(GetNullarySelector("class")); |
| 4824 | VTableDispatchMethods.insert(GetNullarySelector("self")); |
| 4825 | VTableDispatchMethods.insert(GetNullarySelector("isFlipped")); |
| 4826 | VTableDispatchMethods.insert(GetNullarySelector("length")); |
| 4827 | VTableDispatchMethods.insert(GetNullarySelector("count")); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4828 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4829 | // These are vtable-based if GC is disabled. |
| 4830 | // Optimistically use vtable dispatch for hybrid compiles. |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 4831 | if (CGM.getLangOptions().getGC() != LangOptions::GCOnly) { |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4832 | VTableDispatchMethods.insert(GetNullarySelector("retain")); |
| 4833 | VTableDispatchMethods.insert(GetNullarySelector("release")); |
| 4834 | VTableDispatchMethods.insert(GetNullarySelector("autorelease")); |
| 4835 | } |
| 4836 | |
| 4837 | VTableDispatchMethods.insert(GetUnarySelector("allocWithZone")); |
| 4838 | VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass")); |
| 4839 | VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector")); |
| 4840 | VTableDispatchMethods.insert(GetUnarySelector("objectForKey")); |
| 4841 | VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex")); |
| 4842 | VTableDispatchMethods.insert(GetUnarySelector("isEqualToString")); |
| 4843 | VTableDispatchMethods.insert(GetUnarySelector("isEqual")); |
| 4844 | |
| 4845 | // These are vtable-based if GC is enabled. |
| 4846 | // Optimistically use vtable dispatch for hybrid compiles. |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 4847 | if (CGM.getLangOptions().getGC() != LangOptions::NonGC) { |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4848 | VTableDispatchMethods.insert(GetNullarySelector("hash")); |
| 4849 | VTableDispatchMethods.insert(GetUnarySelector("addObject")); |
| 4850 | |
| 4851 | // "countByEnumeratingWithState:objects:count" |
| 4852 | IdentifierInfo *KeyIdents[] = { |
| 4853 | &CGM.getContext().Idents.get("countByEnumeratingWithState"), |
| 4854 | &CGM.getContext().Idents.get("objects"), |
| 4855 | &CGM.getContext().Idents.get("count") |
| 4856 | }; |
| 4857 | VTableDispatchMethods.insert( |
| 4858 | CGM.getContext().Selectors.getSelector(3, KeyIdents)); |
| 4859 | } |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4860 | } |
Daniel Dunbar | f643b9b | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 4861 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4862 | return VTableDispatchMethods.count(Sel); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4863 | } |
| 4864 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4865 | // Metadata flags |
| 4866 | enum MetaDataDlags { |
| 4867 | CLS = 0x0, |
| 4868 | CLS_META = 0x1, |
| 4869 | CLS_ROOT = 0x2, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4870 | OBJC2_CLS_HIDDEN = 0x10, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4871 | CLS_EXCEPTION = 0x20, |
| 4872 | |
| 4873 | /// (Obsolete) ARC-specific: this class has a .release_ivars method |
| 4874 | CLS_HAS_IVAR_RELEASER = 0x40, |
| 4875 | /// class was compiled with -fobjc-arr |
| 4876 | CLS_COMPILED_BY_ARC = 0x80 // (1<<7) |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4877 | }; |
| 4878 | /// BuildClassRoTInitializer - generate meta-data for: |
| 4879 | /// struct _class_ro_t { |
| 4880 | /// uint32_t const flags; |
| 4881 | /// uint32_t const instanceStart; |
| 4882 | /// uint32_t const instanceSize; |
| 4883 | /// uint32_t const reserved; // only when building for 64bit targets |
| 4884 | /// const uint8_t * const ivarLayout; |
| 4885 | /// const char *const name; |
| 4886 | /// const struct _method_list_t * const baseMethods; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4887 | /// const struct _protocol_list_t *const baseProtocols; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4888 | /// const struct _ivar_list_t *const ivars; |
| 4889 | /// const uint8_t * const weakIvarLayout; |
| 4890 | /// const struct _prop_list_t * const properties; |
| 4891 | /// } |
| 4892 | /// |
| 4893 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4894 | unsigned flags, |
| 4895 | unsigned InstanceStart, |
| 4896 | unsigned InstanceSize, |
| 4897 | const ObjCImplementationDecl *ID) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4898 | std::string ClassName = ID->getNameAsString(); |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 4899 | llvm::Constant *Values[10]; // 11 for 64bit targets! |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4900 | |
| 4901 | if (CGM.getLangOptions().ObjCAutoRefCount) |
| 4902 | flags |= CLS_COMPILED_BY_ARC; |
| 4903 | |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4904 | Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
| 4905 | Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart); |
| 4906 | Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4907 | // FIXME. For 64bit targets add 0 here. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4908 | Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4909 | : BuildIvarLayout(ID, true); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4910 | Values[ 4] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4911 | // const struct _method_list_t * const baseMethods; |
| 4912 | std::vector<llvm::Constant*> Methods; |
| 4913 | std::string MethodListName("\01l_OBJC_$_"); |
| 4914 | if (flags & CLS_META) { |
| 4915 | MethodListName += "CLASS_METHODS_" + ID->getNameAsString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4916 | for (ObjCImplementationDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4917 | i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4918 | // Class methods should always be defined. |
| 4919 | Methods.push_back(GetMethodConstant(*i)); |
| 4920 | } |
| 4921 | } else { |
| 4922 | MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4923 | for (ObjCImplementationDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4924 | i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4925 | // Instance methods should always be defined. |
| 4926 | Methods.push_back(GetMethodConstant(*i)); |
| 4927 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4928 | for (ObjCImplementationDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4929 | i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) { |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4930 | ObjCPropertyImplDecl *PID = *i; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4931 | |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4932 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){ |
| 4933 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4934 | |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4935 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 4936 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4937 | Methods.push_back(C); |
| 4938 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 4939 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4940 | Methods.push_back(C); |
| 4941 | } |
| 4942 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4943 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4944 | Values[ 5] = EmitMethodList(MethodListName, |
| 4945 | "__DATA, __objc_const", Methods); |
| 4946 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4947 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4948 | assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4949 | Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4950 | + OID->getName(), |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 4951 | OID->all_referenced_protocol_begin(), |
| 4952 | OID->all_referenced_protocol_end()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4953 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4954 | if (flags & CLS_META) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4955 | Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4956 | else |
| 4957 | Values[ 7] = EmitIvarList(ID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4958 | Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4959 | : BuildIvarLayout(ID, false); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4960 | if (flags & CLS_META) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4961 | Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4962 | else |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4963 | Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(), |
| 4964 | ID, ID->getClassInterface(), ObjCTypes); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 4965 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4966 | Values); |
| 4967 | llvm::GlobalVariable *CLASS_RO_GV = |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4968 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false, |
| 4969 | llvm::GlobalValue::InternalLinkage, |
| 4970 | Init, |
| 4971 | (flags & CLS_META) ? |
| 4972 | std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName : |
| 4973 | std::string("\01l_OBJC_CLASS_RO_$_")+ClassName); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4974 | CLASS_RO_GV->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 4975 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4976 | CLASS_RO_GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4977 | return CLASS_RO_GV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4978 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4979 | } |
| 4980 | |
| 4981 | /// BuildClassMetaData - This routine defines that to-level meta-data |
| 4982 | /// for the given ClassName for: |
| 4983 | /// struct _class_t { |
| 4984 | /// struct _class_t *isa; |
| 4985 | /// struct _class_t * const superclass; |
| 4986 | /// void *cache; |
| 4987 | /// IMP *vtable; |
| 4988 | /// struct class_ro_t *ro; |
| 4989 | /// } |
| 4990 | /// |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4991 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4992 | std::string &ClassName, |
| 4993 | llvm::Constant *IsAGV, |
| 4994 | llvm::Constant *SuperClassGV, |
| 4995 | llvm::Constant *ClassRoGV, |
| 4996 | bool HiddenVisibility) { |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 4997 | llvm::Constant *Values[] = { |
| 4998 | IsAGV, |
| 4999 | SuperClassGV, |
| 5000 | ObjCEmptyCacheVar, // &ObjCEmptyCacheVar |
| 5001 | ObjCEmptyVtableVar, // &ObjCEmptyVtableVar |
| 5002 | ClassRoGV // &CLASS_RO_GV |
| 5003 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5004 | if (!Values[1]) |
| 5005 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5006 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5007 | Values); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5008 | llvm::GlobalVariable *GV = GetClassGlobal(ClassName); |
| 5009 | GV->setInitializer(Init); |
Fariborz Jahanian | dd0db2a | 2009-01-31 01:07:39 +0000 | [diff] [blame] | 5010 | GV->setSection("__DATA, __objc_data"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5011 | GV->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5012 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy)); |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5013 | if (HiddenVisibility) |
| 5014 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5015 | return GV; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5016 | } |
| 5017 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5018 | bool |
Fariborz Jahanian | ecfbdcb | 2009-05-21 01:03:45 +0000 | [diff] [blame] | 5019 | CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5020 | return OD->getClassMethod(GetNullarySelector("load")) != 0; |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5021 | } |
| 5022 | |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 5023 | void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 5024 | uint32_t &InstanceStart, |
| 5025 | uint32_t &InstanceSize) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5026 | const ASTRecordLayout &RL = |
Daniel Dunbar | b4c79e0 | 2009-05-04 21:26:30 +0000 | [diff] [blame] | 5027 | CGM.getContext().getASTObjCImplementationLayout(OID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5028 | |
Daniel Dunbar | 6e8575b | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 5029 | // InstanceSize is really instance end. |
Ken Dyck | ec29903 | 2011-02-11 02:20:09 +0000 | [diff] [blame] | 5030 | InstanceSize = RL.getDataSize().getQuantity(); |
Daniel Dunbar | 6e8575b | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 5031 | |
| 5032 | // If there are no fields, the start is the same as the end. |
| 5033 | if (!RL.getFieldCount()) |
| 5034 | InstanceStart = InstanceSize; |
| 5035 | else |
Ken Dyck | fb67ccd | 2011-04-14 00:43:09 +0000 | [diff] [blame] | 5036 | InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth(); |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 5037 | } |
| 5038 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5039 | void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) { |
| 5040 | std::string ClassName = ID->getNameAsString(); |
| 5041 | if (!ObjCEmptyCacheVar) { |
| 5042 | ObjCEmptyCacheVar = new llvm::GlobalVariable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5043 | CGM.getModule(), |
| 5044 | ObjCTypes.CacheTy, |
| 5045 | false, |
| 5046 | llvm::GlobalValue::ExternalLinkage, |
| 5047 | 0, |
| 5048 | "_objc_empty_cache"); |
| 5049 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5050 | ObjCEmptyVtableVar = new llvm::GlobalVariable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5051 | CGM.getModule(), |
| 5052 | ObjCTypes.ImpnfABITy, |
| 5053 | false, |
| 5054 | llvm::GlobalValue::ExternalLinkage, |
| 5055 | 0, |
| 5056 | "_objc_empty_vtable"); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5057 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5058 | assert(ID->getClassInterface() && |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5059 | "CGObjCNonFragileABIMac::GenerateClass - class is 0"); |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 5060 | // FIXME: Is this correct (that meta class size is never computed)? |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5061 | uint32_t InstanceStart = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5062 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5063 | uint32_t InstanceSize = InstanceStart; |
| 5064 | uint32_t flags = CLS_META; |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5065 | std::string ObjCMetaClassName(getMetaclassSymbolPrefix()); |
| 5066 | std::string ObjCClassName(getClassSymbolPrefix()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5067 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5068 | llvm::GlobalVariable *SuperClassGV, *IsAGV; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5069 | |
| 5070 | bool classIsHidden = |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 5071 | ID->getClassInterface()->getVisibility() == HiddenVisibility; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5072 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5073 | flags |= OBJC2_CLS_HIDDEN; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5074 | if (ID->hasCXXStructors()) |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 5075 | flags |= eClassFlags_ABI2_HasCXXStructors; |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5076 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5077 | // class is root |
| 5078 | flags |= CLS_ROOT; |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5079 | SuperClassGV = GetClassGlobal(ObjCClassName + ClassName); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5080 | IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5081 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5082 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 5083 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 5084 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 5085 | Root = Super; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5086 | IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString()); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5087 | if (Root->isWeakImported()) |
Fariborz Jahanian | 0e93d25 | 2009-12-01 18:25:24 +0000 | [diff] [blame] | 5088 | IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 5089 | // work on super class metadata symbol. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5090 | std::string SuperClassName = |
Fariborz Jahanian | 0e93d25 | 2009-12-01 18:25:24 +0000 | [diff] [blame] | 5091 | ObjCMetaClassName + |
| 5092 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5093 | SuperClassGV = GetClassGlobal(SuperClassName); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5094 | if (ID->getClassInterface()->getSuperClass()->isWeakImported()) |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 5095 | SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5096 | } |
| 5097 | llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags, |
| 5098 | InstanceStart, |
| 5099 | InstanceSize,ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5100 | std::string TClassName = ObjCMetaClassName + ClassName; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5101 | llvm::GlobalVariable *MetaTClass = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5102 | BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV, |
| 5103 | classIsHidden); |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 5104 | DefinedMetaClasses.push_back(MetaTClass); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5105 | |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5106 | // Metadata for the class |
| 5107 | flags = CLS; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5108 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5109 | flags |= OBJC2_CLS_HIDDEN; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5110 | if (ID->hasCXXStructors()) |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 5111 | flags |= eClassFlags_ABI2_HasCXXStructors; |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5112 | |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 5113 | if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface())) |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5114 | flags |= CLS_EXCEPTION; |
| 5115 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5116 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5117 | flags |= CLS_ROOT; |
| 5118 | SuperClassGV = 0; |
Chris Lattner | b7b58b1 | 2009-04-19 06:02:28 +0000 | [diff] [blame] | 5119 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5120 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 5121 | std::string RootClassName = |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5122 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5123 | SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5124 | if (ID->getClassInterface()->getSuperClass()->isWeakImported()) |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 5125 | SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5126 | } |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 5127 | GetClassSizeInfo(ID, InstanceStart, InstanceSize); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5128 | CLASS_RO_GV = BuildClassRoTInitializer(flags, |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 5129 | InstanceStart, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5130 | InstanceSize, |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 5131 | ID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5132 | |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5133 | TClassName = ObjCClassName + ClassName; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5134 | llvm::GlobalVariable *ClassMD = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5135 | BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV, |
| 5136 | classIsHidden); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 5137 | DefinedClasses.push_back(ClassMD); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5138 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5139 | // Determine if this class is also "non-lazy". |
| 5140 | if (ImplementationIsNonLazy(ID)) |
| 5141 | DefinedNonLazyClasses.push_back(ClassMD); |
| 5142 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5143 | // Force the definition of the EHType if necessary. |
| 5144 | if (flags & CLS_EXCEPTION) |
| 5145 | GetInterfaceEHType(ID->getClassInterface(), true); |
Fariborz Jahanian | 64089ce | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 5146 | // Make sure method definition entries are all clear for next implementation. |
| 5147 | MethodDefinitions.clear(); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5148 | } |
| 5149 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5150 | /// GenerateProtocolRef - This routine is called to generate code for |
| 5151 | /// a protocol reference expression; as in: |
| 5152 | /// @code |
| 5153 | /// @protocol(Proto1); |
| 5154 | /// @endcode |
| 5155 | /// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1 |
| 5156 | /// which will hold address of the protocol meta-data. |
| 5157 | /// |
| 5158 | llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5159 | const ObjCProtocolDecl *PD) { |
| 5160 | |
Fariborz Jahanian | 960cd06 | 2009-04-10 18:47:34 +0000 | [diff] [blame] | 5161 | // This routine is called for @protocol only. So, we must build definition |
| 5162 | // of protocol's meta-data (not a reference to it!) |
| 5163 | // |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5164 | llvm::Constant *Init = |
| 5165 | llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD), |
Douglas Gregor | 4c86fdb | 2012-01-17 18:36:30 +0000 | [diff] [blame] | 5166 | ObjCTypes.getExternalProtocolPtrTy()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5167 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5168 | std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_"); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5169 | ProtocolName += PD->getName(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5170 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5171 | llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName); |
| 5172 | if (PTGV) |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 5173 | return Builder.CreateLoad(PTGV); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5174 | PTGV = new llvm::GlobalVariable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5175 | CGM.getModule(), |
| 5176 | Init->getType(), false, |
| 5177 | llvm::GlobalValue::WeakAnyLinkage, |
| 5178 | Init, |
| 5179 | ProtocolName); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5180 | PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip"); |
| 5181 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5182 | CGM.AddUsedGlobal(PTGV); |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 5183 | return Builder.CreateLoad(PTGV); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5184 | } |
| 5185 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5186 | /// GenerateCategory - Build metadata for a category implementation. |
| 5187 | /// struct _category_t { |
| 5188 | /// const char * const name; |
| 5189 | /// struct _class_t *const cls; |
| 5190 | /// const struct _method_list_t * const instance_methods; |
| 5191 | /// const struct _method_list_t * const class_methods; |
| 5192 | /// const struct _protocol_list_t * const protocols; |
| 5193 | /// const struct _prop_list_t * const properties; |
| 5194 | /// } |
| 5195 | /// |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5196 | void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5197 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5198 | const char *Prefix = "\01l_OBJC_$_CATEGORY_"; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5199 | std::string ExtCatName(Prefix + Interface->getNameAsString()+ |
| 5200 | "_$_" + OCD->getNameAsString()); |
| 5201 | std::string ExtClassName(getClassSymbolPrefix() + |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5202 | Interface->getNameAsString()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5203 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5204 | llvm::Constant *Values[6]; |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5205 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 5206 | // meta-class entry symbol |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5207 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5208 | if (Interface->isWeakImported()) |
Fariborz Jahanian | 2cdcc4c | 2009-11-17 22:02:21 +0000 | [diff] [blame] | 5209 | ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
| 5210 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5211 | Values[1] = ClassGV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5212 | std::vector<llvm::Constant*> Methods; |
| 5213 | std::string MethodListName(Prefix); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5214 | MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() + |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5215 | "_$_" + OCD->getNameAsString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5216 | |
| 5217 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5218 | i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5219 | // Instance methods should always be defined. |
| 5220 | Methods.push_back(GetMethodConstant(*i)); |
| 5221 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5222 | |
| 5223 | Values[2] = EmitMethodList(MethodListName, |
| 5224 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5225 | Methods); |
| 5226 | |
| 5227 | MethodListName = Prefix; |
| 5228 | MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" + |
| 5229 | OCD->getNameAsString(); |
| 5230 | Methods.clear(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5231 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5232 | i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5233 | // Class methods should always be defined. |
| 5234 | Methods.push_back(GetMethodConstant(*i)); |
| 5235 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5236 | |
| 5237 | Values[3] = EmitMethodList(MethodListName, |
| 5238 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5239 | Methods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5240 | const ObjCCategoryDecl *Category = |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 5241 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5242 | if (Category) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 5243 | SmallString<256> ExtName; |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5244 | llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_" |
| 5245 | << OCD->getName(); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5246 | Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5247 | + Interface->getName() + "_$_" |
| 5248 | + Category->getName(), |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5249 | Category->protocol_begin(), |
| 5250 | Category->protocol_end()); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5251 | Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(), |
| 5252 | OCD, Category, ObjCTypes); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5253 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5254 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 5255 | Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5256 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5257 | |
| 5258 | llvm::Constant *Init = |
| 5259 | llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy, |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5260 | Values); |
| 5261 | llvm::GlobalVariable *GCATV |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5262 | = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy, |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5263 | false, |
| 5264 | llvm::GlobalValue::InternalLinkage, |
| 5265 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5266 | ExtCatName); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5267 | GCATV->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5268 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5269 | GCATV->setSection("__DATA, __objc_const"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5270 | CGM.AddUsedGlobal(GCATV); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5271 | DefinedCategories.push_back(GCATV); |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5272 | |
| 5273 | // Determine if this category is also "non-lazy". |
| 5274 | if (ImplementationIsNonLazy(OCD)) |
| 5275 | DefinedNonLazyCategories.push_back(GCATV); |
Fariborz Jahanian | 64089ce | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 5276 | // method definition entries must be clear for next implementation. |
| 5277 | MethodDefinitions.clear(); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5278 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5279 | |
| 5280 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 5281 | /// given method if it has been defined. The result is null if the |
| 5282 | /// method has not been defined. The return value has type MethodPtrTy. |
| 5283 | llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5284 | const ObjCMethodDecl *MD) { |
Argyrios Kyrtzidis | 9d50c06 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 5285 | llvm::Function *Fn = GetMethodDefinition(MD); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5286 | if (!Fn) |
| 5287 | return 0; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5288 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5289 | llvm::Constant *Method[] = { |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 5290 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5291 | ObjCTypes.SelectorPtrTy), |
| 5292 | GetMethodVarType(MD), |
| 5293 | llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy) |
| 5294 | }; |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5295 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5296 | } |
| 5297 | |
| 5298 | /// EmitMethodList - Build meta-data for method declarations |
| 5299 | /// struct _method_list_t { |
| 5300 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 5301 | /// uint32_t method_count; |
| 5302 | /// struct _objc_method method_list[method_count]; |
| 5303 | /// } |
| 5304 | /// |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5305 | llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(Twine Name, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5306 | const char *Section, |
| 5307 | const ConstantVector &Methods) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5308 | // Return null for empty list. |
| 5309 | if (Methods.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5310 | return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5311 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5312 | llvm::Constant *Values[3]; |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5313 | // sizeof(struct _objc_method) |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5314 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5315 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5316 | // method_count |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5317 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5318 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5319 | Methods.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 5320 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5321 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5322 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5323 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5324 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5325 | llvm::GlobalValue::InternalLinkage, Init, Name); |
| 5326 | GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType())); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5327 | GV->setSection(Section); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5328 | CGM.AddUsedGlobal(GV); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5329 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5330 | } |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5331 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 5332 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 5333 | /// the given ivar. |
Daniel Dunbar | e83be12 | 2010-04-02 21:14:02 +0000 | [diff] [blame] | 5334 | llvm::GlobalVariable * |
| 5335 | CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID, |
| 5336 | const ObjCIvarDecl *Ivar) { |
| 5337 | const ObjCInterfaceDecl *Container = Ivar->getContainingInterface(); |
Daniel Dunbar | a81419d | 2009-05-05 00:36:57 +0000 | [diff] [blame] | 5338 | std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() + |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 5339 | '.' + Ivar->getNameAsString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5340 | llvm::GlobalVariable *IvarOffsetGV = |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 5341 | CGM.getModule().getGlobalVariable(Name); |
| 5342 | if (!IvarOffsetGV) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5343 | IvarOffsetGV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5344 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 5345 | false, |
| 5346 | llvm::GlobalValue::ExternalLinkage, |
| 5347 | 0, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5348 | Name); |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 5349 | return IvarOffsetGV; |
| 5350 | } |
| 5351 | |
Daniel Dunbar | e83be12 | 2010-04-02 21:14:02 +0000 | [diff] [blame] | 5352 | llvm::Constant * |
| 5353 | CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
| 5354 | const ObjCIvarDecl *Ivar, |
| 5355 | unsigned long int Offset) { |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 5356 | llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5357 | IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy, |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 5358 | Offset)); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5359 | IvarOffsetGV->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5360 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy)); |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 5361 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 5362 | // FIXME: This matches gcc, but shouldn't the visibility be set on the use as |
| 5363 | // well (i.e., in ObjCIvarOffsetVariable). |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 5364 | if (Ivar->getAccessControl() == ObjCIvarDecl::Private || |
| 5365 | Ivar->getAccessControl() == ObjCIvarDecl::Package || |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 5366 | ID->getVisibility() == HiddenVisibility) |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 5367 | IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 5368 | else |
Fariborz Jahanian | 77c9fd2 | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 5369 | IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Bill Wendling | 1f38251 | 2011-05-04 21:37:25 +0000 | [diff] [blame] | 5370 | IvarOffsetGV->setSection("__DATA, __objc_ivar"); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 5371 | return IvarOffsetGV; |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5372 | } |
| 5373 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5374 | /// EmitIvarList - Emit the ivar list for the given |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5375 | /// implementation. The return value has type |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5376 | /// IvarListnfABIPtrTy. |
| 5377 | /// struct _ivar_t { |
| 5378 | /// unsigned long int *offset; // pointer to ivar offset location |
| 5379 | /// char *name; |
| 5380 | /// char *type; |
| 5381 | /// uint32_t alignment; |
| 5382 | /// uint32_t size; |
| 5383 | /// } |
| 5384 | /// struct _ivar_list_t { |
| 5385 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 5386 | /// uint32 count; |
| 5387 | /// struct _iver_t list[count]; |
| 5388 | /// } |
| 5389 | /// |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 5390 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5391 | llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5392 | const ObjCImplementationDecl *ID) { |
| 5393 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5394 | std::vector<llvm::Constant*> Ivars; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5395 | |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 5396 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5397 | assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5398 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5399 | // FIXME. Consolidate this with similar code in GenerateClass. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5400 | |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 5401 | for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin(); |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 5402 | IVD; IVD = IVD->getNextIvar()) { |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 5403 | // Ignore unnamed bit-fields. |
| 5404 | if (!IVD->getDeclName()) |
| 5405 | continue; |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5406 | llvm::Constant *Ivar[5]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5407 | Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD, |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 5408 | ComputeIvarBaseOffset(CGM, ID, IVD)); |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 5409 | Ivar[1] = GetMethodVarName(IVD->getIdentifier()); |
| 5410 | Ivar[2] = GetMethodVarType(IVD); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5411 | llvm::Type *FieldTy = |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 5412 | CGM.getTypes().ConvertTypeForMem(IVD->getType()); |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5413 | unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5414 | unsigned Align = CGM.getContext().getPreferredTypeAlign( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5415 | IVD->getType().getTypePtr()) >> 3; |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5416 | Align = llvm::Log2_32(Align); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5417 | Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align); |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 5418 | // NOTE. Size of a bitfield does not match gcc's, because of the |
| 5419 | // way bitfields are treated special in each. But I am told that |
| 5420 | // 'size' for bitfield ivars is ignored by the runtime so it does |
| 5421 | // not matter. If it matters, there is enough info to get the |
| 5422 | // bitfield right! |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5423 | Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5424 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar)); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5425 | } |
| 5426 | // Return null for empty list. |
| 5427 | if (Ivars.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5428 | return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5429 | |
| 5430 | llvm::Constant *Values[3]; |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5431 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5432 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 5433 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5434 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5435 | Ivars.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 5436 | Values[2] = llvm::ConstantArray::get(AT, Ivars); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5437 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5438 | const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_"; |
| 5439 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5440 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5441 | llvm::GlobalValue::InternalLinkage, |
| 5442 | Init, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5443 | Prefix + OID->getName()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5444 | GV->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5445 | CGM.getTargetData().getABITypeAlignment(Init->getType())); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5446 | GV->setSection("__DATA, __objc_const"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5447 | |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5448 | CGM.AddUsedGlobal(GV); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 5449 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5450 | } |
| 5451 | |
| 5452 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5453 | const ObjCProtocolDecl *PD) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5454 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5455 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5456 | if (!Entry) { |
| 5457 | // We use the initializer as a marker of whether this is a forward |
| 5458 | // reference or not. At module finalization we add the empty |
| 5459 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5460 | Entry = |
| 5461 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false, |
| 5462 | llvm::GlobalValue::ExternalLinkage, |
| 5463 | 0, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5464 | "\01l_OBJC_PROTOCOL_$_" + PD->getName()); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5465 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5466 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5467 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5468 | return Entry; |
| 5469 | } |
| 5470 | |
| 5471 | /// GetOrEmitProtocol - Generate the protocol meta-data: |
| 5472 | /// @code |
| 5473 | /// struct _protocol_t { |
| 5474 | /// id isa; // NULL |
| 5475 | /// const char * const protocol_name; |
| 5476 | /// const struct _protocol_list_t * protocol_list; // super protocols |
| 5477 | /// const struct method_list_t * const instance_methods; |
| 5478 | /// const struct method_list_t * const class_methods; |
| 5479 | /// const struct method_list_t *optionalInstanceMethods; |
| 5480 | /// const struct method_list_t *optionalClassMethods; |
| 5481 | /// const struct _prop_list_t * properties; |
| 5482 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 5483 | /// const uint32_t flags; // = 0 |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5484 | /// const char ** extendedMethodTypes; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5485 | /// } |
| 5486 | /// @endcode |
| 5487 | /// |
| 5488 | |
| 5489 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5490 | const ObjCProtocolDecl *PD) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5491 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5492 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5493 | // Early exit if a defining object has already been generated. |
| 5494 | if (Entry && Entry->hasInitializer()) |
| 5495 | return Entry; |
| 5496 | |
Douglas Gregor | 1d784b2 | 2012-01-01 19:51:50 +0000 | [diff] [blame] | 5497 | // Use the protocol definition, if there is one. |
| 5498 | if (const ObjCProtocolDecl *Def = PD->getDefinition()) |
| 5499 | PD = Def; |
| 5500 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5501 | // Construct method lists. |
| 5502 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 5503 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5504 | std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5505 | for (ObjCProtocolDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5506 | i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5507 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5508 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5509 | if (!C) |
| 5510 | return GetOrEmitProtocolRef(PD); |
| 5511 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5512 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 5513 | OptInstanceMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5514 | OptMethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5515 | } else { |
| 5516 | InstanceMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5517 | MethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5518 | } |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5519 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5520 | |
| 5521 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5522 | i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5523 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5524 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5525 | if (!C) |
| 5526 | return GetOrEmitProtocolRef(PD); |
| 5527 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5528 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 5529 | OptClassMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5530 | OptMethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5531 | } else { |
| 5532 | ClassMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5533 | MethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5534 | } |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5535 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5536 | |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5537 | MethodTypesExt.insert(MethodTypesExt.end(), |
| 5538 | OptMethodTypesExt.begin(), OptMethodTypesExt.end()); |
| 5539 | |
| 5540 | llvm::Constant *Values[11]; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5541 | // isa is NULL |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5542 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5543 | Values[1] = GetClassName(PD->getIdentifier()); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5544 | Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(), |
| 5545 | PD->protocol_begin(), |
| 5546 | PD->protocol_end()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5547 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5548 | Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5549 | + PD->getName(), |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5550 | "__DATA, __objc_const", |
| 5551 | InstanceMethods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5552 | Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5553 | + PD->getName(), |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5554 | "__DATA, __objc_const", |
| 5555 | ClassMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5556 | Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5557 | + PD->getName(), |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5558 | "__DATA, __objc_const", |
| 5559 | OptInstanceMethods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5560 | Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5561 | + PD->getName(), |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5562 | "__DATA, __objc_const", |
| 5563 | OptClassMethods); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5564 | Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(), |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5565 | 0, PD, ObjCTypes); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5566 | uint32_t Size = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5567 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5568 | Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5569 | Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5570 | Values[10] = EmitProtocolMethodTypes("\01l_OBJC_$_PROTOCOL_METHOD_TYPES_" |
| 5571 | + PD->getName(), |
| 5572 | MethodTypesExt, ObjCTypes); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5573 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5574 | Values); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5575 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5576 | if (Entry) { |
| 5577 | // Already created, fix the linkage and update the initializer. |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 5578 | Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5579 | Entry->setInitializer(Init); |
| 5580 | } else { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5581 | Entry = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5582 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, |
| 5583 | false, llvm::GlobalValue::WeakAnyLinkage, Init, |
| 5584 | "\01l_OBJC_PROTOCOL_$_" + PD->getName()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5585 | Entry->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5586 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5587 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5588 | } |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 5589 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5590 | CGM.AddUsedGlobal(Entry); |
| 5591 | |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 5592 | // Use this protocol meta-data to build protocol list table in section |
| 5593 | // __DATA, __objc_protolist |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5594 | llvm::GlobalVariable *PTGV = |
| 5595 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy, |
| 5596 | false, llvm::GlobalValue::WeakAnyLinkage, Entry, |
| 5597 | "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5598 | PTGV->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5599 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy)); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 5600 | PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip"); |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 5601 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5602 | CGM.AddUsedGlobal(PTGV); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5603 | return Entry; |
| 5604 | } |
| 5605 | |
| 5606 | /// EmitProtocolList - Generate protocol list meta-data: |
| 5607 | /// @code |
| 5608 | /// struct _protocol_list_t { |
| 5609 | /// long protocol_count; // Note, this is 32/64 bit |
| 5610 | /// struct _protocol_t[protocol_count]; |
| 5611 | /// } |
| 5612 | /// @endcode |
| 5613 | /// |
| 5614 | llvm::Constant * |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5615 | CGObjCNonFragileABIMac::EmitProtocolList(Twine Name, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5616 | ObjCProtocolDecl::protocol_iterator begin, |
| 5617 | ObjCProtocolDecl::protocol_iterator end) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5618 | std::vector<llvm::Constant*> ProtocolRefs; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5619 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5620 | // Just return null for empty protocol lists |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5621 | if (begin == end) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5622 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5623 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5624 | // FIXME: We shouldn't need to do this lookup here, should we? |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 5625 | SmallString<256> TmpName; |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5626 | Name.toVector(TmpName); |
| 5627 | llvm::GlobalVariable *GV = |
| 5628 | CGM.getModule().getGlobalVariable(TmpName.str(), true); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5629 | if (GV) |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5630 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5631 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5632 | for (; begin != end; ++begin) |
| 5633 | ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented??? |
| 5634 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5635 | // This list is null terminated. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5636 | ProtocolRefs.push_back(llvm::Constant::getNullValue( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5637 | ObjCTypes.ProtocolnfABIPtrTy)); |
| 5638 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5639 | llvm::Constant *Values[2]; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 5640 | Values[0] = |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5641 | llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5642 | Values[1] = |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 5643 | llvm::ConstantArray::get( |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5644 | llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5645 | ProtocolRefs.size()), |
| 5646 | ProtocolRefs); |
| 5647 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5648 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5649 | GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5650 | llvm::GlobalValue::InternalLinkage, |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5651 | Init, Name); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5652 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5653 | GV->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5654 | CGM.getTargetData().getABITypeAlignment(Init->getType())); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5655 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5656 | return llvm::ConstantExpr::getBitCast(GV, |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5657 | ObjCTypes.ProtocolListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5658 | } |
| 5659 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5660 | /// GetMethodDescriptionConstant - This routine build following meta-data: |
| 5661 | /// struct _objc_method { |
| 5662 | /// SEL _cmd; |
| 5663 | /// char *method_type; |
| 5664 | /// char *_imp; |
| 5665 | /// } |
| 5666 | |
| 5667 | llvm::Constant * |
| 5668 | CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5669 | llvm::Constant *Desc[3]; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 5670 | Desc[0] = |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5671 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 5672 | ObjCTypes.SelectorPtrTy); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5673 | Desc[1] = GetMethodVarType(MD); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5674 | if (!Desc[1]) |
| 5675 | return 0; |
| 5676 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5677 | // Protocol methods have no implementation. So, this entry is always NULL. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5678 | Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5679 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5680 | } |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 5681 | |
| 5682 | /// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference. |
| 5683 | /// This code gen. amounts to generating code for: |
| 5684 | /// @code |
| 5685 | /// (type *)((char *)base + _OBJC_IVAR_$_.ivar; |
| 5686 | /// @encode |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5687 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 5688 | LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar( |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 5689 | CodeGen::CodeGenFunction &CGF, |
| 5690 | QualType ObjectTy, |
| 5691 | llvm::Value *BaseValue, |
| 5692 | const ObjCIvarDecl *Ivar, |
| 5693 | unsigned CVRQualifiers) { |
| 5694 | ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 5695 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 5696 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 5697 | } |
| 5698 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 5699 | llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5700 | CodeGen::CodeGenFunction &CGF, |
| 5701 | const ObjCInterfaceDecl *Interface, |
| 5702 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 2da84ff | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 5703 | return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar"); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 5704 | } |
| 5705 | |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5706 | static void appendSelectorForMessageRefTable(std::string &buffer, |
| 5707 | Selector selector) { |
| 5708 | if (selector.isUnarySelector()) { |
| 5709 | buffer += selector.getNameForSlot(0); |
| 5710 | return; |
| 5711 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5712 | |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5713 | for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) { |
| 5714 | buffer += selector.getNameForSlot(i); |
| 5715 | buffer += '_'; |
| 5716 | } |
| 5717 | } |
| 5718 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5719 | /// Emit a "v-table" message send. We emit a weak hidden-visibility |
| 5720 | /// struct, initially containing the selector pointer and a pointer to |
| 5721 | /// a "fixup" variant of the appropriate objc_msgSend. To call, we |
| 5722 | /// load and call the function pointer, passing the address of the |
| 5723 | /// struct as the second parameter. The runtime determines whether |
| 5724 | /// the selector is currently emitted using vtable dispatch; if so, it |
| 5725 | /// substitutes a stub function which simply tail-calls through the |
| 5726 | /// appropriate vtable slot, and if not, it substitues a stub function |
| 5727 | /// which tail-calls objc_msgSend. Both stubs adjust the selector |
| 5728 | /// argument to correctly point to the selector. |
| 5729 | RValue |
| 5730 | CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF, |
| 5731 | ReturnValueSlot returnSlot, |
| 5732 | QualType resultType, |
| 5733 | Selector selector, |
| 5734 | llvm::Value *arg0, |
| 5735 | QualType arg0Type, |
| 5736 | bool isSuper, |
| 5737 | const CallArgList &formalArgs, |
| 5738 | const ObjCMethodDecl *method) { |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5739 | // Compute the actual arguments. |
| 5740 | CallArgList args; |
| 5741 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5742 | // First argument: the receiver / super-call structure. |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5743 | if (!isSuper) |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5744 | arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy); |
| 5745 | args.add(RValue::get(arg0), arg0Type); |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5746 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5747 | // Second argument: a pointer to the message ref structure. Leave |
| 5748 | // the actual argument value blank for now. |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5749 | args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy); |
| 5750 | |
| 5751 | args.insert(args.end(), formalArgs.begin(), formalArgs.end()); |
| 5752 | |
| 5753 | const CGFunctionInfo &fnInfo = |
| 5754 | CGM.getTypes().getFunctionInfo(resultType, args, |
| 5755 | FunctionType::ExtInfo()); |
| 5756 | |
John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 5757 | NullReturnState nullReturn; |
| 5758 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5759 | // Find the function to call and the mangled name for the message |
| 5760 | // ref structure. Using a different mangled name wouldn't actually |
| 5761 | // be a problem; it would just be a waste. |
| 5762 | // |
| 5763 | // The runtime currently never uses vtable dispatch for anything |
| 5764 | // except normal, non-super message-sends. |
| 5765 | // FIXME: don't use this for that. |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5766 | llvm::Constant *fn = 0; |
| 5767 | std::string messageRefName("\01l_"); |
| 5768 | if (CGM.ReturnTypeUsesSRet(fnInfo)) { |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5769 | if (isSuper) { |
| 5770 | fn = ObjCTypes.getMessageSendSuper2StretFixupFn(); |
| 5771 | messageRefName += "objc_msgSendSuper2_stret_fixup"; |
Chris Lattner | 9b7d670 | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 5772 | } else { |
John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 5773 | nullReturn.init(CGF, arg0); |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5774 | fn = ObjCTypes.getMessageSendStretFixupFn(); |
| 5775 | messageRefName += "objc_msgSend_stret_fixup"; |
Chris Lattner | 9b7d670 | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 5776 | } |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5777 | } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) { |
| 5778 | fn = ObjCTypes.getMessageSendFpretFixupFn(); |
| 5779 | messageRefName += "objc_msgSend_fpret_fixup"; |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5780 | } else { |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5781 | if (isSuper) { |
| 5782 | fn = ObjCTypes.getMessageSendSuper2FixupFn(); |
| 5783 | messageRefName += "objc_msgSendSuper2_fixup"; |
Chris Lattner | 9b7d670 | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 5784 | } else { |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5785 | fn = ObjCTypes.getMessageSendFixupFn(); |
| 5786 | messageRefName += "objc_msgSend_fixup"; |
Chris Lattner | 9b7d670 | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 5787 | } |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5788 | } |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5789 | assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend"); |
| 5790 | messageRefName += '_'; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5791 | |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5792 | // Append the selector name, except use underscores anywhere we |
| 5793 | // would have used colons. |
| 5794 | appendSelectorForMessageRefTable(messageRefName, selector); |
| 5795 | |
| 5796 | llvm::GlobalVariable *messageRef |
| 5797 | = CGM.getModule().getGlobalVariable(messageRefName); |
| 5798 | if (!messageRef) { |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5799 | // Build the message ref structure. |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5800 | llvm::Constant *values[] = { fn, GetMethodVarName(selector) }; |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5801 | llvm::Constant *init = llvm::ConstantStruct::getAnon(values); |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5802 | messageRef = new llvm::GlobalVariable(CGM.getModule(), |
| 5803 | init->getType(), |
| 5804 | /*constant*/ false, |
| 5805 | llvm::GlobalValue::WeakAnyLinkage, |
| 5806 | init, |
| 5807 | messageRefName); |
| 5808 | messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 5809 | messageRef->setAlignment(16); |
| 5810 | messageRef->setSection("__DATA, __objc_msgrefs, coalesced"); |
| 5811 | } |
Fariborz Jahanian | 3c52d36 | 2012-01-30 23:39:30 +0000 | [diff] [blame] | 5812 | |
| 5813 | bool requiresnullCheck = false; |
| 5814 | if (CGM.getLangOptions().ObjCAutoRefCount && method) |
| 5815 | for (ObjCMethodDecl::param_const_iterator i = method->param_begin(), |
| 5816 | e = method->param_end(); i != e; ++i) { |
| 5817 | const ParmVarDecl *ParamDecl = (*i); |
| 5818 | if (ParamDecl->hasAttr<NSConsumedAttr>()) { |
| 5819 | if (!nullReturn.NullBB) |
| 5820 | nullReturn.init(CGF, arg0); |
| 5821 | requiresnullCheck = true; |
| 5822 | break; |
| 5823 | } |
| 5824 | } |
| 5825 | |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5826 | llvm::Value *mref = |
| 5827 | CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy); |
| 5828 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5829 | // Update the message ref argument. |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5830 | args[1].RV = RValue::get(mref); |
| 5831 | |
| 5832 | // Load the function to call from the message ref table. |
| 5833 | llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0); |
| 5834 | callee = CGF.Builder.CreateLoad(callee, "msgSend_fn"); |
| 5835 | |
| 5836 | bool variadic = method ? method->isVariadic() : false; |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5837 | llvm::FunctionType *fnType = |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5838 | CGF.getTypes().GetFunctionType(fnInfo, variadic); |
| 5839 | callee = CGF.Builder.CreateBitCast(callee, |
| 5840 | llvm::PointerType::getUnqual(fnType)); |
| 5841 | |
John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 5842 | RValue result = CGF.EmitCall(fnInfo, callee, returnSlot, args); |
Fariborz Jahanian | 3c52d36 | 2012-01-30 23:39:30 +0000 | [diff] [blame] | 5843 | return nullReturn.complete(CGF, result, resultType, formalArgs, |
| 5844 | requiresnullCheck ? method : 0); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5845 | } |
| 5846 | |
| 5847 | /// Generate code for a message send expression in the nonfragile abi. |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5848 | CodeGen::RValue |
| 5849 | CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 5850 | ReturnValueSlot Return, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5851 | QualType ResultType, |
| 5852 | Selector Sel, |
| 5853 | llvm::Value *Receiver, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5854 | const CallArgList &CallArgs, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 5855 | const ObjCInterfaceDecl *Class, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5856 | const ObjCMethodDecl *Method) { |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5857 | return isVTableDispatchedSelector(Sel) |
| 5858 | ? EmitVTableMessageSend(CGF, Return, ResultType, Sel, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5859 | Receiver, CGF.getContext().getObjCIdType(), |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5860 | false, CallArgs, Method) |
| 5861 | : EmitMessageSend(CGF, Return, ResultType, |
| 5862 | EmitSelector(CGF.Builder, Sel), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5863 | Receiver, CGF.getContext().getObjCIdType(), |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5864 | false, CallArgs, Method, ObjCTypes); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5865 | } |
| 5866 | |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5867 | llvm::GlobalVariable * |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5868 | CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5869 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5870 | |
Daniel Dunbar | dfff230 | 2009-03-02 05:18:14 +0000 | [diff] [blame] | 5871 | if (!GV) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5872 | GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5873 | false, llvm::GlobalValue::ExternalLinkage, |
| 5874 | 0, Name); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5875 | } |
| 5876 | |
| 5877 | return GV; |
| 5878 | } |
| 5879 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5880 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder, |
| 5881 | IdentifierInfo *II) { |
| 5882 | llvm::GlobalVariable *&Entry = ClassReferences[II]; |
| 5883 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5884 | if (!Entry) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5885 | std::string ClassName(getClassSymbolPrefix() + II->getName().str()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5886 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5887 | Entry = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5888 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, |
| 5889 | false, llvm::GlobalValue::InternalLinkage, |
| 5890 | ClassGV, |
| 5891 | "\01L_OBJC_CLASSLIST_REFERENCES_$_"); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5892 | Entry->setAlignment( |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5893 | CGM.getTargetData().getABITypeAlignment( |
| 5894 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5895 | Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5896 | CGM.AddUsedGlobal(Entry); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5897 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5898 | |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 5899 | return Builder.CreateLoad(Entry); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5900 | } |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5901 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5902 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder, |
| 5903 | const ObjCInterfaceDecl *ID) { |
| 5904 | return EmitClassRefFromId(Builder, ID->getIdentifier()); |
| 5905 | } |
| 5906 | |
| 5907 | llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef( |
| 5908 | CGBuilderTy &Builder) { |
| 5909 | IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool"); |
| 5910 | return EmitClassRefFromId(Builder, II); |
| 5911 | } |
| 5912 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5913 | llvm::Value * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5914 | CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5915 | const ObjCInterfaceDecl *ID) { |
| 5916 | llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5917 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5918 | if (!Entry) { |
| 5919 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
| 5920 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5921 | Entry = |
| 5922 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5923 | false, llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5924 | ClassGV, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5925 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_"); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5926 | Entry->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5927 | CGM.getTargetData().getABITypeAlignment( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5928 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5929 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5930 | CGM.AddUsedGlobal(Entry); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5931 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5932 | |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 5933 | return Builder.CreateLoad(Entry); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5934 | } |
| 5935 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5936 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 5937 | /// meta-data |
| 5938 | /// |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5939 | llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder, |
| 5940 | const ObjCInterfaceDecl *ID) { |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5941 | llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()]; |
| 5942 | if (Entry) |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 5943 | return Builder.CreateLoad(Entry); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5944 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5945 | std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5946 | llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5947 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5948 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5949 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5950 | MetaClassGV, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5951 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_"); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5952 | Entry->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5953 | CGM.getTargetData().getABITypeAlignment( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5954 | ObjCTypes.ClassnfABIPtrTy)); |
| 5955 | |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5956 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5957 | CGM.AddUsedGlobal(Entry); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5958 | |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 5959 | return Builder.CreateLoad(Entry); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5960 | } |
| 5961 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5962 | /// GetClass - Return a reference to the class for the given interface |
| 5963 | /// decl. |
| 5964 | llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder, |
| 5965 | const ObjCInterfaceDecl *ID) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5966 | if (ID->isWeakImported()) { |
Fariborz Jahanian | 269f8bc | 2009-11-17 22:42:00 +0000 | [diff] [blame] | 5967 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
| 5968 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
| 5969 | ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
| 5970 | } |
| 5971 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5972 | return EmitClassRef(Builder, ID); |
| 5973 | } |
| 5974 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5975 | /// Generates a message send where the super is the receiver. This is |
| 5976 | /// a message send to self with special delivery semantics indicating |
| 5977 | /// which class's method should be called. |
| 5978 | CodeGen::RValue |
| 5979 | CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 5980 | ReturnValueSlot Return, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5981 | QualType ResultType, |
| 5982 | Selector Sel, |
| 5983 | const ObjCInterfaceDecl *Class, |
| 5984 | bool isCategoryImpl, |
| 5985 | llvm::Value *Receiver, |
| 5986 | bool IsClassMessage, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5987 | const CodeGen::CallArgList &CallArgs, |
| 5988 | const ObjCMethodDecl *Method) { |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5989 | // ... |
| 5990 | // Create and init a super structure; this is a (receiver, class) |
| 5991 | // pair we will pass to objc_msgSendSuper. |
| 5992 | llvm::Value *ObjCSuper = |
John McCall | 604da29 | 2011-03-04 08:00:29 +0000 | [diff] [blame] | 5993 | CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5994 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5995 | llvm::Value *ReceiverAsObject = |
| 5996 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 5997 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 5998 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5999 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6000 | // 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] | 6001 | llvm::Value *Target; |
| 6002 | if (IsClassMessage) { |
| 6003 | if (isCategoryImpl) { |
| 6004 | // Message sent to "super' in a class method defined in |
| 6005 | // a category implementation. |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6006 | Target = EmitClassRef(CGF.Builder, Class); |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 6007 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 6008 | Target = CGF.Builder.CreateLoad(Target); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 6009 | } else |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 6010 | Target = EmitMetaClassRef(CGF.Builder, Class); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 6011 | } else |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 6012 | Target = EmitSuperClassRef(CGF.Builder, Class); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6013 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 6014 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext and |
| 6015 | // ObjCTypes types. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6016 | llvm::Type *ClassTy = |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6017 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
| 6018 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
| 6019 | CGF.Builder.CreateStore(Target, |
| 6020 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6021 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6022 | return (isVTableDispatchedSelector(Sel)) |
| 6023 | ? EmitVTableMessageSend(CGF, Return, ResultType, Sel, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6024 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6025 | true, CallArgs, Method) |
| 6026 | : EmitMessageSend(CGF, Return, ResultType, |
| 6027 | EmitSelector(CGF.Builder, Sel), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6028 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 6029 | true, CallArgs, Method, ObjCTypes); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 6030 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6031 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6032 | llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder, |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 6033 | Selector Sel, bool lval) { |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6034 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6035 | |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6036 | if (!Entry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6037 | llvm::Constant *Casted = |
| 6038 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 6039 | ObjCTypes.SelectorPtrTy); |
| 6040 | Entry = |
| 6041 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false, |
| 6042 | llvm::GlobalValue::InternalLinkage, |
| 6043 | Casted, "\01L_OBJC_SELECTOR_REFERENCES_"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 6044 | Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 6045 | CGM.AddUsedGlobal(Entry); |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6046 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6047 | |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 6048 | if (lval) |
| 6049 | return Entry; |
Pete Cooper | b6d7114 | 2011-11-10 21:45:06 +0000 | [diff] [blame] | 6050 | llvm::LoadInst* LI = Builder.CreateLoad(Entry); |
| 6051 | |
| 6052 | LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"), |
| 6053 | llvm::MDNode::get(VMContext, |
| 6054 | ArrayRef<llvm::Value*>())); |
| 6055 | return LI; |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6056 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6057 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 6058 | /// objc_assign_ivar (id src, id *dst, ptrdiff_t) |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6059 | /// |
| 6060 | void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6061 | llvm::Value *src, |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 6062 | llvm::Value *dst, |
| 6063 | llvm::Value *ivarOffset) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6064 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6065 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 6066 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6067 | assert(Size <= 8 && "does not support size > 8"); |
| 6068 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 6069 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 6070 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 6071 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6072 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 6073 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 6074 | CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(), |
| 6075 | src, dst, ivarOffset); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6076 | return; |
| 6077 | } |
| 6078 | |
| 6079 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 6080 | /// objc_assign_strongCast (id src, id *dst) |
| 6081 | /// |
| 6082 | void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6083 | CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6084 | llvm::Value *src, llvm::Value *dst) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6085 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6086 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 6087 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6088 | assert(Size <= 8 && "does not support size > 8"); |
| 6089 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6090 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 6091 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 6092 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6093 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 6094 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 6095 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6096 | src, dst, "weakassign"); |
| 6097 | return; |
| 6098 | } |
| 6099 | |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 6100 | void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6101 | CodeGen::CodeGenFunction &CGF, |
| 6102 | llvm::Value *DestPtr, |
| 6103 | llvm::Value *SrcPtr, |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 6104 | llvm::Value *Size) { |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 6105 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy); |
| 6106 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 6107 | CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(), |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 6108 | DestPtr, SrcPtr, Size); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 6109 | return; |
| 6110 | } |
| 6111 | |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6112 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
| 6113 | /// object: objc_read_weak (id *src) |
| 6114 | /// |
| 6115 | llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6116 | CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6117 | llvm::Value *AddrWeakObj) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6118 | llvm::Type* DestTy = |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6119 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
| 6120 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 6121 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6122 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 6123 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6124 | return read_weak; |
| 6125 | } |
| 6126 | |
| 6127 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 6128 | /// objc_assign_weak (id src, id *dst) |
| 6129 | /// |
| 6130 | void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6131 | llvm::Value *src, llvm::Value *dst) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6132 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6133 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 6134 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6135 | assert(Size <= 8 && "does not support size > 8"); |
| 6136 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 6137 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 6138 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 6139 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6140 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 6141 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 6142 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6143 | src, dst, "weakassign"); |
| 6144 | return; |
| 6145 | } |
| 6146 | |
| 6147 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 6148 | /// objc_assign_global (id src, id *dst) |
| 6149 | /// |
| 6150 | void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 6151 | llvm::Value *src, llvm::Value *dst, |
| 6152 | bool threadlocal) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6153 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6154 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 6155 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6156 | assert(Size <= 8 && "does not support size > 8"); |
| 6157 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 6158 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 6159 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 6160 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6161 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 6162 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 6163 | if (!threadlocal) |
| 6164 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
| 6165 | src, dst, "globalassign"); |
| 6166 | else |
| 6167 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(), |
| 6168 | src, dst, "threadlocalassign"); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6169 | return; |
| 6170 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6171 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6172 | void |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6173 | CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 6174 | const ObjCAtSynchronizedStmt &S) { |
David Chisnall | 05dc91b | 2011-03-25 17:46:35 +0000 | [diff] [blame] | 6175 | EmitAtSynchronizedStmt(CGF, S, |
| 6176 | cast<llvm::Function>(ObjCTypes.getSyncEnterFn()), |
| 6177 | cast<llvm::Function>(ObjCTypes.getSyncExitFn())); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6178 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6179 | |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6180 | llvm::Constant * |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 6181 | CGObjCNonFragileABIMac::GetEHType(QualType T) { |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6182 | // There's a particular fixed type info for 'id'. |
| 6183 | if (T->isObjCIdType() || |
| 6184 | T->isObjCQualifiedIdType()) { |
| 6185 | llvm::Constant *IDEHType = |
| 6186 | CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id"); |
| 6187 | if (!IDEHType) |
| 6188 | IDEHType = |
| 6189 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, |
| 6190 | false, |
| 6191 | llvm::GlobalValue::ExternalLinkage, |
| 6192 | 0, "OBJC_EHTYPE_id"); |
| 6193 | return IDEHType; |
| 6194 | } |
| 6195 | |
| 6196 | // All other types should be Objective-C interface pointer types. |
| 6197 | const ObjCObjectPointerType *PT = |
| 6198 | T->getAs<ObjCObjectPointerType>(); |
| 6199 | assert(PT && "Invalid @catch type."); |
| 6200 | const ObjCInterfaceType *IT = PT->getInterfaceType(); |
| 6201 | assert(IT && "Invalid @catch type."); |
| 6202 | return GetInterfaceEHType(IT->getDecl(), false); |
| 6203 | } |
| 6204 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6205 | void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
| 6206 | const ObjCAtTryStmt &S) { |
David Chisnall | 05dc91b | 2011-03-25 17:46:35 +0000 | [diff] [blame] | 6207 | EmitTryCatchStmt(CGF, S, |
| 6208 | cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()), |
| 6209 | cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()), |
| 6210 | cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn())); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 6211 | } |
| 6212 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6213 | /// EmitThrowStmt - Generate code for a throw statement. |
| 6214 | void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 6215 | const ObjCAtThrowStmt &S) { |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6216 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
John McCall | 2b014d6 | 2011-10-01 10:32:24 +0000 | [diff] [blame] | 6217 | llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr); |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 6218 | Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy); |
Jay Foad | 4c7d9f1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 6219 | CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception) |
John McCall | 7ec404c | 2010-10-16 08:21:07 +0000 | [diff] [blame] | 6220 | .setDoesNotReturn(); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6221 | } else { |
Jay Foad | 4c7d9f1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 6222 | CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn()) |
John McCall | 7ec404c | 2010-10-16 08:21:07 +0000 | [diff] [blame] | 6223 | .setDoesNotReturn(); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6224 | } |
| 6225 | |
John McCall | 7ec404c | 2010-10-16 08:21:07 +0000 | [diff] [blame] | 6226 | CGF.Builder.CreateUnreachable(); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6227 | CGF.Builder.ClearInsertionPoint(); |
| 6228 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6229 | |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6230 | llvm::Constant * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6231 | CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6232 | bool ForDefinition) { |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6233 | llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()]; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6234 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6235 | // If we don't need a definition, return the entry if found or check |
| 6236 | // if we use an external reference. |
| 6237 | if (!ForDefinition) { |
| 6238 | if (Entry) |
| 6239 | return Entry; |
Daniel Dunbar | 7e075cb | 2009-04-07 06:43:45 +0000 | [diff] [blame] | 6240 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6241 | // If this type (or a super class) has the __objc_exception__ |
| 6242 | // attribute, emit an external reference. |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 6243 | if (hasObjCExceptionAttribute(CGM.getContext(), ID)) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6244 | return Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6245 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false, |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6246 | llvm::GlobalValue::ExternalLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6247 | 0, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6248 | ("OBJC_EHTYPE_$_" + |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 6249 | ID->getIdentifier()->getName())); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6250 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6251 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6252 | // Otherwise we need to either make a new entry or fill in the |
| 6253 | // initializer. |
| 6254 | assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition"); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 6255 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6256 | std::string VTableName = "objc_ehtype_vtable"; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6257 | llvm::GlobalVariable *VTableGV = |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6258 | CGM.getModule().getGlobalVariable(VTableName); |
| 6259 | if (!VTableGV) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6260 | VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy, |
| 6261 | false, |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6262 | llvm::GlobalValue::ExternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6263 | 0, VTableName); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6264 | |
Chris Lattner | 8b41868 | 2012-02-07 00:39:47 +0000 | [diff] [blame^] | 6265 | llvm::Value *VTableIdx = llvm::ConstantInt::get(CGM.Int32Ty, 2); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6266 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 6267 | llvm::Constant *Values[] = { |
| 6268 | llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx), |
| 6269 | GetClassName(ID->getIdentifier()), |
| 6270 | GetClassGlobal(ClassName) |
| 6271 | }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 6272 | llvm::Constant *Init = |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 6273 | llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6274 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6275 | if (Entry) { |
| 6276 | Entry->setInitializer(Init); |
| 6277 | } else { |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6278 | Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false, |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6279 | llvm::GlobalValue::WeakAnyLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6280 | Init, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6281 | ("OBJC_EHTYPE_$_" + |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 6282 | ID->getIdentifier()->getName())); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6283 | } |
| 6284 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 6285 | if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility) |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 6286 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 6287 | Entry->setAlignment(CGM.getTargetData().getABITypeAlignment( |
| 6288 | ObjCTypes.EHTypeTy)); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6289 | |
| 6290 | if (ForDefinition) { |
| 6291 | Entry->setSection("__DATA,__objc_const"); |
| 6292 | Entry->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 6293 | } else { |
| 6294 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 6295 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6296 | |
| 6297 | return Entry; |
| 6298 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6299 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 6300 | /* *** */ |
| 6301 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 6302 | CodeGen::CGObjCRuntime * |
| 6303 | CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { |
David Chisnall | 60be607 | 2011-03-22 21:21:24 +0000 | [diff] [blame] | 6304 | if (CGM.getLangOptions().ObjCNonFragileABI) |
| 6305 | return new CGObjCNonFragileABIMac(CGM); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 6306 | return new CGObjCMac(CGM); |
| 6307 | } |