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 }; |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 95 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get( |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 96 | llvm::Type::getDoubleTy(VMContext), |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 97 | params, true), |
| 98 | "objc_msgSend_fpret"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 99 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 100 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 101 | |
Anders Carlsson | eea6480 | 2011-10-31 16:27:11 +0000 | [diff] [blame] | 102 | /// _Complex long double objc_msgSend_fp2ret(id self, SEL op, ...) |
| 103 | /// |
| 104 | /// The messenger used when the return value is returned in two values on the |
| 105 | /// x87 floating point stack; without a special entrypoint, the nil case |
| 106 | /// would be unbalanced. Only used on 64-bit X86. |
| 107 | llvm::Constant *getMessageSendFp2retFn() const { |
| 108 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
| 109 | llvm::Type *longDoubleType = llvm::Type::getX86_FP80Ty(VMContext); |
| 110 | llvm::Type *resultType = |
| 111 | llvm::StructType::get(longDoubleType, longDoubleType, NULL); |
| 112 | |
| 113 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(resultType, |
| 114 | params, true), |
| 115 | "objc_msgSend_fp2ret"); |
| 116 | } |
| 117 | |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 118 | /// id objc_msgSendSuper(struct objc_super *super, SEL op, ...) |
| 119 | /// |
| 120 | /// The messenger used for super calls, which have different dispatch |
| 121 | /// semantics. The class passed is the superclass of the current |
| 122 | /// class. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 123 | llvm::Constant *getMessageSendSuperFn() const { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 124 | llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 125 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 126 | params, true), |
| 127 | "objc_msgSendSuper"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 128 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 129 | |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 130 | /// id objc_msgSendSuper2(struct objc_super *super, SEL op, ...) |
| 131 | /// |
| 132 | /// A slightly different messenger used for super calls. The class |
| 133 | /// passed is the current class. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 134 | llvm::Constant *getMessageSendSuperFn2() const { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 135 | llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 136 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 137 | params, true), |
| 138 | "objc_msgSendSuper2"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 139 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 140 | |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 141 | /// void objc_msgSendSuper_stret(void *stretAddr, struct objc_super *super, |
| 142 | /// SEL op, ...) |
| 143 | /// |
| 144 | /// The messenger used for super calls which return an aggregate indirectly. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 145 | llvm::Constant *getMessageSendSuperStretFn() const { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 146 | llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 147 | return CGM.CreateRuntimeFunction( |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 148 | llvm::FunctionType::get(CGM.VoidTy, params, true), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 149 | "objc_msgSendSuper_stret"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 150 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 151 | |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 152 | /// void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super, |
| 153 | /// SEL op, ...) |
| 154 | /// |
| 155 | /// objc_msgSendSuper_stret with the super2 semantics. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 156 | llvm::Constant *getMessageSendSuperStretFn2() const { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 157 | llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 158 | return CGM.CreateRuntimeFunction( |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 159 | llvm::FunctionType::get(CGM.VoidTy, params, true), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 160 | "objc_msgSendSuper2_stret"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 161 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 162 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 163 | llvm::Constant *getMessageSendSuperFpretFn() const { |
| 164 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 165 | return getMessageSendSuperFn(); |
| 166 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 167 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 168 | llvm::Constant *getMessageSendSuperFpretFn2() const { |
| 169 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 170 | return getMessageSendSuperFn2(); |
| 171 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 172 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 173 | protected: |
| 174 | CodeGen::CodeGenModule &CGM; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 175 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 176 | public: |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 177 | llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy; |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 178 | llvm::Type *Int8PtrTy, *Int8PtrPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 179 | |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 180 | /// ObjectPtrTy - LLVM type for object handles (typeof(id)) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 181 | llvm::Type *ObjectPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 182 | |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 183 | /// PtrObjectPtrTy - LLVM type for id * |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 184 | llvm::Type *PtrObjectPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 185 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 186 | /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL)) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 187 | llvm::Type *SelectorPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 188 | /// ProtocolPtrTy - LLVM type for external protocol handles |
| 189 | /// (typeof(Protocol)) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 190 | llvm::Type *ExternalProtocolPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 191 | |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 192 | // SuperCTy - clang type for struct objc_super. |
| 193 | QualType SuperCTy; |
| 194 | // SuperPtrCTy - clang type for struct objc_super *. |
| 195 | QualType SuperPtrCTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 196 | |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 197 | /// SuperTy - LLVM type for struct objc_super. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 198 | llvm::StructType *SuperTy; |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 199 | /// SuperPtrTy - LLVM type for struct objc_super *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 200 | llvm::Type *SuperPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 201 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 202 | /// PropertyTy - LLVM type for struct objc_property (struct _prop_t |
| 203 | /// in GCC parlance). |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 204 | llvm::StructType *PropertyTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 205 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 206 | /// PropertyListTy - LLVM type for struct objc_property_list |
| 207 | /// (_prop_list_t in GCC parlance). |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 208 | llvm::StructType *PropertyListTy; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 209 | /// PropertyListPtrTy - LLVM type for struct objc_property_list*. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 210 | llvm::Type *PropertyListPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 211 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 212 | // MethodTy - LLVM type for struct objc_method. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 213 | llvm::StructType *MethodTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 214 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 215 | /// CacheTy - LLVM type for struct objc_cache. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 216 | llvm::Type *CacheTy; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 217 | /// CachePtrTy - LLVM type for struct objc_cache *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 218 | llvm::Type *CachePtrTy; |
Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 219 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 220 | llvm::Constant *getGetPropertyFn() { |
| 221 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 222 | ASTContext &Ctx = CGM.getContext(); |
| 223 | // id objc_getProperty (id, SEL, ptrdiff_t, bool) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 224 | SmallVector<CanQualType,4> Params; |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 225 | CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType()); |
| 226 | CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType()); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 227 | Params.push_back(IdType); |
| 228 | Params.push_back(SelType); |
David Chisnall | ab5824e | 2011-03-22 20:03:13 +0000 | [diff] [blame] | 229 | Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified()); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 230 | Params.push_back(Ctx.BoolTy); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 231 | llvm::FunctionType *FTy = |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 232 | Types.GetFunctionType(Types.getFunctionInfo(IdType, Params, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 233 | FunctionType::ExtInfo()), |
| 234 | false); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 235 | return CGM.CreateRuntimeFunction(FTy, "objc_getProperty"); |
| 236 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 237 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 238 | llvm::Constant *getSetPropertyFn() { |
| 239 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 240 | ASTContext &Ctx = CGM.getContext(); |
| 241 | // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 242 | SmallVector<CanQualType,6> Params; |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 243 | CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType()); |
| 244 | CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType()); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 245 | Params.push_back(IdType); |
| 246 | Params.push_back(SelType); |
David Chisnall | ab5824e | 2011-03-22 20:03:13 +0000 | [diff] [blame] | 247 | Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified()); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 248 | Params.push_back(IdType); |
| 249 | Params.push_back(Ctx.BoolTy); |
| 250 | Params.push_back(Ctx.BoolTy); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 251 | llvm::FunctionType *FTy = |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 252 | Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 253 | FunctionType::ExtInfo()), |
| 254 | false); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 255 | return CGM.CreateRuntimeFunction(FTy, "objc_setProperty"); |
| 256 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 257 | |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 258 | |
| 259 | llvm::Constant *getCopyStructFn() { |
| 260 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 261 | ASTContext &Ctx = CGM.getContext(); |
| 262 | // void objc_copyStruct (void *, const void *, size_t, bool, bool) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 263 | SmallVector<CanQualType,5> Params; |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 264 | Params.push_back(Ctx.VoidPtrTy); |
| 265 | Params.push_back(Ctx.VoidPtrTy); |
| 266 | Params.push_back(Ctx.LongTy); |
| 267 | Params.push_back(Ctx.BoolTy); |
| 268 | Params.push_back(Ctx.BoolTy); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 269 | llvm::FunctionType *FTy = |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 270 | Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params, |
| 271 | FunctionType::ExtInfo()), |
| 272 | false); |
| 273 | return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct"); |
| 274 | } |
| 275 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 276 | llvm::Constant *getEnumerationMutationFn() { |
Daniel Dunbar | c1ab900 | 2009-07-11 20:32:50 +0000 | [diff] [blame] | 277 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 278 | ASTContext &Ctx = CGM.getContext(); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 279 | // void objc_enumerationMutation (id) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 280 | SmallVector<CanQualType,1> Params; |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 281 | Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType())); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 282 | llvm::FunctionType *FTy = |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 283 | Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 284 | FunctionType::ExtInfo()), |
| 285 | false); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 286 | return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation"); |
| 287 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 288 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 289 | /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function. |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 290 | llvm::Constant *getGcReadWeakFn() { |
| 291 | // id objc_read_weak (id *) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 292 | llvm::Type *args[] = { ObjectPtrTy->getPointerTo() }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 293 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 294 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 295 | return CGM.CreateRuntimeFunction(FTy, "objc_read_weak"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 298 | /// GcAssignWeakFn -- LLVM objc_assign_weak function. |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 299 | llvm::Constant *getGcAssignWeakFn() { |
| 300 | // id objc_assign_weak (id, id *) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 301 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() }; |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 302 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 303 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 304 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak"); |
| 305 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 306 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 307 | /// GcAssignGlobalFn -- LLVM objc_assign_global function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 308 | llvm::Constant *getGcAssignGlobalFn() { |
| 309 | // id objc_assign_global(id, id *) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 310 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 311 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 312 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 313 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_global"); |
| 314 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 315 | |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 316 | /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function. |
| 317 | llvm::Constant *getGcAssignThreadLocalFn() { |
| 318 | // id objc_assign_threadlocal(id src, id * dest) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 319 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() }; |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 320 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 321 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 322 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal"); |
| 323 | } |
| 324 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 325 | /// GcAssignIvarFn -- LLVM objc_assign_ivar function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 326 | llvm::Constant *getGcAssignIvarFn() { |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 327 | // id objc_assign_ivar(id, id *, ptrdiff_t) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 328 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo(), |
| 329 | CGM.PtrDiffTy }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 330 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 331 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 332 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar"); |
| 333 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 334 | |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 335 | /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function. |
| 336 | llvm::Constant *GcMemmoveCollectableFn() { |
| 337 | // void *objc_memmove_collectable(void *dst, const void *src, size_t size) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 338 | llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, LongTy }; |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 339 | llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, args, false); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 340 | return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable"); |
| 341 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 342 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 343 | /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 344 | llvm::Constant *getGcAssignStrongCastFn() { |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 345 | // id objc_assign_strongCast(id, id *) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 346 | llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 347 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 348 | llvm::FunctionType::get(ObjectPtrTy, args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 349 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast"); |
| 350 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 351 | |
| 352 | /// ExceptionThrowFn - LLVM objc_exception_throw function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 353 | llvm::Constant *getExceptionThrowFn() { |
| 354 | // void objc_exception_throw(id) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 355 | llvm::Type *args[] = { ObjectPtrTy }; |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 356 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 357 | llvm::FunctionType::get(CGM.VoidTy, args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 358 | return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw"); |
| 359 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 360 | |
Fariborz Jahanian | 69677ea | 2010-05-28 17:34:43 +0000 | [diff] [blame] | 361 | /// ExceptionRethrowFn - LLVM objc_exception_rethrow function. |
| 362 | llvm::Constant *getExceptionRethrowFn() { |
| 363 | // void objc_exception_rethrow(void) |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 364 | llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, false); |
Fariborz Jahanian | 69677ea | 2010-05-28 17:34:43 +0000 | [diff] [blame] | 365 | return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow"); |
| 366 | } |
| 367 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 368 | /// SyncEnterFn - LLVM object_sync_enter function. |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 369 | llvm::Constant *getSyncEnterFn() { |
| 370 | // void objc_sync_enter (id) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 371 | llvm::Type *args[] = { ObjectPtrTy }; |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 372 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 373 | llvm::FunctionType::get(CGM.VoidTy, args, false); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 374 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter"); |
| 375 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 376 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 377 | /// SyncExitFn - LLVM object_sync_exit function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 378 | llvm::Constant *getSyncExitFn() { |
| 379 | // void objc_sync_exit (id) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 380 | llvm::Type *args[] = { ObjectPtrTy }; |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 381 | llvm::FunctionType *FTy = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 382 | llvm::FunctionType::get(CGM.VoidTy, args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 383 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit"); |
| 384 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 385 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 386 | llvm::Constant *getSendFn(bool IsSuper) const { |
| 387 | return IsSuper ? getMessageSendSuperFn() : getMessageSendFn(); |
| 388 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 389 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 390 | llvm::Constant *getSendFn2(bool IsSuper) const { |
| 391 | return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn(); |
| 392 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 393 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 394 | llvm::Constant *getSendStretFn(bool IsSuper) const { |
| 395 | return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn(); |
| 396 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 397 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 398 | llvm::Constant *getSendStretFn2(bool IsSuper) const { |
| 399 | return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn(); |
| 400 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 401 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 402 | llvm::Constant *getSendFpretFn(bool IsSuper) const { |
| 403 | return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn(); |
| 404 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 405 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 406 | llvm::Constant *getSendFpretFn2(bool IsSuper) const { |
| 407 | return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn(); |
| 408 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 409 | |
Anders Carlsson | eea6480 | 2011-10-31 16:27:11 +0000 | [diff] [blame] | 410 | llvm::Constant *getSendFp2retFn(bool IsSuper) const { |
| 411 | return IsSuper ? getMessageSendSuperFn() : getMessageSendFp2retFn(); |
| 412 | } |
| 413 | |
| 414 | llvm::Constant *getSendFp2RetFn2(bool IsSuper) const { |
| 415 | return IsSuper ? getMessageSendSuperFn2() : getMessageSendFp2retFn(); |
| 416 | } |
| 417 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 418 | ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm); |
| 419 | ~ObjCCommonTypesHelper(){} |
| 420 | }; |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 421 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 422 | /// ObjCTypesHelper - Helper class that encapsulates lazy |
| 423 | /// construction of varies types used during ObjC generation. |
| 424 | class ObjCTypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 425 | public: |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 426 | /// SymtabTy - LLVM type for struct objc_symtab. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 427 | llvm::StructType *SymtabTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 428 | /// SymtabPtrTy - LLVM type for struct objc_symtab *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 429 | llvm::Type *SymtabPtrTy; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 430 | /// ModuleTy - LLVM type for struct objc_module. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 431 | llvm::StructType *ModuleTy; |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 432 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 433 | /// ProtocolTy - LLVM type for struct objc_protocol. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 434 | llvm::StructType *ProtocolTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 435 | /// ProtocolPtrTy - LLVM type for struct objc_protocol *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 436 | llvm::Type *ProtocolPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 437 | /// ProtocolExtensionTy - LLVM type for struct |
| 438 | /// objc_protocol_extension. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 439 | llvm::StructType *ProtocolExtensionTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 440 | /// ProtocolExtensionTy - LLVM type for struct |
| 441 | /// objc_protocol_extension *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 442 | llvm::Type *ProtocolExtensionPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 443 | /// MethodDescriptionTy - LLVM type for struct |
| 444 | /// objc_method_description. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 445 | llvm::StructType *MethodDescriptionTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 446 | /// MethodDescriptionListTy - LLVM type for struct |
| 447 | /// objc_method_description_list. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 448 | llvm::StructType *MethodDescriptionListTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 449 | /// MethodDescriptionListPtrTy - LLVM type for struct |
| 450 | /// objc_method_description_list *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 451 | llvm::Type *MethodDescriptionListPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 452 | /// ProtocolListTy - LLVM type for struct objc_property_list. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 453 | llvm::StructType *ProtocolListTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 454 | /// ProtocolListPtrTy - LLVM type for struct objc_property_list*. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 455 | llvm::Type *ProtocolListPtrTy; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 456 | /// CategoryTy - LLVM type for struct objc_category. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 457 | llvm::StructType *CategoryTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 458 | /// ClassTy - LLVM type for struct objc_class. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 459 | llvm::StructType *ClassTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 460 | /// ClassPtrTy - LLVM type for struct objc_class *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 461 | llvm::Type *ClassPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 462 | /// ClassExtensionTy - LLVM type for struct objc_class_ext. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 463 | llvm::StructType *ClassExtensionTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 464 | /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 465 | llvm::Type *ClassExtensionPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 466 | // IvarTy - LLVM type for struct objc_ivar. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 467 | llvm::StructType *IvarTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 468 | /// IvarListTy - LLVM type for struct objc_ivar_list. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 469 | llvm::Type *IvarListTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 470 | /// IvarListPtrTy - LLVM type for struct objc_ivar_list *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 471 | llvm::Type *IvarListPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 472 | /// MethodListTy - LLVM type for struct objc_method_list. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 473 | llvm::Type *MethodListTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 474 | /// MethodListPtrTy - LLVM type for struct objc_method_list *. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 475 | llvm::Type *MethodListPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 476 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 477 | /// ExceptionDataTy - LLVM type for struct _objc_exception_data. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 478 | llvm::Type *ExceptionDataTy; |
Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 479 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 480 | /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 481 | llvm::Constant *getExceptionTryEnterFn() { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 482 | llvm::Type *params[] = { ExceptionDataTy->getPointerTo() }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 483 | return CGM.CreateRuntimeFunction( |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 484 | llvm::FunctionType::get(CGM.VoidTy, params, false), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 485 | "objc_exception_try_enter"); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 486 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 487 | |
| 488 | /// ExceptionTryExitFn - LLVM objc_exception_try_exit function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 489 | llvm::Constant *getExceptionTryExitFn() { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 490 | llvm::Type *params[] = { ExceptionDataTy->getPointerTo() }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 491 | return CGM.CreateRuntimeFunction( |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 492 | llvm::FunctionType::get(CGM.VoidTy, params, false), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 493 | "objc_exception_try_exit"); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 494 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 495 | |
| 496 | /// ExceptionExtractFn - LLVM objc_exception_extract function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 497 | llvm::Constant *getExceptionExtractFn() { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 498 | llvm::Type *params[] = { ExceptionDataTy->getPointerTo() }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 499 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 500 | params, false), |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 501 | "objc_exception_extract"); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 502 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 503 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 504 | /// ExceptionMatchFn - LLVM objc_exception_match function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 505 | llvm::Constant *getExceptionMatchFn() { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 506 | llvm::Type *params[] = { ClassPtrTy, ObjectPtrTy }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 507 | return CGM.CreateRuntimeFunction( |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 508 | llvm::FunctionType::get(CGM.Int32Ty, params, false), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 509 | "objc_exception_match"); |
| 510 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 511 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 512 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 513 | /// SetJmpFn - LLVM _setjmp function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 514 | llvm::Constant *getSetJmpFn() { |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 515 | // This is specifically the prototype for x86. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 516 | llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() }; |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 517 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty, |
| 518 | params, false), |
Bill Wendling | a9e269e | 2011-11-29 00:10:10 +0000 | [diff] [blame] | 519 | "_setjmp", |
| 520 | llvm::Attribute::ReturnsTwice); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 521 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 522 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 523 | public: |
| 524 | ObjCTypesHelper(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 525 | ~ObjCTypesHelper() {} |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 526 | }; |
| 527 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 528 | /// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 529 | /// modern abi |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 530 | class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 531 | public: |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 532 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 533 | // MethodListnfABITy - LLVM for struct _method_list_t |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 534 | llvm::StructType *MethodListnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 535 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 536 | // MethodListnfABIPtrTy - LLVM for struct _method_list_t* |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 537 | llvm::Type *MethodListnfABIPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 538 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 539 | // ProtocolnfABITy = LLVM for struct _protocol_t |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 540 | llvm::StructType *ProtocolnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 541 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 542 | // ProtocolnfABIPtrTy = LLVM for struct _protocol_t* |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 543 | llvm::Type *ProtocolnfABIPtrTy; |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 544 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 545 | // ProtocolListnfABITy - LLVM for struct _objc_protocol_list |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 546 | llvm::StructType *ProtocolListnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 547 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 548 | // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list* |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 549 | llvm::Type *ProtocolListnfABIPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 550 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 551 | // ClassnfABITy - LLVM for struct _class_t |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 552 | llvm::StructType *ClassnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 553 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 554 | // ClassnfABIPtrTy - LLVM for struct _class_t* |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 555 | llvm::Type *ClassnfABIPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 556 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 557 | // IvarnfABITy - LLVM for struct _ivar_t |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 558 | llvm::StructType *IvarnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 559 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 560 | // IvarListnfABITy - LLVM for struct _ivar_list_t |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 561 | llvm::StructType *IvarListnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 562 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 563 | // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t* |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 564 | llvm::Type *IvarListnfABIPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 565 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 566 | // ClassRonfABITy - LLVM for struct _class_ro_t |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 567 | llvm::StructType *ClassRonfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 568 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 569 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 570 | llvm::Type *ImpnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 571 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 572 | // CategorynfABITy - LLVM for struct _category_t |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 573 | llvm::StructType *CategorynfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 574 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 575 | // New types for nonfragile abi messaging. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 576 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 577 | // MessageRefTy - LLVM for: |
| 578 | // struct _message_ref_t { |
| 579 | // IMP messenger; |
| 580 | // SEL name; |
| 581 | // }; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 582 | llvm::StructType *MessageRefTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 583 | // MessageRefCTy - clang type for struct _message_ref_t |
| 584 | QualType MessageRefCTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 585 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 586 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 587 | llvm::Type *MessageRefPtrTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 588 | // MessageRefCPtrTy - clang type for struct _message_ref_t* |
| 589 | QualType MessageRefCPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 590 | |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 591 | // MessengerTy - Type of the messenger (shown as IMP above) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 592 | llvm::FunctionType *MessengerTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 593 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 594 | // SuperMessageRefTy - LLVM for: |
| 595 | // struct _super_message_ref_t { |
| 596 | // SUPER_IMP messenger; |
| 597 | // SEL name; |
| 598 | // }; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 599 | llvm::StructType *SuperMessageRefTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 600 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 601 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 602 | llvm::Type *SuperMessageRefPtrTy; |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 603 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 604 | llvm::Constant *getMessageSendFixupFn() { |
| 605 | // id objc_msgSend_fixup(id, struct message_ref_t*, ...) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 606 | llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 607 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 608 | params, true), |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 609 | "objc_msgSend_fixup"); |
| 610 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 611 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 612 | llvm::Constant *getMessageSendFpretFixupFn() { |
| 613 | // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 614 | llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 615 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 616 | params, true), |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 617 | "objc_msgSend_fpret_fixup"); |
| 618 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 619 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 620 | llvm::Constant *getMessageSendStretFixupFn() { |
| 621 | // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 622 | llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 623 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 624 | params, true), |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 625 | "objc_msgSend_stret_fixup"); |
| 626 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 627 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 628 | llvm::Constant *getMessageSendSuper2FixupFn() { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 629 | // id objc_msgSendSuper2_fixup (struct objc_super *, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 630 | // struct _super_message_ref_t*, ...) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 631 | llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 632 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 633 | params, true), |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 634 | "objc_msgSendSuper2_fixup"); |
| 635 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 636 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 637 | llvm::Constant *getMessageSendSuper2StretFixupFn() { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 638 | // id objc_msgSendSuper2_stret_fixup(struct objc_super *, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 639 | // struct _super_message_ref_t*, ...) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 640 | llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy }; |
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_msgSendSuper2_stret_fixup"); |
| 644 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 645 | |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 646 | llvm::Constant *getObjCEndCatchFn() { |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 647 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false), |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 648 | "objc_end_catch"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 649 | |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 650 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 651 | |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 652 | llvm::Constant *getObjCBeginCatchFn() { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 653 | llvm::Type *params[] = { Int8PtrTy }; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 654 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy, |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 655 | params, false), |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 656 | "objc_begin_catch"); |
| 657 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 658 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 659 | llvm::StructType *EHTypeTy; |
| 660 | llvm::Type *EHTypePtrTy; |
Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 661 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 662 | ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm); |
| 663 | ~ObjCNonFragileABITypesHelper(){} |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 664 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 665 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 666 | class CGObjCCommonMac : public CodeGen::CGObjCRuntime { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 667 | public: |
| 668 | // FIXME - accessibility |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 669 | class GC_IVAR { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 670 | public: |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 671 | unsigned ivar_bytepos; |
| 672 | unsigned ivar_size; |
| 673 | GC_IVAR(unsigned bytepos = 0, unsigned size = 0) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 674 | : ivar_bytepos(bytepos), ivar_size(size) {} |
Daniel Dunbar | 0941b49 | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 675 | |
| 676 | // Allow sorting based on byte pos. |
| 677 | bool operator<(const GC_IVAR &b) const { |
| 678 | return ivar_bytepos < b.ivar_bytepos; |
| 679 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 680 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 681 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 682 | class SKIP_SCAN { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 683 | public: |
| 684 | unsigned skip; |
| 685 | unsigned scan; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 686 | SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0) |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 687 | : skip(_skip), scan(_scan) {} |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 688 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 689 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 690 | protected: |
| 691 | CodeGen::CodeGenModule &CGM; |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 692 | llvm::LLVMContext &VMContext; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 693 | // FIXME! May not be needing this after all. |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 694 | unsigned ObjCABI; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 695 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 696 | // gc ivar layout bitmap calculation helper caches. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 697 | SmallVector<GC_IVAR, 16> SkipIvars; |
| 698 | SmallVector<GC_IVAR, 16> IvarsInfo; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 699 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 700 | /// LazySymbols - Symbols to generate a lazy reference for. See |
| 701 | /// DefinedSymbols and FinishModule(). |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 702 | llvm::SetVector<IdentifierInfo*> LazySymbols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 703 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 704 | /// DefinedSymbols - External symbols which are defined by this |
| 705 | /// module. The symbols in this list and LazySymbols are used to add |
| 706 | /// special linker symbols which ensure that Objective-C modules are |
| 707 | /// linked properly. |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 708 | llvm::SetVector<IdentifierInfo*> DefinedSymbols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 709 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 710 | /// ClassNames - uniqued class names. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 711 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 712 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 713 | /// MethodVarNames - uniqued method variable names. |
| 714 | llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 715 | |
Fariborz Jahanian | b9c5b3d | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 716 | /// DefinedCategoryNames - list of category names in form Class_Category. |
| 717 | llvm::SetVector<std::string> DefinedCategoryNames; |
| 718 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 719 | /// MethodVarTypes - uniqued method type signatures. We have to use |
| 720 | /// a StringMap here because have no other unique reference. |
| 721 | llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 722 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 723 | /// MethodDefinitions - map of methods which have been defined in |
| 724 | /// this translation unit. |
| 725 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 726 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 727 | /// PropertyNames - uniqued method variable names. |
| 728 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 729 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 730 | /// ClassReferences - uniqued class references. |
| 731 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 732 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 733 | /// SelectorReferences - uniqued selector references. |
| 734 | llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 735 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 736 | /// Protocols - Protocols for which an objc_protocol structure has |
| 737 | /// been emitted. Forward declarations are handled by creating an |
| 738 | /// empty structure whose initializer is filled in when/if defined. |
| 739 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 740 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 741 | /// DefinedProtocols - Protocols which have actually been |
| 742 | /// defined. We should not need this, see FIXME in GenerateProtocol. |
| 743 | llvm::DenseSet<IdentifierInfo*> DefinedProtocols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 744 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 745 | /// DefinedClasses - List of defined classes. |
| 746 | std::vector<llvm::GlobalValue*> DefinedClasses; |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 747 | |
| 748 | /// DefinedNonLazyClasses - List of defined "non-lazy" classes. |
| 749 | std::vector<llvm::GlobalValue*> DefinedNonLazyClasses; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 750 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 751 | /// DefinedCategories - List of defined categories. |
| 752 | std::vector<llvm::GlobalValue*> DefinedCategories; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 753 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 754 | /// DefinedNonLazyCategories - List of defined "non-lazy" categories. |
| 755 | std::vector<llvm::GlobalValue*> DefinedNonLazyCategories; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 756 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 757 | /// GetNameForMethod - Return a name for the given method. |
| 758 | /// \param[out] NameOut - The return value. |
| 759 | void GetNameForMethod(const ObjCMethodDecl *OMD, |
| 760 | const ObjCContainerDecl *CD, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 761 | SmallVectorImpl<char> &NameOut); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 762 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 763 | /// GetMethodVarName - Return a unique constant for the given |
| 764 | /// selector's name. The return value has type char *. |
| 765 | llvm::Constant *GetMethodVarName(Selector Sel); |
| 766 | llvm::Constant *GetMethodVarName(IdentifierInfo *Ident); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 767 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 768 | /// GetMethodVarType - Return a unique constant for the given |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 769 | /// method's type encoding string. The return value has type char *. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 770 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 771 | // FIXME: This is a horrible name. |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 772 | llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D, |
| 773 | bool Extended = false); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 774 | llvm::Constant *GetMethodVarType(const FieldDecl *D); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 775 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 776 | /// GetPropertyName - Return a unique constant for the given |
| 777 | /// name. The return value has type char *. |
| 778 | llvm::Constant *GetPropertyName(IdentifierInfo *Ident); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 779 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 780 | // FIXME: This can be dropped once string functions are unified. |
| 781 | llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 782 | const Decl *Container); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 783 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 784 | /// GetClassName - Return a unique constant for the given selector's |
| 785 | /// name. The return value has type char *. |
| 786 | llvm::Constant *GetClassName(IdentifierInfo *Ident); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 787 | |
Argyrios Kyrtzidis | 9d50c06 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 788 | llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD); |
| 789 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 790 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 791 | /// implementation for the __strong or __weak case. |
| 792 | /// |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 793 | llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI, |
| 794 | bool ForStrongLayout); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 795 | |
Fariborz Jahanian | b8fd2eb | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 796 | llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 797 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 798 | void BuildAggrIvarRecordLayout(const RecordType *RT, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 799 | unsigned int BytePos, bool ForStrongLayout, |
| 800 | bool &HasUnion); |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 801 | void BuildAggrIvarLayout(const ObjCImplementationDecl *OI, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 802 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 803 | const RecordDecl *RD, |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 804 | const SmallVectorImpl<const FieldDecl*> &RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 805 | unsigned int BytePos, bool ForStrongLayout, |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 806 | bool &HasUnion); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 807 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 808 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 809 | /// ivar layout bitmap. |
| 810 | llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident, |
| 811 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 812 | |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 813 | /// EmitPropertyList - Emit the given property list. The return |
| 814 | /// value has type PropertyListPtrTy. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 815 | llvm::Constant *EmitPropertyList(Twine Name, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 816 | const Decl *Container, |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 817 | const ObjCContainerDecl *OCD, |
| 818 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 819 | |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 820 | /// EmitProtocolMethodTypes - Generate the array of extended method type |
| 821 | /// strings. The return value has type Int8PtrPtrTy. |
| 822 | llvm::Constant *EmitProtocolMethodTypes(Twine Name, |
| 823 | const ConstantVector &MethodTypes, |
| 824 | const ObjCCommonTypesHelper &ObjCTypes); |
| 825 | |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 826 | /// PushProtocolProperties - Push protocol's property on the input stack. |
| 827 | void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet, |
| 828 | std::vector<llvm::Constant*> &Properties, |
| 829 | const Decl *Container, |
| 830 | const ObjCProtocolDecl *PROTO, |
| 831 | const ObjCCommonTypesHelper &ObjCTypes); |
| 832 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 833 | /// GetProtocolRef - Return a reference to the internal protocol |
| 834 | /// description, creating an empty one if it has not been |
| 835 | /// defined. The return value has type ProtocolPtrTy. |
| 836 | llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD); |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 837 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 838 | /// CreateMetadataVar - Create a global variable with internal |
| 839 | /// linkage for use by the Objective-C runtime. |
| 840 | /// |
| 841 | /// This is a convenience wrapper which not only creates the |
| 842 | /// variable, but also sets the section and alignment and adds the |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 843 | /// global to the "llvm.used" list. |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 844 | /// |
| 845 | /// \param Name - The variable name. |
| 846 | /// \param Init - The variable initializer; this is also used to |
| 847 | /// define the type of the variable. |
| 848 | /// \param Section - The section the variable should go into, or 0. |
| 849 | /// \param Align - The alignment for the variable, or 0. |
| 850 | /// \param AddToUsed - Whether the variable should be added to |
Daniel Dunbar | c158306 | 2009-04-14 17:42:51 +0000 | [diff] [blame] | 851 | /// "llvm.used". |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 852 | llvm::GlobalVariable *CreateMetadataVar(Twine Name, |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 853 | llvm::Constant *Init, |
| 854 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 855 | unsigned Align, |
| 856 | bool AddToUsed); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 857 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 858 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 859 | ReturnValueSlot Return, |
| 860 | QualType ResultType, |
| 861 | llvm::Value *Sel, |
| 862 | llvm::Value *Arg0, |
| 863 | QualType Arg0Ty, |
| 864 | bool IsSuper, |
| 865 | const CallArgList &CallArgs, |
| 866 | const ObjCMethodDecl *OMD, |
| 867 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 868 | |
Daniel Dunbar | fce176b | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 869 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 870 | /// level information. |
| 871 | void EmitImageInfo(); |
| 872 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 873 | public: |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 874 | CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 875 | CGM(cgm), VMContext(cgm.getLLVMContext()) { } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 876 | |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 877 | virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 878 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 879 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
| 880 | const ObjCContainerDecl *CD=0); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 881 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 882 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 883 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 884 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 885 | /// declaration, emitting it if necessary. The return value has type |
| 886 | /// ProtocolPtrTy. |
| 887 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 888 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 889 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 890 | /// object for the given declaration, emitting it if needed. These |
| 891 | /// forward references will be filled in with empty bodies if no |
| 892 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 893 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0; |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 894 | virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM, |
| 895 | const CGBlockInfo &blockInfo); |
Fariborz Jahanian | 89ecd41 | 2010-08-04 16:57:49 +0000 | [diff] [blame] | 896 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 897 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 898 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 899 | class CGObjCMac : public CGObjCCommonMac { |
| 900 | private: |
| 901 | ObjCTypesHelper ObjCTypes; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 902 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 903 | /// EmitModuleInfo - Another marker encoding module level |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 904 | /// information. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 905 | void EmitModuleInfo(); |
| 906 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 907 | /// EmitModuleSymols - Emit module symbols, the list of defined |
| 908 | /// classes and categories. The result has type SymtabPtrTy. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 909 | llvm::Constant *EmitModuleSymbols(); |
| 910 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 911 | /// FinishModule - Write out global data structures at the end of |
| 912 | /// processing a translation unit. |
| 913 | void FinishModule(); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 914 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 915 | /// EmitClassExtension - Generate the class extension structure used |
| 916 | /// to store the weak ivar layout and properties. The return value |
| 917 | /// has type ClassExtensionPtrTy. |
| 918 | llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID); |
| 919 | |
| 920 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 921 | /// for the given class. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 922 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 923 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 924 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 925 | llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder, |
| 926 | IdentifierInfo *II); |
| 927 | |
| 928 | llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder); |
| 929 | |
Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 930 | /// EmitSuperClassRef - Emits reference to class's main metadata class. |
| 931 | llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 932 | |
| 933 | /// EmitIvarList - Emit the ivar list for the given |
| 934 | /// implementation. If ForClass is true the list of class ivars |
| 935 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 936 | /// interface ivars will be emitted. The return value has type |
| 937 | /// IvarListPtrTy. |
| 938 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 939 | bool ForClass); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 940 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 941 | /// EmitMetaClass - Emit a forward reference to the class structure |
| 942 | /// for the metaclass of the given interface. The return value has |
| 943 | /// type ClassPtrTy. |
| 944 | llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID); |
| 945 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 946 | /// EmitMetaClass - Emit a class structure for the metaclass of the |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 947 | /// given implementation. The return value has type ClassPtrTy. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 948 | llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID, |
| 949 | llvm::Constant *Protocols, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 950 | const ConstantVector &Methods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 951 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 952 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 953 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 954 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 955 | |
| 956 | /// EmitMethodList - Emit the method list for the given |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 957 | /// implementation. The return value has type MethodListPtrTy. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 958 | llvm::Constant *EmitMethodList(Twine Name, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 959 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 960 | const ConstantVector &Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 961 | |
| 962 | /// EmitMethodDescList - Emit a method description list for a list of |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 963 | /// method declarations. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 964 | /// - TypeName: The name for the type containing the methods. |
| 965 | /// - IsProtocol: True iff these methods are for a protocol. |
| 966 | /// - ClassMethds: True iff these are class methods. |
| 967 | /// - Required: When true, only "required" methods are |
| 968 | /// listed. Similarly, when false only "optional" methods are |
| 969 | /// listed. For classes this should always be true. |
| 970 | /// - begin, end: The method list to output. |
| 971 | /// |
| 972 | /// The return value has type MethodDescriptionListPtrTy. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 973 | llvm::Constant *EmitMethodDescList(Twine Name, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 974 | const char *Section, |
| 975 | const ConstantVector &Methods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 976 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 977 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 978 | /// declaration, emitting it if necessary. The return value has type |
| 979 | /// ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 980 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 981 | |
| 982 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 983 | /// object for the given declaration, emitting it if needed. These |
| 984 | /// forward references will be filled in with empty bodies if no |
| 985 | /// definition is seen. The return value has type ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 986 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 987 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 988 | /// EmitProtocolExtension - Generate the protocol extension |
| 989 | /// structure used to store optional instance and class methods, and |
| 990 | /// protocol properties. The return value has type |
| 991 | /// ProtocolExtensionPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 992 | llvm::Constant * |
| 993 | EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 994 | const ConstantVector &OptInstanceMethods, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 995 | const ConstantVector &OptClassMethods, |
| 996 | const ConstantVector &MethodTypesExt); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 997 | |
| 998 | /// EmitProtocolList - Generate the list of referenced |
| 999 | /// protocols. The return value has type ProtocolListPtrTy. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1000 | llvm::Constant *EmitProtocolList(Twine Name, |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1001 | ObjCProtocolDecl::protocol_iterator begin, |
| 1002 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1003 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1004 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1005 | /// for the given selector. |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1006 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel, |
| 1007 | bool lval=false); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1008 | |
| 1009 | public: |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1010 | CGObjCMac(CodeGen::CodeGenModule &cgm); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1011 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1012 | virtual llvm::Function *ModuleInitFunction(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1013 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1014 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1015 | ReturnValueSlot Return, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1016 | QualType ResultType, |
| 1017 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1018 | llvm::Value *Receiver, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1019 | const CallArgList &CallArgs, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1020 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1021 | const ObjCMethodDecl *Method); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1022 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1023 | virtual CodeGen::RValue |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1024 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1025 | ReturnValueSlot Return, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1026 | QualType ResultType, |
| 1027 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1028 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1029 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1030 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1031 | bool IsClassMessage, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1032 | const CallArgList &CallArgs, |
| 1033 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1034 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1035 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1036 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1037 | |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1038 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel, |
| 1039 | bool lval = false); |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1040 | |
| 1041 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an |
| 1042 | /// untyped one. |
| 1043 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, |
| 1044 | const ObjCMethodDecl *Method); |
| 1045 | |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1046 | virtual llvm::Constant *GetEHType(QualType T); |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1047 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1048 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1049 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1050 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1051 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1052 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1053 | const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1054 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1055 | virtual llvm::Constant *GetPropertyGetFunction(); |
| 1056 | virtual llvm::Constant *GetPropertySetFunction(); |
David Chisnall | 8fac25d | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 1057 | virtual llvm::Constant *GetGetStructFunction(); |
| 1058 | virtual llvm::Constant *GetSetStructFunction(); |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1059 | virtual llvm::Constant *EnumerationMutationFunction(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1060 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1061 | virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
| 1062 | const ObjCAtTryStmt &S); |
| 1063 | virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1064 | const ObjCAtSynchronizedStmt &S); |
| 1065 | void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1066 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 1067 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1068 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1069 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1070 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1071 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1072 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 1073 | llvm::Value *src, llvm::Value *dest, |
| 1074 | bool threadlocal = false); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 1075 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1076 | llvm::Value *src, llvm::Value *dest, |
| 1077 | llvm::Value *ivarOffset); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1078 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 1079 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 1080 | virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
| 1081 | llvm::Value *dest, llvm::Value *src, |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 1082 | llvm::Value *size); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1083 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1084 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1085 | QualType ObjectTy, |
| 1086 | llvm::Value *BaseValue, |
| 1087 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1088 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1089 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1090 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1091 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | 6f40e22 | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1092 | |
| 1093 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 1094 | /// class of the given name. |
| 1095 | virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1096 | llvm_unreachable("CGObjCMac::GetClassGlobal"); |
Fariborz Jahanian | 6f40e22 | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1097 | } |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1098 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1099 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1100 | class CGObjCNonFragileABIMac : public CGObjCCommonMac { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1101 | private: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1102 | ObjCNonFragileABITypesHelper ObjCTypes; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1103 | llvm::GlobalVariable* ObjCEmptyCacheVar; |
| 1104 | llvm::GlobalVariable* ObjCEmptyVtableVar; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1105 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1106 | /// SuperClassReferences - uniqued super class references. |
| 1107 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1108 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1109 | /// MetaClassReferences - uniqued meta class references. |
| 1110 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1111 | |
| 1112 | /// EHTypeReferences - uniqued class ehtype references. |
| 1113 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1114 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1115 | /// VTableDispatchMethods - List of methods for which we generate |
| 1116 | /// vtable-based message dispatch. |
| 1117 | llvm::DenseSet<Selector> VTableDispatchMethods; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1118 | |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 1119 | /// DefinedMetaClasses - List of defined meta-classes. |
| 1120 | std::vector<llvm::GlobalValue*> DefinedMetaClasses; |
| 1121 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1122 | /// isVTableDispatchedSelector - Returns true if SEL is a |
| 1123 | /// vtable-based selector. |
| 1124 | bool isVTableDispatchedSelector(Selector Sel); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1125 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1126 | /// FinishNonFragileABIModule - Write out global data structures at the end of |
| 1127 | /// processing a translation unit. |
| 1128 | void FinishNonFragileABIModule(); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1129 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 1130 | /// AddModuleClassList - Add the given list of class pointers to the |
| 1131 | /// module with the provided symbol and section names. |
| 1132 | void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container, |
| 1133 | const char *SymbolName, |
| 1134 | const char *SectionName); |
| 1135 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1136 | llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags, |
| 1137 | unsigned InstanceStart, |
| 1138 | unsigned InstanceSize, |
| 1139 | const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1140 | llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1141 | llvm::Constant *IsAGV, |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1142 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 1143 | llvm::Constant *ClassRoGV, |
| 1144 | bool HiddenVisibility); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1145 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1146 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1147 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 1148 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1149 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1150 | /// EmitMethodList - Emit the method list for the given |
| 1151 | /// implementation. The return value has type MethodListnfABITy. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1152 | llvm::Constant *EmitMethodList(Twine Name, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1153 | const char *Section, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 1154 | const ConstantVector &Methods); |
| 1155 | /// EmitIvarList - Emit the ivar list for the given |
| 1156 | /// implementation. If ForClass is true the list of class ivars |
| 1157 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 1158 | /// interface ivars will be emitted. The return value has type |
| 1159 | /// IvarListnfABIPtrTy. |
| 1160 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1161 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1162 | llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 1163 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 1164 | unsigned long int offset); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1165 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1166 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1167 | /// declaration, emitting it if necessary. The return value has type |
| 1168 | /// ProtocolPtrTy. |
| 1169 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1170 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1171 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1172 | /// object for the given declaration, emitting it if needed. These |
| 1173 | /// forward references will be filled in with empty bodies if no |
| 1174 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 1175 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1176 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1177 | /// EmitProtocolList - Generate the list of referenced |
| 1178 | /// protocols. The return value has type ProtocolListPtrTy. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1179 | llvm::Constant *EmitProtocolList(Twine Name, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1180 | ObjCProtocolDecl::protocol_iterator begin, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1181 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1182 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1183 | CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1184 | ReturnValueSlot Return, |
| 1185 | QualType ResultType, |
| 1186 | Selector Sel, |
| 1187 | llvm::Value *Receiver, |
| 1188 | QualType Arg0Ty, |
| 1189 | bool IsSuper, |
| 1190 | const CallArgList &CallArgs, |
| 1191 | const ObjCMethodDecl *Method); |
Fariborz Jahanian | 6f40e22 | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1192 | |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 1193 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 1194 | /// class of the given name. |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 1195 | llvm::GlobalVariable *GetClassGlobal(const std::string &Name); |
Fariborz Jahanian | 6f40e22 | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1196 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1197 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1198 | /// for the given class reference. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1199 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1200 | const ObjCInterfaceDecl *ID); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1201 | |
| 1202 | llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder, |
| 1203 | IdentifierInfo *II); |
| 1204 | |
| 1205 | llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1206 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1207 | /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 1208 | /// for the given super class reference. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1209 | llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder, |
| 1210 | const ObjCInterfaceDecl *ID); |
| 1211 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1212 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 1213 | /// meta-data |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1214 | llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1215 | const ObjCInterfaceDecl *ID); |
| 1216 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1217 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 1218 | /// the given ivar. |
| 1219 | /// |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 1220 | llvm::GlobalVariable * ObjCIvarOffsetVariable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1221 | const ObjCInterfaceDecl *ID, |
| 1222 | const ObjCIvarDecl *Ivar); |
| 1223 | |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 1224 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1225 | /// for the given selector. |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1226 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel, |
| 1227 | bool lval=false); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1228 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1229 | /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1230 | /// interface. The return value has type EHTypePtrTy. |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1231 | llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1232 | bool ForDefinition); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1233 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1234 | const char *getMetaclassSymbolPrefix() const { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1235 | return "OBJC_METACLASS_$_"; |
| 1236 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1237 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1238 | const char *getClassSymbolPrefix() const { |
| 1239 | return "OBJC_CLASS_$_"; |
| 1240 | } |
| 1241 | |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 1242 | void GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1243 | uint32_t &InstanceStart, |
| 1244 | uint32_t &InstanceSize); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1245 | |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1246 | // Shamelessly stolen from Analysis/CFRefCount.cpp |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1247 | Selector GetNullarySelector(const char* name) const { |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1248 | IdentifierInfo* II = &CGM.getContext().Idents.get(name); |
| 1249 | return CGM.getContext().Selectors.getSelector(0, &II); |
| 1250 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1251 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1252 | Selector GetUnarySelector(const char* name) const { |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1253 | IdentifierInfo* II = &CGM.getContext().Idents.get(name); |
| 1254 | return CGM.getContext().Selectors.getSelector(1, &II); |
| 1255 | } |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1256 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1257 | /// ImplementationIsNonLazy - Check whether the given category or |
| 1258 | /// class implementation is "non-lazy". |
Fariborz Jahanian | ecfbdcb | 2009-05-21 01:03:45 +0000 | [diff] [blame] | 1259 | bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const; |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1260 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1261 | public: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1262 | CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1263 | // FIXME. All stubs for now! |
| 1264 | virtual llvm::Function *ModuleInitFunction(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1265 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1266 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1267 | ReturnValueSlot Return, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1268 | QualType ResultType, |
| 1269 | Selector Sel, |
| 1270 | llvm::Value *Receiver, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1271 | const CallArgList &CallArgs, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1272 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1273 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1274 | |
| 1275 | virtual CodeGen::RValue |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1276 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1277 | ReturnValueSlot Return, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1278 | QualType ResultType, |
| 1279 | Selector Sel, |
| 1280 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1281 | bool isCategoryImpl, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1282 | llvm::Value *Receiver, |
| 1283 | bool IsClassMessage, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1284 | const CallArgList &CallArgs, |
| 1285 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1286 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1287 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1288 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1289 | |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1290 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel, |
| 1291 | bool lvalue = false) |
| 1292 | { return EmitSelector(Builder, Sel, lvalue); } |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1293 | |
| 1294 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an |
| 1295 | /// untyped one. |
| 1296 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, |
| 1297 | const ObjCMethodDecl *Method) |
| 1298 | { return EmitSelector(Builder, Method->getSelector()); } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1299 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 1300 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1301 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1302 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1303 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 1304 | const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1305 | |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1306 | virtual llvm::Constant *GetEHType(QualType T); |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1307 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1308 | virtual llvm::Constant *GetPropertyGetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1309 | return ObjCTypes.getGetPropertyFn(); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1310 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1311 | virtual llvm::Constant *GetPropertySetFunction() { |
| 1312 | return ObjCTypes.getSetPropertyFn(); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1313 | } |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 1314 | |
David Chisnall | 8fac25d | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 1315 | virtual llvm::Constant *GetSetStructFunction() { |
| 1316 | return ObjCTypes.getCopyStructFn(); |
| 1317 | } |
| 1318 | virtual llvm::Constant *GetGetStructFunction() { |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 1319 | return ObjCTypes.getCopyStructFn(); |
| 1320 | } |
| 1321 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1322 | virtual llvm::Constant *EnumerationMutationFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1323 | return ObjCTypes.getEnumerationMutationFn(); |
Daniel Dunbar | 28ed084 | 2009-02-16 18:48:45 +0000 | [diff] [blame] | 1324 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1325 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1326 | virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
| 1327 | const ObjCAtTryStmt &S); |
| 1328 | virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1329 | const ObjCAtSynchronizedStmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1330 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 1331 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1332 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1333 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1334 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1335 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1336 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 1337 | llvm::Value *src, llvm::Value *dest, |
| 1338 | bool threadlocal = false); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1339 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1340 | llvm::Value *src, llvm::Value *dest, |
| 1341 | llvm::Value *ivarOffset); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1342 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1343 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 1344 | virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
| 1345 | llvm::Value *dest, llvm::Value *src, |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 1346 | llvm::Value *size); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1347 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1348 | QualType ObjectTy, |
| 1349 | llvm::Value *BaseValue, |
| 1350 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1351 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1352 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1353 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1354 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1355 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1356 | |
John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 1357 | /// A helper class for performing the null-initialization of a return |
| 1358 | /// value. |
| 1359 | struct NullReturnState { |
| 1360 | llvm::BasicBlock *NullBB; |
Fariborz Jahanian | 6c29eda | 2011-10-26 20:53:59 +0000 | [diff] [blame] | 1361 | llvm::BasicBlock *callBB; |
| 1362 | NullReturnState() : NullBB(0), callBB(0) {} |
John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 1363 | |
| 1364 | void init(CodeGenFunction &CGF, llvm::Value *receiver) { |
| 1365 | // Make blocks for the null-init and call edges. |
| 1366 | NullBB = CGF.createBasicBlock("msgSend.nullinit"); |
Fariborz Jahanian | 6c29eda | 2011-10-26 20:53:59 +0000 | [diff] [blame] | 1367 | callBB = CGF.createBasicBlock("msgSend.call"); |
John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 1368 | |
| 1369 | // Check for a null receiver and, if there is one, jump to the |
| 1370 | // null-init test. |
| 1371 | llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver); |
| 1372 | CGF.Builder.CreateCondBr(isNull, NullBB, callBB); |
| 1373 | |
| 1374 | // Otherwise, start performing the call. |
| 1375 | CGF.EmitBlock(callBB); |
| 1376 | } |
| 1377 | |
| 1378 | RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType) { |
| 1379 | if (!NullBB) return result; |
| 1380 | |
| 1381 | // Finish the call path. |
| 1382 | llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont"); |
| 1383 | if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB); |
| 1384 | |
| 1385 | // Emit the null-init block and perform the null-initialization there. |
| 1386 | CGF.EmitBlock(NullBB); |
Fariborz Jahanian | 6c29eda | 2011-10-26 20:53:59 +0000 | [diff] [blame] | 1387 | if (!resultType->isAnyComplexType()) { |
| 1388 | assert(result.isAggregate() && "null init of non-aggregate result?"); |
| 1389 | CGF.EmitNullInitialization(result.getAggregateAddr(), resultType); |
| 1390 | // Jump to the continuation block. |
| 1391 | CGF.EmitBlock(contBB); |
| 1392 | return result; |
| 1393 | } |
John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 1394 | |
Fariborz Jahanian | 6c29eda | 2011-10-26 20:53:59 +0000 | [diff] [blame] | 1395 | // _Complex type |
| 1396 | // FIXME. Now easy to handle any other scalar type whose result is returned |
| 1397 | // in memory due to ABI limitations. |
John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 1398 | CGF.EmitBlock(contBB); |
Fariborz Jahanian | 6c29eda | 2011-10-26 20:53:59 +0000 | [diff] [blame] | 1399 | CodeGenFunction::ComplexPairTy CallCV = result.getComplexVal(); |
| 1400 | llvm::Type *MemberType = CallCV.first->getType(); |
| 1401 | llvm::Constant *ZeroCV = llvm::Constant::getNullValue(MemberType); |
| 1402 | // Create phi instruction for scalar complex value. |
| 1403 | llvm::PHINode *PHIReal = CGF.Builder.CreatePHI(MemberType, 2); |
| 1404 | PHIReal->addIncoming(ZeroCV, NullBB); |
| 1405 | PHIReal->addIncoming(CallCV.first, callBB); |
| 1406 | llvm::PHINode *PHIImag = CGF.Builder.CreatePHI(MemberType, 2); |
| 1407 | PHIImag->addIncoming(ZeroCV, NullBB); |
| 1408 | PHIImag->addIncoming(CallCV.second, callBB); |
| 1409 | return RValue::getComplex(PHIReal, PHIImag); |
John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 1410 | } |
| 1411 | }; |
| 1412 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1413 | } // end anonymous namespace |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1414 | |
| 1415 | /* *** Helper Functions *** */ |
| 1416 | |
| 1417 | /// getConstantGEP() - Help routine to construct simple GEPs. |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1418 | static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1419 | llvm::Constant *C, |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1420 | unsigned idx0, |
| 1421 | unsigned idx1) { |
| 1422 | llvm::Value *Idxs[] = { |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1423 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0), |
| 1424 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1425 | }; |
Jay Foad | a5c0434 | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 1426 | return llvm::ConstantExpr::getGetElementPtr(C, Idxs); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1429 | /// hasObjCExceptionAttribute - Return true if this class or any super |
| 1430 | /// class has the __objc_exception__ attribute. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1431 | static bool hasObjCExceptionAttribute(ASTContext &Context, |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 1432 | const ObjCInterfaceDecl *OID) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1433 | if (OID->hasAttr<ObjCExceptionAttr>()) |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1434 | return true; |
| 1435 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 1436 | return hasObjCExceptionAttribute(Context, Super); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1437 | return false; |
| 1438 | } |
| 1439 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1440 | /* *** CGObjCMac Public Interface *** */ |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1441 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1442 | CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1443 | ObjCTypes(cgm) { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1444 | ObjCABI = 1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1445 | EmitImageInfo(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 1448 | /// GetClass - Return a reference to the class for the given interface |
| 1449 | /// decl. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1450 | llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1451 | const ObjCInterfaceDecl *ID) { |
| 1452 | return EmitClassRef(Builder, ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
| 1455 | /// GetSelector - Return the pointer to the unique'd string for this selector. |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1456 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel, |
| 1457 | bool lval) { |
| 1458 | return EmitSelector(Builder, Sel, lval); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1459 | } |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1460 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1461 | *Method) { |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1462 | return EmitSelector(Builder, Method->getSelector()); |
| 1463 | } |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1464 | |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1465 | llvm::Constant *CGObjCMac::GetEHType(QualType T) { |
Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 1466 | if (T->isObjCIdType() || |
| 1467 | T->isObjCQualifiedIdType()) { |
| 1468 | return CGM.GetAddrOfRTTIDescriptor( |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 1469 | CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true); |
Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 1470 | } |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1471 | if (T->isObjCClassType() || |
| 1472 | T->isObjCQualifiedClassType()) { |
| 1473 | return CGM.GetAddrOfRTTIDescriptor( |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 1474 | CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true); |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1475 | } |
| 1476 | if (T->isObjCObjectPointerType()) |
| 1477 | return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true); |
| 1478 | |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1479 | llvm_unreachable("asking for catch type for ObjC type in fragile runtime"); |
| 1480 | return 0; |
| 1481 | } |
| 1482 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1483 | /// Generate a constant CFString object. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1484 | /* |
| 1485 | struct __builtin_CFString { |
| 1486 | const int *isa; // point to __CFConstantStringClassReference |
| 1487 | int flags; |
| 1488 | const char *str; |
| 1489 | long length; |
| 1490 | }; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1491 | */ |
| 1492 | |
Fariborz Jahanian | 33e982b | 2010-04-22 20:26:39 +0000 | [diff] [blame] | 1493 | /// or Generate a constant NSString object. |
| 1494 | /* |
| 1495 | struct __builtin_NSString { |
| 1496 | const int *isa; // point to __NSConstantStringClassReference |
| 1497 | const char *str; |
| 1498 | unsigned int length; |
| 1499 | }; |
| 1500 | */ |
| 1501 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1502 | llvm::Constant *CGObjCCommonMac::GenerateConstantString( |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 1503 | const StringLiteral *SL) { |
Fariborz Jahanian | 33e982b | 2010-04-22 20:26:39 +0000 | [diff] [blame] | 1504 | return (CGM.getLangOptions().NoConstantCFStrings == 0 ? |
| 1505 | CGM.GetAddrOfConstantCFString(SL) : |
Fariborz Jahanian | 4c73307 | 2010-10-19 17:19:29 +0000 | [diff] [blame] | 1506 | CGM.GetAddrOfConstantString(SL)); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | /// Generates a message send where the super is the receiver. This is |
| 1510 | /// a message send to self with special delivery semantics indicating |
| 1511 | /// which class's method should be called. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1512 | CodeGen::RValue |
| 1513 | CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1514 | ReturnValueSlot Return, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1515 | QualType ResultType, |
| 1516 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1517 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1518 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1519 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1520 | bool IsClassMessage, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1521 | const CodeGen::CallArgList &CallArgs, |
| 1522 | const ObjCMethodDecl *Method) { |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1523 | // Create and init a super structure; this is a (receiver, class) |
| 1524 | // pair we will pass to objc_msgSendSuper. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1525 | llvm::Value *ObjCSuper = |
John McCall | 604da29 | 2011-03-04 08:00:29 +0000 | [diff] [blame] | 1526 | CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1527 | llvm::Value *ReceiverAsObject = |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1528 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1529 | CGF.Builder.CreateStore(ReceiverAsObject, |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1530 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1531 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1532 | // If this is a class message the metaclass is passed as the target. |
| 1533 | llvm::Value *Target; |
| 1534 | if (IsClassMessage) { |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1535 | if (isCategoryImpl) { |
| 1536 | // Message sent to 'super' in a class method defined in a category |
| 1537 | // implementation requires an odd treatment. |
| 1538 | // If we are in a class method, we must retrieve the |
| 1539 | // _metaclass_ for the current class, pointed at by |
| 1540 | // the class's "isa" pointer. The following assumes that |
| 1541 | // isa" is the first ivar in a class (which it must be). |
| 1542 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1543 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 1544 | Target = CGF.Builder.CreateLoad(Target); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 1545 | } else { |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1546 | llvm::Value *MetaClassPtr = EmitMetaClassRef(Class); |
| 1547 | llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1); |
| 1548 | llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr); |
| 1549 | Target = Super; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1550 | } |
Fariborz Jahanian | 182f268 | 2009-11-14 02:18:31 +0000 | [diff] [blame] | 1551 | } |
| 1552 | else if (isCategoryImpl) |
| 1553 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1554 | else { |
Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 1555 | llvm::Value *ClassPtr = EmitSuperClassRef(Class); |
| 1556 | ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1); |
| 1557 | Target = CGF.Builder.CreateLoad(ClassPtr); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1558 | } |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1559 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext and |
| 1560 | // ObjCTypes types. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1561 | llvm::Type *ClassTy = |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1562 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1563 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1564 | CGF.Builder.CreateStore(Target, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1565 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1566 | return EmitMessageSend(CGF, Return, ResultType, |
| 1567 | EmitSelector(CGF.Builder, Sel), |
| 1568 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 1569 | true, CallArgs, Method, ObjCTypes); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1570 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1571 | |
| 1572 | /// Generate code for a message send expression. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1573 | CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1574 | ReturnValueSlot Return, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1575 | QualType ResultType, |
| 1576 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1577 | llvm::Value *Receiver, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1578 | const CallArgList &CallArgs, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1579 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1580 | const ObjCMethodDecl *Method) { |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1581 | return EmitMessageSend(CGF, Return, ResultType, |
| 1582 | EmitSelector(CGF.Builder, Sel), |
| 1583 | Receiver, CGF.getContext().getObjCIdType(), |
| 1584 | false, CallArgs, Method, ObjCTypes); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1585 | } |
| 1586 | |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1587 | CodeGen::RValue |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1588 | CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1589 | ReturnValueSlot Return, |
| 1590 | QualType ResultType, |
| 1591 | llvm::Value *Sel, |
| 1592 | llvm::Value *Arg0, |
| 1593 | QualType Arg0Ty, |
| 1594 | bool IsSuper, |
| 1595 | const CallArgList &CallArgs, |
| 1596 | const ObjCMethodDecl *Method, |
| 1597 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1598 | CallArgList ActualArgs; |
Fariborz Jahanian | d019d96 | 2009-04-24 21:07:43 +0000 | [diff] [blame] | 1599 | if (!IsSuper) |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 1600 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy); |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1601 | ActualArgs.add(RValue::get(Arg0), Arg0Ty); |
| 1602 | ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1603 | ActualArgs.addFrom(CallArgs); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1604 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1605 | CodeGenTypes &Types = CGM.getTypes(); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1606 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1607 | FunctionType::ExtInfo()); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1608 | llvm::FunctionType *FTy = |
Daniel Dunbar | 6793966 | 2009-09-17 04:01:40 +0000 | [diff] [blame] | 1609 | Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1610 | |
Anders Carlsson | 7e70fb2 | 2010-06-21 20:59:55 +0000 | [diff] [blame] | 1611 | if (Method) |
| 1612 | assert(CGM.getContext().getCanonicalType(Method->getResultType()) == |
| 1613 | CGM.getContext().getCanonicalType(ResultType) && |
| 1614 | "Result type mismatch!"); |
| 1615 | |
John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 1616 | NullReturnState nullReturn; |
| 1617 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1618 | llvm::Constant *Fn = NULL; |
Daniel Dunbar | dacf9dd | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 1619 | if (CGM.ReturnTypeUsesSRet(FnInfo)) { |
John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 1620 | if (!IsSuper) nullReturn.init(CGF, Arg0); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1621 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1622 | : ObjCTypes.getSendStretFn(IsSuper); |
Daniel Dunbar | dacf9dd | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 1623 | } else if (CGM.ReturnTypeUsesFPRet(ResultType)) { |
| 1624 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper) |
| 1625 | : ObjCTypes.getSendFpretFn(IsSuper); |
Anders Carlsson | eea6480 | 2011-10-31 16:27:11 +0000 | [diff] [blame] | 1626 | } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) { |
| 1627 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper) |
| 1628 | : ObjCTypes.getSendFp2retFn(IsSuper); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1629 | } else { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1630 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1631 | : ObjCTypes.getSendFn(IsSuper); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1632 | } |
Daniel Dunbar | dacf9dd | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 1633 | Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy)); |
John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 1634 | RValue rvalue = CGF.EmitCall(FnInfo, Fn, Return, ActualArgs); |
| 1635 | return nullReturn.complete(CGF, rvalue, ResultType); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1636 | } |
| 1637 | |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1638 | static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) { |
| 1639 | if (FQT.isObjCGCStrong()) |
| 1640 | return Qualifiers::Strong; |
| 1641 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1642 | if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak) |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1643 | return Qualifiers::Weak; |
| 1644 | |
| 1645 | if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType()) |
| 1646 | return Qualifiers::Strong; |
| 1647 | |
| 1648 | if (const PointerType *PT = FQT->getAs<PointerType>()) |
| 1649 | return GetGCAttrTypeForType(Ctx, PT->getPointeeType()); |
| 1650 | |
| 1651 | return Qualifiers::GCNone; |
| 1652 | } |
| 1653 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1654 | llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM, |
| 1655 | const CGBlockInfo &blockInfo) { |
| 1656 | llvm::Constant *nullPtr = |
Fariborz Jahanian | 44034db | 2010-08-04 18:44:59 +0000 | [diff] [blame] | 1657 | llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext)); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1658 | |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 1659 | if (CGM.getLangOptions().getGC() == LangOptions::NonGC && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1660 | !CGM.getLangOptions().ObjCAutoRefCount) |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1661 | return nullPtr; |
| 1662 | |
Fariborz Jahanian | a1f024c | 2010-08-06 16:28:55 +0000 | [diff] [blame] | 1663 | bool hasUnion = false; |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1664 | SkipIvars.clear(); |
| 1665 | IvarsInfo.clear(); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1666 | unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0); |
| 1667 | unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth(); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1668 | |
Fariborz Jahanian | 8197982 | 2010-09-09 00:21:45 +0000 | [diff] [blame] | 1669 | // __isa is the first field in block descriptor and must assume by runtime's |
| 1670 | // convention that it is GC'able. |
| 1671 | IvarsInfo.push_back(GC_IVAR(0, 1)); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1672 | |
| 1673 | const BlockDecl *blockDecl = blockInfo.getBlockDecl(); |
| 1674 | |
| 1675 | // Calculate the basic layout of the block structure. |
| 1676 | const llvm::StructLayout *layout = |
| 1677 | CGM.getTargetData().getStructLayout(blockInfo.StructureType); |
| 1678 | |
| 1679 | // Ignore the optional 'this' capture: C++ objects are not assumed |
| 1680 | // to be GC'ed. |
| 1681 | |
| 1682 | // Walk the captured variables. |
| 1683 | for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(), |
| 1684 | ce = blockDecl->capture_end(); ci != ce; ++ci) { |
| 1685 | const VarDecl *variable = ci->getVariable(); |
| 1686 | QualType type = variable->getType(); |
| 1687 | |
| 1688 | const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); |
| 1689 | |
| 1690 | // Ignore constant captures. |
| 1691 | if (capture.isConstant()) continue; |
| 1692 | |
| 1693 | uint64_t fieldOffset = layout->getElementOffset(capture.getIndex()); |
| 1694 | |
| 1695 | // __block variables are passed by their descriptor address. |
| 1696 | if (ci->isByRef()) { |
| 1697 | IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1)); |
Fariborz Jahanian | c5904b4 | 2010-09-11 01:27:29 +0000 | [diff] [blame] | 1698 | continue; |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1699 | } |
| 1700 | |
| 1701 | assert(!type->isArrayType() && "array variable should not be caught"); |
| 1702 | if (const RecordType *record = type->getAs<RecordType>()) { |
| 1703 | BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion); |
Fariborz Jahanian | e1a4898 | 2010-08-05 21:00:25 +0000 | [diff] [blame] | 1704 | continue; |
| 1705 | } |
Fariborz Jahanian | a1f024c | 2010-08-06 16:28:55 +0000 | [diff] [blame] | 1706 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1707 | Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type); |
| 1708 | unsigned fieldSize = CGM.getContext().getTypeSize(type); |
| 1709 | |
| 1710 | if (GCAttr == Qualifiers::Strong) |
| 1711 | IvarsInfo.push_back(GC_IVAR(fieldOffset, |
| 1712 | fieldSize / WordSizeInBits)); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1713 | else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak) |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1714 | SkipIvars.push_back(GC_IVAR(fieldOffset, |
| 1715 | fieldSize / ByteSizeInBits)); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | if (IvarsInfo.empty()) |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1719 | return nullPtr; |
| 1720 | |
| 1721 | // Sort on byte position; captures might not be allocated in order, |
| 1722 | // and unions can do funny things. |
| 1723 | llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end()); |
| 1724 | llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end()); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1725 | |
| 1726 | std::string BitMap; |
Fariborz Jahanian | b8fd2eb | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 1727 | llvm::Constant *C = BuildIvarLayoutBitmap(BitMap); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1728 | if (CGM.getLangOptions().ObjCGCBitmapPrint) { |
| 1729 | printf("\n block variable layout for block: "); |
| 1730 | const unsigned char *s = (unsigned char*)BitMap.c_str(); |
| 1731 | for (unsigned i = 0; i < BitMap.size(); i++) |
| 1732 | if (!(s[i] & 0xf0)) |
| 1733 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 1734 | else |
| 1735 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 1736 | printf("\n"); |
| 1737 | } |
| 1738 | |
| 1739 | return C; |
Fariborz Jahanian | 89ecd41 | 2010-08-04 16:57:49 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1742 | llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1743 | const ObjCProtocolDecl *PD) { |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1744 | // 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] | 1745 | // resolved. Investigate. Its also wasteful to look this up over and over. |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1746 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1747 | |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1748 | return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1749 | ObjCTypes.ExternalProtocolPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1750 | } |
| 1751 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1752 | void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1753 | // FIXME: We shouldn't need this, the protocol decl should contain enough |
| 1754 | // information to tell us whether this was a declaration or a definition. |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1755 | DefinedProtocols.insert(PD->getIdentifier()); |
| 1756 | |
| 1757 | // If we have generated a forward reference to this protocol, emit |
| 1758 | // it now. Otherwise do nothing, the protocol objects are lazily |
| 1759 | // emitted. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1760 | if (Protocols.count(PD->getIdentifier())) |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1761 | GetOrEmitProtocol(PD); |
| 1762 | } |
| 1763 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1764 | llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1765 | if (DefinedProtocols.count(PD->getIdentifier())) |
| 1766 | return GetOrEmitProtocol(PD); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 1767 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1768 | return GetOrEmitProtocolRef(PD); |
| 1769 | } |
| 1770 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1771 | /* |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1772 | // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions |
| 1773 | struct _objc_protocol { |
| 1774 | struct _objc_protocol_extension *isa; |
| 1775 | char *protocol_name; |
| 1776 | struct _objc_protocol_list *protocol_list; |
| 1777 | struct _objc__method_prototype_list *instance_methods; |
| 1778 | struct _objc__method_prototype_list *class_methods |
| 1779 | }; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1780 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1781 | See EmitProtocolExtension(). |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1782 | */ |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1783 | llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) { |
| 1784 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1785 | |
| 1786 | // Early exit if a defining object has already been generated. |
| 1787 | if (Entry && Entry->hasInitializer()) |
| 1788 | return Entry; |
| 1789 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1790 | // 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] | 1791 | // resolved. Investigate. Its also wasteful to look this up over and over. |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1792 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1793 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1794 | // Construct method lists. |
| 1795 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1796 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1797 | std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1798 | for (ObjCProtocolDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1799 | i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1800 | ObjCMethodDecl *MD = *i; |
| 1801 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 1802 | if (!C) |
| 1803 | return GetOrEmitProtocolRef(PD); |
| 1804 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1805 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1806 | OptInstanceMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1807 | OptMethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1808 | } else { |
| 1809 | InstanceMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1810 | MethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1811 | } |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1812 | } |
| 1813 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1814 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1815 | i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1816 | ObjCMethodDecl *MD = *i; |
| 1817 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 1818 | if (!C) |
| 1819 | return GetOrEmitProtocolRef(PD); |
| 1820 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1821 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1822 | OptClassMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1823 | OptMethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1824 | } else { |
| 1825 | ClassMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1826 | MethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1827 | } |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1830 | MethodTypesExt.insert(MethodTypesExt.end(), |
| 1831 | OptMethodTypesExt.begin(), OptMethodTypesExt.end()); |
| 1832 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1833 | llvm::Constant *Values[] = { |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1834 | EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods, |
| 1835 | MethodTypesExt), |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1836 | GetClassName(PD->getIdentifier()), |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1837 | EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1838 | PD->protocol_begin(), |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1839 | PD->protocol_end()), |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1840 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1841 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1842 | InstanceMethods), |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1843 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1844 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1845 | ClassMethods) |
| 1846 | }; |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 1847 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1848 | Values); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1849 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1850 | if (Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1851 | // Already created, fix the linkage and update the initializer. |
| 1852 | Entry->setLinkage(llvm::GlobalValue::InternalLinkage); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1853 | Entry->setInitializer(Init); |
| 1854 | } else { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1855 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1856 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1857 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1858 | Init, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1859 | "\01L_OBJC_PROTOCOL_" + PD->getName()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1860 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1861 | // FIXME: Is this necessary? Why only for protocol? |
| 1862 | Entry->setAlignment(4); |
| 1863 | } |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 1864 | CGM.AddUsedGlobal(Entry); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1865 | |
| 1866 | return Entry; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1869 | llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1870 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1871 | |
| 1872 | if (!Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1873 | // We use the initializer as a marker of whether this is a forward |
| 1874 | // reference or not. At module finalization we add the empty |
| 1875 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1876 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1877 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1878 | llvm::GlobalValue::ExternalLinkage, |
| 1879 | 0, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1880 | "\01L_OBJC_PROTOCOL_" + PD->getName()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1881 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1882 | // FIXME: Is this necessary? Why only for protocol? |
| 1883 | Entry->setAlignment(4); |
| 1884 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1885 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1886 | return Entry; |
| 1887 | } |
| 1888 | |
| 1889 | /* |
| 1890 | struct _objc_protocol_extension { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1891 | uint32_t size; |
| 1892 | struct objc_method_description_list *optional_instance_methods; |
| 1893 | struct objc_method_description_list *optional_class_methods; |
| 1894 | struct objc_property_list *instance_properties; |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1895 | const char ** extendedMethodTypes; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1896 | }; |
| 1897 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1898 | llvm::Constant * |
| 1899 | CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 1900 | const ConstantVector &OptInstanceMethods, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1901 | const ConstantVector &OptClassMethods, |
| 1902 | const ConstantVector &MethodTypesExt) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1903 | uint64_t Size = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 1904 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy); |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1905 | llvm::Constant *Values[] = { |
| 1906 | llvm::ConstantInt::get(ObjCTypes.IntTy, Size), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1907 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1908 | + PD->getName(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1909 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1910 | OptInstanceMethods), |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1911 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1912 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1913 | OptClassMethods), |
| 1914 | EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1915 | ObjCTypes), |
| 1916 | EmitProtocolMethodTypes("\01L_OBJC_PROTOCOL_METHOD_TYPES_" + PD->getName(), |
| 1917 | MethodTypesExt, ObjCTypes) |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1918 | }; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1919 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1920 | // Return null if no extension bits are used. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1921 | if (Values[1]->isNullValue() && Values[2]->isNullValue() && |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 1922 | Values[3]->isNullValue() && Values[4]->isNullValue()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1923 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1924 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1925 | llvm::Constant *Init = |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 1926 | llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1927 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1928 | // No special section, but goes in llvm.used |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1929 | return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1930 | Init, |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1931 | 0, 0, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1932 | } |
| 1933 | |
| 1934 | /* |
| 1935 | struct objc_protocol_list { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1936 | struct objc_protocol_list *next; |
| 1937 | long count; |
| 1938 | Protocol *list[]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1939 | }; |
| 1940 | */ |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1941 | llvm::Constant * |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1942 | CGObjCMac::EmitProtocolList(Twine Name, |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1943 | ObjCProtocolDecl::protocol_iterator begin, |
| 1944 | ObjCProtocolDecl::protocol_iterator end) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1945 | std::vector<llvm::Constant*> ProtocolRefs; |
| 1946 | |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1947 | for (; begin != end; ++begin) |
| 1948 | ProtocolRefs.push_back(GetProtocolRef(*begin)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1949 | |
| 1950 | // Just return null for empty protocol lists |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1951 | if (ProtocolRefs.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1952 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1953 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1954 | // This list is null terminated. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1955 | ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1956 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 1957 | llvm::Constant *Values[3]; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1958 | // This field is only used by the runtime. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1959 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1960 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1961 | ProtocolRefs.size() - 1); |
| 1962 | Values[2] = |
| 1963 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy, |
| 1964 | ProtocolRefs.size()), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1965 | ProtocolRefs); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1966 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 1967 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1968 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1969 | CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1970 | 4, false); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1971 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1972 | } |
| 1973 | |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 1974 | void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet, |
| 1975 | std::vector<llvm::Constant*> &Properties, |
| 1976 | const Decl *Container, |
| 1977 | const ObjCProtocolDecl *PROTO, |
| 1978 | const ObjCCommonTypesHelper &ObjCTypes) { |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 1979 | for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(), |
| 1980 | E = PROTO->protocol_end(); P != E; ++P) |
| 1981 | PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes); |
| 1982 | for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(), |
| 1983 | E = PROTO->prop_end(); I != E; ++I) { |
| 1984 | const ObjCPropertyDecl *PD = *I; |
| 1985 | if (!PropertySet.insert(PD->getIdentifier())) |
| 1986 | continue; |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1987 | llvm::Constant *Prop[] = { |
| 1988 | GetPropertyName(PD->getIdentifier()), |
| 1989 | GetPropertyTypeString(PD, Container) |
| 1990 | }; |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 1991 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop)); |
| 1992 | } |
| 1993 | } |
| 1994 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1995 | /* |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1996 | struct _objc_property { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1997 | const char * const name; |
| 1998 | const char * const attributes; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1999 | }; |
| 2000 | |
| 2001 | struct _objc_property_list { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2002 | uint32_t entsize; // sizeof (struct _objc_property) |
| 2003 | uint32_t prop_count; |
| 2004 | struct _objc_property[prop_count]; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2005 | }; |
| 2006 | */ |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2007 | llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2008 | const Decl *Container, |
| 2009 | const ObjCContainerDecl *OCD, |
| 2010 | const ObjCCommonTypesHelper &ObjCTypes) { |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2011 | std::vector<llvm::Constant*> Properties; |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2012 | llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2013 | for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(), |
| 2014 | E = OCD->prop_end(); I != E; ++I) { |
Steve Naroff | 93983f8 | 2009-01-11 12:47:58 +0000 | [diff] [blame] | 2015 | const ObjCPropertyDecl *PD = *I; |
Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 2016 | PropertySet.insert(PD->getIdentifier()); |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2017 | llvm::Constant *Prop[] = { |
| 2018 | GetPropertyName(PD->getIdentifier()), |
| 2019 | GetPropertyTypeString(PD, Container) |
| 2020 | }; |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2021 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2022 | Prop)); |
| 2023 | } |
Fariborz Jahanian | 6afbdf5 | 2010-06-22 16:33:55 +0000 | [diff] [blame] | 2024 | if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) { |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 2025 | for (ObjCInterfaceDecl::all_protocol_iterator |
| 2026 | P = OID->all_referenced_protocol_begin(), |
| 2027 | E = OID->all_referenced_protocol_end(); P != E; ++P) |
Fariborz Jahanian | 6afbdf5 | 2010-06-22 16:33:55 +0000 | [diff] [blame] | 2028 | PushProtocolProperties(PropertySet, Properties, Container, (*P), |
| 2029 | ObjCTypes); |
| 2030 | } |
| 2031 | else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) { |
| 2032 | for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(), |
| 2033 | E = CD->protocol_end(); P != E; ++P) |
| 2034 | PushProtocolProperties(PropertySet, Properties, Container, (*P), |
| 2035 | ObjCTypes); |
| 2036 | } |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2037 | |
| 2038 | // Return null for empty list. |
| 2039 | if (Properties.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2040 | return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2041 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2042 | unsigned PropertySize = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2043 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2044 | llvm::Constant *Values[3]; |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2045 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize); |
| 2046 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2047 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy, |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2048 | Properties.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2049 | Values[2] = llvm::ConstantArray::get(AT, Properties); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2050 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2051 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2052 | llvm::GlobalVariable *GV = |
| 2053 | CreateMetadataVar(Name, Init, |
| 2054 | (ObjCABI == 2) ? "__DATA, __objc_const" : |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2055 | "__OBJC,__property,regular,no_dead_strip", |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2056 | (ObjCABI == 2) ? 8 : 4, |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2057 | true); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2058 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 2061 | llvm::Constant *CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name, |
| 2062 | const ConstantVector &MethodTypes, |
| 2063 | const ObjCCommonTypesHelper &ObjCTypes) { |
| 2064 | // Return null for empty list. |
| 2065 | if (MethodTypes.empty()) |
| 2066 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy); |
| 2067 | |
| 2068 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 2069 | MethodTypes.size()); |
| 2070 | llvm::Constant *Init = llvm::ConstantArray::get(AT, MethodTypes); |
| 2071 | |
| 2072 | llvm::GlobalVariable *GV = |
| 2073 | CreateMetadataVar(Name, Init, |
| 2074 | (ObjCABI == 2) ? "__DATA, __objc_const" : 0, |
| 2075 | (ObjCABI == 2) ? 8 : 4, |
| 2076 | true); |
| 2077 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.Int8PtrPtrTy); |
| 2078 | } |
| 2079 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2080 | /* |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2081 | struct objc_method_description_list { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2082 | int count; |
| 2083 | struct objc_method_description list[]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2084 | }; |
| 2085 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2086 | llvm::Constant * |
| 2087 | CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2088 | llvm::Constant *Desc[] = { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2089 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2090 | ObjCTypes.SelectorPtrTy), |
| 2091 | GetMethodVarType(MD) |
| 2092 | }; |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 2093 | if (!Desc[1]) |
| 2094 | return 0; |
| 2095 | |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2096 | return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2097 | Desc); |
| 2098 | } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2099 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2100 | llvm::Constant *CGObjCMac::EmitMethodDescList(Twine Name, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2101 | const char *Section, |
| 2102 | const ConstantVector &Methods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2103 | // Return null for empty list. |
| 2104 | if (Methods.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2105 | return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2106 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2107 | llvm::Constant *Values[2]; |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2108 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2109 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2110 | Methods.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2111 | Values[1] = llvm::ConstantArray::get(AT, Methods); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2112 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2113 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2114 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2115 | return llvm::ConstantExpr::getBitCast(GV, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2116 | ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2117 | } |
| 2118 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2119 | /* |
| 2120 | struct _objc_category { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2121 | char *category_name; |
| 2122 | char *class_name; |
| 2123 | struct _objc_method_list *instance_methods; |
| 2124 | struct _objc_method_list *class_methods; |
| 2125 | struct _objc_protocol_list *protocols; |
| 2126 | uint32_t size; // <rdar://4585769> |
| 2127 | struct _objc_property_list *instance_properties; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2128 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2129 | */ |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2130 | void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2131 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2132 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2133 | // FIXME: This is poor design, the OCD should have a pointer to the category |
| 2134 | // decl. Additionally, note that Category can be null for the @implementation |
| 2135 | // w/o an @interface case. Sema should just create one for us as it does for |
| 2136 | // @implementation so everyone else can live life under a clear blue sky. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2137 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2138 | const ObjCCategoryDecl *Category = |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2139 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2140 | |
| 2141 | llvm::SmallString<256> ExtName; |
| 2142 | llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_' |
| 2143 | << OCD->getName(); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2144 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2145 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2146 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2147 | i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2148 | // Instance methods should always be defined. |
| 2149 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 2150 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2151 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2152 | i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2153 | // Class methods should always be defined. |
| 2154 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 2155 | } |
| 2156 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2157 | llvm::Constant *Values[7]; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2158 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 2159 | Values[1] = GetClassName(Interface->getIdentifier()); |
Fariborz Jahanian | 679cd7f | 2009-04-29 20:40:05 +0000 | [diff] [blame] | 2160 | LazySymbols.insert(Interface->getIdentifier()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2161 | Values[2] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2162 | EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(), |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2163 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2164 | InstanceMethods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2165 | Values[3] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2166 | EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2167 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2168 | ClassMethods); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2169 | if (Category) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2170 | Values[4] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2171 | EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2172 | Category->protocol_begin(), |
| 2173 | Category->protocol_end()); |
| 2174 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2175 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2176 | } |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2177 | Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2178 | |
| 2179 | // If there is no category @interface then there can be no properties. |
| 2180 | if (Category) { |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2181 | Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 2182 | OCD, Category, ObjCTypes); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2183 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2184 | Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2185 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2186 | |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2187 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2188 | Values); |
| 2189 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2190 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2191 | CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init, |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2192 | "__OBJC,__category,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2193 | 4, true); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2194 | DefinedCategories.push_back(GV); |
Fariborz Jahanian | b9c5b3d | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 2195 | DefinedCategoryNames.insert(ExtName.str()); |
Fariborz Jahanian | 64089ce | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 2196 | // method definition entries must be clear for next implementation. |
| 2197 | MethodDefinitions.clear(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2198 | } |
| 2199 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2200 | // FIXME: Get from somewhere? |
| 2201 | enum ClassFlags { |
| 2202 | eClassFlags_Factory = 0x00001, |
| 2203 | eClassFlags_Meta = 0x00002, |
| 2204 | // <rdr://5142207> |
| 2205 | eClassFlags_HasCXXStructors = 0x02000, |
| 2206 | eClassFlags_Hidden = 0x20000, |
| 2207 | eClassFlags_ABI2_Hidden = 0x00010, |
| 2208 | eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634> |
| 2209 | }; |
| 2210 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2211 | /* |
| 2212 | struct _objc_class { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2213 | Class isa; |
| 2214 | Class super_class; |
| 2215 | const char *name; |
| 2216 | long version; |
| 2217 | long info; |
| 2218 | long instance_size; |
| 2219 | struct _objc_ivar_list *ivars; |
| 2220 | struct _objc_method_list *methods; |
| 2221 | struct _objc_cache *cache; |
| 2222 | struct _objc_protocol_list *protocols; |
| 2223 | // Objective-C 1.0 extensions (<rdr://4585769>) |
| 2224 | const char *ivar_layout; |
| 2225 | struct _objc_class_ext *ext; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2226 | }; |
| 2227 | |
| 2228 | See EmitClassExtension(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2229 | */ |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2230 | void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2231 | DefinedSymbols.insert(ID->getIdentifier()); |
| 2232 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2233 | std::string ClassName = ID->getNameAsString(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2234 | // FIXME: Gross |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2235 | ObjCInterfaceDecl *Interface = |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2236 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2237 | llvm::Constant *Protocols = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2238 | EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(), |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 2239 | Interface->all_referenced_protocol_begin(), |
| 2240 | Interface->all_referenced_protocol_end()); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2241 | unsigned Flags = eClassFlags_Factory; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2242 | if (ID->hasCXXStructors()) |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 2243 | Flags |= eClassFlags_HasCXXStructors; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2244 | unsigned Size = |
Ken Dyck | 5f022d8 | 2011-02-09 01:59:34 +0000 | [diff] [blame] | 2245 | CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2246 | |
| 2247 | // FIXME: Set CXX-structors flag. |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 2248 | if (ID->getClassInterface()->getVisibility() == HiddenVisibility) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2249 | Flags |= eClassFlags_Hidden; |
| 2250 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2251 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2252 | for (ObjCImplementationDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2253 | i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2254 | // Instance methods should always be defined. |
| 2255 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 2256 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2257 | for (ObjCImplementationDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2258 | i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2259 | // Class methods should always be defined. |
| 2260 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 2261 | } |
| 2262 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2263 | for (ObjCImplementationDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2264 | i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2265 | ObjCPropertyImplDecl *PID = *i; |
| 2266 | |
| 2267 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 2268 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 2269 | |
| 2270 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 2271 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 2272 | InstanceMethods.push_back(C); |
| 2273 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 2274 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 2275 | InstanceMethods.push_back(C); |
| 2276 | } |
| 2277 | } |
| 2278 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2279 | llvm::Constant *Values[12]; |
Daniel Dunbar | 5384b09 | 2009-05-03 08:56:52 +0000 | [diff] [blame] | 2280 | Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2281 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2282 | // Record a reference to the super class. |
| 2283 | LazySymbols.insert(Super->getIdentifier()); |
| 2284 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2285 | Values[ 1] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2286 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2287 | ObjCTypes.ClassPtrTy); |
| 2288 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2289 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2290 | } |
| 2291 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 2292 | // Version is always 0. |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2293 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2294 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 2295 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2296 | Values[ 6] = EmitIvarList(ID, false); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2297 | Values[ 7] = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2298 | EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(), |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2299 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2300 | InstanceMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2301 | // cache is always NULL. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2302 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2303 | Values[ 9] = Protocols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2304 | Values[10] = BuildIvarLayout(ID, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2305 | Values[11] = EmitClassExtension(ID); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2306 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2307 | Values); |
Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 2308 | std::string Name("\01L_OBJC_CLASS_"); |
| 2309 | Name += ClassName; |
| 2310 | const char *Section = "__OBJC,__class,regular,no_dead_strip"; |
| 2311 | // Check for a forward reference. |
| 2312 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 2313 | if (GV) { |
| 2314 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2315 | "Forward metaclass reference has incorrect type."); |
| 2316 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 2317 | GV->setInitializer(Init); |
| 2318 | GV->setSection(Section); |
| 2319 | GV->setAlignment(4); |
| 2320 | CGM.AddUsedGlobal(GV); |
| 2321 | } |
| 2322 | else |
| 2323 | GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2324 | DefinedClasses.push_back(GV); |
Fariborz Jahanian | 64089ce | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 2325 | // method definition entries must be clear for next implementation. |
| 2326 | MethodDefinitions.clear(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2327 | } |
| 2328 | |
| 2329 | llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID, |
| 2330 | llvm::Constant *Protocols, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2331 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2332 | unsigned Flags = eClassFlags_Meta; |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2333 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2334 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 2335 | if (ID->getClassInterface()->getVisibility() == HiddenVisibility) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2336 | Flags |= eClassFlags_Hidden; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2337 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2338 | llvm::Constant *Values[12]; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2339 | // The isa for the metaclass is the root of the hierarchy. |
| 2340 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 2341 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 2342 | Root = Super; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2343 | Values[ 0] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2344 | llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2345 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2346 | // The super class for the metaclass is emitted as the name of the |
| 2347 | // super class. The runtime fixes this up to point to the |
| 2348 | // *metaclass* for the super class. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2349 | if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2350 | Values[ 1] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2351 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2352 | ObjCTypes.ClassPtrTy); |
| 2353 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2354 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2355 | } |
| 2356 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 2357 | // Version is always 0. |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2358 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2359 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 2360 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2361 | Values[ 6] = EmitIvarList(ID, true); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2362 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2363 | EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2364 | "__OBJC,__cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2365 | Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2366 | // cache is always NULL. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2367 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2368 | Values[ 9] = Protocols; |
| 2369 | // ivar_layout for metaclass is always NULL. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2370 | Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2371 | // The class extension is always unused for metaclasses. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2372 | Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2373 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2374 | Values); |
| 2375 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2376 | std::string Name("\01L_OBJC_METACLASS_"); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2377 | Name += ID->getNameAsCString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2378 | |
| 2379 | // Check for a forward reference. |
| 2380 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 2381 | if (GV) { |
| 2382 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2383 | "Forward metaclass reference has incorrect type."); |
| 2384 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 2385 | GV->setInitializer(Init); |
| 2386 | } else { |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2387 | GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2388 | llvm::GlobalValue::InternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2389 | Init, Name); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2390 | } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2391 | GV->setSection("__OBJC,__meta_class,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2392 | GV->setAlignment(4); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2393 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2394 | |
| 2395 | return GV; |
| 2396 | } |
| 2397 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2398 | llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2399 | std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2400 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2401 | // FIXME: Should we look these up somewhere other than the module. Its a bit |
| 2402 | // silly since we only generate these while processing an implementation, so |
| 2403 | // exactly one pointer would work if know when we entered/exitted an |
| 2404 | // implementation block. |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2405 | |
| 2406 | // Check for an existing forward reference. |
Fariborz Jahanian | b0d2794 | 2009-01-07 20:11:22 +0000 | [diff] [blame] | 2407 | // Previously, metaclass with internal linkage may have been defined. |
| 2408 | // pass 'true' as 2nd argument so it is returned. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2409 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, |
| 2410 | true)) { |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2411 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2412 | "Forward metaclass reference has incorrect type."); |
| 2413 | return GV; |
| 2414 | } else { |
| 2415 | // Generate as an external reference to keep a consistent |
| 2416 | // module. This will be patched up when we emit the metaclass. |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2417 | return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2418 | llvm::GlobalValue::ExternalLinkage, |
| 2419 | 0, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2420 | Name); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2421 | } |
| 2422 | } |
| 2423 | |
Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 2424 | llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) { |
| 2425 | std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString(); |
| 2426 | |
| 2427 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, |
| 2428 | true)) { |
| 2429 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2430 | "Forward class metadata reference has incorrect type."); |
| 2431 | return GV; |
| 2432 | } else { |
| 2433 | return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
| 2434 | llvm::GlobalValue::ExternalLinkage, |
| 2435 | 0, |
| 2436 | Name); |
| 2437 | } |
| 2438 | } |
| 2439 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2440 | /* |
| 2441 | struct objc_class_ext { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2442 | uint32_t size; |
| 2443 | const char *weak_ivar_layout; |
| 2444 | struct _objc_property_list *properties; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2445 | }; |
| 2446 | */ |
| 2447 | llvm::Constant * |
| 2448 | CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2449 | uint64_t Size = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2450 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2451 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2452 | llvm::Constant *Values[3]; |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2453 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 2454 | Values[1] = BuildIvarLayout(ID, false); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2455 | Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 2456 | ID, ID->getClassInterface(), ObjCTypes); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2457 | |
| 2458 | // Return null if no extension bits are used. |
| 2459 | if (Values[1]->isNullValue() && Values[2]->isNullValue()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2460 | return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2461 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2462 | llvm::Constant *Init = |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2463 | llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2464 | return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2465 | Init, "__OBJC,__class_ext,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2466 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2467 | } |
| 2468 | |
| 2469 | /* |
| 2470 | struct objc_ivar { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2471 | char *ivar_name; |
| 2472 | char *ivar_type; |
| 2473 | int ivar_offset; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2474 | }; |
| 2475 | |
| 2476 | struct objc_ivar_list { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2477 | int ivar_count; |
| 2478 | struct objc_ivar list[count]; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2479 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2480 | */ |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2481 | llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2482 | bool ForClass) { |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2483 | std::vector<llvm::Constant*> Ivars; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2484 | |
| 2485 | // When emitting the root class GCC emits ivar entries for the |
| 2486 | // actual class structure. It is not clear if we need to follow this |
| 2487 | // behavior; for now lets try and get away with not doing it. If so, |
| 2488 | // the cleanest solution would be to make up an ObjCInterfaceDecl |
| 2489 | // for the class. |
| 2490 | if (ForClass) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2491 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2492 | |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 2493 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2494 | |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 2495 | for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin(); |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 2496 | IVD; IVD = IVD->getNextIvar()) { |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 2497 | // Ignore unnamed bit-fields. |
| 2498 | if (!IVD->getDeclName()) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2499 | continue; |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2500 | llvm::Constant *Ivar[] = { |
| 2501 | GetMethodVarName(IVD->getIdentifier()), |
| 2502 | GetMethodVarType(IVD), |
| 2503 | llvm::ConstantInt::get(ObjCTypes.IntTy, |
| 2504 | ComputeIvarBaseOffset(CGM, OID, IVD)) |
| 2505 | }; |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2506 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2507 | } |
| 2508 | |
| 2509 | // Return null for empty list. |
| 2510 | if (Ivars.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2511 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2512 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2513 | llvm::Constant *Values[2]; |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2514 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2515 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2516 | Ivars.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2517 | Values[1] = llvm::ConstantArray::get(AT, Ivars); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2518 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2519 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2520 | llvm::GlobalVariable *GV; |
| 2521 | if (ForClass) |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2522 | GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2523 | Init, "__OBJC,__class_vars,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2524 | 4, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2525 | else |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2526 | GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2527 | Init, "__OBJC,__instance_vars,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2528 | 4, true); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2529 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2530 | } |
| 2531 | |
| 2532 | /* |
| 2533 | struct objc_method { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2534 | SEL method_name; |
| 2535 | char *method_types; |
| 2536 | void *method; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2537 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2538 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2539 | struct objc_method_list { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2540 | struct objc_method_list *obsolete; |
| 2541 | int count; |
| 2542 | struct objc_method methods_list[count]; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2543 | }; |
| 2544 | */ |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2545 | |
| 2546 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 2547 | /// given method if it has been defined. The result is null if the |
| 2548 | /// method has not been defined. The return value has type MethodPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2549 | llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) { |
Argyrios Kyrtzidis | 9d50c06 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 2550 | llvm::Function *Fn = GetMethodDefinition(MD); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2551 | if (!Fn) |
| 2552 | return 0; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2553 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2554 | llvm::Constant *Method[] = { |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2555 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2556 | ObjCTypes.SelectorPtrTy), |
| 2557 | GetMethodVarType(MD), |
| 2558 | llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy) |
| 2559 | }; |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2560 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2561 | } |
| 2562 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2563 | llvm::Constant *CGObjCMac::EmitMethodList(Twine Name, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2564 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2565 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2566 | // Return null for empty list. |
| 2567 | if (Methods.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2568 | return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2569 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2570 | llvm::Constant *Values[3]; |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2571 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2572 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2573 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2574 | Methods.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2575 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2576 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2577 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2578 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2579 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2580 | } |
| 2581 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 2582 | llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2583 | const ObjCContainerDecl *CD) { |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 2584 | llvm::SmallString<256> Name; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 2585 | GetNameForMethod(OMD, CD, Name); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2586 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2587 | CodeGenTypes &Types = CGM.getTypes(); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2588 | llvm::FunctionType *MethodTy = |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2589 | Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2590 | llvm::Function *Method = |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 2591 | llvm::Function::Create(MethodTy, |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2592 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 2593 | Name.str(), |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2594 | &CGM.getModule()); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2595 | MethodDefinitions.insert(std::make_pair(OMD, Method)); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2596 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2597 | return Method; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2598 | } |
| 2599 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2600 | llvm::GlobalVariable * |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2601 | CGObjCCommonMac::CreateMetadataVar(Twine Name, |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2602 | llvm::Constant *Init, |
| 2603 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2604 | unsigned Align, |
| 2605 | bool AddToUsed) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2606 | llvm::Type *Ty = Init->getType(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2607 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2608 | new llvm::GlobalVariable(CGM.getModule(), Ty, false, |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2609 | llvm::GlobalValue::InternalLinkage, Init, Name); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2610 | if (Section) |
| 2611 | GV->setSection(Section); |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2612 | if (Align) |
| 2613 | GV->setAlignment(Align); |
| 2614 | if (AddToUsed) |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2615 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2616 | return GV; |
| 2617 | } |
| 2618 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2619 | llvm::Function *CGObjCMac::ModuleInitFunction() { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2620 | // Abuse this interface function as a place to finalize. |
| 2621 | FinishModule(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2622 | return NULL; |
| 2623 | } |
| 2624 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2625 | llvm::Constant *CGObjCMac::GetPropertyGetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2626 | return ObjCTypes.getGetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2627 | } |
| 2628 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2629 | llvm::Constant *CGObjCMac::GetPropertySetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2630 | return ObjCTypes.getSetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2631 | } |
| 2632 | |
David Chisnall | 8fac25d | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 2633 | llvm::Constant *CGObjCMac::GetGetStructFunction() { |
| 2634 | return ObjCTypes.getCopyStructFn(); |
| 2635 | } |
| 2636 | llvm::Constant *CGObjCMac::GetSetStructFunction() { |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 2637 | return ObjCTypes.getCopyStructFn(); |
| 2638 | } |
| 2639 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2640 | llvm::Constant *CGObjCMac::EnumerationMutationFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2641 | return ObjCTypes.getEnumerationMutationFn(); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 2642 | } |
| 2643 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2644 | void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) { |
| 2645 | return EmitTryOrSynchronizedStmt(CGF, S); |
| 2646 | } |
| 2647 | |
| 2648 | void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF, |
| 2649 | const ObjCAtSynchronizedStmt &S) { |
| 2650 | return EmitTryOrSynchronizedStmt(CGF, S); |
| 2651 | } |
| 2652 | |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2653 | namespace { |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 2654 | struct PerformFragileFinally : EHScopeStack::Cleanup { |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2655 | const Stmt &S; |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2656 | llvm::Value *SyncArgSlot; |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2657 | llvm::Value *CallTryExitVar; |
| 2658 | llvm::Value *ExceptionData; |
| 2659 | ObjCTypesHelper &ObjCTypes; |
| 2660 | PerformFragileFinally(const Stmt *S, |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2661 | llvm::Value *SyncArgSlot, |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2662 | llvm::Value *CallTryExitVar, |
| 2663 | llvm::Value *ExceptionData, |
| 2664 | ObjCTypesHelper *ObjCTypes) |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2665 | : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar), |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2666 | ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {} |
| 2667 | |
John McCall | ad346f4 | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 2668 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2669 | // Check whether we need to call objc_exception_try_exit. |
| 2670 | // In optimized code, this branch will always be folded. |
| 2671 | llvm::BasicBlock *FinallyCallExit = |
| 2672 | CGF.createBasicBlock("finally.call_exit"); |
| 2673 | llvm::BasicBlock *FinallyNoCallExit = |
| 2674 | CGF.createBasicBlock("finally.no_call_exit"); |
| 2675 | CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar), |
| 2676 | FinallyCallExit, FinallyNoCallExit); |
| 2677 | |
| 2678 | CGF.EmitBlock(FinallyCallExit); |
| 2679 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData) |
| 2680 | ->setDoesNotThrow(); |
| 2681 | |
| 2682 | CGF.EmitBlock(FinallyNoCallExit); |
| 2683 | |
| 2684 | if (isa<ObjCAtTryStmt>(S)) { |
| 2685 | if (const ObjCAtFinallyStmt* FinallyStmt = |
John McCall | d96a8e7 | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 2686 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) { |
| 2687 | // Save the current cleanup destination in case there's |
| 2688 | // control flow inside the finally statement. |
| 2689 | llvm::Value *CurCleanupDest = |
| 2690 | CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot()); |
| 2691 | |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2692 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
| 2693 | |
John McCall | d96a8e7 | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 2694 | if (CGF.HaveInsertPoint()) { |
| 2695 | CGF.Builder.CreateStore(CurCleanupDest, |
| 2696 | CGF.getNormalCleanupDestSlot()); |
| 2697 | } else { |
| 2698 | // Currently, the end of the cleanup must always exist. |
| 2699 | CGF.EnsureInsertPoint(); |
| 2700 | } |
| 2701 | } |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2702 | } else { |
| 2703 | // Emit objc_sync_exit(expr); as finally's sole statement for |
| 2704 | // @synchronized. |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2705 | llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot); |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2706 | CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg) |
| 2707 | ->setDoesNotThrow(); |
| 2708 | } |
| 2709 | } |
| 2710 | }; |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2711 | |
| 2712 | class FragileHazards { |
| 2713 | CodeGenFunction &CGF; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2714 | SmallVector<llvm::Value*, 20> Locals; |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2715 | llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry; |
| 2716 | |
| 2717 | llvm::InlineAsm *ReadHazard; |
| 2718 | llvm::InlineAsm *WriteHazard; |
| 2719 | |
| 2720 | llvm::FunctionType *GetAsmFnType(); |
| 2721 | |
| 2722 | void collectLocals(); |
| 2723 | void emitReadHazard(CGBuilderTy &Builder); |
| 2724 | |
| 2725 | public: |
| 2726 | FragileHazards(CodeGenFunction &CGF); |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2727 | |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2728 | void emitWriteHazard(); |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2729 | void emitHazardsInNewBlocks(); |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2730 | }; |
| 2731 | } |
| 2732 | |
| 2733 | /// Create the fragile-ABI read and write hazards based on the current |
| 2734 | /// state of the function, which is presumed to be immediately prior |
| 2735 | /// to a @try block. These hazards are used to maintain correct |
| 2736 | /// semantics in the face of optimization and the fragile ABI's |
| 2737 | /// cavalier use of setjmp/longjmp. |
| 2738 | FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) { |
| 2739 | collectLocals(); |
| 2740 | |
| 2741 | if (Locals.empty()) return; |
| 2742 | |
| 2743 | // Collect all the blocks in the function. |
| 2744 | for (llvm::Function::iterator |
| 2745 | I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I) |
| 2746 | BlocksBeforeTry.insert(&*I); |
| 2747 | |
| 2748 | llvm::FunctionType *AsmFnTy = GetAsmFnType(); |
| 2749 | |
| 2750 | // Create a read hazard for the allocas. This inhibits dead-store |
| 2751 | // optimizations and forces the values to memory. This hazard is |
| 2752 | // inserted before any 'throwing' calls in the protected scope to |
| 2753 | // reflect the possibility that the variables might be read from the |
| 2754 | // catch block if the call throws. |
| 2755 | { |
| 2756 | std::string Constraint; |
| 2757 | for (unsigned I = 0, E = Locals.size(); I != E; ++I) { |
| 2758 | if (I) Constraint += ','; |
| 2759 | Constraint += "*m"; |
| 2760 | } |
| 2761 | |
| 2762 | ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false); |
| 2763 | } |
| 2764 | |
| 2765 | // Create a write hazard for the allocas. This inhibits folding |
| 2766 | // loads across the hazard. This hazard is inserted at the |
| 2767 | // beginning of the catch path to reflect the possibility that the |
| 2768 | // variables might have been written within the protected scope. |
| 2769 | { |
| 2770 | std::string Constraint; |
| 2771 | for (unsigned I = 0, E = Locals.size(); I != E; ++I) { |
| 2772 | if (I) Constraint += ','; |
| 2773 | Constraint += "=*m"; |
| 2774 | } |
| 2775 | |
| 2776 | WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false); |
| 2777 | } |
| 2778 | } |
| 2779 | |
| 2780 | /// Emit a write hazard at the current location. |
| 2781 | void FragileHazards::emitWriteHazard() { |
| 2782 | if (Locals.empty()) return; |
| 2783 | |
Jay Foad | 4c7d9f1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 2784 | CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow(); |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2785 | } |
| 2786 | |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2787 | void FragileHazards::emitReadHazard(CGBuilderTy &Builder) { |
| 2788 | assert(!Locals.empty()); |
Jay Foad | 4c7d9f1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 2789 | Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow(); |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2790 | } |
| 2791 | |
| 2792 | /// Emit read hazards in all the protected blocks, i.e. all the blocks |
| 2793 | /// which have been inserted since the beginning of the try. |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2794 | void FragileHazards::emitHazardsInNewBlocks() { |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2795 | if (Locals.empty()) return; |
| 2796 | |
| 2797 | CGBuilderTy Builder(CGF.getLLVMContext()); |
| 2798 | |
| 2799 | // Iterate through all blocks, skipping those prior to the try. |
| 2800 | for (llvm::Function::iterator |
| 2801 | FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) { |
| 2802 | llvm::BasicBlock &BB = *FI; |
| 2803 | if (BlocksBeforeTry.count(&BB)) continue; |
| 2804 | |
| 2805 | // Walk through all the calls in the block. |
| 2806 | for (llvm::BasicBlock::iterator |
| 2807 | BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) { |
| 2808 | llvm::Instruction &I = *BI; |
| 2809 | |
| 2810 | // Ignore instructions that aren't non-intrinsic calls. |
| 2811 | // These are the only calls that can possibly call longjmp. |
| 2812 | if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue; |
| 2813 | if (isa<llvm::IntrinsicInst>(I)) |
| 2814 | continue; |
| 2815 | |
| 2816 | // Ignore call sites marked nounwind. This may be questionable, |
| 2817 | // since 'nounwind' doesn't necessarily mean 'does not call longjmp'. |
| 2818 | llvm::CallSite CS(&I); |
| 2819 | if (CS.doesNotThrow()) continue; |
| 2820 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2821 | // Insert a read hazard before the call. This will ensure that |
| 2822 | // any writes to the locals are performed before making the |
| 2823 | // call. If the call throws, then this is sufficient to |
| 2824 | // guarantee correctness as long as it doesn't also write to any |
| 2825 | // locals. |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2826 | Builder.SetInsertPoint(&BB, BI); |
| 2827 | emitReadHazard(Builder); |
| 2828 | } |
| 2829 | } |
| 2830 | } |
| 2831 | |
| 2832 | static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) { |
| 2833 | if (V) S.insert(V); |
| 2834 | } |
| 2835 | |
| 2836 | void FragileHazards::collectLocals() { |
| 2837 | // Compute a set of allocas to ignore. |
| 2838 | llvm::DenseSet<llvm::Value*> AllocasToIgnore; |
| 2839 | addIfPresent(AllocasToIgnore, CGF.ReturnValue); |
| 2840 | addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest); |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2841 | |
| 2842 | // Collect all the allocas currently in the function. This is |
| 2843 | // probably way too aggressive. |
| 2844 | llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock(); |
| 2845 | for (llvm::BasicBlock::iterator |
| 2846 | I = Entry.begin(), E = Entry.end(); I != E; ++I) |
| 2847 | if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I)) |
| 2848 | Locals.push_back(&*I); |
| 2849 | } |
| 2850 | |
| 2851 | llvm::FunctionType *FragileHazards::GetAsmFnType() { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2852 | SmallVector<llvm::Type *, 16> tys(Locals.size()); |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 2853 | for (unsigned i = 0, e = Locals.size(); i != e; ++i) |
| 2854 | tys[i] = Locals[i]->getType(); |
| 2855 | return llvm::FunctionType::get(CGF.VoidTy, tys, false); |
John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2856 | } |
| 2857 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2858 | /* |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2859 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2860 | Objective-C setjmp-longjmp (sjlj) Exception Handling |
| 2861 | -- |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2862 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2863 | A catch buffer is a setjmp buffer plus: |
| 2864 | - a pointer to the exception that was caught |
| 2865 | - a pointer to the previous exception data buffer |
| 2866 | - two pointers of reserved storage |
| 2867 | Therefore catch buffers form a stack, with a pointer to the top |
| 2868 | of the stack kept in thread-local storage. |
| 2869 | |
| 2870 | objc_exception_try_enter pushes a catch buffer onto the EH stack. |
| 2871 | objc_exception_try_exit pops the given catch buffer, which is |
| 2872 | required to be the top of the EH stack. |
| 2873 | objc_exception_throw pops the top of the EH stack, writes the |
| 2874 | thrown exception into the appropriate field, and longjmps |
| 2875 | to the setjmp buffer. It crashes the process (with a printf |
| 2876 | and an abort()) if there are no catch buffers on the stack. |
| 2877 | objc_exception_extract just reads the exception pointer out of the |
| 2878 | catch buffer. |
| 2879 | |
| 2880 | There's no reason an implementation couldn't use a light-weight |
| 2881 | setjmp here --- something like __builtin_setjmp, but API-compatible |
| 2882 | with the heavyweight setjmp. This will be more important if we ever |
| 2883 | want to implement correct ObjC/C++ exception interactions for the |
| 2884 | fragile ABI. |
| 2885 | |
| 2886 | Note that for this use of setjmp/longjmp to be correct, we may need |
| 2887 | to mark some local variables volatile: if a non-volatile local |
| 2888 | variable is modified between the setjmp and the longjmp, it has |
| 2889 | indeterminate value. For the purposes of LLVM IR, it may be |
| 2890 | sufficient to make loads and stores within the @try (to variables |
| 2891 | declared outside the @try) volatile. This is necessary for |
| 2892 | optimized correctness, but is not currently being done; this is |
| 2893 | being tracked as rdar://problem/8160285 |
| 2894 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2895 | The basic framework for a @try-catch-finally is as follows: |
| 2896 | { |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2897 | objc_exception_data d; |
| 2898 | id _rethrow = null; |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2899 | bool _call_try_exit = true; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2900 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2901 | objc_exception_try_enter(&d); |
| 2902 | if (!setjmp(d.jmp_buf)) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2903 | ... try body ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2904 | } else { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2905 | // exception path |
| 2906 | id _caught = objc_exception_extract(&d); |
| 2907 | |
| 2908 | // enter new try scope for handlers |
| 2909 | if (!setjmp(d.jmp_buf)) { |
| 2910 | ... match exception and execute catch blocks ... |
| 2911 | |
| 2912 | // fell off end, rethrow. |
| 2913 | _rethrow = _caught; |
| 2914 | ... jump-through-finally to finally_rethrow ... |
| 2915 | } else { |
| 2916 | // exception in catch block |
| 2917 | _rethrow = objc_exception_extract(&d); |
| 2918 | _call_try_exit = false; |
| 2919 | ... jump-through-finally to finally_rethrow ... |
| 2920 | } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2921 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2922 | ... jump-through-finally to finally_end ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2923 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2924 | finally: |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2925 | if (_call_try_exit) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2926 | objc_exception_try_exit(&d); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2927 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2928 | ... finally block .... |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2929 | ... dispatch to finally destination ... |
| 2930 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2931 | finally_rethrow: |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2932 | objc_exception_throw(_rethrow); |
| 2933 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2934 | finally_end: |
| 2935 | } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2936 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2937 | This framework differs slightly from the one gcc uses, in that gcc |
| 2938 | uses _rethrow to determine if objc_exception_try_exit should be called |
| 2939 | and if the object should be rethrown. This breaks in the face of |
| 2940 | throwing nil and introduces unnecessary branches. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2941 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2942 | We specialize this framework for a few particular circumstances: |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2943 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2944 | - If there are no catch blocks, then we avoid emitting the second |
| 2945 | exception handling context. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2946 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2947 | - If there is a catch-all catch block (i.e. @catch(...) or @catch(id |
| 2948 | e)) we avoid emitting the code to rethrow an uncaught exception. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2949 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2950 | - FIXME: If there is no @finally block we can do a few more |
| 2951 | simplifications. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2952 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2953 | Rethrows and Jumps-Through-Finally |
| 2954 | -- |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2955 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2956 | '@throw;' is supported by pushing the currently-caught exception |
| 2957 | onto ObjCEHStack while the @catch blocks are emitted. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2958 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2959 | Branches through the @finally block are handled with an ordinary |
| 2960 | normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC |
| 2961 | exceptions are not compatible with C++ exceptions, and this is |
| 2962 | hardly the only place where this will go wrong. |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2963 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2964 | @synchronized(expr) { stmt; } is emitted as if it were: |
| 2965 | id synch_value = expr; |
| 2966 | objc_sync_enter(synch_value); |
| 2967 | @try { stmt; } @finally { objc_sync_exit(synch_value); } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2968 | */ |
| 2969 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2970 | void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 2971 | const Stmt &S) { |
| 2972 | bool isTry = isa<ObjCAtTryStmt>(S); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2973 | |
| 2974 | // A destination for the fall-through edges of the catch handlers to |
| 2975 | // jump to. |
| 2976 | CodeGenFunction::JumpDest FinallyEnd = |
| 2977 | CGF.getJumpDestInCurrentScope("finally.end"); |
| 2978 | |
| 2979 | // A destination for the rethrow edge of the catch handlers to jump |
| 2980 | // to. |
| 2981 | CodeGenFunction::JumpDest FinallyRethrow = |
| 2982 | CGF.getJumpDestInCurrentScope("finally.rethrow"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2983 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2984 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 2985 | // evaluation of the expression must occur before we enter the |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2986 | // @synchronized. We can't avoid a temp here because we need the |
| 2987 | // value to be preserved. If the backend ever does liveness |
| 2988 | // correctly after setjmp, this will be unnecessary. |
| 2989 | llvm::Value *SyncArgSlot = 0; |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2990 | if (!isTry) { |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2991 | llvm::Value *SyncArg = |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2992 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 2993 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2994 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg) |
| 2995 | ->setDoesNotThrow(); |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2996 | |
| 2997 | SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg"); |
| 2998 | CGF.Builder.CreateStore(SyncArg, SyncArgSlot); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2999 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3000 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3001 | // Allocate memory for the setjmp buffer. This needs to be kept |
| 3002 | // live throughout the try and catch blocks. |
| 3003 | llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy, |
| 3004 | "exceptiondata.ptr"); |
| 3005 | |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3006 | // Create the fragile hazards. Note that this will not capture any |
| 3007 | // of the allocas required for exception processing, but will |
| 3008 | // capture the current basic block (which extends all the way to the |
| 3009 | // setjmp call) as "before the @try". |
| 3010 | FragileHazards Hazards(CGF); |
| 3011 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3012 | // Create a flag indicating whether the cleanup needs to call |
| 3013 | // objc_exception_try_exit. This is true except when |
| 3014 | // - no catches match and we're branching through the cleanup |
| 3015 | // just to rethrow the exception, or |
| 3016 | // - a catch matched and we're falling out of the catch handler. |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3017 | // The setjmp-safety rule here is that we should always store to this |
| 3018 | // variable in a place that dominates the branch through the cleanup |
| 3019 | // without passing through any setjmps. |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3020 | llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(), |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 3021 | "_call_try_exit"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3022 | |
John McCall | 9e2213d | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3023 | // A slot containing the exception to rethrow. Only needed when we |
| 3024 | // have both a @catch and a @finally. |
| 3025 | llvm::Value *PropagatingExnVar = 0; |
| 3026 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3027 | // Push a normal cleanup to leave the try scope. |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 3028 | CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S, |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3029 | SyncArgSlot, |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 3030 | CallTryExitVar, |
| 3031 | ExceptionData, |
| 3032 | &ObjCTypes); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3033 | |
| 3034 | // Enter a try block: |
| 3035 | // - Call objc_exception_try_enter to push ExceptionData on top of |
| 3036 | // the EH stack. |
| 3037 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData) |
| 3038 | ->setDoesNotThrow(); |
| 3039 | |
| 3040 | // - Call setjmp on the exception data buffer. |
| 3041 | llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0); |
| 3042 | llvm::Value *GEPIndexes[] = { Zero, Zero, Zero }; |
| 3043 | llvm::Value *SetJmpBuffer = |
Jay Foad | 0f6ac7c | 2011-07-22 08:16:57 +0000 | [diff] [blame] | 3044 | CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer"); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3045 | llvm::CallInst *SetJmpResult = |
| 3046 | CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result"); |
| 3047 | SetJmpResult->setDoesNotThrow(); |
Bill Wendling | 6446c3e | 2011-12-19 23:53:28 +0000 | [diff] [blame] | 3048 | SetJmpResult->setCanReturnTwice(); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3049 | |
| 3050 | // If setjmp returned 0, enter the protected block; otherwise, |
| 3051 | // branch to the handler. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 3052 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 3053 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3054 | llvm::Value *DidCatch = |
John McCall | d96a8e7 | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 3055 | CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception"); |
| 3056 | CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3057 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3058 | // Emit the protected block. |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3059 | CGF.EmitBlock(TryBlock); |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3060 | CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3061 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3062 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3063 | |
| 3064 | CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3065 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3066 | // Emit the exception handler block. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 3067 | CGF.EmitBlock(TryHandler); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3068 | |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3069 | // Don't optimize loads of the in-scope locals across this point. |
| 3070 | Hazards.emitWriteHazard(); |
| 3071 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3072 | // For a @synchronized (or a @try with no catches), just branch |
| 3073 | // through the cleanup to the rethrow block. |
| 3074 | if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) { |
| 3075 | // Tell the cleanup not to re-pop the exit. |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3076 | CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3077 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3078 | |
| 3079 | // Otherwise, we have to match against the caught exceptions. |
| 3080 | } else { |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3081 | // Retrieve the exception object. We may emit multiple blocks but |
| 3082 | // nothing can cross this so the value is already in SSA form. |
| 3083 | llvm::CallInst *Caught = |
| 3084 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 3085 | ExceptionData, "caught"); |
| 3086 | Caught->setDoesNotThrow(); |
| 3087 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3088 | // Push the exception to rethrow onto the EH value stack for the |
| 3089 | // benefit of any @throws in the handlers. |
| 3090 | CGF.ObjCEHValueStack.push_back(Caught); |
| 3091 | |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 3092 | const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3093 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3094 | bool HasFinally = (AtTryStmt->getFinallyStmt() != 0); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3095 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3096 | llvm::BasicBlock *CatchBlock = 0; |
| 3097 | llvm::BasicBlock *CatchHandler = 0; |
| 3098 | if (HasFinally) { |
John McCall | 9e2213d | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3099 | // Save the currently-propagating exception before |
| 3100 | // objc_exception_try_enter clears the exception slot. |
| 3101 | PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(), |
| 3102 | "propagating_exception"); |
| 3103 | CGF.Builder.CreateStore(Caught, PropagatingExnVar); |
| 3104 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3105 | // Enter a new exception try block (in case a @catch block |
| 3106 | // throws an exception). |
| 3107 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData) |
| 3108 | ->setDoesNotThrow(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3109 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3110 | llvm::CallInst *SetJmpResult = |
| 3111 | CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, |
| 3112 | "setjmp.result"); |
| 3113 | SetJmpResult->setDoesNotThrow(); |
Bill Wendling | 6446c3e | 2011-12-19 23:53:28 +0000 | [diff] [blame] | 3114 | SetJmpResult->setCanReturnTwice(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3115 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3116 | llvm::Value *Threw = |
| 3117 | CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception"); |
| 3118 | |
| 3119 | CatchBlock = CGF.createBasicBlock("catch"); |
| 3120 | CatchHandler = CGF.createBasicBlock("catch_for_catch"); |
| 3121 | CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock); |
| 3122 | |
| 3123 | CGF.EmitBlock(CatchBlock); |
| 3124 | } |
| 3125 | |
| 3126 | CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3127 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3128 | // Handle catch list. As a special case we check if everything is |
| 3129 | // matched and avoid generating code for falling off the end if |
| 3130 | // so. |
| 3131 | bool AllMatched = false; |
Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 3132 | for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) { |
| 3133 | const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3134 | |
Douglas Gregor | c00d8e1 | 2010-04-26 16:46:50 +0000 | [diff] [blame] | 3135 | const VarDecl *CatchParam = CatchStmt->getCatchParamDecl(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3136 | const ObjCObjectPointerType *OPT = 0; |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 3137 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3138 | // catch(...) always matches. |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3139 | if (!CatchParam) { |
| 3140 | AllMatched = true; |
| 3141 | } else { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3142 | OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3143 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3144 | // catch(id e) always matches under this ABI, since only |
| 3145 | // ObjC exceptions end up here in the first place. |
Daniel Dunbar | 97f61d1 | 2008-09-27 22:21:14 +0000 | [diff] [blame] | 3146 | // FIXME: For the time being we also match id<X>; this should |
| 3147 | // be rejected by Sema instead. |
Eli Friedman | 818e96f | 2009-07-11 00:57:02 +0000 | [diff] [blame] | 3148 | if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType())) |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3149 | AllMatched = true; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3150 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3151 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3152 | // 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] | 3153 | if (AllMatched) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3154 | CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF); |
| 3155 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3156 | if (CatchParam) { |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 3157 | CGF.EmitAutoVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 3158 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3159 | |
| 3160 | // These types work out because ConvertType(id) == i8*. |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 3161 | CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3162 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3163 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3164 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3165 | |
| 3166 | // The scope of the catch variable ends right here. |
| 3167 | CatchVarCleanups.ForceCleanup(); |
| 3168 | |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3169 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3170 | break; |
| 3171 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3172 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3173 | assert(OPT && "Unexpected non-object pointer type in @catch"); |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 3174 | const ObjCObjectType *ObjTy = OPT->getObjectType(); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3175 | |
| 3176 | // FIXME: @catch (Class c) ? |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 3177 | ObjCInterfaceDecl *IDecl = ObjTy->getInterface(); |
| 3178 | assert(IDecl && "Catch parameter must have Objective-C type!"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3179 | |
| 3180 | // Check if the @catch block matches the exception object. |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 3181 | llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3182 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3183 | llvm::CallInst *Match = |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 3184 | CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(), |
| 3185 | Class, Caught, "match"); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3186 | Match->setDoesNotThrow(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3187 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3188 | llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match"); |
| 3189 | llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3190 | |
| 3191 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 3192 | MatchedBlock, NextCatchBlock); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3193 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3194 | // Emit the @catch block. |
| 3195 | CGF.EmitBlock(MatchedBlock); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3196 | |
| 3197 | // Collect any cleanups for the catch variable. The scope lasts until |
| 3198 | // the end of the catch body. |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3199 | CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3200 | |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 3201 | CGF.EmitAutoVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 3202 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3203 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3204 | // Initialize the catch variable. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3205 | llvm::Value *Tmp = |
| 3206 | CGF.Builder.CreateBitCast(Caught, |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 3207 | CGF.ConvertType(CatchParam->getType())); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 3208 | CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3209 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3210 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3211 | |
| 3212 | // We're done with the catch variable. |
| 3213 | CatchVarCleanups.ForceCleanup(); |
| 3214 | |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3215 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3216 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3217 | CGF.EmitBlock(NextCatchBlock); |
| 3218 | } |
| 3219 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3220 | CGF.ObjCEHValueStack.pop_back(); |
| 3221 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3222 | // If nothing wanted anything to do with the caught exception, |
| 3223 | // kill the extract call. |
| 3224 | if (Caught->use_empty()) |
| 3225 | Caught->eraseFromParent(); |
| 3226 | |
| 3227 | if (!AllMatched) |
| 3228 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 3229 | |
| 3230 | if (HasFinally) { |
| 3231 | // Emit the exception handler for the @catch blocks. |
| 3232 | CGF.EmitBlock(CatchHandler); |
| 3233 | |
| 3234 | // In theory we might now need a write hazard, but actually it's |
| 3235 | // unnecessary because there's no local-accessing code between |
| 3236 | // the try's write hazard and here. |
| 3237 | //Hazards.emitWriteHazard(); |
| 3238 | |
John McCall | 9e2213d | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3239 | // Extract the new exception and save it to the |
| 3240 | // propagating-exception slot. |
| 3241 | assert(PropagatingExnVar); |
| 3242 | llvm::CallInst *NewCaught = |
| 3243 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 3244 | ExceptionData, "caught"); |
| 3245 | NewCaught->setDoesNotThrow(); |
| 3246 | CGF.Builder.CreateStore(NewCaught, PropagatingExnVar); |
| 3247 | |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3248 | // Don't pop the catch handler; the throw already did. |
| 3249 | CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3250 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3251 | } |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3252 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3253 | |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3254 | // Insert read hazards as required in the new blocks. |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3255 | Hazards.emitHazardsInNewBlocks(); |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3256 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3257 | // Pop the cleanup. |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3258 | CGF.Builder.restoreIP(TryFallthroughIP); |
| 3259 | if (CGF.HaveInsertPoint()) |
| 3260 | CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3261 | CGF.PopCleanupBlock(); |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3262 | CGF.EmitBlock(FinallyEnd.getBlock(), true); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3263 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3264 | // Emit the rethrow block. |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3265 | CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP(); |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 3266 | CGF.EmitBlock(FinallyRethrow.getBlock(), true); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3267 | if (CGF.HaveInsertPoint()) { |
John McCall | 9e2213d | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3268 | // If we have a propagating-exception variable, check it. |
| 3269 | llvm::Value *PropagatingExn; |
| 3270 | if (PropagatingExnVar) { |
| 3271 | PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar); |
John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3272 | |
John McCall | 9e2213d | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3273 | // Otherwise, just look in the buffer for the exception to throw. |
| 3274 | } else { |
| 3275 | llvm::CallInst *Caught = |
| 3276 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 3277 | ExceptionData); |
| 3278 | Caught->setDoesNotThrow(); |
| 3279 | PropagatingExn = Caught; |
| 3280 | } |
| 3281 | |
| 3282 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn) |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3283 | ->setDoesNotThrow(); |
| 3284 | CGF.Builder.CreateUnreachable(); |
Fariborz Jahanian | f2878e5 | 2008-11-21 19:21:53 +0000 | [diff] [blame] | 3285 | } |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3286 | |
John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3287 | CGF.Builder.restoreIP(SavedIP); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 3288 | } |
| 3289 | |
| 3290 | void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3291 | const ObjCAtThrowStmt &S) { |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 3292 | llvm::Value *ExceptionAsObject; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3293 | |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 3294 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
John McCall | 2b014d6 | 2011-10-01 10:32:24 +0000 | [diff] [blame] | 3295 | llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3296 | ExceptionAsObject = |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 3297 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy); |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 3298 | } else { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3299 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3300 | "Unexpected rethrow outside @catch block."); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 3301 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 3302 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3303 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3304 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject) |
| 3305 | ->setDoesNotReturn(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3306 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 3307 | |
| 3308 | // Clear the insertion point to indicate we are in unreachable code. |
| 3309 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 3310 | } |
| 3311 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3312 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 3313 | /// object: objc_read_weak (id *src) |
| 3314 | /// |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3315 | llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3316 | llvm::Value *AddrWeakObj) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3317 | llvm::Type* DestTy = |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3318 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
| 3319 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, |
| 3320 | ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 3321 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3322 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 3323 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 3324 | return read_weak; |
| 3325 | } |
| 3326 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3327 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 3328 | /// objc_assign_weak (id src, id *dst) |
| 3329 | /// |
| 3330 | void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3331 | llvm::Value *src, llvm::Value *dst) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3332 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3333 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3334 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3335 | assert(Size <= 8 && "does not support size > 8"); |
| 3336 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3337 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 3338 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 3339 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 3340 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 3341 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 3342 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3343 | src, dst, "weakassign"); |
| 3344 | return; |
| 3345 | } |
| 3346 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 3347 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 3348 | /// objc_assign_global (id src, id *dst) |
| 3349 | /// |
| 3350 | void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 3351 | llvm::Value *src, llvm::Value *dst, |
| 3352 | bool threadlocal) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3353 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3354 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3355 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3356 | assert(Size <= 8 && "does not support size > 8"); |
| 3357 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3358 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 3359 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 3360 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 3361 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 3362 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 3363 | if (!threadlocal) |
| 3364 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
| 3365 | src, dst, "globalassign"); |
| 3366 | else |
| 3367 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(), |
| 3368 | src, dst, "threadlocalassign"); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 3369 | return; |
| 3370 | } |
| 3371 | |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 3372 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 3373 | /// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset) |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 3374 | /// |
| 3375 | void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 3376 | llvm::Value *src, llvm::Value *dst, |
| 3377 | llvm::Value *ivarOffset) { |
| 3378 | assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL"); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3379 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3380 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3381 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3382 | assert(Size <= 8 && "does not support size > 8"); |
| 3383 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3384 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 3385 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 3386 | } |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 3387 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 3388 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 3389 | CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(), |
| 3390 | src, dst, ivarOffset); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 3391 | return; |
| 3392 | } |
| 3393 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 3394 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 3395 | /// objc_assign_strongCast (id src, id *dst) |
| 3396 | /// |
| 3397 | void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3398 | llvm::Value *src, llvm::Value *dst) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3399 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3400 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3401 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3402 | assert(Size <= 8 && "does not support size > 8"); |
| 3403 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3404 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 3405 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 3406 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 3407 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 3408 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 3409 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 3410 | src, dst, "weakassign"); |
| 3411 | return; |
| 3412 | } |
| 3413 | |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 3414 | void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3415 | llvm::Value *DestPtr, |
| 3416 | llvm::Value *SrcPtr, |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 3417 | llvm::Value *size) { |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 3418 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy); |
| 3419 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 3420 | CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(), |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 3421 | DestPtr, SrcPtr, size); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 3422 | return; |
| 3423 | } |
| 3424 | |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 3425 | /// EmitObjCValueForIvar - Code Gen for ivar reference. |
| 3426 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 3427 | LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 3428 | QualType ObjectTy, |
| 3429 | llvm::Value *BaseValue, |
| 3430 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 3431 | unsigned CVRQualifiers) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3432 | const ObjCInterfaceDecl *ID = |
| 3433 | ObjectTy->getAs<ObjCObjectType>()->getInterface(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 3434 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 3435 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 3436 | } |
| 3437 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 3438 | llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 3439 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 3440 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 3441 | uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3442 | return llvm::ConstantInt::get( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3443 | CGM.getTypes().ConvertType(CGM.getContext().LongTy), |
| 3444 | Offset); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 3445 | } |
| 3446 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3447 | /* *** Private Interface *** */ |
| 3448 | |
| 3449 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 3450 | /// level information. |
| 3451 | /// |
| 3452 | /// See: <rdr://4810609&4810587&4810587> |
| 3453 | /// struct IMAGE_INFO { |
| 3454 | /// unsigned version; |
| 3455 | /// unsigned flags; |
| 3456 | /// }; |
| 3457 | enum ImageInfoFlags { |
Daniel Dunbar | fce176b | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 3458 | eImageInfo_FixAndContinue = (1 << 0), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3459 | eImageInfo_GarbageCollected = (1 << 1), |
| 3460 | eImageInfo_GCOnly = (1 << 2), |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 3461 | eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set. |
| 3462 | |
Daniel Dunbar | fce176b | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 3463 | // A flag indicating that the module has no instances of a @synthesize of a |
| 3464 | // superclass variable. <rdar://problem/6803242> |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 3465 | eImageInfo_CorrectedSynthesize = (1 << 4) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3466 | }; |
| 3467 | |
Daniel Dunbar | fce176b | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 3468 | void CGObjCCommonMac::EmitImageInfo() { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3469 | unsigned version = 0; // Version is unused? |
| 3470 | unsigned flags = 0; |
| 3471 | |
| 3472 | // FIXME: Fix and continue? |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 3473 | if (CGM.getLangOptions().getGC() != LangOptions::NonGC) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3474 | flags |= eImageInfo_GarbageCollected; |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 3475 | if (CGM.getLangOptions().getGC() == LangOptions::GCOnly) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3476 | flags |= eImageInfo_GCOnly; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3477 | |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 3478 | // We never allow @synthesize of a superclass property. |
| 3479 | flags |= eImageInfo_CorrectedSynthesize; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3480 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3481 | llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext); |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3482 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3483 | // Emitted as int[2]; |
| 3484 | llvm::Constant *values[2] = { |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3485 | llvm::ConstantInt::get(Int32Ty, version), |
| 3486 | llvm::ConstantInt::get(Int32Ty, flags) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3487 | }; |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3488 | llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3489 | |
| 3490 | const char *Section; |
| 3491 | if (ObjCABI == 1) |
| 3492 | Section = "__OBJC, __image_info,regular"; |
| 3493 | else |
| 3494 | Section = "__DATA, __objc_imageinfo, regular, no_dead_strip"; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3495 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3496 | CreateMetadataVar("\01L_OBJC_IMAGE_INFO", |
Jay Foad | 9735760 | 2011-06-22 09:24:39 +0000 | [diff] [blame] | 3497 | llvm::ConstantArray::get(AT, values), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3498 | Section, |
| 3499 | 0, |
| 3500 | true); |
| 3501 | GV->setConstant(true); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3502 | } |
| 3503 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3504 | |
| 3505 | // struct objc_module { |
| 3506 | // unsigned long version; |
| 3507 | // unsigned long size; |
| 3508 | // const char *name; |
| 3509 | // Symtab symtab; |
| 3510 | // }; |
| 3511 | |
| 3512 | // FIXME: Get from somewhere |
| 3513 | static const int ModuleVersion = 7; |
| 3514 | |
| 3515 | void CGObjCMac::EmitModuleInfo() { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3516 | uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3517 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 3518 | llvm::Constant *Values[] = { |
| 3519 | llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion), |
| 3520 | llvm::ConstantInt::get(ObjCTypes.LongTy, Size), |
| 3521 | // This used to be the filename, now it is unused. <rdr://4327263> |
| 3522 | GetClassName(&CGM.getContext().Idents.get("")), |
| 3523 | EmitModuleSymbols() |
| 3524 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3525 | CreateMetadataVar("\01L_OBJC_MODULES", |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 3526 | llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3527 | "__OBJC,__module_info,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 3528 | 4, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3529 | } |
| 3530 | |
| 3531 | llvm::Constant *CGObjCMac::EmitModuleSymbols() { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3532 | unsigned NumClasses = DefinedClasses.size(); |
| 3533 | unsigned NumCategories = DefinedCategories.size(); |
| 3534 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3535 | // Return null if no symbols were defined. |
| 3536 | if (!NumClasses && !NumCategories) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3537 | return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3538 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3539 | llvm::Constant *Values[5]; |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3540 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3541 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3542 | Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses); |
| 3543 | Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3544 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3545 | // The runtime expects exactly the list of defined classes followed |
| 3546 | // by the list of defined categories, in a single array. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3547 | std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3548 | for (unsigned i=0; i<NumClasses; i++) |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3549 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3550 | ObjCTypes.Int8PtrTy); |
| 3551 | for (unsigned i=0; i<NumCategories; i++) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3552 | Symbols[NumClasses + i] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3553 | llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3554 | ObjCTypes.Int8PtrTy); |
| 3555 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3556 | Values[4] = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3557 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3558 | NumClasses + NumCategories), |
| 3559 | Symbols); |
| 3560 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3561 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3562 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3563 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3564 | CreateMetadataVar("\01L_OBJC_SYMBOLS", Init, |
| 3565 | "__OBJC,__symbols,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 3566 | 4, true); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3567 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3568 | } |
| 3569 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3570 | llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder, |
| 3571 | IdentifierInfo *II) { |
| 3572 | LazySymbols.insert(II); |
| 3573 | |
| 3574 | llvm::GlobalVariable *&Entry = ClassReferences[II]; |
| 3575 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3576 | if (!Entry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3577 | llvm::Constant *Casted = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3578 | llvm::ConstantExpr::getBitCast(GetClassName(II), |
| 3579 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3580 | Entry = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3581 | CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted, |
| 3582 | "__OBJC,__cls_refs,literal_pointers,no_dead_strip", |
| 3583 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3584 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3585 | |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 3586 | return Builder.CreateLoad(Entry); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3587 | } |
| 3588 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3589 | llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder, |
| 3590 | const ObjCInterfaceDecl *ID) { |
| 3591 | return EmitClassRefFromId(Builder, ID->getIdentifier()); |
| 3592 | } |
| 3593 | |
| 3594 | llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) { |
| 3595 | IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool"); |
| 3596 | return EmitClassRefFromId(Builder, II); |
| 3597 | } |
| 3598 | |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 3599 | llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel, |
| 3600 | bool lvalue) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3601 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3602 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3603 | if (!Entry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3604 | llvm::Constant *Casted = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3605 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3606 | ObjCTypes.SelectorPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3607 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3608 | CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted, |
| 3609 | "__OBJC,__message_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 3610 | 4, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3611 | } |
| 3612 | |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 3613 | if (lvalue) |
| 3614 | return Entry; |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 3615 | return Builder.CreateLoad(Entry); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3616 | } |
| 3617 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3618 | llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3619 | llvm::GlobalVariable *&Entry = ClassNames[Ident]; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3620 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3621 | if (!Entry) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3622 | Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 3623 | llvm::ConstantArray::get(VMContext, |
| 3624 | Ident->getNameStart()), |
Daniel Dunbar | af19ac4 | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 3625 | ((ObjCABI == 2) ? |
| 3626 | "__TEXT,__objc_classname,cstring_literals" : |
| 3627 | "__TEXT,__cstring,cstring_literals"), |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3628 | 1, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3629 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3630 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3631 | } |
| 3632 | |
Argyrios Kyrtzidis | 9d50c06 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 3633 | llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) { |
| 3634 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator |
| 3635 | I = MethodDefinitions.find(MD); |
| 3636 | if (I != MethodDefinitions.end()) |
| 3637 | return I->second; |
| 3638 | |
Argyrios Kyrtzidis | 9d50c06 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 3639 | return NULL; |
| 3640 | } |
| 3641 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 3642 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 3643 | /// ivar layout bitmap. |
| 3644 | llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3645 | const ObjCCommonTypesHelper &ObjCTypes) { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3646 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 3647 | } |
| 3648 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3649 | void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3650 | unsigned int BytePos, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3651 | bool ForStrongLayout, |
| 3652 | bool &HasUnion) { |
| 3653 | const RecordDecl *RD = RT->getDecl(); |
| 3654 | // FIXME - Use iterator. |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3655 | SmallVector<const FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end()); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3656 | llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3657 | const llvm::StructLayout *RecLayout = |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3658 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3659 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3660 | BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos, |
| 3661 | ForStrongLayout, HasUnion); |
| 3662 | } |
| 3663 | |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 3664 | void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3665 | const llvm::StructLayout *Layout, |
| 3666 | const RecordDecl *RD, |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3667 | const SmallVectorImpl<const FieldDecl*> &RecFields, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3668 | unsigned int BytePos, bool ForStrongLayout, |
| 3669 | bool &HasUnion) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3670 | bool IsUnion = (RD && RD->isUnion()); |
| 3671 | uint64_t MaxUnionIvarSize = 0; |
| 3672 | uint64_t MaxSkippedUnionIvarSize = 0; |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3673 | const FieldDecl *MaxField = 0; |
| 3674 | const FieldDecl *MaxSkippedField = 0; |
| 3675 | const FieldDecl *LastFieldBitfieldOrUnnamed = 0; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3676 | uint64_t MaxFieldOffset = 0; |
| 3677 | uint64_t MaxSkippedFieldOffset = 0; |
Argyrios Kyrtzidis | 0dc7509 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 3678 | uint64_t LastBitfieldOrUnnamedOffset = 0; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3679 | uint64_t FirstFieldDelta = 0; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3680 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3681 | if (RecFields.empty()) |
| 3682 | return; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3683 | unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0); |
| 3684 | unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3685 | if (!RD && CGM.getLangOptions().ObjCAutoRefCount) { |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3686 | const FieldDecl *FirstField = RecFields[0]; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3687 | FirstFieldDelta = |
| 3688 | ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField)); |
| 3689 | } |
| 3690 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3691 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3692 | const FieldDecl *Field = RecFields[i]; |
Daniel Dunbar | e05cc98 | 2009-05-03 23:35:23 +0000 | [diff] [blame] | 3693 | uint64_t FieldOffset; |
Anders Carlsson | fc514ab | 2009-07-24 17:23:54 +0000 | [diff] [blame] | 3694 | if (RD) { |
Daniel Dunbar | f8f8eba | 2010-04-14 17:02:21 +0000 | [diff] [blame] | 3695 | // Note that 'i' here is actually the field index inside RD of Field, |
| 3696 | // although this dependency is hidden. |
| 3697 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3698 | FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta; |
Anders Carlsson | fc514ab | 2009-07-24 17:23:54 +0000 | [diff] [blame] | 3699 | } else |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3700 | FieldOffset = |
| 3701 | ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta; |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3702 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3703 | // Skip over unnamed or bitfields |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3704 | if (!Field->getIdentifier() || Field->isBitField()) { |
Argyrios Kyrtzidis | 0dc7509 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 3705 | LastFieldBitfieldOrUnnamed = Field; |
| 3706 | LastBitfieldOrUnnamedOffset = FieldOffset; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3707 | continue; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3708 | } |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3709 | |
Argyrios Kyrtzidis | 0dc7509 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 3710 | LastFieldBitfieldOrUnnamed = 0; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3711 | QualType FQT = Field->getType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3712 | if (FQT->isRecordType() || FQT->isUnionType()) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3713 | if (FQT->isUnionType()) |
| 3714 | HasUnion = true; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3715 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3716 | BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(), |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3717 | BytePos + FieldOffset, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3718 | ForStrongLayout, HasUnion); |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3719 | continue; |
| 3720 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3721 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3722 | if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3723 | const ConstantArrayType *CArray = |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3724 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3725 | uint64_t ElCount = CArray->getSize().getZExtValue(); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3726 | assert(CArray && "only array with known element size is supported"); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3727 | FQT = CArray->getElementType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3728 | while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 3729 | const ConstantArrayType *CArray = |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3730 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3731 | ElCount *= CArray->getSize().getZExtValue(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3732 | FQT = CArray->getElementType(); |
| 3733 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3734 | |
| 3735 | assert(!FQT->isUnionType() && |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3736 | "layout for array of unions not supported"); |
Fariborz Jahanian | f96bdf4 | 2011-01-03 19:23:18 +0000 | [diff] [blame] | 3737 | if (FQT->isRecordType() && ElCount) { |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3738 | int OldIndex = IvarsInfo.size() - 1; |
| 3739 | int OldSkIndex = SkipIvars.size() -1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3740 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3741 | const RecordType *RT = FQT->getAs<RecordType>(); |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3742 | BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3743 | ForStrongLayout, HasUnion); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3744 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3745 | // Replicate layout information for each array element. Note that |
| 3746 | // one element is already done. |
| 3747 | uint64_t ElIx = 1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3748 | for (int FirstIndex = IvarsInfo.size() - 1, |
| 3749 | FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3750 | uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits; |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3751 | for (int i = OldIndex+1; i <= FirstIndex; ++i) |
| 3752 | IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx, |
| 3753 | IvarsInfo[i].ivar_size)); |
| 3754 | for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i) |
| 3755 | SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx, |
| 3756 | SkipIvars[i].ivar_size)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3757 | } |
| 3758 | continue; |
| 3759 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3760 | } |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3761 | // At this point, we are done with Record/Union and array there of. |
| 3762 | // For other arrays we are down to its element type. |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3763 | Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3764 | |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3765 | unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3766 | if ((ForStrongLayout && GCAttr == Qualifiers::Strong) |
| 3767 | || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) { |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3768 | if (IsUnion) { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3769 | uint64_t UnionIvarSize = FieldSize / WordSizeInBits; |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3770 | if (UnionIvarSize > MaxUnionIvarSize) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3771 | MaxUnionIvarSize = UnionIvarSize; |
| 3772 | MaxField = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3773 | MaxFieldOffset = FieldOffset; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3774 | } |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3775 | } else { |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3776 | IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3777 | FieldSize / WordSizeInBits)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3778 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3779 | } else if ((ForStrongLayout && |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3780 | (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)) |
| 3781 | || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) { |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3782 | if (IsUnion) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 3783 | // FIXME: Why the asymmetry? We divide by word size in bits on other |
| 3784 | // side. |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3785 | uint64_t UnionIvarSize = FieldSize; |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3786 | if (UnionIvarSize > MaxSkippedUnionIvarSize) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3787 | MaxSkippedUnionIvarSize = UnionIvarSize; |
| 3788 | MaxSkippedField = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3789 | MaxSkippedFieldOffset = FieldOffset; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3790 | } |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3791 | } else { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3792 | // FIXME: Why the asymmetry, we divide by byte size in bits here? |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3793 | SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3794 | FieldSize / ByteSizeInBits)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3795 | } |
| 3796 | } |
| 3797 | } |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3798 | |
Argyrios Kyrtzidis | 0dc7509 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 3799 | if (LastFieldBitfieldOrUnnamed) { |
| 3800 | if (LastFieldBitfieldOrUnnamed->isBitField()) { |
| 3801 | // Last field was a bitfield. Must update skip info. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3802 | uint64_t BitFieldSize |
| 3803 | = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext()); |
Argyrios Kyrtzidis | 0dc7509 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 3804 | GC_IVAR skivar; |
| 3805 | skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset; |
| 3806 | skivar.ivar_size = (BitFieldSize / ByteSizeInBits) |
| 3807 | + ((BitFieldSize % ByteSizeInBits) != 0); |
| 3808 | SkipIvars.push_back(skivar); |
| 3809 | } else { |
| 3810 | assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed"); |
| 3811 | // Last field was unnamed. Must update skip info. |
| 3812 | unsigned FieldSize |
| 3813 | = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType()); |
| 3814 | SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset, |
| 3815 | FieldSize / ByteSizeInBits)); |
| 3816 | } |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3817 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3818 | |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3819 | if (MaxField) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3820 | IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3821 | MaxUnionIvarSize)); |
| 3822 | if (MaxSkippedField) |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3823 | SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3824 | MaxSkippedUnionIvarSize)); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3825 | } |
| 3826 | |
Fariborz Jahanian | b8fd2eb | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 3827 | /// BuildIvarLayoutBitmap - This routine is the horsework for doing all |
| 3828 | /// the computations and returning the layout bitmap (for ivar or blocks) in |
| 3829 | /// the given argument BitMap string container. Routine reads |
| 3830 | /// two containers, IvarsInfo and SkipIvars which are assumed to be |
| 3831 | /// filled already by the caller. |
| 3832 | llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3833 | unsigned int WordsToScan, WordsToSkip; |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3834 | llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3835 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3836 | // Build the string of skip/scan nibbles |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3837 | SmallVector<SKIP_SCAN, 32> SkipScanIvars; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3838 | unsigned int WordSize = |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3839 | CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3840 | if (IvarsInfo[0].ivar_bytepos == 0) { |
| 3841 | WordsToSkip = 0; |
| 3842 | WordsToScan = IvarsInfo[0].ivar_size; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3843 | } else { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3844 | WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize; |
| 3845 | WordsToScan = IvarsInfo[0].ivar_size; |
| 3846 | } |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3847 | for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3848 | unsigned int TailPrevGCObjC = |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3849 | IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3850 | if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3851 | // consecutive 'scanned' object pointers. |
| 3852 | WordsToScan += IvarsInfo[i].ivar_size; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3853 | } else { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3854 | // Skip over 'gc'able object pointer which lay over each other. |
| 3855 | if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos) |
| 3856 | continue; |
| 3857 | // Must skip over 1 or more words. We save current skip/scan values |
| 3858 | // and start a new pair. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3859 | SKIP_SCAN SkScan; |
| 3860 | SkScan.skip = WordsToSkip; |
| 3861 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3862 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3863 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3864 | // Skip the hole. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3865 | SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize; |
| 3866 | SkScan.scan = 0; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3867 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3868 | WordsToSkip = 0; |
| 3869 | WordsToScan = IvarsInfo[i].ivar_size; |
| 3870 | } |
| 3871 | } |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3872 | if (WordsToScan > 0) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3873 | SKIP_SCAN SkScan; |
| 3874 | SkScan.skip = WordsToSkip; |
| 3875 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3876 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3877 | } |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3878 | |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3879 | if (!SkipIvars.empty()) { |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3880 | unsigned int LastIndex = SkipIvars.size()-1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3881 | int LastByteSkipped = |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3882 | SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3883 | LastIndex = IvarsInfo.size()-1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3884 | int LastByteScanned = |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3885 | IvarsInfo[LastIndex].ivar_bytepos + |
| 3886 | IvarsInfo[LastIndex].ivar_size * WordSize; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3887 | // 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] | 3888 | if (LastByteSkipped > LastByteScanned) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3889 | unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3890 | SKIP_SCAN SkScan; |
| 3891 | SkScan.skip = TotalWords - (LastByteScanned/WordSize); |
| 3892 | SkScan.scan = 0; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3893 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3894 | } |
| 3895 | } |
| 3896 | // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced |
| 3897 | // as 0xMN. |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3898 | int SkipScan = SkipScanIvars.size()-1; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3899 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3900 | if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0 |
| 3901 | && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) { |
| 3902 | // 0xM0 followed by 0x0N detected. |
| 3903 | SkipScanIvars[i].scan = SkipScanIvars[i+1].scan; |
| 3904 | for (int j = i+1; j < SkipScan; j++) |
| 3905 | SkipScanIvars[j] = SkipScanIvars[j+1]; |
| 3906 | --SkipScan; |
| 3907 | } |
| 3908 | } |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3909 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3910 | // Generate the string. |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3911 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3912 | unsigned char byte; |
| 3913 | unsigned int skip_small = SkipScanIvars[i].skip % 0xf; |
| 3914 | unsigned int scan_small = SkipScanIvars[i].scan % 0xf; |
| 3915 | unsigned int skip_big = SkipScanIvars[i].skip / 0xf; |
| 3916 | unsigned int scan_big = SkipScanIvars[i].scan / 0xf; |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3917 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3918 | // first skip big. |
| 3919 | for (unsigned int ix = 0; ix < skip_big; ix++) |
| 3920 | BitMap += (unsigned char)(0xf0); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3921 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3922 | // next (skip small, scan) |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3923 | if (skip_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3924 | byte = skip_small << 4; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3925 | if (scan_big > 0) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3926 | byte |= 0xf; |
| 3927 | --scan_big; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3928 | } else if (scan_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3929 | byte |= scan_small; |
| 3930 | scan_small = 0; |
| 3931 | } |
| 3932 | BitMap += byte; |
| 3933 | } |
| 3934 | // next scan big |
| 3935 | for (unsigned int ix = 0; ix < scan_big; ix++) |
| 3936 | BitMap += (unsigned char)(0x0f); |
| 3937 | // last scan small |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3938 | if (scan_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3939 | byte = scan_small; |
| 3940 | BitMap += byte; |
| 3941 | } |
| 3942 | } |
| 3943 | // null terminate string. |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3944 | unsigned char zero = 0; |
| 3945 | BitMap += zero; |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3946 | |
| 3947 | llvm::GlobalVariable * Entry = |
| 3948 | CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
| 3949 | llvm::ConstantArray::get(VMContext, BitMap.c_str()), |
Daniel Dunbar | af19ac4 | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 3950 | ((ObjCABI == 2) ? |
| 3951 | "__TEXT,__objc_classname,cstring_literals" : |
| 3952 | "__TEXT,__cstring,cstring_literals"), |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3953 | 1, true); |
| 3954 | return getConstantGEP(VMContext, Entry, 0, 0); |
| 3955 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3956 | |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3957 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 3958 | /// implementation for the __strong or __weak case. |
| 3959 | /// The layout map displays which words in ivar list must be skipped |
| 3960 | /// and which must be scanned by GC (see below). String is built of bytes. |
| 3961 | /// Each byte is divided up in two nibbles (4-bit each). Left nibble is count |
| 3962 | /// of words to skip and right nibble is count of words to scan. So, each |
| 3963 | /// nibble represents up to 15 workds to skip or scan. Skipping the rest is |
| 3964 | /// represented by a 0x00 byte which also ends the string. |
| 3965 | /// 1. when ForStrongLayout is true, following ivars are scanned: |
| 3966 | /// - id, Class |
| 3967 | /// - object * |
| 3968 | /// - __strong anything |
| 3969 | /// |
| 3970 | /// 2. When ForStrongLayout is false, following ivars are scanned: |
| 3971 | /// - __weak anything |
| 3972 | /// |
| 3973 | llvm::Constant *CGObjCCommonMac::BuildIvarLayout( |
| 3974 | const ObjCImplementationDecl *OMD, |
| 3975 | bool ForStrongLayout) { |
| 3976 | bool hasUnion = false; |
| 3977 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3978 | llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 3979 | if (CGM.getLangOptions().getGC() == LangOptions::NonGC && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3980 | !CGM.getLangOptions().ObjCAutoRefCount) |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3981 | return llvm::Constant::getNullValue(PtrTy); |
| 3982 | |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3983 | const ObjCInterfaceDecl *OI = OMD->getClassInterface(); |
| 3984 | SmallVector<const FieldDecl*, 32> RecFields; |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 3985 | if (CGM.getLangOptions().ObjCAutoRefCount) { |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3986 | for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin(); |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 3987 | IVD; IVD = IVD->getNextIvar()) |
| 3988 | RecFields.push_back(cast<FieldDecl>(IVD)); |
| 3989 | } |
| 3990 | else { |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3991 | SmallVector<const ObjCIvarDecl*, 32> Ivars; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3992 | CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3993 | |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3994 | // FIXME: This is not ideal; we shouldn't have to do this copy. |
| 3995 | RecFields.append(Ivars.begin(), Ivars.end()); |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 3996 | } |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3997 | |
| 3998 | if (RecFields.empty()) |
| 3999 | return llvm::Constant::getNullValue(PtrTy); |
| 4000 | |
| 4001 | SkipIvars.clear(); |
| 4002 | IvarsInfo.clear(); |
| 4003 | |
| 4004 | BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion); |
| 4005 | if (IvarsInfo.empty()) |
| 4006 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | b8fd2eb | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 4007 | // Sort on byte position in case we encounterred a union nested in |
| 4008 | // the ivar list. |
| 4009 | if (hasUnion && !IvarsInfo.empty()) |
| 4010 | std::sort(IvarsInfo.begin(), IvarsInfo.end()); |
| 4011 | if (hasUnion && !SkipIvars.empty()) |
| 4012 | std::sort(SkipIvars.begin(), SkipIvars.end()); |
| 4013 | |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4014 | std::string BitMap; |
Fariborz Jahanian | b8fd2eb | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 4015 | llvm::Constant *C = BuildIvarLayoutBitmap(BitMap); |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4016 | |
| 4017 | if (CGM.getLangOptions().ObjCGCBitmapPrint) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4018 | printf("\n%s ivar layout for class '%s': ", |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 4019 | ForStrongLayout ? "strong" : "weak", |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 4020 | OMD->getClassInterface()->getName().data()); |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 4021 | const unsigned char *s = (unsigned char*)BitMap.c_str(); |
| 4022 | for (unsigned i = 0; i < BitMap.size(); i++) |
| 4023 | if (!(s[i] & 0xf0)) |
| 4024 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 4025 | else |
| 4026 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 4027 | printf("\n"); |
| 4028 | } |
Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 4029 | return C; |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 4030 | } |
| 4031 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4032 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4033 | llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; |
| 4034 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4035 | // FIXME: Avoid std::string copying. |
| 4036 | if (!Entry) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4037 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_", |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 4038 | llvm::ConstantArray::get(VMContext, Sel.getAsString()), |
Daniel Dunbar | af19ac4 | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4039 | ((ObjCABI == 2) ? |
| 4040 | "__TEXT,__objc_methname,cstring_literals" : |
| 4041 | "__TEXT,__cstring,cstring_literals"), |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4042 | 1, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4043 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4044 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4045 | } |
| 4046 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4047 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4048 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4049 | return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); |
| 4050 | } |
| 4051 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4052 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) { |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4053 | std::string TypeStr; |
| 4054 | CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); |
| 4055 | |
| 4056 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4057 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4058 | if (!Entry) |
| 4059 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 4060 | llvm::ConstantArray::get(VMContext, TypeStr), |
Daniel Dunbar | af19ac4 | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4061 | ((ObjCABI == 2) ? |
| 4062 | "__TEXT,__objc_methtype,cstring_literals" : |
| 4063 | "__TEXT,__cstring,cstring_literals"), |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4064 | 1, true); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4065 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4066 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4067 | } |
| 4068 | |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4069 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D, |
| 4070 | bool Extended) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4071 | std::string TypeStr; |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4072 | if (CGM.getContext().getObjCEncodingForMethodDecl( |
| 4073 | const_cast<ObjCMethodDecl*>(D), |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4074 | TypeStr, Extended)) |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4075 | return 0; |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4076 | |
| 4077 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
| 4078 | |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4079 | if (!Entry) |
| 4080 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 4081 | llvm::ConstantArray::get(VMContext, TypeStr), |
Daniel Dunbar | af19ac4 | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4082 | ((ObjCABI == 2) ? |
| 4083 | "__TEXT,__objc_methtype,cstring_literals" : |
| 4084 | "__TEXT,__cstring,cstring_literals"), |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4085 | 1, true); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4086 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4087 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4088 | } |
| 4089 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4090 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4091 | llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4092 | llvm::GlobalVariable *&Entry = PropertyNames[Ident]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4093 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4094 | if (!Entry) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4095 | Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_", |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4096 | llvm::ConstantArray::get(VMContext, |
| 4097 | Ident->getNameStart()), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4098 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4099 | 1, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4100 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4101 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4102 | } |
| 4103 | |
| 4104 | // FIXME: Merge into a single cstring creation function. |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4105 | // FIXME: This Decl should be more precise. |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4106 | llvm::Constant * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4107 | CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 4108 | const Decl *Container) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4109 | std::string TypeStr; |
| 4110 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4111 | return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); |
| 4112 | } |
| 4113 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4114 | void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D, |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4115 | const ObjCContainerDecl *CD, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4116 | SmallVectorImpl<char> &Name) { |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4117 | llvm::raw_svector_ostream OS(Name); |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 4118 | assert (CD && "Missing container decl in GetNameForMethod"); |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4119 | OS << '\01' << (D->isInstanceMethod() ? '-' : '+') |
| 4120 | << '[' << CD->getName(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4121 | if (const ObjCCategoryImplDecl *CID = |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4122 | dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 4123 | OS << '(' << CID << ')'; |
Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4124 | OS << ' ' << D->getSelector().getAsString() << ']'; |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 4125 | } |
| 4126 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4127 | void CGObjCMac::FinishModule() { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4128 | EmitModuleInfo(); |
| 4129 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4130 | // Emit the dummy bodies for any protocols which were referenced but |
| 4131 | // never defined. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4132 | for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4133 | I = Protocols.begin(), e = Protocols.end(); I != e; ++I) { |
| 4134 | if (I->second->hasInitializer()) |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4135 | continue; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4136 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 4137 | llvm::Constant *Values[5]; |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4138 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4139 | Values[1] = GetClassName(I->first); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4140 | Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4141 | Values[3] = Values[4] = |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4142 | llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4143 | I->second->setLinkage(llvm::GlobalValue::InternalLinkage); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 4144 | I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4145 | Values)); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4146 | CGM.AddUsedGlobal(I->second); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4147 | } |
| 4148 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4149 | // Add assembler directives to add lazy undefined symbol references |
| 4150 | // for classes which are referenced but not defined. This is |
| 4151 | // important for correct linker interaction. |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4152 | // |
| 4153 | // FIXME: It would be nice if we had an LLVM construct for this. |
| 4154 | if (!LazySymbols.empty() || !DefinedSymbols.empty()) { |
| 4155 | llvm::SmallString<256> Asm; |
| 4156 | Asm += CGM.getModule().getModuleInlineAsm(); |
| 4157 | if (!Asm.empty() && Asm.back() != '\n') |
| 4158 | Asm += '\n'; |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4159 | |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4160 | llvm::raw_svector_ostream OS(Asm); |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4161 | for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(), |
| 4162 | e = DefinedSymbols.end(); I != e; ++I) |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 4163 | OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n" |
| 4164 | << "\t.globl .objc_class_name_" << (*I)->getName() << "\n"; |
Chris Lattner | afd5eda | 2010-04-17 18:26:20 +0000 | [diff] [blame] | 4165 | for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(), |
Fariborz Jahanian | b9c5b3d | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 4166 | e = LazySymbols.end(); I != e; ++I) { |
Chris Lattner | afd5eda | 2010-04-17 18:26:20 +0000 | [diff] [blame] | 4167 | OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n"; |
Fariborz Jahanian | b9c5b3d | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 4168 | } |
| 4169 | |
| 4170 | for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) { |
| 4171 | OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n" |
| 4172 | << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n"; |
| 4173 | } |
Chris Lattner | afd5eda | 2010-04-17 18:26:20 +0000 | [diff] [blame] | 4174 | |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4175 | CGM.getModule().setModuleInlineAsm(OS.str()); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4176 | } |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4177 | } |
| 4178 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4179 | CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4180 | : CGObjCCommonMac(cgm), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4181 | ObjCTypes(cgm) { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4182 | ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4183 | ObjCABI = 2; |
| 4184 | } |
| 4185 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4186 | /* *** */ |
| 4187 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4188 | ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4189 | : VMContext(cgm.getLLVMContext()), CGM(cgm) { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4190 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 4191 | ASTContext &Ctx = CGM.getContext(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4192 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4193 | ShortTy = Types.ConvertType(Ctx.ShortTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4194 | IntTy = Types.ConvertType(Ctx.IntTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4195 | LongTy = Types.ConvertType(Ctx.LongTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4196 | LongLongTy = Types.ConvertType(Ctx.LongLongTy); |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 4197 | Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4198 | Int8PtrPtrTy = llvm::PointerType::getUnqual(Int8PtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4199 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4200 | ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4201 | PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4202 | SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4203 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 4204 | // FIXME: It would be nice to unify this with the opaque type, so that the IR |
| 4205 | // comes out a bit cleaner. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4206 | llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4207 | ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4208 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4209 | // I'm not sure I like this. The implicit coordination is a bit |
| 4210 | // gross. We should solve this in a reasonable fashion because this |
| 4211 | // is a pretty common task (match some runtime data structure with |
| 4212 | // an LLVM data structure). |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4213 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4214 | // FIXME: This is leaked. |
| 4215 | // FIXME: Merge with rewriter code? |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4216 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4217 | // struct _objc_super { |
| 4218 | // id self; |
| 4219 | // Class cls; |
| 4220 | // } |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4221 | RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct, |
Daniel Dunbar | daa3ac5 | 2010-04-29 16:29:11 +0000 | [diff] [blame] | 4222 | Ctx.getTranslationUnitDecl(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4223 | SourceLocation(), SourceLocation(), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4224 | &Ctx.Idents.get("_objc_super")); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4225 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4226 | Ctx.getObjCIdType(), 0, 0, false, false)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4227 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4228 | Ctx.getObjCClassType(), 0, 0, false, false)); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4229 | RD->completeDefinition(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4230 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4231 | SuperCTy = Ctx.getTagDeclType(RD); |
| 4232 | SuperPtrCTy = Ctx.getPointerType(SuperCTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4233 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4234 | SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4235 | SuperPtrTy = llvm::PointerType::getUnqual(SuperTy); |
| 4236 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4237 | // struct _prop_t { |
| 4238 | // char *name; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4239 | // char *attributes; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4240 | // } |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4241 | PropertyTy = llvm::StructType::create("struct._prop_t", |
| 4242 | Int8PtrTy, Int8PtrTy, NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4243 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4244 | // struct _prop_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4245 | // uint32_t entsize; // sizeof(struct _prop_t) |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4246 | // uint32_t count_of_properties; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4247 | // struct _prop_t prop_list[count_of_properties]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4248 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4249 | PropertyListTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4250 | llvm::StructType::create("struct._prop_list_t", IntTy, IntTy, |
| 4251 | llvm::ArrayType::get(PropertyTy, 0), NULL); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4252 | // struct _prop_list_t * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4253 | PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4254 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4255 | // struct _objc_method { |
| 4256 | // SEL _cmd; |
| 4257 | // char *method_type; |
| 4258 | // char *_imp; |
| 4259 | // } |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4260 | MethodTy = llvm::StructType::create("struct._objc_method", |
| 4261 | SelectorPtrTy, Int8PtrTy, Int8PtrTy, |
| 4262 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4263 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4264 | // struct _objc_cache * |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4265 | CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache"); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4266 | CachePtrTy = llvm::PointerType::getUnqual(CacheTy); |
Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 4267 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4268 | } |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4269 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4270 | ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4271 | : ObjCCommonTypesHelper(cgm) { |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4272 | // struct _objc_method_description { |
| 4273 | // SEL name; |
| 4274 | // char *types; |
| 4275 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4276 | MethodDescriptionTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4277 | llvm::StructType::create("struct._objc_method_description", |
| 4278 | SelectorPtrTy, Int8PtrTy, NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4279 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4280 | // struct _objc_method_description_list { |
| 4281 | // int count; |
| 4282 | // struct _objc_method_description[1]; |
| 4283 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4284 | MethodDescriptionListTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4285 | llvm::StructType::create("struct._objc_method_description_list", |
| 4286 | IntTy, |
| 4287 | llvm::ArrayType::get(MethodDescriptionTy, 0),NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4288 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4289 | // struct _objc_method_description_list * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4290 | MethodDescriptionListPtrTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4291 | llvm::PointerType::getUnqual(MethodDescriptionListTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4292 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4293 | // Protocol description structures |
| 4294 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4295 | // struct _objc_protocol_extension { |
| 4296 | // uint32_t size; // sizeof(struct _objc_protocol_extension) |
| 4297 | // struct _objc_method_description_list *optional_instance_methods; |
| 4298 | // struct _objc_method_description_list *optional_class_methods; |
| 4299 | // struct _objc_property_list *instance_properties; |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4300 | // const char ** extendedMethodTypes; |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4301 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4302 | ProtocolExtensionTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4303 | llvm::StructType::create("struct._objc_protocol_extension", |
| 4304 | IntTy, MethodDescriptionListPtrTy, |
| 4305 | MethodDescriptionListPtrTy, PropertyListPtrTy, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4306 | Int8PtrPtrTy, NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4307 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4308 | // struct _objc_protocol_extension * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4309 | ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4310 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4311 | // Handle recursive construction of Protocol and ProtocolList types |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4312 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4313 | ProtocolTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4314 | llvm::StructType::create(VMContext, "struct._objc_protocol"); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4315 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4316 | ProtocolListTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4317 | llvm::StructType::create(VMContext, "struct._objc_protocol_list"); |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4318 | ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy), |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4319 | LongTy, |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4320 | llvm::ArrayType::get(ProtocolTy, 0), |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4321 | NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4322 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4323 | // struct _objc_protocol { |
| 4324 | // struct _objc_protocol_extension *isa; |
| 4325 | // char *protocol_name; |
| 4326 | // struct _objc_protocol **_objc_protocol_list; |
| 4327 | // struct _objc_method_description_list *instance_methods; |
| 4328 | // struct _objc_method_description_list *class_methods; |
| 4329 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4330 | ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy, |
| 4331 | llvm::PointerType::getUnqual(ProtocolListTy), |
| 4332 | MethodDescriptionListPtrTy, |
| 4333 | MethodDescriptionListPtrTy, |
| 4334 | NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4335 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4336 | // struct _objc_protocol_list * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4337 | ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4338 | |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4339 | ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4340 | |
| 4341 | // Class description structures |
| 4342 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4343 | // struct _objc_ivar { |
| 4344 | // char *ivar_name; |
| 4345 | // char *ivar_type; |
| 4346 | // int ivar_offset; |
| 4347 | // } |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4348 | IvarTy = llvm::StructType::create("struct._objc_ivar", |
| 4349 | Int8PtrTy, Int8PtrTy, IntTy, NULL); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4350 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4351 | // struct _objc_ivar_list * |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4352 | IvarListTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4353 | llvm::StructType::create(VMContext, "struct._objc_ivar_list"); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4354 | IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4355 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4356 | // struct _objc_method_list * |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4357 | MethodListTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4358 | llvm::StructType::create(VMContext, "struct._objc_method_list"); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4359 | MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4360 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4361 | // struct _objc_class_extension * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4362 | ClassExtensionTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4363 | llvm::StructType::create("struct._objc_class_extension", |
| 4364 | IntTy, Int8PtrTy, PropertyListPtrTy, NULL); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4365 | ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4366 | |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4367 | ClassTy = llvm::StructType::create(VMContext, "struct._objc_class"); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4368 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4369 | // struct _objc_class { |
| 4370 | // Class isa; |
| 4371 | // Class super_class; |
| 4372 | // char *name; |
| 4373 | // long version; |
| 4374 | // long info; |
| 4375 | // long instance_size; |
| 4376 | // struct _objc_ivar_list *ivars; |
| 4377 | // struct _objc_method_list *methods; |
| 4378 | // struct _objc_cache *cache; |
| 4379 | // struct _objc_protocol_list *protocols; |
| 4380 | // char *ivar_layout; |
| 4381 | // struct _objc_class_ext *ext; |
| 4382 | // }; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4383 | ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy), |
| 4384 | llvm::PointerType::getUnqual(ClassTy), |
| 4385 | Int8PtrTy, |
| 4386 | LongTy, |
| 4387 | LongTy, |
| 4388 | LongTy, |
| 4389 | IvarListPtrTy, |
| 4390 | MethodListPtrTy, |
| 4391 | CachePtrTy, |
| 4392 | ProtocolListPtrTy, |
| 4393 | Int8PtrTy, |
| 4394 | ClassExtensionPtrTy, |
| 4395 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4396 | |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4397 | ClassPtrTy = llvm::PointerType::getUnqual(ClassTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4398 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4399 | // struct _objc_category { |
| 4400 | // char *category_name; |
| 4401 | // char *class_name; |
| 4402 | // struct _objc_method_list *instance_method; |
| 4403 | // struct _objc_method_list *class_method; |
| 4404 | // uint32_t size; // sizeof(struct _objc_category) |
| 4405 | // struct _objc_property_list *instance_properties;// category's @property |
| 4406 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4407 | CategoryTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4408 | llvm::StructType::create("struct._objc_category", |
| 4409 | Int8PtrTy, Int8PtrTy, MethodListPtrTy, |
| 4410 | MethodListPtrTy, ProtocolListPtrTy, |
| 4411 | IntTy, PropertyListPtrTy, NULL); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 4412 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4413 | // Global metadata structures |
| 4414 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4415 | // struct _objc_symtab { |
| 4416 | // long sel_ref_cnt; |
| 4417 | // SEL *refs; |
| 4418 | // short cls_def_cnt; |
| 4419 | // short cat_def_cnt; |
| 4420 | // char *defs[cls_def_cnt + cat_def_cnt]; |
| 4421 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4422 | SymtabTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4423 | llvm::StructType::create("struct._objc_symtab", |
| 4424 | LongTy, SelectorPtrTy, ShortTy, ShortTy, |
| 4425 | llvm::ArrayType::get(Int8PtrTy, 0), NULL); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4426 | SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4427 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 4428 | // struct _objc_module { |
| 4429 | // long version; |
| 4430 | // long size; // sizeof(struct _objc_module) |
| 4431 | // char *name; |
| 4432 | // struct _objc_symtab* symtab; |
| 4433 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4434 | ModuleTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4435 | llvm::StructType::create("struct._objc_module", |
| 4436 | LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 4437 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4438 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 4439 | // FIXME: This is the size of the setjmp buffer and should be target |
| 4440 | // specific. 18 is what's used on 32-bit X86. |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 4441 | uint64_t SetJmpBufferSize = 18; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4442 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 4443 | // Exceptions |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4444 | llvm::Type *StackPtrTy = llvm::ArrayType::get( |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 4445 | llvm::Type::getInt8PtrTy(VMContext), 4); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4446 | |
| 4447 | ExceptionDataTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4448 | llvm::StructType::create("struct._objc_exception_data", |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4449 | llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext), |
| 4450 | SetJmpBufferSize), |
| 4451 | StackPtrTy, NULL); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 4452 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 4453 | } |
| 4454 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4455 | ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4456 | : ObjCCommonTypesHelper(cgm) { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4457 | // struct _method_list_t { |
| 4458 | // uint32_t entsize; // sizeof(struct _objc_method) |
| 4459 | // uint32_t method_count; |
| 4460 | // struct _objc_method method_list[method_count]; |
| 4461 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4462 | MethodListnfABITy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4463 | llvm::StructType::create("struct.__method_list_t", IntTy, IntTy, |
| 4464 | llvm::ArrayType::get(MethodTy, 0), NULL); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4465 | // struct method_list_t * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4466 | MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4467 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4468 | // struct _protocol_t { |
| 4469 | // id isa; // NULL |
| 4470 | // const char * const protocol_name; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4471 | // const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4472 | // const struct method_list_t * const instance_methods; |
| 4473 | // const struct method_list_t * const class_methods; |
| 4474 | // const struct method_list_t *optionalInstanceMethods; |
| 4475 | // const struct method_list_t *optionalClassMethods; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4476 | // const struct _prop_list_t * properties; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4477 | // const uint32_t size; // sizeof(struct _protocol_t) |
| 4478 | // const uint32_t flags; // = 0 |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4479 | // const char ** extendedMethodTypes; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4480 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4481 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4482 | // Holder for struct _protocol_list_t * |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4483 | ProtocolListnfABITy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4484 | llvm::StructType::create(VMContext, "struct._objc_protocol_list"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4485 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4486 | ProtocolnfABITy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4487 | llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy, |
| 4488 | llvm::PointerType::getUnqual(ProtocolListnfABITy), |
| 4489 | MethodListnfABIPtrTy, MethodListnfABIPtrTy, |
| 4490 | MethodListnfABIPtrTy, MethodListnfABIPtrTy, |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 4491 | PropertyListPtrTy, IntTy, IntTy, Int8PtrPtrTy, |
| 4492 | NULL); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4493 | |
| 4494 | // struct _protocol_t* |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4495 | ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4496 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4497 | // struct _protocol_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4498 | // long protocol_count; // Note, this is 32/64 bit |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4499 | // struct _protocol_t *[protocol_count]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4500 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4501 | ProtocolListnfABITy->setBody(LongTy, |
| 4502 | llvm::ArrayType::get(ProtocolnfABIPtrTy, 0), |
| 4503 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4504 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4505 | // struct _objc_protocol_list* |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4506 | ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4507 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4508 | // struct _ivar_t { |
| 4509 | // unsigned long int *offset; // pointer to ivar offset location |
| 4510 | // char *name; |
| 4511 | // char *type; |
| 4512 | // uint32_t alignment; |
| 4513 | // uint32_t size; |
| 4514 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4515 | IvarnfABITy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4516 | llvm::StructType::create("struct._ivar_t", |
| 4517 | llvm::PointerType::getUnqual(LongTy), |
| 4518 | Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4519 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4520 | // struct _ivar_list_t { |
| 4521 | // uint32 entsize; // sizeof(struct _ivar_t) |
| 4522 | // uint32 count; |
| 4523 | // struct _iver_t list[count]; |
| 4524 | // } |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4525 | IvarListnfABITy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4526 | llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy, |
| 4527 | llvm::ArrayType::get(IvarnfABITy, 0), NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4528 | |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4529 | IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4530 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4531 | // struct _class_ro_t { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4532 | // uint32_t const flags; |
| 4533 | // uint32_t const instanceStart; |
| 4534 | // uint32_t const instanceSize; |
| 4535 | // uint32_t const reserved; // only when building for 64bit targets |
| 4536 | // const uint8_t * const ivarLayout; |
| 4537 | // const char *const name; |
| 4538 | // const struct _method_list_t * const baseMethods; |
| 4539 | // const struct _objc_protocol_list *const baseProtocols; |
| 4540 | // const struct _ivar_list_t *const ivars; |
| 4541 | // const uint8_t * const weakIvarLayout; |
| 4542 | // const struct _prop_list_t * const properties; |
| 4543 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4544 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4545 | // FIXME. Add 'reserved' field in 64bit abi mode! |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4546 | ClassRonfABITy = llvm::StructType::create("struct._class_ro_t", |
| 4547 | IntTy, IntTy, IntTy, Int8PtrTy, |
| 4548 | Int8PtrTy, MethodListnfABIPtrTy, |
| 4549 | ProtocolListnfABIPtrTy, |
| 4550 | IvarListnfABIPtrTy, |
| 4551 | Int8PtrTy, PropertyListPtrTy, NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4552 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4553 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4554 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 4555 | ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false) |
| 4556 | ->getPointerTo(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4557 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4558 | // struct _class_t { |
| 4559 | // struct _class_t *isa; |
| 4560 | // struct _class_t * const superclass; |
| 4561 | // void *cache; |
| 4562 | // IMP *vtable; |
| 4563 | // struct class_ro_t *ro; |
| 4564 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4565 | |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4566 | ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t"); |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4567 | ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy), |
| 4568 | llvm::PointerType::getUnqual(ClassnfABITy), |
| 4569 | CachePtrTy, |
| 4570 | llvm::PointerType::getUnqual(ImpnfABITy), |
| 4571 | llvm::PointerType::getUnqual(ClassRonfABITy), |
| 4572 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4573 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4574 | // LLVM for struct _class_t * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4575 | ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4576 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4577 | // struct _category_t { |
| 4578 | // const char * const name; |
| 4579 | // struct _class_t *const cls; |
| 4580 | // const struct _method_list_t * const instance_methods; |
| 4581 | // const struct _method_list_t * const class_methods; |
| 4582 | // const struct _protocol_list_t * const protocols; |
| 4583 | // const struct _prop_list_t * const properties; |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 4584 | // } |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4585 | CategorynfABITy = llvm::StructType::create("struct._category_t", |
| 4586 | Int8PtrTy, ClassnfABIPtrTy, |
| 4587 | MethodListnfABIPtrTy, |
| 4588 | MethodListnfABIPtrTy, |
| 4589 | ProtocolListnfABIPtrTy, |
| 4590 | PropertyListPtrTy, |
| 4591 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4592 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4593 | // New types for nonfragile abi messaging. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4594 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 4595 | ASTContext &Ctx = CGM.getContext(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4596 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4597 | // MessageRefTy - LLVM for: |
| 4598 | // struct _message_ref_t { |
| 4599 | // IMP messenger; |
| 4600 | // SEL name; |
| 4601 | // }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4602 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4603 | // First the clang type for struct _message_ref_t |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4604 | RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct, |
Daniel Dunbar | daa3ac5 | 2010-04-29 16:29:11 +0000 | [diff] [blame] | 4605 | Ctx.getTranslationUnitDecl(), |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4606 | SourceLocation(), SourceLocation(), |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4607 | &Ctx.Idents.get("_message_ref_t")); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4608 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4609 | Ctx.VoidPtrTy, 0, 0, false, false)); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4610 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4611 | Ctx.getObjCSelType(), 0, 0, false, false)); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4612 | RD->completeDefinition(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4613 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4614 | MessageRefCTy = Ctx.getTagDeclType(RD); |
| 4615 | MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy); |
| 4616 | MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4617 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4618 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4619 | MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4620 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4621 | // SuperMessageRefTy - LLVM for: |
| 4622 | // struct _super_message_ref_t { |
| 4623 | // SUPER_IMP messenger; |
| 4624 | // SEL name; |
| 4625 | // }; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4626 | SuperMessageRefTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4627 | llvm::StructType::create("struct._super_message_ref_t", |
| 4628 | ImpnfABITy, SelectorPtrTy, NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4629 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4630 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4631 | SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy); |
Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 4632 | |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 4633 | |
| 4634 | // struct objc_typeinfo { |
| 4635 | // const void** vtable; // objc_ehtype_vtable + 2 |
| 4636 | // const char* name; // c++ typeinfo string |
| 4637 | // Class cls; |
| 4638 | // }; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4639 | EHTypeTy = |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4640 | llvm::StructType::create("struct._objc_typeinfo", |
| 4641 | llvm::PointerType::getUnqual(Int8PtrTy), |
| 4642 | Int8PtrTy, ClassnfABIPtrTy, NULL); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4643 | EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 4644 | } |
| 4645 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4646 | llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4647 | FinishNonFragileABIModule(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4648 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4649 | return NULL; |
| 4650 | } |
| 4651 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4652 | void CGObjCNonFragileABIMac::AddModuleClassList(const |
| 4653 | std::vector<llvm::GlobalValue*> |
| 4654 | &Container, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4655 | const char *SymbolName, |
| 4656 | const char *SectionName) { |
| 4657 | unsigned NumClasses = Container.size(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4658 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4659 | if (!NumClasses) |
| 4660 | return; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4661 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4662 | std::vector<llvm::Constant*> Symbols(NumClasses); |
| 4663 | for (unsigned i=0; i<NumClasses; i++) |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4664 | Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i], |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4665 | ObjCTypes.Int8PtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4666 | llvm::Constant* Init = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4667 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4668 | NumClasses), |
| 4669 | Symbols); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4670 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4671 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4672 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4673 | llvm::GlobalValue::InternalLinkage, |
| 4674 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4675 | SymbolName); |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 4676 | GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType())); |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4677 | GV->setSection(SectionName); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4678 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4679 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4680 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4681 | void CGObjCNonFragileABIMac::FinishNonFragileABIModule() { |
| 4682 | // nonfragile abi has no module definition. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4683 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4684 | // Build list of all implemented class addresses in array |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4685 | // L_OBJC_LABEL_CLASS_$. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4686 | AddModuleClassList(DefinedClasses, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4687 | "\01L_OBJC_LABEL_CLASS_$", |
| 4688 | "__DATA, __objc_classlist, regular, no_dead_strip"); |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4689 | |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4690 | for (unsigned i = 0; i < DefinedClasses.size(); i++) { |
| 4691 | llvm::GlobalValue *IMPLGV = DefinedClasses[i]; |
| 4692 | if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage) |
| 4693 | continue; |
| 4694 | IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4695 | } |
| 4696 | |
Fariborz Jahanian | 0e93d25 | 2009-12-01 18:25:24 +0000 | [diff] [blame] | 4697 | for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) { |
| 4698 | llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i]; |
| 4699 | if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage) |
| 4700 | continue; |
| 4701 | IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 4702 | } |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4703 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4704 | AddModuleClassList(DefinedNonLazyClasses, |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4705 | "\01L_OBJC_LABEL_NONLAZY_CLASS_$", |
| 4706 | "__DATA, __objc_nlclslist, regular, no_dead_strip"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4707 | |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4708 | // Build list of all implemented category addresses in array |
| 4709 | // L_OBJC_LABEL_CATEGORY_$. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4710 | AddModuleClassList(DefinedCategories, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4711 | "\01L_OBJC_LABEL_CATEGORY_$", |
| 4712 | "__DATA, __objc_catlist, regular, no_dead_strip"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4713 | AddModuleClassList(DefinedNonLazyCategories, |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4714 | "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$", |
| 4715 | "__DATA, __objc_nlcatlist, regular, no_dead_strip"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4716 | |
Daniel Dunbar | fce176b | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 4717 | EmitImageInfo(); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4718 | } |
| 4719 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4720 | /// isVTableDispatchedSelector - Returns true if SEL is not in the list of |
| 4721 | /// VTableDispatchMethods; false otherwise. What this means is that |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4722 | /// except for the 19 selectors in the list, we generate 32bit-style |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4723 | /// message dispatch call for all the rest. |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4724 | bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) { |
| 4725 | // At various points we've experimented with using vtable-based |
| 4726 | // dispatch for all methods. |
Daniel Dunbar | f643b9b | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 4727 | switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) { |
| 4728 | default: |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4729 | llvm_unreachable("Invalid dispatch method!"); |
Daniel Dunbar | f643b9b | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 4730 | case CodeGenOptions::Legacy: |
Fariborz Jahanian | 776dbf9 | 2010-04-19 17:53:30 +0000 | [diff] [blame] | 4731 | return false; |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4732 | case CodeGenOptions::NonLegacy: |
| 4733 | return true; |
Daniel Dunbar | f643b9b | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 4734 | case CodeGenOptions::Mixed: |
| 4735 | break; |
| 4736 | } |
| 4737 | |
| 4738 | // If so, see whether this selector is in the white-list of things which must |
| 4739 | // 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] | 4740 | if (VTableDispatchMethods.empty()) { |
| 4741 | VTableDispatchMethods.insert(GetNullarySelector("alloc")); |
| 4742 | VTableDispatchMethods.insert(GetNullarySelector("class")); |
| 4743 | VTableDispatchMethods.insert(GetNullarySelector("self")); |
| 4744 | VTableDispatchMethods.insert(GetNullarySelector("isFlipped")); |
| 4745 | VTableDispatchMethods.insert(GetNullarySelector("length")); |
| 4746 | VTableDispatchMethods.insert(GetNullarySelector("count")); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4747 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4748 | // These are vtable-based if GC is disabled. |
| 4749 | // Optimistically use vtable dispatch for hybrid compiles. |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 4750 | if (CGM.getLangOptions().getGC() != LangOptions::GCOnly) { |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4751 | VTableDispatchMethods.insert(GetNullarySelector("retain")); |
| 4752 | VTableDispatchMethods.insert(GetNullarySelector("release")); |
| 4753 | VTableDispatchMethods.insert(GetNullarySelector("autorelease")); |
| 4754 | } |
| 4755 | |
| 4756 | VTableDispatchMethods.insert(GetUnarySelector("allocWithZone")); |
| 4757 | VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass")); |
| 4758 | VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector")); |
| 4759 | VTableDispatchMethods.insert(GetUnarySelector("objectForKey")); |
| 4760 | VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex")); |
| 4761 | VTableDispatchMethods.insert(GetUnarySelector("isEqualToString")); |
| 4762 | VTableDispatchMethods.insert(GetUnarySelector("isEqual")); |
| 4763 | |
| 4764 | // These are vtable-based if GC is enabled. |
| 4765 | // Optimistically use vtable dispatch for hybrid compiles. |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 4766 | if (CGM.getLangOptions().getGC() != LangOptions::NonGC) { |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4767 | VTableDispatchMethods.insert(GetNullarySelector("hash")); |
| 4768 | VTableDispatchMethods.insert(GetUnarySelector("addObject")); |
| 4769 | |
| 4770 | // "countByEnumeratingWithState:objects:count" |
| 4771 | IdentifierInfo *KeyIdents[] = { |
| 4772 | &CGM.getContext().Idents.get("countByEnumeratingWithState"), |
| 4773 | &CGM.getContext().Idents.get("objects"), |
| 4774 | &CGM.getContext().Idents.get("count") |
| 4775 | }; |
| 4776 | VTableDispatchMethods.insert( |
| 4777 | CGM.getContext().Selectors.getSelector(3, KeyIdents)); |
| 4778 | } |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4779 | } |
Daniel Dunbar | f643b9b | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 4780 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4781 | return VTableDispatchMethods.count(Sel); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4782 | } |
| 4783 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4784 | // Metadata flags |
| 4785 | enum MetaDataDlags { |
| 4786 | CLS = 0x0, |
| 4787 | CLS_META = 0x1, |
| 4788 | CLS_ROOT = 0x2, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4789 | OBJC2_CLS_HIDDEN = 0x10, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4790 | CLS_EXCEPTION = 0x20, |
| 4791 | |
| 4792 | /// (Obsolete) ARC-specific: this class has a .release_ivars method |
| 4793 | CLS_HAS_IVAR_RELEASER = 0x40, |
| 4794 | /// class was compiled with -fobjc-arr |
| 4795 | CLS_COMPILED_BY_ARC = 0x80 // (1<<7) |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4796 | }; |
| 4797 | /// BuildClassRoTInitializer - generate meta-data for: |
| 4798 | /// struct _class_ro_t { |
| 4799 | /// uint32_t const flags; |
| 4800 | /// uint32_t const instanceStart; |
| 4801 | /// uint32_t const instanceSize; |
| 4802 | /// uint32_t const reserved; // only when building for 64bit targets |
| 4803 | /// const uint8_t * const ivarLayout; |
| 4804 | /// const char *const name; |
| 4805 | /// const struct _method_list_t * const baseMethods; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4806 | /// const struct _protocol_list_t *const baseProtocols; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4807 | /// const struct _ivar_list_t *const ivars; |
| 4808 | /// const uint8_t * const weakIvarLayout; |
| 4809 | /// const struct _prop_list_t * const properties; |
| 4810 | /// } |
| 4811 | /// |
| 4812 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4813 | unsigned flags, |
| 4814 | unsigned InstanceStart, |
| 4815 | unsigned InstanceSize, |
| 4816 | const ObjCImplementationDecl *ID) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4817 | std::string ClassName = ID->getNameAsString(); |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 4818 | llvm::Constant *Values[10]; // 11 for 64bit targets! |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4819 | |
| 4820 | if (CGM.getLangOptions().ObjCAutoRefCount) |
| 4821 | flags |= CLS_COMPILED_BY_ARC; |
| 4822 | |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4823 | Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
| 4824 | Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart); |
| 4825 | Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4826 | // FIXME. For 64bit targets add 0 here. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4827 | Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4828 | : BuildIvarLayout(ID, true); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4829 | Values[ 4] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4830 | // const struct _method_list_t * const baseMethods; |
| 4831 | std::vector<llvm::Constant*> Methods; |
| 4832 | std::string MethodListName("\01l_OBJC_$_"); |
| 4833 | if (flags & CLS_META) { |
| 4834 | MethodListName += "CLASS_METHODS_" + ID->getNameAsString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4835 | for (ObjCImplementationDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4836 | i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4837 | // Class methods should always be defined. |
| 4838 | Methods.push_back(GetMethodConstant(*i)); |
| 4839 | } |
| 4840 | } else { |
| 4841 | MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4842 | for (ObjCImplementationDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4843 | i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4844 | // Instance methods should always be defined. |
| 4845 | Methods.push_back(GetMethodConstant(*i)); |
| 4846 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4847 | for (ObjCImplementationDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4848 | i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) { |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4849 | ObjCPropertyImplDecl *PID = *i; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4850 | |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4851 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){ |
| 4852 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4853 | |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4854 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 4855 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4856 | Methods.push_back(C); |
| 4857 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 4858 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4859 | Methods.push_back(C); |
| 4860 | } |
| 4861 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4862 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4863 | Values[ 5] = EmitMethodList(MethodListName, |
| 4864 | "__DATA, __objc_const", Methods); |
| 4865 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4866 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4867 | assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4868 | Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4869 | + OID->getName(), |
Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 4870 | OID->all_referenced_protocol_begin(), |
| 4871 | OID->all_referenced_protocol_end()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4872 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4873 | if (flags & CLS_META) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4874 | Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4875 | else |
| 4876 | Values[ 7] = EmitIvarList(ID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4877 | Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4878 | : BuildIvarLayout(ID, false); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4879 | if (flags & CLS_META) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4880 | Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4881 | else |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4882 | Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(), |
| 4883 | ID, ID->getClassInterface(), ObjCTypes); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 4884 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4885 | Values); |
| 4886 | llvm::GlobalVariable *CLASS_RO_GV = |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4887 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false, |
| 4888 | llvm::GlobalValue::InternalLinkage, |
| 4889 | Init, |
| 4890 | (flags & CLS_META) ? |
| 4891 | std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName : |
| 4892 | std::string("\01l_OBJC_CLASS_RO_$_")+ClassName); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4893 | CLASS_RO_GV->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 4894 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4895 | CLASS_RO_GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4896 | return CLASS_RO_GV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4897 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4898 | } |
| 4899 | |
| 4900 | /// BuildClassMetaData - This routine defines that to-level meta-data |
| 4901 | /// for the given ClassName for: |
| 4902 | /// struct _class_t { |
| 4903 | /// struct _class_t *isa; |
| 4904 | /// struct _class_t * const superclass; |
| 4905 | /// void *cache; |
| 4906 | /// IMP *vtable; |
| 4907 | /// struct class_ro_t *ro; |
| 4908 | /// } |
| 4909 | /// |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4910 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4911 | std::string &ClassName, |
| 4912 | llvm::Constant *IsAGV, |
| 4913 | llvm::Constant *SuperClassGV, |
| 4914 | llvm::Constant *ClassRoGV, |
| 4915 | bool HiddenVisibility) { |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 4916 | llvm::Constant *Values[] = { |
| 4917 | IsAGV, |
| 4918 | SuperClassGV, |
| 4919 | ObjCEmptyCacheVar, // &ObjCEmptyCacheVar |
| 4920 | ObjCEmptyVtableVar, // &ObjCEmptyVtableVar |
| 4921 | ClassRoGV // &CLASS_RO_GV |
| 4922 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4923 | if (!Values[1]) |
| 4924 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4925 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4926 | Values); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4927 | llvm::GlobalVariable *GV = GetClassGlobal(ClassName); |
| 4928 | GV->setInitializer(Init); |
Fariborz Jahanian | dd0db2a | 2009-01-31 01:07:39 +0000 | [diff] [blame] | 4929 | GV->setSection("__DATA, __objc_data"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4930 | GV->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 4931 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy)); |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4932 | if (HiddenVisibility) |
| 4933 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4934 | return GV; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4935 | } |
| 4936 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4937 | bool |
Fariborz Jahanian | ecfbdcb | 2009-05-21 01:03:45 +0000 | [diff] [blame] | 4938 | CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4939 | return OD->getClassMethod(GetNullarySelector("load")) != 0; |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4940 | } |
| 4941 | |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 4942 | void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4943 | uint32_t &InstanceStart, |
| 4944 | uint32_t &InstanceSize) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4945 | const ASTRecordLayout &RL = |
Daniel Dunbar | b4c79e0 | 2009-05-04 21:26:30 +0000 | [diff] [blame] | 4946 | CGM.getContext().getASTObjCImplementationLayout(OID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4947 | |
Daniel Dunbar | 6e8575b | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 4948 | // InstanceSize is really instance end. |
Ken Dyck | ec29903 | 2011-02-11 02:20:09 +0000 | [diff] [blame] | 4949 | InstanceSize = RL.getDataSize().getQuantity(); |
Daniel Dunbar | 6e8575b | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 4950 | |
| 4951 | // If there are no fields, the start is the same as the end. |
| 4952 | if (!RL.getFieldCount()) |
| 4953 | InstanceStart = InstanceSize; |
| 4954 | else |
Ken Dyck | fb67ccd | 2011-04-14 00:43:09 +0000 | [diff] [blame] | 4955 | InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth(); |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4956 | } |
| 4957 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4958 | void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) { |
| 4959 | std::string ClassName = ID->getNameAsString(); |
| 4960 | if (!ObjCEmptyCacheVar) { |
| 4961 | ObjCEmptyCacheVar = new llvm::GlobalVariable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4962 | CGM.getModule(), |
| 4963 | ObjCTypes.CacheTy, |
| 4964 | false, |
| 4965 | llvm::GlobalValue::ExternalLinkage, |
| 4966 | 0, |
| 4967 | "_objc_empty_cache"); |
| 4968 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4969 | ObjCEmptyVtableVar = new llvm::GlobalVariable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4970 | CGM.getModule(), |
| 4971 | ObjCTypes.ImpnfABITy, |
| 4972 | false, |
| 4973 | llvm::GlobalValue::ExternalLinkage, |
| 4974 | 0, |
| 4975 | "_objc_empty_vtable"); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4976 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4977 | assert(ID->getClassInterface() && |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4978 | "CGObjCNonFragileABIMac::GenerateClass - class is 0"); |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 4979 | // FIXME: Is this correct (that meta class size is never computed)? |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4980 | uint32_t InstanceStart = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4981 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4982 | uint32_t InstanceSize = InstanceStart; |
| 4983 | uint32_t flags = CLS_META; |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4984 | std::string ObjCMetaClassName(getMetaclassSymbolPrefix()); |
| 4985 | std::string ObjCClassName(getClassSymbolPrefix()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4986 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4987 | llvm::GlobalVariable *SuperClassGV, *IsAGV; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4988 | |
| 4989 | bool classIsHidden = |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 4990 | ID->getClassInterface()->getVisibility() == HiddenVisibility; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4991 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4992 | flags |= OBJC2_CLS_HIDDEN; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4993 | if (ID->hasCXXStructors()) |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 4994 | flags |= eClassFlags_ABI2_HasCXXStructors; |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4995 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4996 | // class is root |
| 4997 | flags |= CLS_ROOT; |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4998 | SuperClassGV = GetClassGlobal(ObjCClassName + ClassName); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4999 | IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5000 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5001 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 5002 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 5003 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 5004 | Root = Super; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5005 | IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString()); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5006 | if (Root->isWeakImported()) |
Fariborz Jahanian | 0e93d25 | 2009-12-01 18:25:24 +0000 | [diff] [blame] | 5007 | IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 5008 | // work on super class metadata symbol. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5009 | std::string SuperClassName = |
Fariborz Jahanian | 0e93d25 | 2009-12-01 18:25:24 +0000 | [diff] [blame] | 5010 | ObjCMetaClassName + |
| 5011 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5012 | SuperClassGV = GetClassGlobal(SuperClassName); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5013 | if (ID->getClassInterface()->getSuperClass()->isWeakImported()) |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 5014 | SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 5015 | } |
| 5016 | llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags, |
| 5017 | InstanceStart, |
| 5018 | InstanceSize,ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5019 | std::string TClassName = ObjCMetaClassName + ClassName; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5020 | llvm::GlobalVariable *MetaTClass = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5021 | BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV, |
| 5022 | classIsHidden); |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 5023 | DefinedMetaClasses.push_back(MetaTClass); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5024 | |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5025 | // Metadata for the class |
| 5026 | flags = CLS; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5027 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5028 | flags |= OBJC2_CLS_HIDDEN; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5029 | if (ID->hasCXXStructors()) |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 5030 | flags |= eClassFlags_ABI2_HasCXXStructors; |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5031 | |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 5032 | if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface())) |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5033 | flags |= CLS_EXCEPTION; |
| 5034 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5035 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5036 | flags |= CLS_ROOT; |
| 5037 | SuperClassGV = 0; |
Chris Lattner | b7b58b1 | 2009-04-19 06:02:28 +0000 | [diff] [blame] | 5038 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5039 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 5040 | std::string RootClassName = |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5041 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5042 | SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5043 | if (ID->getClassInterface()->getSuperClass()->isWeakImported()) |
Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 5044 | SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5045 | } |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 5046 | GetClassSizeInfo(ID, InstanceStart, InstanceSize); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5047 | CLASS_RO_GV = BuildClassRoTInitializer(flags, |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 5048 | InstanceStart, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5049 | InstanceSize, |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 5050 | ID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5051 | |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5052 | TClassName = ObjCClassName + ClassName; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5053 | llvm::GlobalVariable *ClassMD = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5054 | BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV, |
| 5055 | classIsHidden); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 5056 | DefinedClasses.push_back(ClassMD); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5057 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5058 | // Determine if this class is also "non-lazy". |
| 5059 | if (ImplementationIsNonLazy(ID)) |
| 5060 | DefinedNonLazyClasses.push_back(ClassMD); |
| 5061 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5062 | // Force the definition of the EHType if necessary. |
| 5063 | if (flags & CLS_EXCEPTION) |
| 5064 | GetInterfaceEHType(ID->getClassInterface(), true); |
Fariborz Jahanian | 64089ce | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 5065 | // Make sure method definition entries are all clear for next implementation. |
| 5066 | MethodDefinitions.clear(); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5067 | } |
| 5068 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5069 | /// GenerateProtocolRef - This routine is called to generate code for |
| 5070 | /// a protocol reference expression; as in: |
| 5071 | /// @code |
| 5072 | /// @protocol(Proto1); |
| 5073 | /// @endcode |
| 5074 | /// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1 |
| 5075 | /// which will hold address of the protocol meta-data. |
| 5076 | /// |
| 5077 | llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5078 | const ObjCProtocolDecl *PD) { |
| 5079 | |
Fariborz Jahanian | 960cd06 | 2009-04-10 18:47:34 +0000 | [diff] [blame] | 5080 | // This routine is called for @protocol only. So, we must build definition |
| 5081 | // of protocol's meta-data (not a reference to it!) |
| 5082 | // |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5083 | llvm::Constant *Init = |
| 5084 | llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD), |
| 5085 | ObjCTypes.ExternalProtocolPtrTy); |
| 5086 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5087 | std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_"); |
Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5088 | ProtocolName += PD->getName(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5089 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5090 | llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName); |
| 5091 | if (PTGV) |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 5092 | return Builder.CreateLoad(PTGV); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5093 | PTGV = new llvm::GlobalVariable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5094 | CGM.getModule(), |
| 5095 | Init->getType(), false, |
| 5096 | llvm::GlobalValue::WeakAnyLinkage, |
| 5097 | Init, |
| 5098 | ProtocolName); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5099 | PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip"); |
| 5100 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5101 | CGM.AddUsedGlobal(PTGV); |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 5102 | return Builder.CreateLoad(PTGV); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5103 | } |
| 5104 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5105 | /// GenerateCategory - Build metadata for a category implementation. |
| 5106 | /// struct _category_t { |
| 5107 | /// const char * const name; |
| 5108 | /// struct _class_t *const cls; |
| 5109 | /// const struct _method_list_t * const instance_methods; |
| 5110 | /// const struct _method_list_t * const class_methods; |
| 5111 | /// const struct _protocol_list_t * const protocols; |
| 5112 | /// const struct _prop_list_t * const properties; |
| 5113 | /// } |
| 5114 | /// |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5115 | void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5116 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5117 | const char *Prefix = "\01l_OBJC_$_CATEGORY_"; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5118 | std::string ExtCatName(Prefix + Interface->getNameAsString()+ |
| 5119 | "_$_" + OCD->getNameAsString()); |
| 5120 | std::string ExtClassName(getClassSymbolPrefix() + |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5121 | Interface->getNameAsString()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5122 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5123 | llvm::Constant *Values[6]; |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5124 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 5125 | // meta-class entry symbol |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5126 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5127 | if (Interface->isWeakImported()) |
Fariborz Jahanian | 2cdcc4c | 2009-11-17 22:02:21 +0000 | [diff] [blame] | 5128 | ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
| 5129 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5130 | Values[1] = ClassGV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5131 | std::vector<llvm::Constant*> Methods; |
| 5132 | std::string MethodListName(Prefix); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5133 | MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() + |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5134 | "_$_" + OCD->getNameAsString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5135 | |
| 5136 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5137 | i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5138 | // Instance methods should always be defined. |
| 5139 | Methods.push_back(GetMethodConstant(*i)); |
| 5140 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5141 | |
| 5142 | Values[2] = EmitMethodList(MethodListName, |
| 5143 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5144 | Methods); |
| 5145 | |
| 5146 | MethodListName = Prefix; |
| 5147 | MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" + |
| 5148 | OCD->getNameAsString(); |
| 5149 | Methods.clear(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5150 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5151 | i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5152 | // Class methods should always be defined. |
| 5153 | Methods.push_back(GetMethodConstant(*i)); |
| 5154 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5155 | |
| 5156 | Values[3] = EmitMethodList(MethodListName, |
| 5157 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5158 | Methods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5159 | const ObjCCategoryDecl *Category = |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 5160 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5161 | if (Category) { |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5162 | llvm::SmallString<256> ExtName; |
| 5163 | llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_" |
| 5164 | << OCD->getName(); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5165 | Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5166 | + Interface->getName() + "_$_" |
| 5167 | + Category->getName(), |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5168 | Category->protocol_begin(), |
| 5169 | Category->protocol_end()); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5170 | Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(), |
| 5171 | OCD, Category, ObjCTypes); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5172 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5173 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 5174 | Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5175 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5176 | |
| 5177 | llvm::Constant *Init = |
| 5178 | llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy, |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5179 | Values); |
| 5180 | llvm::GlobalVariable *GCATV |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5181 | = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy, |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5182 | false, |
| 5183 | llvm::GlobalValue::InternalLinkage, |
| 5184 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5185 | ExtCatName); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5186 | GCATV->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5187 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5188 | GCATV->setSection("__DATA, __objc_const"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5189 | CGM.AddUsedGlobal(GCATV); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5190 | DefinedCategories.push_back(GCATV); |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5191 | |
| 5192 | // Determine if this category is also "non-lazy". |
| 5193 | if (ImplementationIsNonLazy(OCD)) |
| 5194 | DefinedNonLazyCategories.push_back(GCATV); |
Fariborz Jahanian | 64089ce | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 5195 | // method definition entries must be clear for next implementation. |
| 5196 | MethodDefinitions.clear(); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5197 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5198 | |
| 5199 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 5200 | /// given method if it has been defined. The result is null if the |
| 5201 | /// method has not been defined. The return value has type MethodPtrTy. |
| 5202 | llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5203 | const ObjCMethodDecl *MD) { |
Argyrios Kyrtzidis | 9d50c06 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 5204 | llvm::Function *Fn = GetMethodDefinition(MD); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5205 | if (!Fn) |
| 5206 | return 0; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5207 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5208 | llvm::Constant *Method[] = { |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 5209 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5210 | ObjCTypes.SelectorPtrTy), |
| 5211 | GetMethodVarType(MD), |
| 5212 | llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy) |
| 5213 | }; |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5214 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5215 | } |
| 5216 | |
| 5217 | /// EmitMethodList - Build meta-data for method declarations |
| 5218 | /// struct _method_list_t { |
| 5219 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 5220 | /// uint32_t method_count; |
| 5221 | /// struct _objc_method method_list[method_count]; |
| 5222 | /// } |
| 5223 | /// |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5224 | llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(Twine Name, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5225 | const char *Section, |
| 5226 | const ConstantVector &Methods) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5227 | // Return null for empty list. |
| 5228 | if (Methods.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5229 | return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5230 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5231 | llvm::Constant *Values[3]; |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5232 | // sizeof(struct _objc_method) |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5233 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5234 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5235 | // method_count |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5236 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5237 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5238 | Methods.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 5239 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5240 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5241 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5242 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5243 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5244 | llvm::GlobalValue::InternalLinkage, Init, Name); |
| 5245 | GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType())); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5246 | GV->setSection(Section); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5247 | CGM.AddUsedGlobal(GV); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5248 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5249 | } |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5250 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 5251 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 5252 | /// the given ivar. |
Daniel Dunbar | e83be12 | 2010-04-02 21:14:02 +0000 | [diff] [blame] | 5253 | llvm::GlobalVariable * |
| 5254 | CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID, |
| 5255 | const ObjCIvarDecl *Ivar) { |
| 5256 | const ObjCInterfaceDecl *Container = Ivar->getContainingInterface(); |
Daniel Dunbar | a81419d | 2009-05-05 00:36:57 +0000 | [diff] [blame] | 5257 | std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() + |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 5258 | '.' + Ivar->getNameAsString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5259 | llvm::GlobalVariable *IvarOffsetGV = |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 5260 | CGM.getModule().getGlobalVariable(Name); |
| 5261 | if (!IvarOffsetGV) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5262 | IvarOffsetGV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5263 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 5264 | false, |
| 5265 | llvm::GlobalValue::ExternalLinkage, |
| 5266 | 0, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5267 | Name); |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 5268 | return IvarOffsetGV; |
| 5269 | } |
| 5270 | |
Daniel Dunbar | e83be12 | 2010-04-02 21:14:02 +0000 | [diff] [blame] | 5271 | llvm::Constant * |
| 5272 | CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
| 5273 | const ObjCIvarDecl *Ivar, |
| 5274 | unsigned long int Offset) { |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 5275 | llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5276 | IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy, |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 5277 | Offset)); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5278 | IvarOffsetGV->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5279 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy)); |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 5280 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 5281 | // FIXME: This matches gcc, but shouldn't the visibility be set on the use as |
| 5282 | // well (i.e., in ObjCIvarOffsetVariable). |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 5283 | if (Ivar->getAccessControl() == ObjCIvarDecl::Private || |
| 5284 | Ivar->getAccessControl() == ObjCIvarDecl::Package || |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 5285 | ID->getVisibility() == HiddenVisibility) |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 5286 | IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 5287 | else |
Fariborz Jahanian | 77c9fd2 | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 5288 | IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Bill Wendling | 1f38251 | 2011-05-04 21:37:25 +0000 | [diff] [blame] | 5289 | IvarOffsetGV->setSection("__DATA, __objc_ivar"); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 5290 | return IvarOffsetGV; |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5291 | } |
| 5292 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5293 | /// EmitIvarList - Emit the ivar list for the given |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5294 | /// implementation. The return value has type |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5295 | /// IvarListnfABIPtrTy. |
| 5296 | /// struct _ivar_t { |
| 5297 | /// unsigned long int *offset; // pointer to ivar offset location |
| 5298 | /// char *name; |
| 5299 | /// char *type; |
| 5300 | /// uint32_t alignment; |
| 5301 | /// uint32_t size; |
| 5302 | /// } |
| 5303 | /// struct _ivar_list_t { |
| 5304 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 5305 | /// uint32 count; |
| 5306 | /// struct _iver_t list[count]; |
| 5307 | /// } |
| 5308 | /// |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 5309 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5310 | llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5311 | const ObjCImplementationDecl *ID) { |
| 5312 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5313 | std::vector<llvm::Constant*> Ivars; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5314 | |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 5315 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5316 | assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5317 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5318 | // FIXME. Consolidate this with similar code in GenerateClass. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5319 | |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 5320 | for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin(); |
Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 5321 | IVD; IVD = IVD->getNextIvar()) { |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 5322 | // Ignore unnamed bit-fields. |
| 5323 | if (!IVD->getDeclName()) |
| 5324 | continue; |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5325 | llvm::Constant *Ivar[5]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5326 | Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD, |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 5327 | ComputeIvarBaseOffset(CGM, ID, IVD)); |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 5328 | Ivar[1] = GetMethodVarName(IVD->getIdentifier()); |
| 5329 | Ivar[2] = GetMethodVarType(IVD); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5330 | llvm::Type *FieldTy = |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 5331 | CGM.getTypes().ConvertTypeForMem(IVD->getType()); |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5332 | unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5333 | unsigned Align = CGM.getContext().getPreferredTypeAlign( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5334 | IVD->getType().getTypePtr()) >> 3; |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5335 | Align = llvm::Log2_32(Align); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5336 | Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align); |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 5337 | // NOTE. Size of a bitfield does not match gcc's, because of the |
| 5338 | // way bitfields are treated special in each. But I am told that |
| 5339 | // 'size' for bitfield ivars is ignored by the runtime so it does |
| 5340 | // not matter. If it matters, there is enough info to get the |
| 5341 | // bitfield right! |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5342 | Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5343 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar)); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5344 | } |
| 5345 | // Return null for empty list. |
| 5346 | if (Ivars.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5347 | return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5348 | |
| 5349 | llvm::Constant *Values[3]; |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5350 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5351 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 5352 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5353 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5354 | Ivars.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 5355 | Values[2] = llvm::ConstantArray::get(AT, Ivars); |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5356 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5357 | const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_"; |
| 5358 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5359 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5360 | llvm::GlobalValue::InternalLinkage, |
| 5361 | Init, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5362 | Prefix + OID->getName()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5363 | GV->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5364 | CGM.getTargetData().getABITypeAlignment(Init->getType())); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5365 | GV->setSection("__DATA, __objc_const"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5366 | |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5367 | CGM.AddUsedGlobal(GV); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 5368 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5369 | } |
| 5370 | |
| 5371 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5372 | const ObjCProtocolDecl *PD) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5373 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5374 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5375 | if (!Entry) { |
| 5376 | // We use the initializer as a marker of whether this is a forward |
| 5377 | // reference or not. At module finalization we add the empty |
| 5378 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5379 | Entry = |
| 5380 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false, |
| 5381 | llvm::GlobalValue::ExternalLinkage, |
| 5382 | 0, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5383 | "\01l_OBJC_PROTOCOL_$_" + PD->getName()); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5384 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5385 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5386 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5387 | return Entry; |
| 5388 | } |
| 5389 | |
| 5390 | /// GetOrEmitProtocol - Generate the protocol meta-data: |
| 5391 | /// @code |
| 5392 | /// struct _protocol_t { |
| 5393 | /// id isa; // NULL |
| 5394 | /// const char * const protocol_name; |
| 5395 | /// const struct _protocol_list_t * protocol_list; // super protocols |
| 5396 | /// const struct method_list_t * const instance_methods; |
| 5397 | /// const struct method_list_t * const class_methods; |
| 5398 | /// const struct method_list_t *optionalInstanceMethods; |
| 5399 | /// const struct method_list_t *optionalClassMethods; |
| 5400 | /// const struct _prop_list_t * properties; |
| 5401 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 5402 | /// const uint32_t flags; // = 0 |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5403 | /// const char ** extendedMethodTypes; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5404 | /// } |
| 5405 | /// @endcode |
| 5406 | /// |
| 5407 | |
| 5408 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5409 | const ObjCProtocolDecl *PD) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5410 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5411 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5412 | // Early exit if a defining object has already been generated. |
| 5413 | if (Entry && Entry->hasInitializer()) |
| 5414 | return Entry; |
| 5415 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5416 | // Construct method lists. |
| 5417 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 5418 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5419 | std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5420 | for (ObjCProtocolDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5421 | i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5422 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5423 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5424 | if (!C) |
| 5425 | return GetOrEmitProtocolRef(PD); |
| 5426 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5427 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 5428 | OptInstanceMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5429 | OptMethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5430 | } else { |
| 5431 | InstanceMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5432 | MethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5433 | } |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5434 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5435 | |
| 5436 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5437 | i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5438 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5439 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5440 | if (!C) |
| 5441 | return GetOrEmitProtocolRef(PD); |
| 5442 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5443 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 5444 | OptClassMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5445 | OptMethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5446 | } else { |
| 5447 | ClassMethods.push_back(C); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5448 | MethodTypesExt.push_back(GetMethodVarType(MD, true)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5449 | } |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5450 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5451 | |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5452 | MethodTypesExt.insert(MethodTypesExt.end(), |
| 5453 | OptMethodTypesExt.begin(), OptMethodTypesExt.end()); |
| 5454 | |
| 5455 | llvm::Constant *Values[11]; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5456 | // isa is NULL |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5457 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5458 | Values[1] = GetClassName(PD->getIdentifier()); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5459 | Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(), |
| 5460 | PD->protocol_begin(), |
| 5461 | PD->protocol_end()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5462 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5463 | Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5464 | + PD->getName(), |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5465 | "__DATA, __objc_const", |
| 5466 | InstanceMethods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5467 | Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5468 | + PD->getName(), |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5469 | "__DATA, __objc_const", |
| 5470 | ClassMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5471 | Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5472 | + PD->getName(), |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5473 | "__DATA, __objc_const", |
| 5474 | OptInstanceMethods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5475 | Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_" |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5476 | + PD->getName(), |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5477 | "__DATA, __objc_const", |
| 5478 | OptClassMethods); |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5479 | Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(), |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5480 | 0, PD, ObjCTypes); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5481 | uint32_t Size = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5482 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5483 | Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5484 | Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy); |
Bob Wilson | dc8dab6 | 2011-11-30 01:57:58 +0000 | [diff] [blame] | 5485 | Values[10] = EmitProtocolMethodTypes("\01l_OBJC_$_PROTOCOL_METHOD_TYPES_" |
| 5486 | + PD->getName(), |
| 5487 | MethodTypesExt, ObjCTypes); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5488 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5489 | Values); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5490 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5491 | if (Entry) { |
| 5492 | // Already created, fix the linkage and update the initializer. |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 5493 | Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5494 | Entry->setInitializer(Init); |
| 5495 | } else { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5496 | Entry = |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5497 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, |
| 5498 | false, llvm::GlobalValue::WeakAnyLinkage, Init, |
| 5499 | "\01l_OBJC_PROTOCOL_$_" + PD->getName()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5500 | Entry->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5501 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5502 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5503 | } |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 5504 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5505 | CGM.AddUsedGlobal(Entry); |
| 5506 | |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 5507 | // Use this protocol meta-data to build protocol list table in section |
| 5508 | // __DATA, __objc_protolist |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5509 | llvm::GlobalVariable *PTGV = |
| 5510 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy, |
| 5511 | false, llvm::GlobalValue::WeakAnyLinkage, Entry, |
| 5512 | "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5513 | PTGV->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5514 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy)); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 5515 | PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip"); |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 5516 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5517 | CGM.AddUsedGlobal(PTGV); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5518 | return Entry; |
| 5519 | } |
| 5520 | |
| 5521 | /// EmitProtocolList - Generate protocol list meta-data: |
| 5522 | /// @code |
| 5523 | /// struct _protocol_list_t { |
| 5524 | /// long protocol_count; // Note, this is 32/64 bit |
| 5525 | /// struct _protocol_t[protocol_count]; |
| 5526 | /// } |
| 5527 | /// @endcode |
| 5528 | /// |
| 5529 | llvm::Constant * |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5530 | CGObjCNonFragileABIMac::EmitProtocolList(Twine Name, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5531 | ObjCProtocolDecl::protocol_iterator begin, |
| 5532 | ObjCProtocolDecl::protocol_iterator end) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5533 | std::vector<llvm::Constant*> ProtocolRefs; |
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 | // Just return null for empty protocol lists |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5536 | if (begin == end) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5537 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5538 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5539 | // FIXME: We shouldn't need to do this lookup here, should we? |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5540 | llvm::SmallString<256> TmpName; |
| 5541 | Name.toVector(TmpName); |
| 5542 | llvm::GlobalVariable *GV = |
| 5543 | CGM.getModule().getGlobalVariable(TmpName.str(), true); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5544 | if (GV) |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5545 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5546 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5547 | for (; begin != end; ++begin) |
| 5548 | ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented??? |
| 5549 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5550 | // This list is null terminated. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5551 | ProtocolRefs.push_back(llvm::Constant::getNullValue( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5552 | ObjCTypes.ProtocolnfABIPtrTy)); |
| 5553 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5554 | llvm::Constant *Values[2]; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 5555 | Values[0] = |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5556 | llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5557 | Values[1] = |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 5558 | llvm::ConstantArray::get( |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5559 | llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5560 | ProtocolRefs.size()), |
| 5561 | ProtocolRefs); |
| 5562 | |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5563 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5564 | GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5565 | llvm::GlobalValue::InternalLinkage, |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5566 | Init, Name); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5567 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5568 | GV->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5569 | CGM.getTargetData().getABITypeAlignment(Init->getType())); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5570 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5571 | return llvm::ConstantExpr::getBitCast(GV, |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5572 | ObjCTypes.ProtocolListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5573 | } |
| 5574 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5575 | /// GetMethodDescriptionConstant - This routine build following meta-data: |
| 5576 | /// struct _objc_method { |
| 5577 | /// SEL _cmd; |
| 5578 | /// char *method_type; |
| 5579 | /// char *_imp; |
| 5580 | /// } |
| 5581 | |
| 5582 | llvm::Constant * |
| 5583 | CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5584 | llvm::Constant *Desc[3]; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 5585 | Desc[0] = |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5586 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 5587 | ObjCTypes.SelectorPtrTy); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5588 | Desc[1] = GetMethodVarType(MD); |
Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5589 | if (!Desc[1]) |
| 5590 | return 0; |
| 5591 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5592 | // Protocol methods have no implementation. So, this entry is always NULL. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5593 | Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5594 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5595 | } |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 5596 | |
| 5597 | /// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference. |
| 5598 | /// This code gen. amounts to generating code for: |
| 5599 | /// @code |
| 5600 | /// (type *)((char *)base + _OBJC_IVAR_$_.ivar; |
| 5601 | /// @encode |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5602 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 5603 | LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar( |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 5604 | CodeGen::CodeGenFunction &CGF, |
| 5605 | QualType ObjectTy, |
| 5606 | llvm::Value *BaseValue, |
| 5607 | const ObjCIvarDecl *Ivar, |
| 5608 | unsigned CVRQualifiers) { |
| 5609 | ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 5610 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 5611 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 5612 | } |
| 5613 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 5614 | llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5615 | CodeGen::CodeGenFunction &CGF, |
| 5616 | const ObjCInterfaceDecl *Interface, |
| 5617 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 2da84ff | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 5618 | return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar"); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 5619 | } |
| 5620 | |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5621 | static void appendSelectorForMessageRefTable(std::string &buffer, |
| 5622 | Selector selector) { |
| 5623 | if (selector.isUnarySelector()) { |
| 5624 | buffer += selector.getNameForSlot(0); |
| 5625 | return; |
| 5626 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5627 | |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5628 | for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) { |
| 5629 | buffer += selector.getNameForSlot(i); |
| 5630 | buffer += '_'; |
| 5631 | } |
| 5632 | } |
| 5633 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5634 | /// Emit a "v-table" message send. We emit a weak hidden-visibility |
| 5635 | /// struct, initially containing the selector pointer and a pointer to |
| 5636 | /// a "fixup" variant of the appropriate objc_msgSend. To call, we |
| 5637 | /// load and call the function pointer, passing the address of the |
| 5638 | /// struct as the second parameter. The runtime determines whether |
| 5639 | /// the selector is currently emitted using vtable dispatch; if so, it |
| 5640 | /// substitutes a stub function which simply tail-calls through the |
| 5641 | /// appropriate vtable slot, and if not, it substitues a stub function |
| 5642 | /// which tail-calls objc_msgSend. Both stubs adjust the selector |
| 5643 | /// argument to correctly point to the selector. |
| 5644 | RValue |
| 5645 | CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF, |
| 5646 | ReturnValueSlot returnSlot, |
| 5647 | QualType resultType, |
| 5648 | Selector selector, |
| 5649 | llvm::Value *arg0, |
| 5650 | QualType arg0Type, |
| 5651 | bool isSuper, |
| 5652 | const CallArgList &formalArgs, |
| 5653 | const ObjCMethodDecl *method) { |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5654 | // Compute the actual arguments. |
| 5655 | CallArgList args; |
| 5656 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5657 | // First argument: the receiver / super-call structure. |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5658 | if (!isSuper) |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5659 | arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy); |
| 5660 | args.add(RValue::get(arg0), arg0Type); |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5661 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5662 | // Second argument: a pointer to the message ref structure. Leave |
| 5663 | // the actual argument value blank for now. |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5664 | args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy); |
| 5665 | |
| 5666 | args.insert(args.end(), formalArgs.begin(), formalArgs.end()); |
| 5667 | |
| 5668 | const CGFunctionInfo &fnInfo = |
| 5669 | CGM.getTypes().getFunctionInfo(resultType, args, |
| 5670 | FunctionType::ExtInfo()); |
| 5671 | |
John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 5672 | NullReturnState nullReturn; |
| 5673 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5674 | // Find the function to call and the mangled name for the message |
| 5675 | // ref structure. Using a different mangled name wouldn't actually |
| 5676 | // be a problem; it would just be a waste. |
| 5677 | // |
| 5678 | // The runtime currently never uses vtable dispatch for anything |
| 5679 | // except normal, non-super message-sends. |
| 5680 | // FIXME: don't use this for that. |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5681 | llvm::Constant *fn = 0; |
| 5682 | std::string messageRefName("\01l_"); |
| 5683 | if (CGM.ReturnTypeUsesSRet(fnInfo)) { |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5684 | if (isSuper) { |
| 5685 | fn = ObjCTypes.getMessageSendSuper2StretFixupFn(); |
| 5686 | messageRefName += "objc_msgSendSuper2_stret_fixup"; |
Chris Lattner | 9b7d670 | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 5687 | } else { |
John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 5688 | nullReturn.init(CGF, arg0); |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5689 | fn = ObjCTypes.getMessageSendStretFixupFn(); |
| 5690 | messageRefName += "objc_msgSend_stret_fixup"; |
Chris Lattner | 9b7d670 | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 5691 | } |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5692 | } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) { |
| 5693 | fn = ObjCTypes.getMessageSendFpretFixupFn(); |
| 5694 | messageRefName += "objc_msgSend_fpret_fixup"; |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5695 | } else { |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5696 | if (isSuper) { |
| 5697 | fn = ObjCTypes.getMessageSendSuper2FixupFn(); |
| 5698 | messageRefName += "objc_msgSendSuper2_fixup"; |
Chris Lattner | 9b7d670 | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 5699 | } else { |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5700 | fn = ObjCTypes.getMessageSendFixupFn(); |
| 5701 | messageRefName += "objc_msgSend_fixup"; |
Chris Lattner | 9b7d670 | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 5702 | } |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5703 | } |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5704 | assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend"); |
| 5705 | messageRefName += '_'; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5706 | |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5707 | // Append the selector name, except use underscores anywhere we |
| 5708 | // would have used colons. |
| 5709 | appendSelectorForMessageRefTable(messageRefName, selector); |
| 5710 | |
| 5711 | llvm::GlobalVariable *messageRef |
| 5712 | = CGM.getModule().getGlobalVariable(messageRefName); |
| 5713 | if (!messageRef) { |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5714 | // Build the message ref structure. |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5715 | llvm::Constant *values[] = { fn, GetMethodVarName(selector) }; |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5716 | llvm::Constant *init = llvm::ConstantStruct::getAnon(values); |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5717 | messageRef = new llvm::GlobalVariable(CGM.getModule(), |
| 5718 | init->getType(), |
| 5719 | /*constant*/ false, |
| 5720 | llvm::GlobalValue::WeakAnyLinkage, |
| 5721 | init, |
| 5722 | messageRefName); |
| 5723 | messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 5724 | messageRef->setAlignment(16); |
| 5725 | messageRef->setSection("__DATA, __objc_msgrefs, coalesced"); |
| 5726 | } |
| 5727 | llvm::Value *mref = |
| 5728 | CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy); |
| 5729 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5730 | // Update the message ref argument. |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5731 | args[1].RV = RValue::get(mref); |
| 5732 | |
| 5733 | // Load the function to call from the message ref table. |
| 5734 | llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0); |
| 5735 | callee = CGF.Builder.CreateLoad(callee, "msgSend_fn"); |
| 5736 | |
| 5737 | bool variadic = method ? method->isVariadic() : false; |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5738 | llvm::FunctionType *fnType = |
John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5739 | CGF.getTypes().GetFunctionType(fnInfo, variadic); |
| 5740 | callee = CGF.Builder.CreateBitCast(callee, |
| 5741 | llvm::PointerType::getUnqual(fnType)); |
| 5742 | |
John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 5743 | RValue result = CGF.EmitCall(fnInfo, callee, returnSlot, args); |
| 5744 | return nullReturn.complete(CGF, result, resultType); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5745 | } |
| 5746 | |
| 5747 | /// Generate code for a message send expression in the nonfragile abi. |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5748 | CodeGen::RValue |
| 5749 | CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 5750 | ReturnValueSlot Return, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5751 | QualType ResultType, |
| 5752 | Selector Sel, |
| 5753 | llvm::Value *Receiver, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5754 | const CallArgList &CallArgs, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 5755 | const ObjCInterfaceDecl *Class, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5756 | const ObjCMethodDecl *Method) { |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5757 | return isVTableDispatchedSelector(Sel) |
| 5758 | ? EmitVTableMessageSend(CGF, Return, ResultType, Sel, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5759 | Receiver, CGF.getContext().getObjCIdType(), |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5760 | false, CallArgs, Method) |
| 5761 | : EmitMessageSend(CGF, Return, ResultType, |
| 5762 | EmitSelector(CGF.Builder, Sel), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5763 | Receiver, CGF.getContext().getObjCIdType(), |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5764 | false, CallArgs, Method, ObjCTypes); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5765 | } |
| 5766 | |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5767 | llvm::GlobalVariable * |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5768 | CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5769 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5770 | |
Daniel Dunbar | dfff230 | 2009-03-02 05:18:14 +0000 | [diff] [blame] | 5771 | if (!GV) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5772 | GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5773 | false, llvm::GlobalValue::ExternalLinkage, |
| 5774 | 0, Name); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5775 | } |
| 5776 | |
| 5777 | return GV; |
| 5778 | } |
| 5779 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5780 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder, |
| 5781 | IdentifierInfo *II) { |
| 5782 | llvm::GlobalVariable *&Entry = ClassReferences[II]; |
| 5783 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5784 | if (!Entry) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5785 | std::string ClassName(getClassSymbolPrefix() + II->getName().str()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5786 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5787 | Entry = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5788 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, |
| 5789 | false, llvm::GlobalValue::InternalLinkage, |
| 5790 | ClassGV, |
| 5791 | "\01L_OBJC_CLASSLIST_REFERENCES_$_"); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5792 | Entry->setAlignment( |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5793 | CGM.getTargetData().getABITypeAlignment( |
| 5794 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5795 | Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5796 | CGM.AddUsedGlobal(Entry); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5797 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5798 | |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 5799 | return Builder.CreateLoad(Entry); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5800 | } |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5801 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5802 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder, |
| 5803 | const ObjCInterfaceDecl *ID) { |
| 5804 | return EmitClassRefFromId(Builder, ID->getIdentifier()); |
| 5805 | } |
| 5806 | |
| 5807 | llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef( |
| 5808 | CGBuilderTy &Builder) { |
| 5809 | IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool"); |
| 5810 | return EmitClassRefFromId(Builder, II); |
| 5811 | } |
| 5812 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5813 | llvm::Value * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5814 | CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5815 | const ObjCInterfaceDecl *ID) { |
| 5816 | llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5817 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5818 | if (!Entry) { |
| 5819 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
| 5820 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5821 | Entry = |
| 5822 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5823 | false, llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5824 | ClassGV, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5825 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_"); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5826 | Entry->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5827 | CGM.getTargetData().getABITypeAlignment( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5828 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5829 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5830 | CGM.AddUsedGlobal(Entry); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5831 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5832 | |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 5833 | return Builder.CreateLoad(Entry); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5834 | } |
| 5835 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5836 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 5837 | /// meta-data |
| 5838 | /// |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5839 | llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder, |
| 5840 | const ObjCInterfaceDecl *ID) { |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5841 | llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()]; |
| 5842 | if (Entry) |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 5843 | return Builder.CreateLoad(Entry); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5844 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5845 | std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5846 | llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5847 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5848 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5849 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5850 | MetaClassGV, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5851 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_"); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5852 | Entry->setAlignment( |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5853 | CGM.getTargetData().getABITypeAlignment( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5854 | ObjCTypes.ClassnfABIPtrTy)); |
| 5855 | |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5856 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5857 | CGM.AddUsedGlobal(Entry); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5858 | |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 5859 | return Builder.CreateLoad(Entry); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5860 | } |
| 5861 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5862 | /// GetClass - Return a reference to the class for the given interface |
| 5863 | /// decl. |
| 5864 | llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder, |
| 5865 | const ObjCInterfaceDecl *ID) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5866 | if (ID->isWeakImported()) { |
Fariborz Jahanian | 269f8bc | 2009-11-17 22:42:00 +0000 | [diff] [blame] | 5867 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
| 5868 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
| 5869 | ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); |
| 5870 | } |
| 5871 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5872 | return EmitClassRef(Builder, ID); |
| 5873 | } |
| 5874 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5875 | /// Generates a message send where the super is the receiver. This is |
| 5876 | /// a message send to self with special delivery semantics indicating |
| 5877 | /// which class's method should be called. |
| 5878 | CodeGen::RValue |
| 5879 | CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 5880 | ReturnValueSlot Return, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5881 | QualType ResultType, |
| 5882 | Selector Sel, |
| 5883 | const ObjCInterfaceDecl *Class, |
| 5884 | bool isCategoryImpl, |
| 5885 | llvm::Value *Receiver, |
| 5886 | bool IsClassMessage, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5887 | const CodeGen::CallArgList &CallArgs, |
| 5888 | const ObjCMethodDecl *Method) { |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5889 | // ... |
| 5890 | // Create and init a super structure; this is a (receiver, class) |
| 5891 | // pair we will pass to objc_msgSendSuper. |
| 5892 | llvm::Value *ObjCSuper = |
John McCall | 604da29 | 2011-03-04 08:00:29 +0000 | [diff] [blame] | 5893 | CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5894 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5895 | llvm::Value *ReceiverAsObject = |
| 5896 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 5897 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 5898 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5899 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5900 | // 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] | 5901 | llvm::Value *Target; |
| 5902 | if (IsClassMessage) { |
| 5903 | if (isCategoryImpl) { |
| 5904 | // Message sent to "super' in a class method defined in |
| 5905 | // a category implementation. |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5906 | Target = EmitClassRef(CGF.Builder, Class); |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5907 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 5908 | Target = CGF.Builder.CreateLoad(Target); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5909 | } else |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5910 | Target = EmitMetaClassRef(CGF.Builder, Class); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5911 | } else |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5912 | Target = EmitSuperClassRef(CGF.Builder, Class); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5913 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 5914 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext and |
| 5915 | // ObjCTypes types. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5916 | llvm::Type *ClassTy = |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5917 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
| 5918 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
| 5919 | CGF.Builder.CreateStore(Target, |
| 5920 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5921 | |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5922 | return (isVTableDispatchedSelector(Sel)) |
| 5923 | ? EmitVTableMessageSend(CGF, Return, ResultType, Sel, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5924 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5925 | true, CallArgs, Method) |
| 5926 | : EmitMessageSend(CGF, Return, ResultType, |
| 5927 | EmitSelector(CGF.Builder, Sel), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5928 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5929 | true, CallArgs, Method, ObjCTypes); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5930 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5931 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5932 | llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder, |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 5933 | Selector Sel, bool lval) { |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5934 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5935 | |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5936 | if (!Entry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5937 | llvm::Constant *Casted = |
| 5938 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 5939 | ObjCTypes.SelectorPtrTy); |
| 5940 | Entry = |
| 5941 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false, |
| 5942 | llvm::GlobalValue::InternalLinkage, |
| 5943 | Casted, "\01L_OBJC_SELECTOR_REFERENCES_"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5944 | Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5945 | CGM.AddUsedGlobal(Entry); |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5946 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5947 | |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 5948 | if (lval) |
| 5949 | return Entry; |
Pete Cooper | b6d7114 | 2011-11-10 21:45:06 +0000 | [diff] [blame] | 5950 | llvm::LoadInst* LI = Builder.CreateLoad(Entry); |
| 5951 | |
| 5952 | LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"), |
| 5953 | llvm::MDNode::get(VMContext, |
| 5954 | ArrayRef<llvm::Value*>())); |
| 5955 | return LI; |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5956 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5957 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 5958 | /// objc_assign_ivar (id src, id *dst, ptrdiff_t) |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5959 | /// |
| 5960 | void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5961 | llvm::Value *src, |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 5962 | llvm::Value *dst, |
| 5963 | llvm::Value *ivarOffset) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5964 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5965 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5966 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5967 | assert(Size <= 8 && "does not support size > 8"); |
| 5968 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5969 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5970 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5971 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5972 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5973 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 5974 | CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(), |
| 5975 | src, dst, ivarOffset); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5976 | return; |
| 5977 | } |
| 5978 | |
| 5979 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 5980 | /// objc_assign_strongCast (id src, id *dst) |
| 5981 | /// |
| 5982 | void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5983 | CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5984 | llvm::Value *src, llvm::Value *dst) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5985 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5986 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5987 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5988 | assert(Size <= 8 && "does not support size > 8"); |
| 5989 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5990 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5991 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5992 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5993 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5994 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5995 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5996 | src, dst, "weakassign"); |
| 5997 | return; |
| 5998 | } |
| 5999 | |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 6000 | void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6001 | CodeGen::CodeGenFunction &CGF, |
| 6002 | llvm::Value *DestPtr, |
| 6003 | llvm::Value *SrcPtr, |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 6004 | llvm::Value *Size) { |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 6005 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy); |
| 6006 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 6007 | CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(), |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 6008 | DestPtr, SrcPtr, Size); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 6009 | return; |
| 6010 | } |
| 6011 | |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6012 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
| 6013 | /// object: objc_read_weak (id *src) |
| 6014 | /// |
| 6015 | llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6016 | CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6017 | llvm::Value *AddrWeakObj) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6018 | llvm::Type* DestTy = |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6019 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
| 6020 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 6021 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6022 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 6023 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6024 | return read_weak; |
| 6025 | } |
| 6026 | |
| 6027 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 6028 | /// objc_assign_weak (id src, id *dst) |
| 6029 | /// |
| 6030 | void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6031 | llvm::Value *src, llvm::Value *dst) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6032 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6033 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 6034 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6035 | assert(Size <= 8 && "does not support size > 8"); |
| 6036 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 6037 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 6038 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 6039 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6040 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 6041 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 6042 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6043 | src, dst, "weakassign"); |
| 6044 | return; |
| 6045 | } |
| 6046 | |
| 6047 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 6048 | /// objc_assign_global (id src, id *dst) |
| 6049 | /// |
| 6050 | void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 6051 | llvm::Value *src, llvm::Value *dst, |
| 6052 | bool threadlocal) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 6053 | llvm::Type * SrcTy = src->getType(); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6054 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 6055 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 6056 | assert(Size <= 8 && "does not support size > 8"); |
| 6057 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 6058 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 6059 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 6060 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6061 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 6062 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 6063 | if (!threadlocal) |
| 6064 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
| 6065 | src, dst, "globalassign"); |
| 6066 | else |
| 6067 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(), |
| 6068 | src, dst, "threadlocalassign"); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6069 | return; |
| 6070 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6071 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6072 | void |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6073 | CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 6074 | const ObjCAtSynchronizedStmt &S) { |
David Chisnall | 05dc91b | 2011-03-25 17:46:35 +0000 | [diff] [blame] | 6075 | EmitAtSynchronizedStmt(CGF, S, |
| 6076 | cast<llvm::Function>(ObjCTypes.getSyncEnterFn()), |
| 6077 | cast<llvm::Function>(ObjCTypes.getSyncExitFn())); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6078 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6079 | |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6080 | llvm::Constant * |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 6081 | CGObjCNonFragileABIMac::GetEHType(QualType T) { |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6082 | // There's a particular fixed type info for 'id'. |
| 6083 | if (T->isObjCIdType() || |
| 6084 | T->isObjCQualifiedIdType()) { |
| 6085 | llvm::Constant *IDEHType = |
| 6086 | CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id"); |
| 6087 | if (!IDEHType) |
| 6088 | IDEHType = |
| 6089 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, |
| 6090 | false, |
| 6091 | llvm::GlobalValue::ExternalLinkage, |
| 6092 | 0, "OBJC_EHTYPE_id"); |
| 6093 | return IDEHType; |
| 6094 | } |
| 6095 | |
| 6096 | // All other types should be Objective-C interface pointer types. |
| 6097 | const ObjCObjectPointerType *PT = |
| 6098 | T->getAs<ObjCObjectPointerType>(); |
| 6099 | assert(PT && "Invalid @catch type."); |
| 6100 | const ObjCInterfaceType *IT = PT->getInterfaceType(); |
| 6101 | assert(IT && "Invalid @catch type."); |
| 6102 | return GetInterfaceEHType(IT->getDecl(), false); |
| 6103 | } |
| 6104 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6105 | void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
| 6106 | const ObjCAtTryStmt &S) { |
David Chisnall | 05dc91b | 2011-03-25 17:46:35 +0000 | [diff] [blame] | 6107 | EmitTryCatchStmt(CGF, S, |
| 6108 | cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()), |
| 6109 | cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()), |
| 6110 | cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn())); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 6111 | } |
| 6112 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6113 | /// EmitThrowStmt - Generate code for a throw statement. |
| 6114 | void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 6115 | const ObjCAtThrowStmt &S) { |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6116 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
John McCall | 2b014d6 | 2011-10-01 10:32:24 +0000 | [diff] [blame] | 6117 | llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr); |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 6118 | Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy); |
Jay Foad | 4c7d9f1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 6119 | CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception) |
John McCall | 7ec404c | 2010-10-16 08:21:07 +0000 | [diff] [blame] | 6120 | .setDoesNotReturn(); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6121 | } else { |
Jay Foad | 4c7d9f1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 6122 | CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn()) |
John McCall | 7ec404c | 2010-10-16 08:21:07 +0000 | [diff] [blame] | 6123 | .setDoesNotReturn(); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6124 | } |
| 6125 | |
John McCall | 7ec404c | 2010-10-16 08:21:07 +0000 | [diff] [blame] | 6126 | CGF.Builder.CreateUnreachable(); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6127 | CGF.Builder.ClearInsertionPoint(); |
| 6128 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6129 | |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6130 | llvm::Constant * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6131 | CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6132 | bool ForDefinition) { |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6133 | llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()]; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6134 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6135 | // If we don't need a definition, return the entry if found or check |
| 6136 | // if we use an external reference. |
| 6137 | if (!ForDefinition) { |
| 6138 | if (Entry) |
| 6139 | return Entry; |
Daniel Dunbar | 7e075cb | 2009-04-07 06:43:45 +0000 | [diff] [blame] | 6140 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6141 | // If this type (or a super class) has the __objc_exception__ |
| 6142 | // attribute, emit an external reference. |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 6143 | if (hasObjCExceptionAttribute(CGM.getContext(), ID)) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6144 | return Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6145 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false, |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6146 | llvm::GlobalValue::ExternalLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6147 | 0, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6148 | ("OBJC_EHTYPE_$_" + |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 6149 | ID->getIdentifier()->getName())); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6150 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6151 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6152 | // Otherwise we need to either make a new entry or fill in the |
| 6153 | // initializer. |
| 6154 | assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition"); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 6155 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6156 | std::string VTableName = "objc_ehtype_vtable"; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6157 | llvm::GlobalVariable *VTableGV = |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6158 | CGM.getModule().getGlobalVariable(VTableName); |
| 6159 | if (!VTableGV) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6160 | VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy, |
| 6161 | false, |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6162 | llvm::GlobalValue::ExternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6163 | 0, VTableName); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6164 | |
Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 6165 | llvm::Value *VTableIdx = |
| 6166 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6167 | |
Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 6168 | llvm::Constant *Values[] = { |
| 6169 | llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx), |
| 6170 | GetClassName(ID->getIdentifier()), |
| 6171 | GetClassGlobal(ClassName) |
| 6172 | }; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 6173 | llvm::Constant *Init = |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 6174 | llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6175 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6176 | if (Entry) { |
| 6177 | Entry->setInitializer(Init); |
| 6178 | } else { |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6179 | Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false, |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6180 | llvm::GlobalValue::WeakAnyLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6181 | Init, |
Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6182 | ("OBJC_EHTYPE_$_" + |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 6183 | ID->getIdentifier()->getName())); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6184 | } |
| 6185 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 6186 | if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility) |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 6187 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 6188 | Entry->setAlignment(CGM.getTargetData().getABITypeAlignment( |
| 6189 | ObjCTypes.EHTypeTy)); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6190 | |
| 6191 | if (ForDefinition) { |
| 6192 | Entry->setSection("__DATA,__objc_const"); |
| 6193 | Entry->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 6194 | } else { |
| 6195 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 6196 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6197 | |
| 6198 | return Entry; |
| 6199 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6200 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 6201 | /* *** */ |
| 6202 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 6203 | CodeGen::CGObjCRuntime * |
| 6204 | CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { |
David Chisnall | 60be607 | 2011-03-22 21:21:24 +0000 | [diff] [blame] | 6205 | if (CGM.getLangOptions().ObjCNonFragileABI) |
| 6206 | return new CGObjCNonFragileABIMac(CGM); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 6207 | return new CGObjCMac(CGM); |
| 6208 | } |