| 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; | 
|  | 178 | llvm::Type *Int8PtrTy; | 
| 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 | 
|  | 769 | /// selector's name. 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. | 
|  | 772 | llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D); | 
| Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 773 | llvm::Constant *GetMethodVarType(const FieldDecl *D); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 774 |  | 
| Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 775 | /// GetPropertyName - Return a unique constant for the given | 
|  | 776 | /// name. The return value has type char *. | 
|  | 777 | llvm::Constant *GetPropertyName(IdentifierInfo *Ident); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 778 |  | 
| Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 779 | // FIXME: This can be dropped once string functions are unified. | 
|  | 780 | llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD, | 
|  | 781 | const Decl *Container); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 782 |  | 
| Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 783 | /// GetClassName - Return a unique constant for the given selector's | 
|  | 784 | /// name. The return value has type char *. | 
|  | 785 | llvm::Constant *GetClassName(IdentifierInfo *Ident); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 786 |  | 
| Argyrios Kyrtzidis | 9d50c06 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 787 | llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD); | 
|  | 788 |  | 
| Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 789 | /// BuildIvarLayout - Builds ivar layout bitmap for the class | 
|  | 790 | /// implementation for the __strong or __weak case. | 
|  | 791 | /// | 
| Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 792 | llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI, | 
|  | 793 | bool ForStrongLayout); | 
| Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 794 |  | 
| Fariborz Jahanian | b8fd2eb | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 795 | llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 796 |  | 
| Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 797 | void BuildAggrIvarRecordLayout(const RecordType *RT, | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 798 | unsigned int BytePos, bool ForStrongLayout, | 
|  | 799 | bool &HasUnion); | 
| Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 800 | void BuildAggrIvarLayout(const ObjCImplementationDecl *OI, | 
| Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 801 | const llvm::StructLayout *Layout, | 
| Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 802 | const RecordDecl *RD, | 
| Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 803 | const SmallVectorImpl<const FieldDecl*> &RecFields, | 
| Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 804 | unsigned int BytePos, bool ForStrongLayout, | 
| Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 805 | bool &HasUnion); | 
| Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 806 |  | 
| Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 807 | /// GetIvarLayoutName - Returns a unique constant for the given | 
|  | 808 | /// ivar layout bitmap. | 
|  | 809 | llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident, | 
|  | 810 | const ObjCCommonTypesHelper &ObjCTypes); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 811 |  | 
| Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 812 | /// EmitPropertyList - Emit the given property list. The return | 
|  | 813 | /// value has type PropertyListPtrTy. | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 814 | llvm::Constant *EmitPropertyList(Twine Name, | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 815 | const Decl *Container, | 
| Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 816 | const ObjCContainerDecl *OCD, | 
|  | 817 | const ObjCCommonTypesHelper &ObjCTypes); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 818 |  | 
| Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 819 | /// PushProtocolProperties - Push protocol's property on the input stack. | 
|  | 820 | void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet, | 
|  | 821 | std::vector<llvm::Constant*> &Properties, | 
|  | 822 | const Decl *Container, | 
|  | 823 | const ObjCProtocolDecl *PROTO, | 
|  | 824 | const ObjCCommonTypesHelper &ObjCTypes); | 
|  | 825 |  | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 826 | /// GetProtocolRef - Return a reference to the internal protocol | 
|  | 827 | /// description, creating an empty one if it has not been | 
|  | 828 | /// defined. The return value has type ProtocolPtrTy. | 
|  | 829 | llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD); | 
| Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 830 |  | 
| Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 831 | /// CreateMetadataVar - Create a global variable with internal | 
|  | 832 | /// linkage for use by the Objective-C runtime. | 
|  | 833 | /// | 
|  | 834 | /// This is a convenience wrapper which not only creates the | 
|  | 835 | /// variable, but also sets the section and alignment and adds the | 
| Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 836 | /// global to the "llvm.used" list. | 
| Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 837 | /// | 
|  | 838 | /// \param Name - The variable name. | 
|  | 839 | /// \param Init - The variable initializer; this is also used to | 
|  | 840 | /// define the type of the variable. | 
|  | 841 | /// \param Section - The section the variable should go into, or 0. | 
|  | 842 | /// \param Align - The alignment for the variable, or 0. | 
|  | 843 | /// \param AddToUsed - Whether the variable should be added to | 
| Daniel Dunbar | c158306 | 2009-04-14 17:42:51 +0000 | [diff] [blame] | 844 | /// "llvm.used". | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 845 | llvm::GlobalVariable *CreateMetadataVar(Twine Name, | 
| Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 846 | llvm::Constant *Init, | 
|  | 847 | const char *Section, | 
| Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 848 | unsigned Align, | 
|  | 849 | bool AddToUsed); | 
| Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 850 |  | 
| John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 851 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, | 
|  | 852 | ReturnValueSlot Return, | 
|  | 853 | QualType ResultType, | 
|  | 854 | llvm::Value *Sel, | 
|  | 855 | llvm::Value *Arg0, | 
|  | 856 | QualType Arg0Ty, | 
|  | 857 | bool IsSuper, | 
|  | 858 | const CallArgList &CallArgs, | 
|  | 859 | const ObjCMethodDecl *OMD, | 
|  | 860 | const ObjCCommonTypesHelper &ObjCTypes); | 
| Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 861 |  | 
| Daniel Dunbar | fce176b | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 862 | /// EmitImageInfo - Emit the image info marker used to encode some module | 
|  | 863 | /// level information. | 
|  | 864 | void EmitImageInfo(); | 
|  | 865 |  | 
| Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 866 | public: | 
| Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 867 | CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 868 | CGM(cgm), VMContext(cgm.getLLVMContext()) { } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 869 |  | 
| David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 870 | virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 871 |  | 
| Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 872 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, | 
|  | 873 | const ObjCContainerDecl *CD=0); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 874 |  | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 875 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 876 |  | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 877 | /// GetOrEmitProtocol - Get the protocol object for the given | 
|  | 878 | /// declaration, emitting it if necessary. The return value has type | 
|  | 879 | /// ProtocolPtrTy. | 
|  | 880 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=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 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol | 
|  | 883 | /// object for the given declaration, emitting it if needed. These | 
|  | 884 | /// forward references will be filled in with empty bodies if no | 
|  | 885 | /// definition is seen. The return value has type ProtocolPtrTy. | 
|  | 886 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0; | 
| John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 887 | virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM, | 
|  | 888 | const CGBlockInfo &blockInfo); | 
| Fariborz Jahanian | 89ecd41 | 2010-08-04 16:57:49 +0000 | [diff] [blame] | 889 |  | 
| Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 890 | }; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 891 |  | 
| Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 892 | class CGObjCMac : public CGObjCCommonMac { | 
|  | 893 | private: | 
|  | 894 | ObjCTypesHelper ObjCTypes; | 
| Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 895 |  | 
| Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 896 | /// EmitModuleInfo - Another marker encoding module level | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 897 | /// information. | 
| Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 898 | void EmitModuleInfo(); | 
|  | 899 |  | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 900 | /// EmitModuleSymols - Emit module symbols, the list of defined | 
|  | 901 | /// classes and categories. The result has type SymtabPtrTy. | 
| Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 902 | llvm::Constant *EmitModuleSymbols(); | 
|  | 903 |  | 
| Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 904 | /// FinishModule - Write out global data structures at the end of | 
|  | 905 | /// processing a translation unit. | 
|  | 906 | void FinishModule(); | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 907 |  | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 908 | /// EmitClassExtension - Generate the class extension structure used | 
|  | 909 | /// to store the weak ivar layout and properties. The return value | 
|  | 910 | /// has type ClassExtensionPtrTy. | 
|  | 911 | llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID); | 
|  | 912 |  | 
|  | 913 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, | 
|  | 914 | /// for the given class. | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 915 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 916 | const ObjCInterfaceDecl *ID); | 
| Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 917 |  | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 918 | llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder, | 
|  | 919 | IdentifierInfo *II); | 
|  | 920 |  | 
|  | 921 | llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder); | 
|  | 922 |  | 
| Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 923 | /// EmitSuperClassRef - Emits reference to class's main metadata class. | 
|  | 924 | llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 925 |  | 
|  | 926 | /// EmitIvarList - Emit the ivar list for the given | 
|  | 927 | /// implementation. If ForClass is true the list of class ivars | 
|  | 928 | /// (i.e. metaclass ivars) is emitted, otherwise the list of | 
|  | 929 | /// interface ivars will be emitted. The return value has type | 
|  | 930 | /// IvarListPtrTy. | 
|  | 931 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID, | 
| Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 932 | bool ForClass); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 933 |  | 
| Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 934 | /// EmitMetaClass - Emit a forward reference to the class structure | 
|  | 935 | /// for the metaclass of the given interface. The return value has | 
|  | 936 | /// type ClassPtrTy. | 
|  | 937 | llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID); | 
|  | 938 |  | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 939 | /// EmitMetaClass - Emit a class structure for the metaclass of the | 
| Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 940 | /// given implementation. The return value has type ClassPtrTy. | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 941 | llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID, | 
|  | 942 | llvm::Constant *Protocols, | 
| Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 943 | const ConstantVector &Methods); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 944 |  | 
| Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 945 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 946 |  | 
| Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 947 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 948 |  | 
|  | 949 | /// EmitMethodList - Emit the method list for the given | 
| Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 950 | /// implementation. The return value has type MethodListPtrTy. | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 951 | llvm::Constant *EmitMethodList(Twine Name, | 
| Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 952 | const char *Section, | 
| Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 953 | const ConstantVector &Methods); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 954 |  | 
|  | 955 | /// EmitMethodDescList - Emit a method description list for a list of | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 956 | /// method declarations. | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 957 | ///  - TypeName: The name for the type containing the methods. | 
|  | 958 | ///  - IsProtocol: True iff these methods are for a protocol. | 
|  | 959 | ///  - ClassMethds: True iff these are class methods. | 
|  | 960 | ///  - Required: When true, only "required" methods are | 
|  | 961 | ///    listed. Similarly, when false only "optional" methods are | 
|  | 962 | ///    listed. For classes this should always be true. | 
|  | 963 | ///  - begin, end: The method list to output. | 
|  | 964 | /// | 
|  | 965 | /// The return value has type MethodDescriptionListPtrTy. | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 966 | llvm::Constant *EmitMethodDescList(Twine Name, | 
| Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 967 | const char *Section, | 
|  | 968 | const ConstantVector &Methods); | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 969 |  | 
| Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 970 | /// GetOrEmitProtocol - Get the protocol object for the given | 
|  | 971 | /// declaration, emitting it if necessary. The return value has type | 
|  | 972 | /// ProtocolPtrTy. | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 973 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); | 
| Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 974 |  | 
|  | 975 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol | 
|  | 976 | /// object for the given declaration, emitting it if needed. These | 
|  | 977 | /// forward references will be filled in with empty bodies if no | 
|  | 978 | /// definition is seen. The return value has type ProtocolPtrTy. | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 979 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); | 
| Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 980 |  | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 981 | /// EmitProtocolExtension - Generate the protocol extension | 
|  | 982 | /// structure used to store optional instance and class methods, and | 
|  | 983 | /// protocol properties. The return value has type | 
|  | 984 | /// ProtocolExtensionPtrTy. | 
| Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 985 | llvm::Constant * | 
|  | 986 | EmitProtocolExtension(const ObjCProtocolDecl *PD, | 
|  | 987 | const ConstantVector &OptInstanceMethods, | 
|  | 988 | const ConstantVector &OptClassMethods); | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 989 |  | 
|  | 990 | /// EmitProtocolList - Generate the list of referenced | 
|  | 991 | /// protocols. The return value has type ProtocolListPtrTy. | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 992 | llvm::Constant *EmitProtocolList(Twine Name, | 
| Daniel Dunbar | dbc93370 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 993 | ObjCProtocolDecl::protocol_iterator begin, | 
|  | 994 | ObjCProtocolDecl::protocol_iterator end); | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 995 |  | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 996 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, | 
|  | 997 | /// for the given selector. | 
| Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 998 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel, | 
|  | 999 | bool lval=false); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1000 |  | 
|  | 1001 | public: | 
| Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1002 | CGObjCMac(CodeGen::CodeGenModule &cgm); | 
| Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1003 |  | 
| Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1004 | virtual llvm::Function *ModuleInitFunction(); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1005 |  | 
| Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1006 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, | 
| John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1007 | ReturnValueSlot Return, | 
| Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1008 | QualType ResultType, | 
|  | 1009 | Selector Sel, | 
| Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1010 | llvm::Value *Receiver, | 
| Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1011 | const CallArgList &CallArgs, | 
| David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1012 | const ObjCInterfaceDecl *Class, | 
| Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1013 | const ObjCMethodDecl *Method); | 
| Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1014 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1015 | virtual CodeGen::RValue | 
| Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1016 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, | 
| John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1017 | ReturnValueSlot Return, | 
| Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1018 | QualType ResultType, | 
|  | 1019 | Selector Sel, | 
| Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1020 | const ObjCInterfaceDecl *Class, | 
| Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1021 | bool isCategoryImpl, | 
| Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1022 | llvm::Value *Receiver, | 
| Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1023 | bool IsClassMessage, | 
| Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1024 | const CallArgList &CallArgs, | 
|  | 1025 | const ObjCMethodDecl *Method); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1026 |  | 
| Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1027 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1028 | const ObjCInterfaceDecl *ID); | 
| Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1029 |  | 
| Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1030 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel, | 
|  | 1031 | bool lval = false); | 
| Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1032 |  | 
|  | 1033 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an | 
|  | 1034 | /// untyped one. | 
|  | 1035 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, | 
|  | 1036 | const ObjCMethodDecl *Method); | 
|  | 1037 |  | 
| Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1038 | virtual llvm::Constant *GetEHType(QualType T); | 
| John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1039 |  | 
| Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1040 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); | 
| Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1041 |  | 
| Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1042 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); | 
| Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1043 |  | 
| Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1044 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, | 
| Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1045 | const ObjCProtocolDecl *PD); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1046 |  | 
| Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1047 | virtual llvm::Constant *GetPropertyGetFunction(); | 
|  | 1048 | virtual llvm::Constant *GetPropertySetFunction(); | 
| David Chisnall | 8fac25d | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 1049 | virtual llvm::Constant *GetGetStructFunction(); | 
|  | 1050 | virtual llvm::Constant *GetSetStructFunction(); | 
| Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1051 | virtual llvm::Constant *EnumerationMutationFunction(); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1052 |  | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1053 | virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF, | 
|  | 1054 | const ObjCAtTryStmt &S); | 
|  | 1055 | virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, | 
|  | 1056 | const ObjCAtSynchronizedStmt &S); | 
|  | 1057 | void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S); | 
| Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1058 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, | 
|  | 1059 | const ObjCAtThrowStmt &S); | 
| Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1060 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1061 | llvm::Value *AddrWeakObj); | 
| Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1062 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1063 | llvm::Value *src, llvm::Value *dst); | 
| Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1064 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, | 
| Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 1065 | llvm::Value *src, llvm::Value *dest, | 
|  | 1066 | bool threadlocal = false); | 
| Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 1067 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, | 
| Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1068 | llvm::Value *src, llvm::Value *dest, | 
|  | 1069 | llvm::Value *ivarOffset); | 
| Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1070 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, | 
|  | 1071 | llvm::Value *src, llvm::Value *dest); | 
| Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 1072 | virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, | 
|  | 1073 | llvm::Value *dest, llvm::Value *src, | 
| Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 1074 | llvm::Value *size); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1075 |  | 
| Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1076 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, | 
|  | 1077 | QualType ObjectTy, | 
|  | 1078 | llvm::Value *BaseValue, | 
|  | 1079 | const ObjCIvarDecl *Ivar, | 
| Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1080 | unsigned CVRQualifiers); | 
| Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1081 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, | 
| Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1082 | const ObjCInterfaceDecl *Interface, | 
| Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1083 | const ObjCIvarDecl *Ivar); | 
| Fariborz Jahanian | 6f40e22 | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1084 |  | 
|  | 1085 | /// GetClassGlobal - Return the global variable for the Objective-C | 
|  | 1086 | /// class of the given name. | 
|  | 1087 | virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) { | 
| David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1088 | llvm_unreachable("CGObjCMac::GetClassGlobal"); | 
| Fariborz Jahanian | 6f40e22 | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1089 | } | 
| Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1090 | }; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1091 |  | 
| Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1092 | class CGObjCNonFragileABIMac : public CGObjCCommonMac { | 
| Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1093 | private: | 
| Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1094 | ObjCNonFragileABITypesHelper ObjCTypes; | 
| Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1095 | llvm::GlobalVariable* ObjCEmptyCacheVar; | 
|  | 1096 | llvm::GlobalVariable* ObjCEmptyVtableVar; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1097 |  | 
| Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1098 | /// SuperClassReferences - uniqued super class references. | 
|  | 1099 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1100 |  | 
| Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1101 | /// MetaClassReferences - uniqued meta class references. | 
|  | 1102 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences; | 
| Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1103 |  | 
|  | 1104 | /// EHTypeReferences - uniqued class ehtype references. | 
|  | 1105 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1106 |  | 
| John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1107 | /// VTableDispatchMethods - List of methods for which we generate | 
|  | 1108 | /// vtable-based message dispatch. | 
|  | 1109 | llvm::DenseSet<Selector> VTableDispatchMethods; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1110 |  | 
| Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 1111 | /// DefinedMetaClasses - List of defined meta-classes. | 
|  | 1112 | std::vector<llvm::GlobalValue*> DefinedMetaClasses; | 
|  | 1113 |  | 
| John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1114 | /// isVTableDispatchedSelector - Returns true if SEL is a | 
|  | 1115 | /// vtable-based selector. | 
|  | 1116 | bool isVTableDispatchedSelector(Selector Sel); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1117 |  | 
| Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1118 | /// FinishNonFragileABIModule - Write out global data structures at the end of | 
|  | 1119 | /// processing a translation unit. | 
|  | 1120 | void FinishNonFragileABIModule(); | 
| Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1121 |  | 
| Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 1122 | /// AddModuleClassList - Add the given list of class pointers to the | 
|  | 1123 | /// module with the provided symbol and section names. | 
|  | 1124 | void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container, | 
|  | 1125 | const char *SymbolName, | 
|  | 1126 | const char *SectionName); | 
|  | 1127 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1128 | llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags, | 
|  | 1129 | unsigned InstanceStart, | 
|  | 1130 | unsigned InstanceSize, | 
|  | 1131 | const ObjCImplementationDecl *ID); | 
| Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1132 | llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName, | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1133 | llvm::Constant *IsAGV, | 
| Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1134 | llvm::Constant *SuperClassGV, | 
| Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 1135 | llvm::Constant *ClassRoGV, | 
|  | 1136 | bool HiddenVisibility); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1137 |  | 
| Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1138 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1139 |  | 
| Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 1140 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1141 |  | 
| Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1142 | /// EmitMethodList - Emit the method list for the given | 
|  | 1143 | /// implementation. The return value has type MethodListnfABITy. | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1144 | llvm::Constant *EmitMethodList(Twine Name, | 
| Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1145 | const char *Section, | 
| Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 1146 | const ConstantVector &Methods); | 
|  | 1147 | /// EmitIvarList - Emit the ivar list for the given | 
|  | 1148 | /// implementation. If ForClass is true the list of class ivars | 
|  | 1149 | /// (i.e. metaclass ivars) is emitted, otherwise the list of | 
|  | 1150 | /// interface ivars will be emitted. The return value has type | 
|  | 1151 | /// IvarListnfABIPtrTy. | 
|  | 1152 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1153 |  | 
| Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1154 | llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, | 
| Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 1155 | const ObjCIvarDecl *Ivar, | 
| Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 1156 | unsigned long int offset); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1157 |  | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1158 | /// GetOrEmitProtocol - Get the protocol object for the given | 
|  | 1159 | /// declaration, emitting it if necessary. The return value has type | 
|  | 1160 | /// ProtocolPtrTy. | 
|  | 1161 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1162 |  | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1163 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol | 
|  | 1164 | /// object for the given declaration, emitting it if needed. These | 
|  | 1165 | /// forward references will be filled in with empty bodies if no | 
|  | 1166 | /// definition is seen. The return value has type ProtocolPtrTy. | 
|  | 1167 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1168 |  | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1169 | /// EmitProtocolList - Generate the list of referenced | 
|  | 1170 | /// protocols. The return value has type ProtocolListPtrTy. | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1171 | llvm::Constant *EmitProtocolList(Twine Name, | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1172 | ObjCProtocolDecl::protocol_iterator begin, | 
| Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1173 | ObjCProtocolDecl::protocol_iterator end); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1174 |  | 
| John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1175 | CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF, | 
|  | 1176 | ReturnValueSlot Return, | 
|  | 1177 | QualType ResultType, | 
|  | 1178 | Selector Sel, | 
|  | 1179 | llvm::Value *Receiver, | 
|  | 1180 | QualType Arg0Ty, | 
|  | 1181 | bool IsSuper, | 
|  | 1182 | const CallArgList &CallArgs, | 
|  | 1183 | const ObjCMethodDecl *Method); | 
| Fariborz Jahanian | 6f40e22 | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1184 |  | 
| Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 1185 | /// GetClassGlobal - Return the global variable for the Objective-C | 
|  | 1186 | /// class of the given name. | 
| Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 1187 | llvm::GlobalVariable *GetClassGlobal(const std::string &Name); | 
| Fariborz Jahanian | 6f40e22 | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 1188 |  | 
| Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1189 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, | 
| Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1190 | /// for the given class reference. | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1191 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, | 
| Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1192 | const ObjCInterfaceDecl *ID); | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1193 |  | 
|  | 1194 | llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder, | 
|  | 1195 | IdentifierInfo *II); | 
|  | 1196 |  | 
|  | 1197 | llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1198 |  | 
| Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1199 | /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, | 
|  | 1200 | /// for the given super class reference. | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1201 | llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder, | 
|  | 1202 | const ObjCInterfaceDecl *ID); | 
|  | 1203 |  | 
| Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1204 | /// EmitMetaClassRef - Return a Value * of the address of _class_t | 
|  | 1205 | /// meta-data | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1206 | llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder, | 
| Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1207 | const ObjCInterfaceDecl *ID); | 
|  | 1208 |  | 
| Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1209 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for | 
|  | 1210 | /// the given ivar. | 
|  | 1211 | /// | 
| Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 1212 | llvm::GlobalVariable * ObjCIvarOffsetVariable( | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1213 | const ObjCInterfaceDecl *ID, | 
|  | 1214 | const ObjCIvarDecl *Ivar); | 
|  | 1215 |  | 
| Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 1216 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, | 
|  | 1217 | /// for the given selector. | 
| Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1218 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel, | 
|  | 1219 | bool lval=false); | 
| Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1220 |  | 
| Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1221 | /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C | 
| Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1222 | /// interface. The return value has type EHTypePtrTy. | 
| John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1223 | llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID, | 
| Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1224 | bool ForDefinition); | 
| Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1225 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1226 | const char *getMetaclassSymbolPrefix() const { | 
| Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1227 | return "OBJC_METACLASS_$_"; | 
|  | 1228 | } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1229 |  | 
| Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1230 | const char *getClassSymbolPrefix() const { | 
|  | 1231 | return "OBJC_CLASS_$_"; | 
|  | 1232 | } | 
|  | 1233 |  | 
| Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 1234 | void GetClassSizeInfo(const ObjCImplementationDecl *OID, | 
| Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1235 | uint32_t &InstanceStart, | 
|  | 1236 | uint32_t &InstanceSize); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1237 |  | 
| Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1238 | // Shamelessly stolen from Analysis/CFRefCount.cpp | 
| Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1239 | Selector GetNullarySelector(const char* name) const { | 
| Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1240 | IdentifierInfo* II = &CGM.getContext().Idents.get(name); | 
|  | 1241 | return CGM.getContext().Selectors.getSelector(0, &II); | 
|  | 1242 | } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1243 |  | 
| Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1244 | Selector GetUnarySelector(const char* name) const { | 
| Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1245 | IdentifierInfo* II = &CGM.getContext().Idents.get(name); | 
|  | 1246 | return CGM.getContext().Selectors.getSelector(1, &II); | 
|  | 1247 | } | 
| Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1248 |  | 
| Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1249 | /// ImplementationIsNonLazy - Check whether the given category or | 
|  | 1250 | /// class implementation is "non-lazy". | 
| Fariborz Jahanian | ecfbdcb | 2009-05-21 01:03:45 +0000 | [diff] [blame] | 1251 | bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const; | 
| Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1252 |  | 
| Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1253 | public: | 
| Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1254 | CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm); | 
| Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1255 | // FIXME. All stubs for now! | 
|  | 1256 | virtual llvm::Function *ModuleInitFunction(); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1257 |  | 
| Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1258 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, | 
| John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1259 | ReturnValueSlot Return, | 
| Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1260 | QualType ResultType, | 
|  | 1261 | Selector Sel, | 
|  | 1262 | llvm::Value *Receiver, | 
| Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1263 | const CallArgList &CallArgs, | 
| David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1264 | const ObjCInterfaceDecl *Class, | 
| Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1265 | const ObjCMethodDecl *Method); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1266 |  | 
|  | 1267 | virtual CodeGen::RValue | 
| Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1268 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, | 
| John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1269 | ReturnValueSlot Return, | 
| Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1270 | QualType ResultType, | 
|  | 1271 | Selector Sel, | 
|  | 1272 | const ObjCInterfaceDecl *Class, | 
| Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1273 | bool isCategoryImpl, | 
| Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1274 | llvm::Value *Receiver, | 
|  | 1275 | bool IsClassMessage, | 
| Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1276 | const CallArgList &CallArgs, | 
|  | 1277 | const ObjCMethodDecl *Method); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1278 |  | 
| Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1279 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, | 
| Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1280 | const ObjCInterfaceDecl *ID); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1281 |  | 
| Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1282 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel, | 
|  | 1283 | bool lvalue = false) | 
|  | 1284 | { return EmitSelector(Builder, Sel, lvalue); } | 
| Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1285 |  | 
|  | 1286 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an | 
|  | 1287 | /// untyped one. | 
|  | 1288 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, | 
|  | 1289 | const ObjCMethodDecl *Method) | 
|  | 1290 | { return EmitSelector(Builder, Method->getSelector()); } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1291 |  | 
| Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 1292 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1293 |  | 
| Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1294 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); | 
| Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1295 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, | 
| Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 1296 | const ObjCProtocolDecl *PD); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1297 |  | 
| Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1298 | virtual llvm::Constant *GetEHType(QualType T); | 
| John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1299 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1300 | virtual llvm::Constant *GetPropertyGetFunction() { | 
| Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1301 | return ObjCTypes.getGetPropertyFn(); | 
| Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1302 | } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1303 | virtual llvm::Constant *GetPropertySetFunction() { | 
|  | 1304 | return ObjCTypes.getSetPropertyFn(); | 
| Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1305 | } | 
| Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 1306 |  | 
| David Chisnall | 8fac25d | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 1307 | virtual llvm::Constant *GetSetStructFunction() { | 
|  | 1308 | return ObjCTypes.getCopyStructFn(); | 
|  | 1309 | } | 
|  | 1310 | virtual llvm::Constant *GetGetStructFunction() { | 
| Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 1311 | return ObjCTypes.getCopyStructFn(); | 
|  | 1312 | } | 
|  | 1313 |  | 
| Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1314 | virtual llvm::Constant *EnumerationMutationFunction() { | 
| Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1315 | return ObjCTypes.getEnumerationMutationFn(); | 
| Daniel Dunbar | 28ed084 | 2009-02-16 18:48:45 +0000 | [diff] [blame] | 1316 | } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1317 |  | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1318 | virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF, | 
|  | 1319 | const ObjCAtTryStmt &S); | 
|  | 1320 | virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, | 
|  | 1321 | const ObjCAtSynchronizedStmt &S); | 
| Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1322 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, | 
| Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 1323 | const ObjCAtThrowStmt &S); | 
| Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1324 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, | 
| Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1325 | llvm::Value *AddrWeakObj); | 
| Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1326 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, | 
| Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1327 | llvm::Value *src, llvm::Value *dst); | 
| Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1328 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, | 
| Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 1329 | llvm::Value *src, llvm::Value *dest, | 
|  | 1330 | bool threadlocal = false); | 
| Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1331 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, | 
| Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 1332 | llvm::Value *src, llvm::Value *dest, | 
|  | 1333 | llvm::Value *ivarOffset); | 
| Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1334 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, | 
| Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1335 | llvm::Value *src, llvm::Value *dest); | 
| Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 1336 | virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, | 
|  | 1337 | llvm::Value *dest, llvm::Value *src, | 
| Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 1338 | llvm::Value *size); | 
| Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1339 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, | 
|  | 1340 | QualType ObjectTy, | 
|  | 1341 | llvm::Value *BaseValue, | 
|  | 1342 | const ObjCIvarDecl *Ivar, | 
| Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1343 | unsigned CVRQualifiers); | 
| Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1344 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, | 
| Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1345 | const ObjCInterfaceDecl *Interface, | 
| Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1346 | const ObjCIvarDecl *Ivar); | 
| Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1347 | }; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1348 |  | 
| John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 1349 | /// A helper class for performing the null-initialization of a return | 
|  | 1350 | /// value. | 
|  | 1351 | struct NullReturnState { | 
|  | 1352 | llvm::BasicBlock *NullBB; | 
| Fariborz Jahanian | 6c29eda | 2011-10-26 20:53:59 +0000 | [diff] [blame] | 1353 | llvm::BasicBlock *callBB; | 
|  | 1354 | NullReturnState() : NullBB(0), callBB(0) {} | 
| John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 1355 |  | 
|  | 1356 | void init(CodeGenFunction &CGF, llvm::Value *receiver) { | 
|  | 1357 | // Make blocks for the null-init and call edges. | 
|  | 1358 | NullBB = CGF.createBasicBlock("msgSend.nullinit"); | 
| Fariborz Jahanian | 6c29eda | 2011-10-26 20:53:59 +0000 | [diff] [blame] | 1359 | callBB = CGF.createBasicBlock("msgSend.call"); | 
| John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 1360 |  | 
|  | 1361 | // Check for a null receiver and, if there is one, jump to the | 
|  | 1362 | // null-init test. | 
|  | 1363 | llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver); | 
|  | 1364 | CGF.Builder.CreateCondBr(isNull, NullBB, callBB); | 
|  | 1365 |  | 
|  | 1366 | // Otherwise, start performing the call. | 
|  | 1367 | CGF.EmitBlock(callBB); | 
|  | 1368 | } | 
|  | 1369 |  | 
|  | 1370 | RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType) { | 
|  | 1371 | if (!NullBB) return result; | 
|  | 1372 |  | 
|  | 1373 | // Finish the call path. | 
|  | 1374 | llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont"); | 
|  | 1375 | if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB); | 
|  | 1376 |  | 
|  | 1377 | // Emit the null-init block and perform the null-initialization there. | 
|  | 1378 | CGF.EmitBlock(NullBB); | 
| Fariborz Jahanian | 6c29eda | 2011-10-26 20:53:59 +0000 | [diff] [blame] | 1379 | if (!resultType->isAnyComplexType()) { | 
|  | 1380 | assert(result.isAggregate() && "null init of non-aggregate result?"); | 
|  | 1381 | CGF.EmitNullInitialization(result.getAggregateAddr(), resultType); | 
|  | 1382 | // Jump to the continuation block. | 
|  | 1383 | CGF.EmitBlock(contBB); | 
|  | 1384 | return result; | 
|  | 1385 | } | 
| John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 1386 |  | 
| Fariborz Jahanian | 6c29eda | 2011-10-26 20:53:59 +0000 | [diff] [blame] | 1387 | // _Complex type | 
|  | 1388 | // FIXME. Now easy to handle any other scalar type whose result is returned | 
|  | 1389 | // in memory due to ABI limitations. | 
| John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 1390 | CGF.EmitBlock(contBB); | 
| Fariborz Jahanian | 6c29eda | 2011-10-26 20:53:59 +0000 | [diff] [blame] | 1391 | CodeGenFunction::ComplexPairTy CallCV = result.getComplexVal(); | 
|  | 1392 | llvm::Type *MemberType = CallCV.first->getType(); | 
|  | 1393 | llvm::Constant *ZeroCV = llvm::Constant::getNullValue(MemberType); | 
|  | 1394 | // Create phi instruction for scalar complex value. | 
|  | 1395 | llvm::PHINode *PHIReal = CGF.Builder.CreatePHI(MemberType, 2); | 
|  | 1396 | PHIReal->addIncoming(ZeroCV, NullBB); | 
|  | 1397 | PHIReal->addIncoming(CallCV.first, callBB); | 
|  | 1398 | llvm::PHINode *PHIImag = CGF.Builder.CreatePHI(MemberType, 2); | 
|  | 1399 | PHIImag->addIncoming(ZeroCV, NullBB); | 
|  | 1400 | PHIImag->addIncoming(CallCV.second, callBB); | 
|  | 1401 | return RValue::getComplex(PHIReal, PHIImag); | 
| John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 1402 | } | 
|  | 1403 | }; | 
|  | 1404 |  | 
| Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1405 | } // end anonymous namespace | 
| Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1406 |  | 
|  | 1407 | /* *** Helper Functions *** */ | 
|  | 1408 |  | 
|  | 1409 | /// getConstantGEP() - Help routine to construct simple GEPs. | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1410 | static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext, | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1411 | llvm::Constant *C, | 
| Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1412 | unsigned idx0, | 
|  | 1413 | unsigned idx1) { | 
|  | 1414 | llvm::Value *Idxs[] = { | 
| Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1415 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0), | 
|  | 1416 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1) | 
| Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1417 | }; | 
| Jay Foad | a5c0434 | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 1418 | return llvm::ConstantExpr::getGetElementPtr(C, Idxs); | 
| Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1419 | } | 
|  | 1420 |  | 
| Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1421 | /// hasObjCExceptionAttribute - Return true if this class or any super | 
|  | 1422 | /// class has the __objc_exception__ attribute. | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1423 | static bool hasObjCExceptionAttribute(ASTContext &Context, | 
| Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 1424 | const ObjCInterfaceDecl *OID) { | 
| Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1425 | if (OID->hasAttr<ObjCExceptionAttr>()) | 
| Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1426 | return true; | 
|  | 1427 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) | 
| Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 1428 | return hasObjCExceptionAttribute(Context, Super); | 
| Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1429 | return false; | 
|  | 1430 | } | 
|  | 1431 |  | 
| Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1432 | /* *** CGObjCMac Public Interface *** */ | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1433 |  | 
| Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1434 | CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm), | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1435 | ObjCTypes(cgm) { | 
| Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1436 | ObjCABI = 1; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1437 | EmitImageInfo(); | 
| Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1438 | } | 
|  | 1439 |  | 
| Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 1440 | /// GetClass - Return a reference to the class for the given interface | 
|  | 1441 | /// decl. | 
| Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1442 | llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder, | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1443 | const ObjCInterfaceDecl *ID) { | 
|  | 1444 | return EmitClassRef(Builder, ID); | 
| Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1445 | } | 
|  | 1446 |  | 
|  | 1447 | /// GetSelector - Return the pointer to the unique'd string for this selector. | 
| Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1448 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel, | 
|  | 1449 | bool lval) { | 
|  | 1450 | return EmitSelector(Builder, Sel, lval); | 
| Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1451 | } | 
| Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1452 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1453 | *Method) { | 
| Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1454 | return EmitSelector(Builder, Method->getSelector()); | 
|  | 1455 | } | 
| Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1456 |  | 
| Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1457 | llvm::Constant *CGObjCMac::GetEHType(QualType T) { | 
| Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 1458 | if (T->isObjCIdType() || | 
|  | 1459 | T->isObjCQualifiedIdType()) { | 
|  | 1460 | return CGM.GetAddrOfRTTIDescriptor( | 
| Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 1461 | CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true); | 
| Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 1462 | } | 
| Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1463 | if (T->isObjCClassType() || | 
|  | 1464 | T->isObjCQualifiedClassType()) { | 
|  | 1465 | return CGM.GetAddrOfRTTIDescriptor( | 
| Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 1466 | CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true); | 
| Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1467 | } | 
|  | 1468 | if (T->isObjCObjectPointerType()) | 
|  | 1469 | return CGM.GetAddrOfRTTIDescriptor(T,  /*ForEH=*/true); | 
|  | 1470 |  | 
| John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1471 | llvm_unreachable("asking for catch type for ObjC type in fragile runtime"); | 
|  | 1472 | return 0; | 
|  | 1473 | } | 
|  | 1474 |  | 
| Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1475 | /// Generate a constant CFString object. | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1476 | /* | 
|  | 1477 | struct __builtin_CFString { | 
|  | 1478 | const int *isa; // point to __CFConstantStringClassReference | 
|  | 1479 | int flags; | 
|  | 1480 | const char *str; | 
|  | 1481 | long length; | 
|  | 1482 | }; | 
| Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1483 | */ | 
|  | 1484 |  | 
| Fariborz Jahanian | 33e982b | 2010-04-22 20:26:39 +0000 | [diff] [blame] | 1485 | /// or Generate a constant NSString object. | 
|  | 1486 | /* | 
|  | 1487 | struct __builtin_NSString { | 
|  | 1488 | const int *isa; // point to __NSConstantStringClassReference | 
|  | 1489 | const char *str; | 
|  | 1490 | unsigned int length; | 
|  | 1491 | }; | 
|  | 1492 | */ | 
|  | 1493 |  | 
| Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1494 | llvm::Constant *CGObjCCommonMac::GenerateConstantString( | 
| David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 1495 | const StringLiteral *SL) { | 
| Fariborz Jahanian | 33e982b | 2010-04-22 20:26:39 +0000 | [diff] [blame] | 1496 | return (CGM.getLangOptions().NoConstantCFStrings == 0 ? | 
|  | 1497 | CGM.GetAddrOfConstantCFString(SL) : | 
| Fariborz Jahanian | 4c73307 | 2010-10-19 17:19:29 +0000 | [diff] [blame] | 1498 | CGM.GetAddrOfConstantString(SL)); | 
| Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1499 | } | 
|  | 1500 |  | 
|  | 1501 | /// Generates a message send where the super is the receiver.  This is | 
|  | 1502 | /// a message send to self with special delivery semantics indicating | 
|  | 1503 | /// which class's method should be called. | 
| Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1504 | CodeGen::RValue | 
|  | 1505 | CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, | 
| John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1506 | ReturnValueSlot Return, | 
| Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1507 | QualType ResultType, | 
|  | 1508 | Selector Sel, | 
| Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1509 | const ObjCInterfaceDecl *Class, | 
| Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1510 | bool isCategoryImpl, | 
| Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1511 | llvm::Value *Receiver, | 
| Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1512 | bool IsClassMessage, | 
| Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1513 | const CodeGen::CallArgList &CallArgs, | 
|  | 1514 | const ObjCMethodDecl *Method) { | 
| Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1515 | // Create and init a super structure; this is a (receiver, class) | 
|  | 1516 | // pair we will pass to objc_msgSendSuper. | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1517 | llvm::Value *ObjCSuper = | 
| John McCall | 604da29 | 2011-03-04 08:00:29 +0000 | [diff] [blame] | 1518 | CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super"); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1519 | llvm::Value *ReceiverAsObject = | 
| Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1520 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1521 | CGF.Builder.CreateStore(ReceiverAsObject, | 
| Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1522 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); | 
| Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1523 |  | 
| Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1524 | // If this is a class message the metaclass is passed as the target. | 
|  | 1525 | llvm::Value *Target; | 
|  | 1526 | if (IsClassMessage) { | 
| Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1527 | if (isCategoryImpl) { | 
|  | 1528 | // Message sent to 'super' in a class method defined in a category | 
|  | 1529 | // implementation requires an odd treatment. | 
|  | 1530 | // If we are in a class method, we must retrieve the | 
|  | 1531 | // _metaclass_ for the current class, pointed at by | 
|  | 1532 | // the class's "isa" pointer.  The following assumes that | 
|  | 1533 | // isa" is the first ivar in a class (which it must be). | 
|  | 1534 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); | 
|  | 1535 | Target = CGF.Builder.CreateStructGEP(Target, 0); | 
|  | 1536 | Target = CGF.Builder.CreateLoad(Target); | 
| Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 1537 | } else { | 
| Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1538 | llvm::Value *MetaClassPtr = EmitMetaClassRef(Class); | 
|  | 1539 | llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1); | 
|  | 1540 | llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr); | 
|  | 1541 | Target = Super; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1542 | } | 
| Fariborz Jahanian | 182f268 | 2009-11-14 02:18:31 +0000 | [diff] [blame] | 1543 | } | 
|  | 1544 | else if (isCategoryImpl) | 
|  | 1545 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); | 
|  | 1546 | else { | 
| Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 1547 | llvm::Value *ClassPtr = EmitSuperClassRef(Class); | 
|  | 1548 | ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1); | 
|  | 1549 | Target = CGF.Builder.CreateLoad(ClassPtr); | 
| Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1550 | } | 
| Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1551 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext and | 
|  | 1552 | // ObjCTypes types. | 
| Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1553 | llvm::Type *ClassTy = | 
| Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1554 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); | 
| Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1555 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1556 | CGF.Builder.CreateStore(Target, | 
| Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1557 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); | 
| John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1558 | return EmitMessageSend(CGF, Return, ResultType, | 
|  | 1559 | EmitSelector(CGF.Builder, Sel), | 
|  | 1560 | ObjCSuper, ObjCTypes.SuperPtrCTy, | 
|  | 1561 | true, CallArgs, Method, ObjCTypes); | 
| Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1562 | } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1563 |  | 
|  | 1564 | /// Generate code for a message send expression. | 
| Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1565 | CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, | 
| John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1566 | ReturnValueSlot Return, | 
| Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1567 | QualType ResultType, | 
|  | 1568 | Selector Sel, | 
| Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1569 | llvm::Value *Receiver, | 
| Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1570 | const CallArgList &CallArgs, | 
| David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1571 | const ObjCInterfaceDecl *Class, | 
| Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1572 | const ObjCMethodDecl *Method) { | 
| John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1573 | return EmitMessageSend(CGF, Return, ResultType, | 
|  | 1574 | EmitSelector(CGF.Builder, Sel), | 
|  | 1575 | Receiver, CGF.getContext().getObjCIdType(), | 
|  | 1576 | false, CallArgs, Method, ObjCTypes); | 
| Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1577 | } | 
|  | 1578 |  | 
| Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1579 | CodeGen::RValue | 
| John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 1580 | CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF, | 
|  | 1581 | ReturnValueSlot Return, | 
|  | 1582 | QualType ResultType, | 
|  | 1583 | llvm::Value *Sel, | 
|  | 1584 | llvm::Value *Arg0, | 
|  | 1585 | QualType Arg0Ty, | 
|  | 1586 | bool IsSuper, | 
|  | 1587 | const CallArgList &CallArgs, | 
|  | 1588 | const ObjCMethodDecl *Method, | 
|  | 1589 | const ObjCCommonTypesHelper &ObjCTypes) { | 
| Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1590 | CallArgList ActualArgs; | 
| Fariborz Jahanian | d019d96 | 2009-04-24 21:07:43 +0000 | [diff] [blame] | 1591 | if (!IsSuper) | 
| Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 1592 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy); | 
| Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1593 | ActualArgs.add(RValue::get(Arg0), Arg0Ty); | 
|  | 1594 | ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType()); | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1595 | ActualArgs.addFrom(CallArgs); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1596 |  | 
| Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1597 | CodeGenTypes &Types = CGM.getTypes(); | 
| John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1598 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs, | 
| Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1599 | FunctionType::ExtInfo()); | 
| Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1600 | llvm::FunctionType *FTy = | 
| Daniel Dunbar | 6793966 | 2009-09-17 04:01:40 +0000 | [diff] [blame] | 1601 | Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1602 |  | 
| Anders Carlsson | 7e70fb2 | 2010-06-21 20:59:55 +0000 | [diff] [blame] | 1603 | if (Method) | 
|  | 1604 | assert(CGM.getContext().getCanonicalType(Method->getResultType()) == | 
|  | 1605 | CGM.getContext().getCanonicalType(ResultType) && | 
|  | 1606 | "Result type mismatch!"); | 
|  | 1607 |  | 
| John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 1608 | NullReturnState nullReturn; | 
|  | 1609 |  | 
| Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1610 | llvm::Constant *Fn = NULL; | 
| Daniel Dunbar | dacf9dd | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 1611 | if (CGM.ReturnTypeUsesSRet(FnInfo)) { | 
| John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 1612 | if (!IsSuper) nullReturn.init(CGF, Arg0); | 
| Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1613 | Fn = (ObjCABI == 2) ?  ObjCTypes.getSendStretFn2(IsSuper) | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1614 | : ObjCTypes.getSendStretFn(IsSuper); | 
| Daniel Dunbar | dacf9dd | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 1615 | } else if (CGM.ReturnTypeUsesFPRet(ResultType)) { | 
|  | 1616 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper) | 
|  | 1617 | : ObjCTypes.getSendFpretFn(IsSuper); | 
| Anders Carlsson | eea6480 | 2011-10-31 16:27:11 +0000 | [diff] [blame] | 1618 | } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) { | 
|  | 1619 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper) | 
|  | 1620 | : ObjCTypes.getSendFp2retFn(IsSuper); | 
| Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1621 | } else { | 
| Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1622 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper) | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1623 | : ObjCTypes.getSendFn(IsSuper); | 
| Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1624 | } | 
| Daniel Dunbar | dacf9dd | 2010-07-14 23:39:36 +0000 | [diff] [blame] | 1625 | Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy)); | 
| John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 1626 | RValue rvalue = CGF.EmitCall(FnInfo, Fn, Return, ActualArgs); | 
|  | 1627 | return nullReturn.complete(CGF, rvalue, ResultType); | 
| Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1628 | } | 
|  | 1629 |  | 
| Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1630 | static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) { | 
|  | 1631 | if (FQT.isObjCGCStrong()) | 
|  | 1632 | return Qualifiers::Strong; | 
|  | 1633 |  | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1634 | if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak) | 
| Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1635 | return Qualifiers::Weak; | 
|  | 1636 |  | 
|  | 1637 | if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType()) | 
|  | 1638 | return Qualifiers::Strong; | 
|  | 1639 |  | 
|  | 1640 | if (const PointerType *PT = FQT->getAs<PointerType>()) | 
|  | 1641 | return GetGCAttrTypeForType(Ctx, PT->getPointeeType()); | 
|  | 1642 |  | 
|  | 1643 | return Qualifiers::GCNone; | 
|  | 1644 | } | 
|  | 1645 |  | 
| John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1646 | llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM, | 
|  | 1647 | const CGBlockInfo &blockInfo) { | 
|  | 1648 | llvm::Constant *nullPtr = | 
| Fariborz Jahanian | 44034db | 2010-08-04 18:44:59 +0000 | [diff] [blame] | 1649 | llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext)); | 
| John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1650 |  | 
| Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 1651 | if (CGM.getLangOptions().getGC() == LangOptions::NonGC && | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1652 | !CGM.getLangOptions().ObjCAutoRefCount) | 
| John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1653 | return nullPtr; | 
|  | 1654 |  | 
| Fariborz Jahanian | a1f024c | 2010-08-06 16:28:55 +0000 | [diff] [blame] | 1655 | bool hasUnion = false; | 
| Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1656 | SkipIvars.clear(); | 
|  | 1657 | IvarsInfo.clear(); | 
| Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1658 | unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0); | 
|  | 1659 | unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth(); | 
| Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1660 |  | 
| Fariborz Jahanian | 8197982 | 2010-09-09 00:21:45 +0000 | [diff] [blame] | 1661 | // __isa is the first field in block descriptor and must assume by runtime's | 
|  | 1662 | // convention that it is GC'able. | 
|  | 1663 | IvarsInfo.push_back(GC_IVAR(0, 1)); | 
| John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1664 |  | 
|  | 1665 | const BlockDecl *blockDecl = blockInfo.getBlockDecl(); | 
|  | 1666 |  | 
|  | 1667 | // Calculate the basic layout of the block structure. | 
|  | 1668 | const llvm::StructLayout *layout = | 
|  | 1669 | CGM.getTargetData().getStructLayout(blockInfo.StructureType); | 
|  | 1670 |  | 
|  | 1671 | // Ignore the optional 'this' capture: C++ objects are not assumed | 
|  | 1672 | // to be GC'ed. | 
|  | 1673 |  | 
|  | 1674 | // Walk the captured variables. | 
|  | 1675 | for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(), | 
|  | 1676 | ce = blockDecl->capture_end(); ci != ce; ++ci) { | 
|  | 1677 | const VarDecl *variable = ci->getVariable(); | 
|  | 1678 | QualType type = variable->getType(); | 
|  | 1679 |  | 
|  | 1680 | const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable); | 
|  | 1681 |  | 
|  | 1682 | // Ignore constant captures. | 
|  | 1683 | if (capture.isConstant()) continue; | 
|  | 1684 |  | 
|  | 1685 | uint64_t fieldOffset = layout->getElementOffset(capture.getIndex()); | 
|  | 1686 |  | 
|  | 1687 | // __block variables are passed by their descriptor address. | 
|  | 1688 | if (ci->isByRef()) { | 
|  | 1689 | IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1)); | 
| Fariborz Jahanian | c5904b4 | 2010-09-11 01:27:29 +0000 | [diff] [blame] | 1690 | continue; | 
| John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1691 | } | 
|  | 1692 |  | 
|  | 1693 | assert(!type->isArrayType() && "array variable should not be caught"); | 
|  | 1694 | if (const RecordType *record = type->getAs<RecordType>()) { | 
|  | 1695 | BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion); | 
| Fariborz Jahanian | e1a4898 | 2010-08-05 21:00:25 +0000 | [diff] [blame] | 1696 | continue; | 
|  | 1697 | } | 
| Fariborz Jahanian | a1f024c | 2010-08-06 16:28:55 +0000 | [diff] [blame] | 1698 |  | 
| John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1699 | Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type); | 
|  | 1700 | unsigned fieldSize = CGM.getContext().getTypeSize(type); | 
|  | 1701 |  | 
|  | 1702 | if (GCAttr == Qualifiers::Strong) | 
|  | 1703 | IvarsInfo.push_back(GC_IVAR(fieldOffset, | 
|  | 1704 | fieldSize / WordSizeInBits)); | 
| Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1705 | else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak) | 
| John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1706 | SkipIvars.push_back(GC_IVAR(fieldOffset, | 
|  | 1707 | fieldSize / ByteSizeInBits)); | 
| Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1708 | } | 
|  | 1709 |  | 
|  | 1710 | if (IvarsInfo.empty()) | 
| John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1711 | return nullPtr; | 
|  | 1712 |  | 
|  | 1713 | // Sort on byte position; captures might not be allocated in order, | 
|  | 1714 | // and unions can do funny things. | 
|  | 1715 | llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end()); | 
|  | 1716 | llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end()); | 
| Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1717 |  | 
|  | 1718 | std::string BitMap; | 
| Fariborz Jahanian | b8fd2eb | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 1719 | llvm::Constant *C = BuildIvarLayoutBitmap(BitMap); | 
| Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 1720 | if (CGM.getLangOptions().ObjCGCBitmapPrint) { | 
|  | 1721 | printf("\n block variable layout for block: "); | 
|  | 1722 | const unsigned char *s = (unsigned char*)BitMap.c_str(); | 
|  | 1723 | for (unsigned i = 0; i < BitMap.size(); i++) | 
|  | 1724 | if (!(s[i] & 0xf0)) | 
|  | 1725 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); | 
|  | 1726 | else | 
|  | 1727 | printf("0x%x%s",  s[i], s[i] != 0 ? ", " : ""); | 
|  | 1728 | printf("\n"); | 
|  | 1729 | } | 
|  | 1730 |  | 
|  | 1731 | return C; | 
| Fariborz Jahanian | 89ecd41 | 2010-08-04 16:57:49 +0000 | [diff] [blame] | 1732 | } | 
|  | 1733 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1734 | llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder, | 
| Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1735 | const ObjCProtocolDecl *PD) { | 
| Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1736 | // 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] | 1737 | // resolved. Investigate. Its also wasteful to look this up over and over. | 
| Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1738 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); | 
|  | 1739 |  | 
| Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1740 | return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1741 | ObjCTypes.ExternalProtocolPtrTy); | 
| Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1742 | } | 
|  | 1743 |  | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1744 | void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) { | 
| Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1745 | // FIXME: We shouldn't need this, the protocol decl should contain enough | 
|  | 1746 | // information to tell us whether this was a declaration or a definition. | 
| Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1747 | DefinedProtocols.insert(PD->getIdentifier()); | 
|  | 1748 |  | 
|  | 1749 | // If we have generated a forward reference to this protocol, emit | 
|  | 1750 | // it now. Otherwise do nothing, the protocol objects are lazily | 
|  | 1751 | // emitted. | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1752 | if (Protocols.count(PD->getIdentifier())) | 
| Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1753 | GetOrEmitProtocol(PD); | 
|  | 1754 | } | 
|  | 1755 |  | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1756 | llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) { | 
| Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1757 | if (DefinedProtocols.count(PD->getIdentifier())) | 
|  | 1758 | return GetOrEmitProtocol(PD); | 
| Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 1759 |  | 
| Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1760 | return GetOrEmitProtocolRef(PD); | 
|  | 1761 | } | 
|  | 1762 |  | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1763 | /* | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1764 | // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions | 
|  | 1765 | struct _objc_protocol { | 
|  | 1766 | struct _objc_protocol_extension *isa; | 
|  | 1767 | char *protocol_name; | 
|  | 1768 | struct _objc_protocol_list *protocol_list; | 
|  | 1769 | struct _objc__method_prototype_list *instance_methods; | 
|  | 1770 | struct _objc__method_prototype_list *class_methods | 
|  | 1771 | }; | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1772 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1773 | See EmitProtocolExtension(). | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1774 | */ | 
| Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1775 | llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) { | 
|  | 1776 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; | 
|  | 1777 |  | 
|  | 1778 | // Early exit if a defining object has already been generated. | 
|  | 1779 | if (Entry && Entry->hasInitializer()) | 
|  | 1780 | return Entry; | 
|  | 1781 |  | 
| Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1782 | // 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] | 1783 | // resolved. Investigate. Its also wasteful to look this up over and over. | 
| Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1784 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); | 
|  | 1785 |  | 
| Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1786 | // Construct method lists. | 
|  | 1787 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; | 
|  | 1788 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1789 | for (ObjCProtocolDecl::instmeth_iterator | 
| Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1790 | i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) { | 
| Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1791 | ObjCMethodDecl *MD = *i; | 
|  | 1792 | llvm::Constant *C = GetMethodDescriptionConstant(MD); | 
| Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 1793 | if (!C) | 
|  | 1794 | return GetOrEmitProtocolRef(PD); | 
|  | 1795 |  | 
| Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1796 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { | 
|  | 1797 | OptInstanceMethods.push_back(C); | 
|  | 1798 | } else { | 
|  | 1799 | InstanceMethods.push_back(C); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1800 | } | 
| Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1801 | } | 
|  | 1802 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1803 | for (ObjCProtocolDecl::classmeth_iterator | 
| Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1804 | i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) { | 
| Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1805 | ObjCMethodDecl *MD = *i; | 
|  | 1806 | llvm::Constant *C = GetMethodDescriptionConstant(MD); | 
| Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 1807 | if (!C) | 
|  | 1808 | return GetOrEmitProtocolRef(PD); | 
|  | 1809 |  | 
| Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1810 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { | 
|  | 1811 | OptClassMethods.push_back(C); | 
|  | 1812 | } else { | 
|  | 1813 | ClassMethods.push_back(C); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1814 | } | 
| Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1815 | } | 
|  | 1816 |  | 
| Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1817 | llvm::Constant *Values[] = { | 
|  | 1818 | EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods), | 
|  | 1819 | GetClassName(PD->getIdentifier()), | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1820 | EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(), | 
| Daniel Dunbar | dbc93370 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1821 | PD->protocol_begin(), | 
| Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1822 | PD->protocol_end()), | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1823 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(), | 
| Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1824 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", | 
| Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1825 | InstanceMethods), | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1826 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(), | 
| Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1827 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", | 
| Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1828 | ClassMethods) | 
|  | 1829 | }; | 
| Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 1830 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1831 | Values); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1832 |  | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1833 | if (Entry) { | 
| Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1834 | // Already created, fix the linkage and update the initializer. | 
|  | 1835 | Entry->setLinkage(llvm::GlobalValue::InternalLinkage); | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1836 | Entry->setInitializer(Init); | 
|  | 1837 | } else { | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1838 | Entry = | 
| Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1839 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false, | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1840 | llvm::GlobalValue::InternalLinkage, | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1841 | Init, | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1842 | "\01L_OBJC_PROTOCOL_" + PD->getName()); | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1843 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1844 | // FIXME: Is this necessary? Why only for protocol? | 
|  | 1845 | Entry->setAlignment(4); | 
|  | 1846 | } | 
| Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 1847 | CGM.AddUsedGlobal(Entry); | 
| Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1848 |  | 
|  | 1849 | return Entry; | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1850 | } | 
|  | 1851 |  | 
| Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1852 | llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) { | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1853 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; | 
|  | 1854 |  | 
|  | 1855 | if (!Entry) { | 
| Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1856 | // We use the initializer as a marker of whether this is a forward | 
|  | 1857 | // reference or not. At module finalization we add the empty | 
|  | 1858 | // contents for protocols which were referenced but never defined. | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1859 | Entry = | 
| Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1860 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false, | 
| Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1861 | llvm::GlobalValue::ExternalLinkage, | 
|  | 1862 | 0, | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1863 | "\01L_OBJC_PROTOCOL_" + PD->getName()); | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1864 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1865 | // FIXME: Is this necessary? Why only for protocol? | 
|  | 1866 | Entry->setAlignment(4); | 
|  | 1867 | } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1868 |  | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1869 | return Entry; | 
|  | 1870 | } | 
|  | 1871 |  | 
|  | 1872 | /* | 
|  | 1873 | struct _objc_protocol_extension { | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1874 | uint32_t size; | 
|  | 1875 | struct objc_method_description_list *optional_instance_methods; | 
|  | 1876 | struct objc_method_description_list *optional_class_methods; | 
|  | 1877 | struct objc_property_list *instance_properties; | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1878 | }; | 
|  | 1879 | */ | 
| Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1880 | llvm::Constant * | 
|  | 1881 | CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD, | 
|  | 1882 | const ConstantVector &OptInstanceMethods, | 
|  | 1883 | const ConstantVector &OptClassMethods) { | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1884 | uint64_t Size = | 
| Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 1885 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy); | 
| Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1886 | llvm::Constant *Values[] = { | 
|  | 1887 | llvm::ConstantInt::get(ObjCTypes.IntTy, Size), | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1888 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_" | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1889 | + PD->getName(), | 
| Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1890 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", | 
| Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1891 | OptInstanceMethods), | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1892 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(), | 
| Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1893 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", | 
| Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1894 | OptClassMethods), | 
|  | 1895 | EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD, | 
|  | 1896 | ObjCTypes) | 
|  | 1897 | }; | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1898 |  | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1899 | // Return null if no extension bits are used. | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1900 | if (Values[1]->isNullValue() && Values[2]->isNullValue() && | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1901 | Values[3]->isNullValue()) | 
| Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1902 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1903 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1904 | llvm::Constant *Init = | 
| Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 1905 | llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values); | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1906 |  | 
| Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1907 | // No special section, but goes in llvm.used | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 1908 | return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(), | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1909 | Init, | 
| Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1910 | 0, 0, true); | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1911 | } | 
|  | 1912 |  | 
|  | 1913 | /* | 
|  | 1914 | struct objc_protocol_list { | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1915 | struct objc_protocol_list *next; | 
|  | 1916 | long count; | 
|  | 1917 | Protocol *list[]; | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1918 | }; | 
|  | 1919 | */ | 
| Daniel Dunbar | dbc93370 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1920 | llvm::Constant * | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1921 | CGObjCMac::EmitProtocolList(Twine Name, | 
| Daniel Dunbar | dbc93370 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1922 | ObjCProtocolDecl::protocol_iterator begin, | 
|  | 1923 | ObjCProtocolDecl::protocol_iterator end) { | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1924 | std::vector<llvm::Constant*> ProtocolRefs; | 
|  | 1925 |  | 
| Daniel Dunbar | dbc93370 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1926 | for (; begin != end; ++begin) | 
|  | 1927 | ProtocolRefs.push_back(GetProtocolRef(*begin)); | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1928 |  | 
|  | 1929 | // Just return null for empty protocol lists | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1930 | if (ProtocolRefs.empty()) | 
| Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1931 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1932 |  | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1933 | // This list is null terminated. | 
| Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1934 | ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy)); | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1935 |  | 
| Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 1936 | llvm::Constant *Values[3]; | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1937 | // This field is only used by the runtime. | 
| Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1938 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); | 
| Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1939 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1940 | ProtocolRefs.size() - 1); | 
|  | 1941 | Values[2] = | 
|  | 1942 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy, | 
|  | 1943 | ProtocolRefs.size()), | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1944 | ProtocolRefs); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1945 |  | 
| Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 1946 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1947 | llvm::GlobalVariable *GV = | 
| Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1948 | CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip", | 
| Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1949 | 4, false); | 
| Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1950 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy); | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1951 | } | 
|  | 1952 |  | 
| Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 1953 | void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet, | 
|  | 1954 | std::vector<llvm::Constant*> &Properties, | 
|  | 1955 | const Decl *Container, | 
|  | 1956 | const ObjCProtocolDecl *PROTO, | 
|  | 1957 | const ObjCCommonTypesHelper &ObjCTypes) { | 
| Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 1958 | for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(), | 
|  | 1959 | E = PROTO->protocol_end(); P != E; ++P) | 
|  | 1960 | PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes); | 
|  | 1961 | for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(), | 
|  | 1962 | E = PROTO->prop_end(); I != E; ++I) { | 
|  | 1963 | const ObjCPropertyDecl *PD = *I; | 
|  | 1964 | if (!PropertySet.insert(PD->getIdentifier())) | 
|  | 1965 | continue; | 
| Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1966 | llvm::Constant *Prop[] = { | 
|  | 1967 | GetPropertyName(PD->getIdentifier()), | 
|  | 1968 | GetPropertyTypeString(PD, Container) | 
|  | 1969 | }; | 
| Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 1970 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop)); | 
|  | 1971 | } | 
|  | 1972 | } | 
|  | 1973 |  | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1974 | /* | 
| Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1975 | struct _objc_property { | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1976 | const char * const name; | 
|  | 1977 | const char * const attributes; | 
| Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1978 | }; | 
|  | 1979 |  | 
|  | 1980 | struct _objc_property_list { | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1981 | uint32_t entsize; // sizeof (struct _objc_property) | 
|  | 1982 | uint32_t prop_count; | 
|  | 1983 | struct _objc_property[prop_count]; | 
| Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1984 | }; | 
|  | 1985 | */ | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1986 | llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name, | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1987 | const Decl *Container, | 
|  | 1988 | const ObjCContainerDecl *OCD, | 
|  | 1989 | const ObjCCommonTypesHelper &ObjCTypes) { | 
| Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1990 | std::vector<llvm::Constant*> Properties; | 
| Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 1991 | llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1992 | for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(), | 
|  | 1993 | E = OCD->prop_end(); I != E; ++I) { | 
| Steve Naroff | 93983f8 | 2009-01-11 12:47:58 +0000 | [diff] [blame] | 1994 | const ObjCPropertyDecl *PD = *I; | 
| Fariborz Jahanian | 191dcd7 | 2009-12-12 21:26:21 +0000 | [diff] [blame] | 1995 | PropertySet.insert(PD->getIdentifier()); | 
| Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 1996 | llvm::Constant *Prop[] = { | 
|  | 1997 | GetPropertyName(PD->getIdentifier()), | 
|  | 1998 | GetPropertyTypeString(PD, Container) | 
|  | 1999 | }; | 
| Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2000 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, | 
| Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2001 | Prop)); | 
|  | 2002 | } | 
| Fariborz Jahanian | 6afbdf5 | 2010-06-22 16:33:55 +0000 | [diff] [blame] | 2003 | if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) { | 
| Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 2004 | for (ObjCInterfaceDecl::all_protocol_iterator | 
|  | 2005 | P = OID->all_referenced_protocol_begin(), | 
|  | 2006 | E = OID->all_referenced_protocol_end(); P != E; ++P) | 
| Fariborz Jahanian | 6afbdf5 | 2010-06-22 16:33:55 +0000 | [diff] [blame] | 2007 | PushProtocolProperties(PropertySet, Properties, Container, (*P), | 
|  | 2008 | ObjCTypes); | 
|  | 2009 | } | 
|  | 2010 | else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) { | 
|  | 2011 | for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(), | 
|  | 2012 | E = CD->protocol_end(); P != E; ++P) | 
|  | 2013 | PushProtocolProperties(PropertySet, Properties, Container, (*P), | 
|  | 2014 | ObjCTypes); | 
|  | 2015 | } | 
| Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2016 |  | 
|  | 2017 | // Return null for empty list. | 
|  | 2018 | if (Properties.empty()) | 
| Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2019 | return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); | 
| Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2020 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2021 | unsigned PropertySize = | 
| Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2022 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy); | 
| Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2023 | llvm::Constant *Values[3]; | 
| Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2024 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize); | 
|  | 2025 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size()); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2026 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy, | 
| Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2027 | Properties.size()); | 
| Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2028 | Values[2] = llvm::ConstantArray::get(AT, Properties); | 
| Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2029 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); | 
| Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2030 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2031 | llvm::GlobalVariable *GV = | 
|  | 2032 | CreateMetadataVar(Name, Init, | 
|  | 2033 | (ObjCABI == 2) ? "__DATA, __objc_const" : | 
| Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2034 | "__OBJC,__property,regular,no_dead_strip", | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2035 | (ObjCABI == 2) ? 8 : 4, | 
| Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2036 | true); | 
| Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2037 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy); | 
| Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2038 | } | 
|  | 2039 |  | 
|  | 2040 | /* | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2041 | struct objc_method_description_list { | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2042 | int count; | 
|  | 2043 | struct objc_method_description list[]; | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2044 | }; | 
|  | 2045 | */ | 
| Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2046 | llvm::Constant * | 
|  | 2047 | CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { | 
| Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2048 | llvm::Constant *Desc[] = { | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2049 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), | 
| Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2050 | ObjCTypes.SelectorPtrTy), | 
|  | 2051 | GetMethodVarType(MD) | 
|  | 2052 | }; | 
| Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 2053 | if (!Desc[1]) | 
|  | 2054 | return 0; | 
|  | 2055 |  | 
| Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2056 | return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy, | 
| Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2057 | Desc); | 
|  | 2058 | } | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2059 |  | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2060 | llvm::Constant *CGObjCMac::EmitMethodDescList(Twine Name, | 
| Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2061 | const char *Section, | 
|  | 2062 | const ConstantVector &Methods) { | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2063 | // Return null for empty list. | 
|  | 2064 | if (Methods.empty()) | 
| Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2065 | return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2066 |  | 
| Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2067 | llvm::Constant *Values[2]; | 
| Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2068 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2069 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy, | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2070 | Methods.size()); | 
| Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2071 | Values[1] = llvm::ConstantArray::get(AT, Methods); | 
| Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2072 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2073 |  | 
| Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2074 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2075 | return llvm::ConstantExpr::getBitCast(GV, | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2076 | ObjCTypes.MethodDescriptionListPtrTy); | 
| Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2077 | } | 
|  | 2078 |  | 
| Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2079 | /* | 
|  | 2080 | struct _objc_category { | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2081 | char *category_name; | 
|  | 2082 | char *class_name; | 
|  | 2083 | struct _objc_method_list *instance_methods; | 
|  | 2084 | struct _objc_method_list *class_methods; | 
|  | 2085 | struct _objc_protocol_list *protocols; | 
|  | 2086 | uint32_t size; // <rdar://4585769> | 
|  | 2087 | struct _objc_property_list *instance_properties; | 
| Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2088 | }; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2089 | */ | 
| Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2090 | void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { | 
| Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2091 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy); | 
| Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2092 |  | 
| Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2093 | // FIXME: This is poor design, the OCD should have a pointer to the category | 
|  | 2094 | // decl. Additionally, note that Category can be null for the @implementation | 
|  | 2095 | // w/o an @interface case. Sema should just create one for us as it does for | 
|  | 2096 | // @implementation so everyone else can live life under a clear blue sky. | 
| Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2097 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2098 | const ObjCCategoryDecl *Category = | 
| Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2099 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2100 |  | 
|  | 2101 | llvm::SmallString<256> ExtName; | 
|  | 2102 | llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_' | 
|  | 2103 | << OCD->getName(); | 
| Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2104 |  | 
| Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2105 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2106 | for (ObjCCategoryImplDecl::instmeth_iterator | 
| Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2107 | i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) { | 
| Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2108 | // Instance methods should always be defined. | 
|  | 2109 | InstanceMethods.push_back(GetMethodConstant(*i)); | 
|  | 2110 | } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2111 | for (ObjCCategoryImplDecl::classmeth_iterator | 
| Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2112 | i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) { | 
| Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2113 | // Class methods should always be defined. | 
|  | 2114 | ClassMethods.push_back(GetMethodConstant(*i)); | 
|  | 2115 | } | 
|  | 2116 |  | 
| Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2117 | llvm::Constant *Values[7]; | 
| Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2118 | Values[0] = GetClassName(OCD->getIdentifier()); | 
|  | 2119 | Values[1] = GetClassName(Interface->getIdentifier()); | 
| Fariborz Jahanian | 679cd7f | 2009-04-29 20:40:05 +0000 | [diff] [blame] | 2120 | LazySymbols.insert(Interface->getIdentifier()); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2121 | Values[2] = | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2122 | EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(), | 
| Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2123 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", | 
| Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2124 | InstanceMethods); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2125 | Values[3] = | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2126 | EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(), | 
| Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2127 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", | 
| Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2128 | ClassMethods); | 
| Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2129 | if (Category) { | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2130 | Values[4] = | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2131 | EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(), | 
| Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2132 | Category->protocol_begin(), | 
|  | 2133 | Category->protocol_end()); | 
|  | 2134 | } else { | 
| Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2135 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); | 
| Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2136 | } | 
| Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2137 | Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); | 
| Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2138 |  | 
|  | 2139 | // If there is no category @interface then there can be no properties. | 
|  | 2140 | if (Category) { | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2141 | Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(), | 
| Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 2142 | OCD, Category, ObjCTypes); | 
| Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2143 | } else { | 
| Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2144 | Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); | 
| Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 2145 | } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2146 |  | 
| Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2147 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy, | 
| Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2148 | Values); | 
|  | 2149 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2150 | llvm::GlobalVariable *GV = | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2151 | CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init, | 
| Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2152 | "__OBJC,__category,regular,no_dead_strip", | 
| Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2153 | 4, true); | 
| Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2154 | DefinedCategories.push_back(GV); | 
| Fariborz Jahanian | b9c5b3d | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 2155 | DefinedCategoryNames.insert(ExtName.str()); | 
| Fariborz Jahanian | 64089ce | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 2156 | // method definition entries must be clear for next implementation. | 
|  | 2157 | MethodDefinitions.clear(); | 
| Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2158 | } | 
|  | 2159 |  | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2160 | // FIXME: Get from somewhere? | 
|  | 2161 | enum ClassFlags { | 
|  | 2162 | eClassFlags_Factory              = 0x00001, | 
|  | 2163 | eClassFlags_Meta                 = 0x00002, | 
|  | 2164 | // <rdr://5142207> | 
|  | 2165 | eClassFlags_HasCXXStructors      = 0x02000, | 
|  | 2166 | eClassFlags_Hidden               = 0x20000, | 
|  | 2167 | eClassFlags_ABI2_Hidden          = 0x00010, | 
|  | 2168 | eClassFlags_ABI2_HasCXXStructors = 0x00004   // <rdr://4923634> | 
|  | 2169 | }; | 
|  | 2170 |  | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2171 | /* | 
|  | 2172 | struct _objc_class { | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2173 | Class isa; | 
|  | 2174 | Class super_class; | 
|  | 2175 | const char *name; | 
|  | 2176 | long version; | 
|  | 2177 | long info; | 
|  | 2178 | long instance_size; | 
|  | 2179 | struct _objc_ivar_list *ivars; | 
|  | 2180 | struct _objc_method_list *methods; | 
|  | 2181 | struct _objc_cache *cache; | 
|  | 2182 | struct _objc_protocol_list *protocols; | 
|  | 2183 | // Objective-C 1.0 extensions (<rdr://4585769>) | 
|  | 2184 | const char *ivar_layout; | 
|  | 2185 | struct _objc_class_ext *ext; | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2186 | }; | 
|  | 2187 |  | 
|  | 2188 | See EmitClassExtension(); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2189 | */ | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2190 | void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) { | 
| Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2191 | DefinedSymbols.insert(ID->getIdentifier()); | 
|  | 2192 |  | 
| Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2193 | std::string ClassName = ID->getNameAsString(); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2194 | // FIXME: Gross | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2195 | ObjCInterfaceDecl *Interface = | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2196 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2197 | llvm::Constant *Protocols = | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2198 | EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(), | 
| Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 2199 | Interface->all_referenced_protocol_begin(), | 
|  | 2200 | Interface->all_referenced_protocol_end()); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2201 | unsigned Flags = eClassFlags_Factory; | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2202 | if (ID->hasCXXStructors()) | 
| Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 2203 | Flags |= eClassFlags_HasCXXStructors; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2204 | unsigned Size = | 
| Ken Dyck | 5f022d8 | 2011-02-09 01:59:34 +0000 | [diff] [blame] | 2205 | CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity(); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2206 |  | 
|  | 2207 | // FIXME: Set CXX-structors flag. | 
| John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 2208 | if (ID->getClassInterface()->getVisibility() == HiddenVisibility) | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2209 | Flags |= eClassFlags_Hidden; | 
|  | 2210 |  | 
| Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2211 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2212 | for (ObjCImplementationDecl::instmeth_iterator | 
| Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2213 | i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) { | 
| Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2214 | // Instance methods should always be defined. | 
|  | 2215 | InstanceMethods.push_back(GetMethodConstant(*i)); | 
|  | 2216 | } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2217 | for (ObjCImplementationDecl::classmeth_iterator | 
| Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2218 | i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) { | 
| Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2219 | // Class methods should always be defined. | 
|  | 2220 | ClassMethods.push_back(GetMethodConstant(*i)); | 
|  | 2221 | } | 
|  | 2222 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2223 | for (ObjCImplementationDecl::propimpl_iterator | 
| Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2224 | i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) { | 
| Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2225 | ObjCPropertyImplDecl *PID = *i; | 
|  | 2226 |  | 
|  | 2227 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { | 
|  | 2228 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); | 
|  | 2229 |  | 
|  | 2230 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) | 
|  | 2231 | if (llvm::Constant *C = GetMethodConstant(MD)) | 
|  | 2232 | InstanceMethods.push_back(C); | 
|  | 2233 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) | 
|  | 2234 | if (llvm::Constant *C = GetMethodConstant(MD)) | 
|  | 2235 | InstanceMethods.push_back(C); | 
|  | 2236 | } | 
|  | 2237 | } | 
|  | 2238 |  | 
| Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2239 | llvm::Constant *Values[12]; | 
| Daniel Dunbar | 5384b09 | 2009-05-03 08:56:52 +0000 | [diff] [blame] | 2240 | Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2241 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) { | 
| Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2242 | // Record a reference to the super class. | 
|  | 2243 | LazySymbols.insert(Super->getIdentifier()); | 
|  | 2244 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2245 | Values[ 1] = | 
| Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2246 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2247 | ObjCTypes.ClassPtrTy); | 
|  | 2248 | } else { | 
| Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2249 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2250 | } | 
|  | 2251 | Values[ 2] = GetClassName(ID->getIdentifier()); | 
|  | 2252 | // Version is always 0. | 
| Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2253 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); | 
|  | 2254 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); | 
|  | 2255 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); | 
| Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2256 | Values[ 6] = EmitIvarList(ID, false); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2257 | Values[ 7] = | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2258 | EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(), | 
| Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2259 | "__OBJC,__inst_meth,regular,no_dead_strip", | 
| Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2260 | InstanceMethods); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2261 | // cache is always NULL. | 
| Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2262 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2263 | Values[ 9] = Protocols; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2264 | Values[10] = BuildIvarLayout(ID, true); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2265 | Values[11] = EmitClassExtension(ID); | 
| Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2266 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2267 | Values); | 
| Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 2268 | std::string Name("\01L_OBJC_CLASS_"); | 
|  | 2269 | Name += ClassName; | 
|  | 2270 | const char *Section = "__OBJC,__class,regular,no_dead_strip"; | 
|  | 2271 | // Check for a forward reference. | 
|  | 2272 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); | 
|  | 2273 | if (GV) { | 
|  | 2274 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && | 
|  | 2275 | "Forward metaclass reference has incorrect type."); | 
|  | 2276 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); | 
|  | 2277 | GV->setInitializer(Init); | 
|  | 2278 | GV->setSection(Section); | 
|  | 2279 | GV->setAlignment(4); | 
|  | 2280 | CGM.AddUsedGlobal(GV); | 
|  | 2281 | } | 
|  | 2282 | else | 
|  | 2283 | GV = CreateMetadataVar(Name, Init, Section, 4, true); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2284 | DefinedClasses.push_back(GV); | 
| Fariborz Jahanian | 64089ce | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 2285 | // method definition entries must be clear for next implementation. | 
|  | 2286 | MethodDefinitions.clear(); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2287 | } | 
|  | 2288 |  | 
|  | 2289 | llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID, | 
|  | 2290 | llvm::Constant *Protocols, | 
| Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2291 | const ConstantVector &Methods) { | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2292 | unsigned Flags = eClassFlags_Meta; | 
| Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2293 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2294 |  | 
| John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 2295 | if (ID->getClassInterface()->getVisibility() == HiddenVisibility) | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2296 | Flags |= eClassFlags_Hidden; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2297 |  | 
| Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2298 | llvm::Constant *Values[12]; | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2299 | // The isa for the metaclass is the root of the hierarchy. | 
|  | 2300 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); | 
|  | 2301 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) | 
|  | 2302 | Root = Super; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2303 | Values[ 0] = | 
| Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2304 | llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()), | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2305 | ObjCTypes.ClassPtrTy); | 
| Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2306 | // The super class for the metaclass is emitted as the name of the | 
|  | 2307 | // super class. The runtime fixes this up to point to the | 
|  | 2308 | // *metaclass* for the super class. | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2309 | if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) { | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2310 | Values[ 1] = | 
| Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2311 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2312 | ObjCTypes.ClassPtrTy); | 
|  | 2313 | } else { | 
| Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2314 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2315 | } | 
|  | 2316 | Values[ 2] = GetClassName(ID->getIdentifier()); | 
|  | 2317 | // Version is always 0. | 
| Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2318 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); | 
|  | 2319 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); | 
|  | 2320 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); | 
| Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2321 | Values[ 6] = EmitIvarList(ID, true); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2322 | Values[ 7] = | 
| Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2323 | EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(), | 
| Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2324 | "__OBJC,__cls_meth,regular,no_dead_strip", | 
| Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2325 | Methods); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2326 | // cache is always NULL. | 
| Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2327 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2328 | Values[ 9] = Protocols; | 
|  | 2329 | // ivar_layout for metaclass is always NULL. | 
| Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2330 | Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2331 | // The class extension is always unused for metaclasses. | 
| Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2332 | Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); | 
| Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2333 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2334 | Values); | 
|  | 2335 |  | 
| Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2336 | std::string Name("\01L_OBJC_METACLASS_"); | 
| Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2337 | Name += ID->getNameAsCString(); | 
| Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2338 |  | 
|  | 2339 | // Check for a forward reference. | 
|  | 2340 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); | 
|  | 2341 | if (GV) { | 
|  | 2342 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && | 
|  | 2343 | "Forward metaclass reference has incorrect type."); | 
|  | 2344 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); | 
|  | 2345 | GV->setInitializer(Init); | 
|  | 2346 | } else { | 
| Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2347 | GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, | 
| Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2348 | llvm::GlobalValue::InternalLinkage, | 
| Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2349 | Init, Name); | 
| Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2350 | } | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2351 | GV->setSection("__OBJC,__meta_class,regular,no_dead_strip"); | 
| Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2352 | GV->setAlignment(4); | 
| Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2353 | CGM.AddUsedGlobal(GV); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2354 |  | 
|  | 2355 | return GV; | 
|  | 2356 | } | 
|  | 2357 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2358 | llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) { | 
| Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2359 | std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString(); | 
| Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2360 |  | 
| Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2361 | // FIXME: Should we look these up somewhere other than the module. Its a bit | 
|  | 2362 | // silly since we only generate these while processing an implementation, so | 
|  | 2363 | // exactly one pointer would work if know when we entered/exitted an | 
|  | 2364 | // implementation block. | 
| Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2365 |  | 
|  | 2366 | // Check for an existing forward reference. | 
| Fariborz Jahanian | b0d2794 | 2009-01-07 20:11:22 +0000 | [diff] [blame] | 2367 | // Previously, metaclass with internal linkage may have been defined. | 
|  | 2368 | // pass 'true' as 2nd argument so it is returned. | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2369 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, | 
|  | 2370 | true)) { | 
| Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2371 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && | 
|  | 2372 | "Forward metaclass reference has incorrect type."); | 
|  | 2373 | return GV; | 
|  | 2374 | } else { | 
|  | 2375 | // Generate as an external reference to keep a consistent | 
|  | 2376 | // module. This will be patched up when we emit the metaclass. | 
| Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2377 | return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, | 
| Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2378 | llvm::GlobalValue::ExternalLinkage, | 
|  | 2379 | 0, | 
| Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2380 | Name); | 
| Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2381 | } | 
|  | 2382 | } | 
|  | 2383 |  | 
| Fariborz Jahanian | b0069ee | 2009-11-12 20:14:24 +0000 | [diff] [blame] | 2384 | llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) { | 
|  | 2385 | std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString(); | 
|  | 2386 |  | 
|  | 2387 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, | 
|  | 2388 | true)) { | 
|  | 2389 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && | 
|  | 2390 | "Forward class metadata reference has incorrect type."); | 
|  | 2391 | return GV; | 
|  | 2392 | } else { | 
|  | 2393 | return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, | 
|  | 2394 | llvm::GlobalValue::ExternalLinkage, | 
|  | 2395 | 0, | 
|  | 2396 | Name); | 
|  | 2397 | } | 
|  | 2398 | } | 
|  | 2399 |  | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2400 | /* | 
|  | 2401 | struct objc_class_ext { | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2402 | uint32_t size; | 
|  | 2403 | const char *weak_ivar_layout; | 
|  | 2404 | struct _objc_property_list *properties; | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2405 | }; | 
|  | 2406 | */ | 
|  | 2407 | llvm::Constant * | 
|  | 2408 | CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) { | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2409 | uint64_t Size = | 
| Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2410 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2411 |  | 
| Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2412 | llvm::Constant *Values[3]; | 
| Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2413 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); | 
| Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 2414 | Values[1] = BuildIvarLayout(ID, false); | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2415 | Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(), | 
| Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 2416 | ID, ID->getClassInterface(), ObjCTypes); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2417 |  | 
|  | 2418 | // Return null if no extension bits are used. | 
|  | 2419 | if (Values[1]->isNullValue() && Values[2]->isNullValue()) | 
| Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2420 | return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2421 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2422 | llvm::Constant *Init = | 
| Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2423 | llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values); | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2424 | return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(), | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2425 | Init, "__OBJC,__class_ext,regular,no_dead_strip", | 
| Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2426 | 4, true); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2427 | } | 
|  | 2428 |  | 
|  | 2429 | /* | 
|  | 2430 | struct objc_ivar { | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2431 | char *ivar_name; | 
|  | 2432 | char *ivar_type; | 
|  | 2433 | int ivar_offset; | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2434 | }; | 
|  | 2435 |  | 
|  | 2436 | struct objc_ivar_list { | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2437 | int ivar_count; | 
|  | 2438 | struct objc_ivar list[count]; | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2439 | }; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2440 | */ | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2441 | llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID, | 
| Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2442 | bool ForClass) { | 
| Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2443 | std::vector<llvm::Constant*> Ivars; | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2444 |  | 
|  | 2445 | // When emitting the root class GCC emits ivar entries for the | 
|  | 2446 | // actual class structure. It is not clear if we need to follow this | 
|  | 2447 | // behavior; for now lets try and get away with not doing it. If so, | 
|  | 2448 | // the cleanest solution would be to make up an ObjCInterfaceDecl | 
|  | 2449 | // for the class. | 
|  | 2450 | if (ForClass) | 
| Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2451 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2452 |  | 
| Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 2453 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2454 |  | 
| Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 2455 | for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin(); | 
| Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 2456 | IVD; IVD = IVD->getNextIvar()) { | 
| Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 2457 | // Ignore unnamed bit-fields. | 
|  | 2458 | if (!IVD->getDeclName()) | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2459 | continue; | 
| Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2460 | llvm::Constant *Ivar[] = { | 
|  | 2461 | GetMethodVarName(IVD->getIdentifier()), | 
|  | 2462 | GetMethodVarType(IVD), | 
|  | 2463 | llvm::ConstantInt::get(ObjCTypes.IntTy, | 
|  | 2464 | ComputeIvarBaseOffset(CGM, OID, IVD)) | 
|  | 2465 | }; | 
| Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2466 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar)); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2467 | } | 
|  | 2468 |  | 
|  | 2469 | // Return null for empty list. | 
|  | 2470 | if (Ivars.empty()) | 
| Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2471 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2472 |  | 
| Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2473 | llvm::Constant *Values[2]; | 
| Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2474 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2475 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy, | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2476 | Ivars.size()); | 
| Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2477 | Values[1] = llvm::ConstantArray::get(AT, Ivars); | 
| Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2478 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2479 |  | 
| Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2480 | llvm::GlobalVariable *GV; | 
|  | 2481 | if (ForClass) | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2482 | GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(), | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2483 | Init, "__OBJC,__class_vars,regular,no_dead_strip", | 
| Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2484 | 4, true); | 
| Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2485 | else | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 2486 | GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(), | 
| Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2487 | Init, "__OBJC,__instance_vars,regular,no_dead_strip", | 
| Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2488 | 4, true); | 
| Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2489 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2490 | } | 
|  | 2491 |  | 
|  | 2492 | /* | 
|  | 2493 | struct objc_method { | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2494 | SEL method_name; | 
|  | 2495 | char *method_types; | 
|  | 2496 | void *method; | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2497 | }; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2498 |  | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2499 | struct objc_method_list { | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2500 | struct objc_method_list *obsolete; | 
|  | 2501 | int count; | 
|  | 2502 | struct objc_method methods_list[count]; | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2503 | }; | 
|  | 2504 | */ | 
| Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2505 |  | 
|  | 2506 | /// GetMethodConstant - Return a struct objc_method constant for the | 
|  | 2507 | /// given method if it has been defined. The result is null if the | 
|  | 2508 | /// method has not been defined. The return value has type MethodPtrTy. | 
| Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2509 | llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) { | 
| Argyrios Kyrtzidis | 9d50c06 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 2510 | llvm::Function *Fn = GetMethodDefinition(MD); | 
| Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2511 | if (!Fn) | 
|  | 2512 | return 0; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2513 |  | 
| Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2514 | llvm::Constant *Method[] = { | 
| Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2515 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), | 
| Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 2516 | ObjCTypes.SelectorPtrTy), | 
|  | 2517 | GetMethodVarType(MD), | 
|  | 2518 | llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy) | 
|  | 2519 | }; | 
| Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2520 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); | 
| Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2521 | } | 
|  | 2522 |  | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2523 | llvm::Constant *CGObjCMac::EmitMethodList(Twine Name, | 
| Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2524 | const char *Section, | 
| Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2525 | const ConstantVector &Methods) { | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2526 | // Return null for empty list. | 
|  | 2527 | if (Methods.empty()) | 
| Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2528 | return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2529 |  | 
| Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2530 | llvm::Constant *Values[3]; | 
| Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2531 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); | 
| Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2532 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2533 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2534 | Methods.size()); | 
| Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2535 | Values[2] = llvm::ConstantArray::get(AT, Methods); | 
| Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2536 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2537 |  | 
| Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2538 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); | 
| Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2539 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy); | 
| Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2540 | } | 
|  | 2541 |  | 
| Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 2542 | llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD, | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2543 | const ObjCContainerDecl *CD) { | 
| Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 2544 | llvm::SmallString<256> Name; | 
| Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 2545 | GetNameForMethod(OMD, CD, Name); | 
| Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2546 |  | 
| Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2547 | CodeGenTypes &Types = CGM.getTypes(); | 
| Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2548 | llvm::FunctionType *MethodTy = | 
| Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2549 | Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic()); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2550 | llvm::Function *Method = | 
| Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 2551 | llvm::Function::Create(MethodTy, | 
| Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2552 | llvm::GlobalValue::InternalLinkage, | 
| Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 2553 | Name.str(), | 
| Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2554 | &CGM.getModule()); | 
| Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2555 | MethodDefinitions.insert(std::make_pair(OMD, Method)); | 
| Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2556 |  | 
| Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2557 | return Method; | 
| Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2558 | } | 
|  | 2559 |  | 
| Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2560 | llvm::GlobalVariable * | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2561 | CGObjCCommonMac::CreateMetadataVar(Twine Name, | 
| Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2562 | llvm::Constant *Init, | 
|  | 2563 | const char *Section, | 
| Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2564 | unsigned Align, | 
|  | 2565 | bool AddToUsed) { | 
| Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2566 | llvm::Type *Ty = Init->getType(); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2567 | llvm::GlobalVariable *GV = | 
| Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2568 | new llvm::GlobalVariable(CGM.getModule(), Ty, false, | 
| Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2569 | llvm::GlobalValue::InternalLinkage, Init, Name); | 
| Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2570 | if (Section) | 
|  | 2571 | GV->setSection(Section); | 
| Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2572 | if (Align) | 
|  | 2573 | GV->setAlignment(Align); | 
|  | 2574 | if (AddToUsed) | 
| Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2575 | CGM.AddUsedGlobal(GV); | 
| Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2576 | return GV; | 
|  | 2577 | } | 
|  | 2578 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2579 | llvm::Function *CGObjCMac::ModuleInitFunction() { | 
| Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2580 | // Abuse this interface function as a place to finalize. | 
|  | 2581 | FinishModule(); | 
| Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2582 | return NULL; | 
|  | 2583 | } | 
|  | 2584 |  | 
| Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2585 | llvm::Constant *CGObjCMac::GetPropertyGetFunction() { | 
| Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2586 | return ObjCTypes.getGetPropertyFn(); | 
| Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2587 | } | 
|  | 2588 |  | 
| Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2589 | llvm::Constant *CGObjCMac::GetPropertySetFunction() { | 
| Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2590 | return ObjCTypes.getSetPropertyFn(); | 
| Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2591 | } | 
|  | 2592 |  | 
| David Chisnall | 8fac25d | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 2593 | llvm::Constant *CGObjCMac::GetGetStructFunction() { | 
|  | 2594 | return ObjCTypes.getCopyStructFn(); | 
|  | 2595 | } | 
|  | 2596 | llvm::Constant *CGObjCMac::GetSetStructFunction() { | 
| Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 2597 | return ObjCTypes.getCopyStructFn(); | 
|  | 2598 | } | 
|  | 2599 |  | 
| Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2600 | llvm::Constant *CGObjCMac::EnumerationMutationFunction() { | 
| Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2601 | return ObjCTypes.getEnumerationMutationFn(); | 
| Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 2602 | } | 
|  | 2603 |  | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2604 | void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) { | 
|  | 2605 | return EmitTryOrSynchronizedStmt(CGF, S); | 
|  | 2606 | } | 
|  | 2607 |  | 
|  | 2608 | void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF, | 
|  | 2609 | const ObjCAtSynchronizedStmt &S) { | 
|  | 2610 | return EmitTryOrSynchronizedStmt(CGF, S); | 
|  | 2611 | } | 
|  | 2612 |  | 
| John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2613 | namespace { | 
| John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 2614 | struct PerformFragileFinally : EHScopeStack::Cleanup { | 
| John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2615 | const Stmt &S; | 
| John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2616 | llvm::Value *SyncArgSlot; | 
| John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2617 | llvm::Value *CallTryExitVar; | 
|  | 2618 | llvm::Value *ExceptionData; | 
|  | 2619 | ObjCTypesHelper &ObjCTypes; | 
|  | 2620 | PerformFragileFinally(const Stmt *S, | 
| John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2621 | llvm::Value *SyncArgSlot, | 
| John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2622 | llvm::Value *CallTryExitVar, | 
|  | 2623 | llvm::Value *ExceptionData, | 
|  | 2624 | ObjCTypesHelper *ObjCTypes) | 
| John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2625 | : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar), | 
| John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2626 | ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {} | 
|  | 2627 |  | 
| John McCall | ad346f4 | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 2628 | void Emit(CodeGenFunction &CGF, Flags flags) { | 
| John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2629 | // Check whether we need to call objc_exception_try_exit. | 
|  | 2630 | // In optimized code, this branch will always be folded. | 
|  | 2631 | llvm::BasicBlock *FinallyCallExit = | 
|  | 2632 | CGF.createBasicBlock("finally.call_exit"); | 
|  | 2633 | llvm::BasicBlock *FinallyNoCallExit = | 
|  | 2634 | CGF.createBasicBlock("finally.no_call_exit"); | 
|  | 2635 | CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar), | 
|  | 2636 | FinallyCallExit, FinallyNoCallExit); | 
|  | 2637 |  | 
|  | 2638 | CGF.EmitBlock(FinallyCallExit); | 
|  | 2639 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData) | 
|  | 2640 | ->setDoesNotThrow(); | 
|  | 2641 |  | 
|  | 2642 | CGF.EmitBlock(FinallyNoCallExit); | 
|  | 2643 |  | 
|  | 2644 | if (isa<ObjCAtTryStmt>(S)) { | 
|  | 2645 | if (const ObjCAtFinallyStmt* FinallyStmt = | 
| John McCall | d96a8e7 | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 2646 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) { | 
|  | 2647 | // Save the current cleanup destination in case there's | 
|  | 2648 | // control flow inside the finally statement. | 
|  | 2649 | llvm::Value *CurCleanupDest = | 
|  | 2650 | CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot()); | 
|  | 2651 |  | 
| John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2652 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); | 
|  | 2653 |  | 
| John McCall | d96a8e7 | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 2654 | if (CGF.HaveInsertPoint()) { | 
|  | 2655 | CGF.Builder.CreateStore(CurCleanupDest, | 
|  | 2656 | CGF.getNormalCleanupDestSlot()); | 
|  | 2657 | } else { | 
|  | 2658 | // Currently, the end of the cleanup must always exist. | 
|  | 2659 | CGF.EnsureInsertPoint(); | 
|  | 2660 | } | 
|  | 2661 | } | 
| John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2662 | } else { | 
|  | 2663 | // Emit objc_sync_exit(expr); as finally's sole statement for | 
|  | 2664 | // @synchronized. | 
| John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2665 | llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot); | 
| John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2666 | CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg) | 
|  | 2667 | ->setDoesNotThrow(); | 
|  | 2668 | } | 
|  | 2669 | } | 
|  | 2670 | }; | 
| John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2671 |  | 
|  | 2672 | class FragileHazards { | 
|  | 2673 | CodeGenFunction &CGF; | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2674 | SmallVector<llvm::Value*, 20> Locals; | 
| John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2675 | llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry; | 
|  | 2676 |  | 
|  | 2677 | llvm::InlineAsm *ReadHazard; | 
|  | 2678 | llvm::InlineAsm *WriteHazard; | 
|  | 2679 |  | 
|  | 2680 | llvm::FunctionType *GetAsmFnType(); | 
|  | 2681 |  | 
|  | 2682 | void collectLocals(); | 
|  | 2683 | void emitReadHazard(CGBuilderTy &Builder); | 
|  | 2684 |  | 
|  | 2685 | public: | 
|  | 2686 | FragileHazards(CodeGenFunction &CGF); | 
| John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2687 |  | 
| John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2688 | void emitWriteHazard(); | 
| John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2689 | void emitHazardsInNewBlocks(); | 
| John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2690 | }; | 
|  | 2691 | } | 
|  | 2692 |  | 
|  | 2693 | /// Create the fragile-ABI read and write hazards based on the current | 
|  | 2694 | /// state of the function, which is presumed to be immediately prior | 
|  | 2695 | /// to a @try block.  These hazards are used to maintain correct | 
|  | 2696 | /// semantics in the face of optimization and the fragile ABI's | 
|  | 2697 | /// cavalier use of setjmp/longjmp. | 
|  | 2698 | FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) { | 
|  | 2699 | collectLocals(); | 
|  | 2700 |  | 
|  | 2701 | if (Locals.empty()) return; | 
|  | 2702 |  | 
|  | 2703 | // Collect all the blocks in the function. | 
|  | 2704 | for (llvm::Function::iterator | 
|  | 2705 | I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I) | 
|  | 2706 | BlocksBeforeTry.insert(&*I); | 
|  | 2707 |  | 
|  | 2708 | llvm::FunctionType *AsmFnTy = GetAsmFnType(); | 
|  | 2709 |  | 
|  | 2710 | // Create a read hazard for the allocas.  This inhibits dead-store | 
|  | 2711 | // optimizations and forces the values to memory.  This hazard is | 
|  | 2712 | // inserted before any 'throwing' calls in the protected scope to | 
|  | 2713 | // reflect the possibility that the variables might be read from the | 
|  | 2714 | // catch block if the call throws. | 
|  | 2715 | { | 
|  | 2716 | std::string Constraint; | 
|  | 2717 | for (unsigned I = 0, E = Locals.size(); I != E; ++I) { | 
|  | 2718 | if (I) Constraint += ','; | 
|  | 2719 | Constraint += "*m"; | 
|  | 2720 | } | 
|  | 2721 |  | 
|  | 2722 | ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false); | 
|  | 2723 | } | 
|  | 2724 |  | 
|  | 2725 | // Create a write hazard for the allocas.  This inhibits folding | 
|  | 2726 | // loads across the hazard.  This hazard is inserted at the | 
|  | 2727 | // beginning of the catch path to reflect the possibility that the | 
|  | 2728 | // variables might have been written within the protected scope. | 
|  | 2729 | { | 
|  | 2730 | std::string Constraint; | 
|  | 2731 | for (unsigned I = 0, E = Locals.size(); I != E; ++I) { | 
|  | 2732 | if (I) Constraint += ','; | 
|  | 2733 | Constraint += "=*m"; | 
|  | 2734 | } | 
|  | 2735 |  | 
|  | 2736 | WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false); | 
|  | 2737 | } | 
|  | 2738 | } | 
|  | 2739 |  | 
|  | 2740 | /// Emit a write hazard at the current location. | 
|  | 2741 | void FragileHazards::emitWriteHazard() { | 
|  | 2742 | if (Locals.empty()) return; | 
|  | 2743 |  | 
| Jay Foad | 4c7d9f1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 2744 | CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow(); | 
| John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2745 | } | 
|  | 2746 |  | 
| John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2747 | void FragileHazards::emitReadHazard(CGBuilderTy &Builder) { | 
|  | 2748 | assert(!Locals.empty()); | 
| Jay Foad | 4c7d9f1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 2749 | Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow(); | 
| John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2750 | } | 
|  | 2751 |  | 
|  | 2752 | /// Emit read hazards in all the protected blocks, i.e. all the blocks | 
|  | 2753 | /// which have been inserted since the beginning of the try. | 
| John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2754 | void FragileHazards::emitHazardsInNewBlocks() { | 
| John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2755 | if (Locals.empty()) return; | 
|  | 2756 |  | 
|  | 2757 | CGBuilderTy Builder(CGF.getLLVMContext()); | 
|  | 2758 |  | 
|  | 2759 | // Iterate through all blocks, skipping those prior to the try. | 
|  | 2760 | for (llvm::Function::iterator | 
|  | 2761 | FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) { | 
|  | 2762 | llvm::BasicBlock &BB = *FI; | 
|  | 2763 | if (BlocksBeforeTry.count(&BB)) continue; | 
|  | 2764 |  | 
|  | 2765 | // Walk through all the calls in the block. | 
|  | 2766 | for (llvm::BasicBlock::iterator | 
|  | 2767 | BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) { | 
|  | 2768 | llvm::Instruction &I = *BI; | 
|  | 2769 |  | 
|  | 2770 | // Ignore instructions that aren't non-intrinsic calls. | 
|  | 2771 | // These are the only calls that can possibly call longjmp. | 
|  | 2772 | if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue; | 
|  | 2773 | if (isa<llvm::IntrinsicInst>(I)) | 
|  | 2774 | continue; | 
|  | 2775 |  | 
|  | 2776 | // Ignore call sites marked nounwind.  This may be questionable, | 
|  | 2777 | // since 'nounwind' doesn't necessarily mean 'does not call longjmp'. | 
|  | 2778 | llvm::CallSite CS(&I); | 
|  | 2779 | if (CS.doesNotThrow()) continue; | 
|  | 2780 |  | 
| John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2781 | // Insert a read hazard before the call.  This will ensure that | 
|  | 2782 | // any writes to the locals are performed before making the | 
|  | 2783 | // call.  If the call throws, then this is sufficient to | 
|  | 2784 | // guarantee correctness as long as it doesn't also write to any | 
|  | 2785 | // locals. | 
| John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2786 | Builder.SetInsertPoint(&BB, BI); | 
|  | 2787 | emitReadHazard(Builder); | 
|  | 2788 | } | 
|  | 2789 | } | 
|  | 2790 | } | 
|  | 2791 |  | 
|  | 2792 | static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) { | 
|  | 2793 | if (V) S.insert(V); | 
|  | 2794 | } | 
|  | 2795 |  | 
|  | 2796 | void FragileHazards::collectLocals() { | 
|  | 2797 | // Compute a set of allocas to ignore. | 
|  | 2798 | llvm::DenseSet<llvm::Value*> AllocasToIgnore; | 
|  | 2799 | addIfPresent(AllocasToIgnore, CGF.ReturnValue); | 
|  | 2800 | addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest); | 
| John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2801 |  | 
|  | 2802 | // Collect all the allocas currently in the function.  This is | 
|  | 2803 | // probably way too aggressive. | 
|  | 2804 | llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock(); | 
|  | 2805 | for (llvm::BasicBlock::iterator | 
|  | 2806 | I = Entry.begin(), E = Entry.end(); I != E; ++I) | 
|  | 2807 | if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I)) | 
|  | 2808 | Locals.push_back(&*I); | 
|  | 2809 | } | 
|  | 2810 |  | 
|  | 2811 | llvm::FunctionType *FragileHazards::GetAsmFnType() { | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2812 | SmallVector<llvm::Type *, 16> tys(Locals.size()); | 
| John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 2813 | for (unsigned i = 0, e = Locals.size(); i != e; ++i) | 
|  | 2814 | tys[i] = Locals[i]->getType(); | 
|  | 2815 | return llvm::FunctionType::get(CGF.VoidTy, tys, false); | 
| John McCall | cc50529 | 2010-07-21 06:59:36 +0000 | [diff] [blame] | 2816 | } | 
|  | 2817 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2818 | /* | 
| Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2819 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2820 | Objective-C setjmp-longjmp (sjlj) Exception Handling | 
|  | 2821 | -- | 
| Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2822 |  | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2823 | A catch buffer is a setjmp buffer plus: | 
|  | 2824 | - a pointer to the exception that was caught | 
|  | 2825 | - a pointer to the previous exception data buffer | 
|  | 2826 | - two pointers of reserved storage | 
|  | 2827 | Therefore catch buffers form a stack, with a pointer to the top | 
|  | 2828 | of the stack kept in thread-local storage. | 
|  | 2829 |  | 
|  | 2830 | objc_exception_try_enter pushes a catch buffer onto the EH stack. | 
|  | 2831 | objc_exception_try_exit pops the given catch buffer, which is | 
|  | 2832 | required to be the top of the EH stack. | 
|  | 2833 | objc_exception_throw pops the top of the EH stack, writes the | 
|  | 2834 | thrown exception into the appropriate field, and longjmps | 
|  | 2835 | to the setjmp buffer.  It crashes the process (with a printf | 
|  | 2836 | and an abort()) if there are no catch buffers on the stack. | 
|  | 2837 | objc_exception_extract just reads the exception pointer out of the | 
|  | 2838 | catch buffer. | 
|  | 2839 |  | 
|  | 2840 | There's no reason an implementation couldn't use a light-weight | 
|  | 2841 | setjmp here --- something like __builtin_setjmp, but API-compatible | 
|  | 2842 | with the heavyweight setjmp.  This will be more important if we ever | 
|  | 2843 | want to implement correct ObjC/C++ exception interactions for the | 
|  | 2844 | fragile ABI. | 
|  | 2845 |  | 
|  | 2846 | Note that for this use of setjmp/longjmp to be correct, we may need | 
|  | 2847 | to mark some local variables volatile: if a non-volatile local | 
|  | 2848 | variable is modified between the setjmp and the longjmp, it has | 
|  | 2849 | indeterminate value.  For the purposes of LLVM IR, it may be | 
|  | 2850 | sufficient to make loads and stores within the @try (to variables | 
|  | 2851 | declared outside the @try) volatile.  This is necessary for | 
|  | 2852 | optimized correctness, but is not currently being done; this is | 
|  | 2853 | being tracked as rdar://problem/8160285 | 
|  | 2854 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2855 | The basic framework for a @try-catch-finally is as follows: | 
|  | 2856 | { | 
| Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2857 | objc_exception_data d; | 
|  | 2858 | id _rethrow = null; | 
| Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2859 | bool _call_try_exit = true; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2860 |  | 
| Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2861 | objc_exception_try_enter(&d); | 
|  | 2862 | if (!setjmp(d.jmp_buf)) { | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2863 | ... try body ... | 
| Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2864 | } else { | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2865 | // exception path | 
|  | 2866 | id _caught = objc_exception_extract(&d); | 
|  | 2867 |  | 
|  | 2868 | // enter new try scope for handlers | 
|  | 2869 | if (!setjmp(d.jmp_buf)) { | 
|  | 2870 | ... match exception and execute catch blocks ... | 
|  | 2871 |  | 
|  | 2872 | // fell off end, rethrow. | 
|  | 2873 | _rethrow = _caught; | 
|  | 2874 | ... jump-through-finally to finally_rethrow ... | 
|  | 2875 | } else { | 
|  | 2876 | // exception in catch block | 
|  | 2877 | _rethrow = objc_exception_extract(&d); | 
|  | 2878 | _call_try_exit = false; | 
|  | 2879 | ... jump-through-finally to finally_rethrow ... | 
|  | 2880 | } | 
| Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2881 | } | 
| Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2882 | ... jump-through-finally to finally_end ... | 
| Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2883 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2884 | finally: | 
| Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2885 | if (_call_try_exit) | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2886 | objc_exception_try_exit(&d); | 
| Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2887 |  | 
| Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2888 | ... finally block .... | 
| Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2889 | ... dispatch to finally destination ... | 
|  | 2890 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2891 | finally_rethrow: | 
| Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2892 | objc_exception_throw(_rethrow); | 
|  | 2893 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2894 | finally_end: | 
|  | 2895 | } | 
| Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2896 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2897 | This framework differs slightly from the one gcc uses, in that gcc | 
|  | 2898 | uses _rethrow to determine if objc_exception_try_exit should be called | 
|  | 2899 | and if the object should be rethrown. This breaks in the face of | 
|  | 2900 | throwing nil and introduces unnecessary branches. | 
| Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2901 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2902 | We specialize this framework for a few particular circumstances: | 
| Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2903 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2904 | - If there are no catch blocks, then we avoid emitting the second | 
|  | 2905 | exception handling context. | 
| Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2906 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2907 | - If there is a catch-all catch block (i.e. @catch(...) or @catch(id | 
|  | 2908 | e)) we avoid emitting the code to rethrow an uncaught exception. | 
| Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2909 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2910 | - FIXME: If there is no @finally block we can do a few more | 
|  | 2911 | simplifications. | 
| Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2912 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2913 | Rethrows and Jumps-Through-Finally | 
|  | 2914 | -- | 
| Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2915 |  | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2916 | '@throw;' is supported by pushing the currently-caught exception | 
|  | 2917 | onto ObjCEHStack while the @catch blocks are emitted. | 
| Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2918 |  | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2919 | Branches through the @finally block are handled with an ordinary | 
|  | 2920 | normal cleanup.  We do not register an EH cleanup; fragile-ABI ObjC | 
|  | 2921 | exceptions are not compatible with C++ exceptions, and this is | 
|  | 2922 | hardly the only place where this will go wrong. | 
| Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2923 |  | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2924 | @synchronized(expr) { stmt; } is emitted as if it were: | 
|  | 2925 | id synch_value = expr; | 
|  | 2926 | objc_sync_enter(synch_value); | 
|  | 2927 | @try { stmt; } @finally { objc_sync_exit(synch_value); } | 
| Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2928 | */ | 
|  | 2929 |  | 
| Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2930 | void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, | 
|  | 2931 | const Stmt &S) { | 
|  | 2932 | bool isTry = isa<ObjCAtTryStmt>(S); | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2933 |  | 
|  | 2934 | // A destination for the fall-through edges of the catch handlers to | 
|  | 2935 | // jump to. | 
|  | 2936 | CodeGenFunction::JumpDest FinallyEnd = | 
|  | 2937 | CGF.getJumpDestInCurrentScope("finally.end"); | 
|  | 2938 |  | 
|  | 2939 | // A destination for the rethrow edge of the catch handlers to jump | 
|  | 2940 | // to. | 
|  | 2941 | CodeGenFunction::JumpDest FinallyRethrow = | 
|  | 2942 | CGF.getJumpDestInCurrentScope("finally.rethrow"); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2943 |  | 
| Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2944 | // For @synchronized, call objc_sync_enter(sync.expr). The | 
|  | 2945 | // evaluation of the expression must occur before we enter the | 
| John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2946 | // @synchronized.  We can't avoid a temp here because we need the | 
|  | 2947 | // value to be preserved.  If the backend ever does liveness | 
|  | 2948 | // correctly after setjmp, this will be unnecessary. | 
|  | 2949 | llvm::Value *SyncArgSlot = 0; | 
| Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2950 | if (!isTry) { | 
| John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2951 | llvm::Value *SyncArg = | 
| Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2952 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); | 
|  | 2953 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2954 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg) | 
|  | 2955 | ->setDoesNotThrow(); | 
| John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2956 |  | 
|  | 2957 | SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg"); | 
|  | 2958 | CGF.Builder.CreateStore(SyncArg, SyncArgSlot); | 
| Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2959 | } | 
| Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2960 |  | 
| John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2961 | // Allocate memory for the setjmp buffer.  This needs to be kept | 
|  | 2962 | // live throughout the try and catch blocks. | 
|  | 2963 | llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy, | 
|  | 2964 | "exceptiondata.ptr"); | 
|  | 2965 |  | 
| John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 2966 | // Create the fragile hazards.  Note that this will not capture any | 
|  | 2967 | // of the allocas required for exception processing, but will | 
|  | 2968 | // capture the current basic block (which extends all the way to the | 
|  | 2969 | // setjmp call) as "before the @try". | 
|  | 2970 | FragileHazards Hazards(CGF); | 
|  | 2971 |  | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2972 | // Create a flag indicating whether the cleanup needs to call | 
|  | 2973 | // objc_exception_try_exit.  This is true except when | 
|  | 2974 | //   - no catches match and we're branching through the cleanup | 
|  | 2975 | //     just to rethrow the exception, or | 
|  | 2976 | //   - a catch matched and we're falling out of the catch handler. | 
| John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2977 | // The setjmp-safety rule here is that we should always store to this | 
|  | 2978 | // variable in a place that dominates the branch through the cleanup | 
|  | 2979 | // without passing through any setjmps. | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2980 | llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(), | 
| Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2981 | "_call_try_exit"); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2982 |  | 
| John McCall | 9e2213d | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 2983 | // A slot containing the exception to rethrow.  Only needed when we | 
|  | 2984 | // have both a @catch and a @finally. | 
|  | 2985 | llvm::Value *PropagatingExnVar = 0; | 
|  | 2986 |  | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2987 | // Push a normal cleanup to leave the try scope. | 
| John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 2988 | CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S, | 
| John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 2989 | SyncArgSlot, | 
| John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 2990 | CallTryExitVar, | 
|  | 2991 | ExceptionData, | 
|  | 2992 | &ObjCTypes); | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2993 |  | 
|  | 2994 | // Enter a try block: | 
|  | 2995 | //  - Call objc_exception_try_enter to push ExceptionData on top of | 
|  | 2996 | //    the EH stack. | 
|  | 2997 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData) | 
|  | 2998 | ->setDoesNotThrow(); | 
|  | 2999 |  | 
|  | 3000 | //  - Call setjmp on the exception data buffer. | 
|  | 3001 | llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0); | 
|  | 3002 | llvm::Value *GEPIndexes[] = { Zero, Zero, Zero }; | 
|  | 3003 | llvm::Value *SetJmpBuffer = | 
| Jay Foad | 0f6ac7c | 2011-07-22 08:16:57 +0000 | [diff] [blame] | 3004 | CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer"); | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3005 | llvm::CallInst *SetJmpResult = | 
|  | 3006 | CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result"); | 
|  | 3007 | SetJmpResult->setDoesNotThrow(); | 
|  | 3008 |  | 
|  | 3009 | // If setjmp returned 0, enter the protected block; otherwise, | 
|  | 3010 | // branch to the handler. | 
| Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 3011 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); | 
|  | 3012 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3013 | llvm::Value *DidCatch = | 
| John McCall | d96a8e7 | 2010-08-11 00:16:14 +0000 | [diff] [blame] | 3014 | CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception"); | 
|  | 3015 | CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock); | 
| Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3016 |  | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3017 | // Emit the protected block. | 
| Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3018 | CGF.EmitBlock(TryBlock); | 
| John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3019 | CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3020 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3021 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); | 
| John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3022 |  | 
|  | 3023 | CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP(); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3024 |  | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3025 | // Emit the exception handler block. | 
| Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 3026 | CGF.EmitBlock(TryHandler); | 
| Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3027 |  | 
| John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3028 | // Don't optimize loads of the in-scope locals across this point. | 
|  | 3029 | Hazards.emitWriteHazard(); | 
|  | 3030 |  | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3031 | // For a @synchronized (or a @try with no catches), just branch | 
|  | 3032 | // through the cleanup to the rethrow block. | 
|  | 3033 | if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) { | 
|  | 3034 | // Tell the cleanup not to re-pop the exit. | 
| John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3035 | CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar); | 
| Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3036 | CGF.EmitBranchThroughCleanup(FinallyRethrow); | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3037 |  | 
|  | 3038 | // Otherwise, we have to match against the caught exceptions. | 
|  | 3039 | } else { | 
| John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3040 | // Retrieve the exception object.  We may emit multiple blocks but | 
|  | 3041 | // nothing can cross this so the value is already in SSA form. | 
|  | 3042 | llvm::CallInst *Caught = | 
|  | 3043 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), | 
|  | 3044 | ExceptionData, "caught"); | 
|  | 3045 | Caught->setDoesNotThrow(); | 
|  | 3046 |  | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3047 | // Push the exception to rethrow onto the EH value stack for the | 
|  | 3048 | // benefit of any @throws in the handlers. | 
|  | 3049 | CGF.ObjCEHValueStack.push_back(Caught); | 
|  | 3050 |  | 
| Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 3051 | const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3052 |  | 
| John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3053 | bool HasFinally = (AtTryStmt->getFinallyStmt() != 0); | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3054 |  | 
| John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3055 | llvm::BasicBlock *CatchBlock = 0; | 
|  | 3056 | llvm::BasicBlock *CatchHandler = 0; | 
|  | 3057 | if (HasFinally) { | 
| John McCall | 9e2213d | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3058 | // Save the currently-propagating exception before | 
|  | 3059 | // objc_exception_try_enter clears the exception slot. | 
|  | 3060 | PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(), | 
|  | 3061 | "propagating_exception"); | 
|  | 3062 | CGF.Builder.CreateStore(Caught, PropagatingExnVar); | 
|  | 3063 |  | 
| John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3064 | // Enter a new exception try block (in case a @catch block | 
|  | 3065 | // throws an exception). | 
|  | 3066 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData) | 
|  | 3067 | ->setDoesNotThrow(); | 
| Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3068 |  | 
| John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3069 | llvm::CallInst *SetJmpResult = | 
|  | 3070 | CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, | 
|  | 3071 | "setjmp.result"); | 
|  | 3072 | SetJmpResult->setDoesNotThrow(); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3073 |  | 
| John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3074 | llvm::Value *Threw = | 
|  | 3075 | CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception"); | 
|  | 3076 |  | 
|  | 3077 | CatchBlock = CGF.createBasicBlock("catch"); | 
|  | 3078 | CatchHandler = CGF.createBasicBlock("catch_for_catch"); | 
|  | 3079 | CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock); | 
|  | 3080 |  | 
|  | 3081 | CGF.EmitBlock(CatchBlock); | 
|  | 3082 | } | 
|  | 3083 |  | 
|  | 3084 | CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3085 |  | 
| Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3086 | // Handle catch list. As a special case we check if everything is | 
|  | 3087 | // matched and avoid generating code for falling off the end if | 
|  | 3088 | // so. | 
|  | 3089 | bool AllMatched = false; | 
| Douglas Gregor | 8f5e3dd | 2010-04-23 22:50:49 +0000 | [diff] [blame] | 3090 | for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) { | 
|  | 3091 | const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I); | 
| Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3092 |  | 
| Douglas Gregor | c00d8e1 | 2010-04-26 16:46:50 +0000 | [diff] [blame] | 3093 | const VarDecl *CatchParam = CatchStmt->getCatchParamDecl(); | 
| Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3094 | const ObjCObjectPointerType *OPT = 0; | 
| Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 3095 |  | 
| Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3096 | // catch(...) always matches. | 
| Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3097 | if (!CatchParam) { | 
|  | 3098 | AllMatched = true; | 
|  | 3099 | } else { | 
| John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3100 | OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>(); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3101 |  | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3102 | // catch(id e) always matches under this ABI, since only | 
|  | 3103 | // ObjC exceptions end up here in the first place. | 
| Daniel Dunbar | 97f61d1 | 2008-09-27 22:21:14 +0000 | [diff] [blame] | 3104 | // FIXME: For the time being we also match id<X>; this should | 
|  | 3105 | // be rejected by Sema instead. | 
| Eli Friedman | 818e96f | 2009-07-11 00:57:02 +0000 | [diff] [blame] | 3106 | if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType())) | 
| Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3107 | AllMatched = true; | 
| Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3108 | } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3109 |  | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3110 | // 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] | 3111 | if (AllMatched) { | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3112 | CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF); | 
|  | 3113 |  | 
| Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3114 | if (CatchParam) { | 
| John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 3115 | CGF.EmitAutoVarDecl(*CatchParam); | 
| Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 3116 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3117 |  | 
|  | 3118 | // These types work out because ConvertType(id) == i8*. | 
| Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 3119 | CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam)); | 
| Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3120 | } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3121 |  | 
| Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3122 | CGF.EmitStmt(CatchStmt->getCatchBody()); | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3123 |  | 
|  | 3124 | // The scope of the catch variable ends right here. | 
|  | 3125 | CatchVarCleanups.ForceCleanup(); | 
|  | 3126 |  | 
| Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3127 | CGF.EmitBranchThroughCleanup(FinallyEnd); | 
| Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3128 | break; | 
|  | 3129 | } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3130 |  | 
| Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3131 | assert(OPT && "Unexpected non-object pointer type in @catch"); | 
| John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 3132 | const ObjCObjectType *ObjTy = OPT->getObjectType(); | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3133 |  | 
|  | 3134 | // FIXME: @catch (Class c) ? | 
| John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 3135 | ObjCInterfaceDecl *IDecl = ObjTy->getInterface(); | 
|  | 3136 | assert(IDecl && "Catch parameter must have Objective-C type!"); | 
| Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3137 |  | 
|  | 3138 | // Check if the @catch block matches the exception object. | 
| John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 3139 | llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3140 |  | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3141 | llvm::CallInst *Match = | 
| Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 3142 | CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(), | 
|  | 3143 | Class, Caught, "match"); | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3144 | Match->setDoesNotThrow(); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3145 |  | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3146 | llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match"); | 
|  | 3147 | llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next"); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3148 |  | 
|  | 3149 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"), | 
| Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 3150 | MatchedBlock, NextCatchBlock); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3151 |  | 
| Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3152 | // Emit the @catch block. | 
|  | 3153 | CGF.EmitBlock(MatchedBlock); | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3154 |  | 
|  | 3155 | // Collect any cleanups for the catch variable.  The scope lasts until | 
|  | 3156 | // the end of the catch body. | 
| John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3157 | CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF); | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3158 |  | 
| John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 3159 | CGF.EmitAutoVarDecl(*CatchParam); | 
| Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 3160 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); | 
| Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3161 |  | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3162 | // Initialize the catch variable. | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3163 | llvm::Value *Tmp = | 
|  | 3164 | CGF.Builder.CreateBitCast(Caught, | 
| Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 3165 | CGF.ConvertType(CatchParam->getType())); | 
| Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 3166 | CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam)); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3167 |  | 
| Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 3168 | CGF.EmitStmt(CatchStmt->getCatchBody()); | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3169 |  | 
|  | 3170 | // We're done with the catch variable. | 
|  | 3171 | CatchVarCleanups.ForceCleanup(); | 
|  | 3172 |  | 
| Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3173 | CGF.EmitBranchThroughCleanup(FinallyEnd); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3174 |  | 
| Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3175 | CGF.EmitBlock(NextCatchBlock); | 
|  | 3176 | } | 
|  | 3177 |  | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3178 | CGF.ObjCEHValueStack.pop_back(); | 
|  | 3179 |  | 
| John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3180 | // If nothing wanted anything to do with the caught exception, | 
|  | 3181 | // kill the extract call. | 
|  | 3182 | if (Caught->use_empty()) | 
|  | 3183 | Caught->eraseFromParent(); | 
|  | 3184 |  | 
|  | 3185 | if (!AllMatched) | 
|  | 3186 | CGF.EmitBranchThroughCleanup(FinallyRethrow); | 
|  | 3187 |  | 
|  | 3188 | if (HasFinally) { | 
|  | 3189 | // Emit the exception handler for the @catch blocks. | 
|  | 3190 | CGF.EmitBlock(CatchHandler); | 
|  | 3191 |  | 
|  | 3192 | // In theory we might now need a write hazard, but actually it's | 
|  | 3193 | // unnecessary because there's no local-accessing code between | 
|  | 3194 | // the try's write hazard and here. | 
|  | 3195 | //Hazards.emitWriteHazard(); | 
|  | 3196 |  | 
| John McCall | 9e2213d | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3197 | // Extract the new exception and save it to the | 
|  | 3198 | // propagating-exception slot. | 
|  | 3199 | assert(PropagatingExnVar); | 
|  | 3200 | llvm::CallInst *NewCaught = | 
|  | 3201 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), | 
|  | 3202 | ExceptionData, "caught"); | 
|  | 3203 | NewCaught->setDoesNotThrow(); | 
|  | 3204 | CGF.Builder.CreateStore(NewCaught, PropagatingExnVar); | 
|  | 3205 |  | 
| John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3206 | // Don't pop the catch handler; the throw already did. | 
|  | 3207 | CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar); | 
| Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3208 | CGF.EmitBranchThroughCleanup(FinallyRethrow); | 
| Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 3209 | } | 
| Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3210 | } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3211 |  | 
| John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3212 | // Insert read hazards as required in the new blocks. | 
| John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3213 | Hazards.emitHazardsInNewBlocks(); | 
| John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3214 |  | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3215 | // Pop the cleanup. | 
| John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3216 | CGF.Builder.restoreIP(TryFallthroughIP); | 
|  | 3217 | if (CGF.HaveInsertPoint()) | 
|  | 3218 | CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar); | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3219 | CGF.PopCleanupBlock(); | 
| John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3220 | CGF.EmitBlock(FinallyEnd.getBlock(), true); | 
| Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 3221 |  | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3222 | // Emit the rethrow block. | 
| John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3223 | CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP(); | 
| John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 3224 | CGF.EmitBlock(FinallyRethrow.getBlock(), true); | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3225 | if (CGF.HaveInsertPoint()) { | 
| John McCall | 9e2213d | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3226 | // If we have a propagating-exception variable, check it. | 
|  | 3227 | llvm::Value *PropagatingExn; | 
|  | 3228 | if (PropagatingExnVar) { | 
|  | 3229 | PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar); | 
| John McCall | 0b25172 | 2010-08-04 05:59:32 +0000 | [diff] [blame] | 3230 |  | 
| John McCall | 9e2213d | 2010-10-04 23:42:51 +0000 | [diff] [blame] | 3231 | // Otherwise, just look in the buffer for the exception to throw. | 
|  | 3232 | } else { | 
|  | 3233 | llvm::CallInst *Caught = | 
|  | 3234 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), | 
|  | 3235 | ExceptionData); | 
|  | 3236 | Caught->setDoesNotThrow(); | 
|  | 3237 | PropagatingExn = Caught; | 
|  | 3238 | } | 
|  | 3239 |  | 
|  | 3240 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn) | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3241 | ->setDoesNotThrow(); | 
|  | 3242 | CGF.Builder.CreateUnreachable(); | 
| Fariborz Jahanian | f2878e5 | 2008-11-21 19:21:53 +0000 | [diff] [blame] | 3243 | } | 
| Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3244 |  | 
| John McCall | 87bb582 | 2010-07-31 23:20:56 +0000 | [diff] [blame] | 3245 | CGF.Builder.restoreIP(SavedIP); | 
| Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 3246 | } | 
|  | 3247 |  | 
|  | 3248 | void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, | 
| Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 3249 | const ObjCAtThrowStmt &S) { | 
| Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 3250 | llvm::Value *ExceptionAsObject; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3251 |  | 
| Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 3252 | if (const Expr *ThrowExpr = S.getThrowExpr()) { | 
| John McCall | 2b014d6 | 2011-10-01 10:32:24 +0000 | [diff] [blame] | 3253 | llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3254 | ExceptionAsObject = | 
| Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 3255 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy); | 
| Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 3256 | } else { | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3257 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && | 
| Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 3258 | "Unexpected rethrow outside @catch block."); | 
| Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 3259 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); | 
| Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 3260 | } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3261 |  | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3262 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject) | 
|  | 3263 | ->setDoesNotReturn(); | 
| Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 3264 | CGF.Builder.CreateUnreachable(); | 
| Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 3265 |  | 
|  | 3266 | // Clear the insertion point to indicate we are in unreachable code. | 
|  | 3267 | CGF.Builder.ClearInsertionPoint(); | 
| Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 3268 | } | 
|  | 3269 |  | 
| Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3270 | /// EmitObjCWeakRead - Code gen for loading value of a __weak | 
| Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 3271 | /// object: objc_read_weak (id *src) | 
|  | 3272 | /// | 
| Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3273 | llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3274 | llvm::Value *AddrWeakObj) { | 
| Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3275 | llvm::Type* DestTy = | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3276 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); | 
|  | 3277 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, | 
|  | 3278 | ObjCTypes.PtrObjectPtrTy); | 
| Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 3279 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), | 
| Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3280 | AddrWeakObj, "weakread"); | 
| Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 3281 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); | 
| Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 3282 | return read_weak; | 
|  | 3283 | } | 
|  | 3284 |  | 
| Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3285 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. | 
|  | 3286 | /// objc_assign_weak (id src, id *dst) | 
|  | 3287 | /// | 
|  | 3288 | void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3289 | llvm::Value *src, llvm::Value *dst) { | 
| Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3290 | llvm::Type * SrcTy = src->getType(); | 
| Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3291 | if (!isa<llvm::PointerType>(SrcTy)) { | 
| Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3292 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); | 
| Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3293 | assert(Size <= 8 && "does not support size > 8"); | 
|  | 3294 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3295 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); | 
| Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 3296 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); | 
|  | 3297 | } | 
| Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 3298 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); | 
|  | 3299 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); | 
| Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 3300 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), | 
| Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3301 | src, dst, "weakassign"); | 
|  | 3302 | return; | 
|  | 3303 | } | 
|  | 3304 |  | 
| Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 3305 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. | 
|  | 3306 | /// objc_assign_global (id src, id *dst) | 
|  | 3307 | /// | 
|  | 3308 | void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, | 
| Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 3309 | llvm::Value *src, llvm::Value *dst, | 
|  | 3310 | bool threadlocal) { | 
| Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3311 | llvm::Type * SrcTy = src->getType(); | 
| Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3312 | if (!isa<llvm::PointerType>(SrcTy)) { | 
| Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3313 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); | 
| Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3314 | assert(Size <= 8 && "does not support size > 8"); | 
|  | 3315 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3316 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); | 
| Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 3317 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); | 
|  | 3318 | } | 
| Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 3319 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); | 
|  | 3320 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); | 
| Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 3321 | if (!threadlocal) | 
|  | 3322 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), | 
|  | 3323 | src, dst, "globalassign"); | 
|  | 3324 | else | 
|  | 3325 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(), | 
|  | 3326 | src, dst, "threadlocalassign"); | 
| Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 3327 | return; | 
|  | 3328 | } | 
|  | 3329 |  | 
| Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 3330 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. | 
| Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 3331 | /// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset) | 
| Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 3332 | /// | 
|  | 3333 | void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, | 
| Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 3334 | llvm::Value *src, llvm::Value *dst, | 
|  | 3335 | llvm::Value *ivarOffset) { | 
|  | 3336 | assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL"); | 
| Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3337 | llvm::Type * SrcTy = src->getType(); | 
| Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3338 | if (!isa<llvm::PointerType>(SrcTy)) { | 
| Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3339 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); | 
| Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3340 | assert(Size <= 8 && "does not support size > 8"); | 
|  | 3341 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3342 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); | 
| Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 3343 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); | 
|  | 3344 | } | 
| Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 3345 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); | 
|  | 3346 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); | 
| Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 3347 | CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(), | 
|  | 3348 | src, dst, ivarOffset); | 
| Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 3349 | return; | 
|  | 3350 | } | 
|  | 3351 |  | 
| Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 3352 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. | 
|  | 3353 | /// objc_assign_strongCast (id src, id *dst) | 
|  | 3354 | /// | 
|  | 3355 | void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3356 | llvm::Value *src, llvm::Value *dst) { | 
| Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3357 | llvm::Type * SrcTy = src->getType(); | 
| Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3358 | if (!isa<llvm::PointerType>(SrcTy)) { | 
| Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3359 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); | 
| Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3360 | assert(Size <= 8 && "does not support size > 8"); | 
|  | 3361 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3362 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); | 
| Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 3363 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); | 
|  | 3364 | } | 
| Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 3365 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); | 
|  | 3366 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); | 
| Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 3367 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), | 
| Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 3368 | src, dst, "weakassign"); | 
|  | 3369 | return; | 
|  | 3370 | } | 
|  | 3371 |  | 
| Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 3372 | void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3373 | llvm::Value *DestPtr, | 
|  | 3374 | llvm::Value *SrcPtr, | 
| Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 3375 | llvm::Value *size) { | 
| Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 3376 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy); | 
|  | 3377 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy); | 
| Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 3378 | CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(), | 
| Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 3379 | DestPtr, SrcPtr, size); | 
| Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 3380 | return; | 
|  | 3381 | } | 
|  | 3382 |  | 
| Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 3383 | /// EmitObjCValueForIvar - Code Gen for ivar reference. | 
|  | 3384 | /// | 
| Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 3385 | LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, | 
|  | 3386 | QualType ObjectTy, | 
|  | 3387 | llvm::Value *BaseValue, | 
|  | 3388 | const ObjCIvarDecl *Ivar, | 
| Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 3389 | unsigned CVRQualifiers) { | 
| John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3390 | const ObjCInterfaceDecl *ID = | 
|  | 3391 | ObjectTy->getAs<ObjCObjectType>()->getInterface(); | 
| Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 3392 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, | 
|  | 3393 | EmitIvarOffset(CGF, ID, Ivar)); | 
| Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 3394 | } | 
|  | 3395 |  | 
| Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 3396 | llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, | 
| Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 3397 | const ObjCInterfaceDecl *Interface, | 
| Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 3398 | const ObjCIvarDecl *Ivar) { | 
| Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 3399 | uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar); | 
| Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3400 | return llvm::ConstantInt::get( | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3401 | CGM.getTypes().ConvertType(CGM.getContext().LongTy), | 
|  | 3402 | Offset); | 
| Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 3403 | } | 
|  | 3404 |  | 
| Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3405 | /* *** Private Interface *** */ | 
|  | 3406 |  | 
|  | 3407 | /// EmitImageInfo - Emit the image info marker used to encode some module | 
|  | 3408 | /// level information. | 
|  | 3409 | /// | 
|  | 3410 | /// See: <rdr://4810609&4810587&4810587> | 
|  | 3411 | /// struct IMAGE_INFO { | 
|  | 3412 | ///   unsigned version; | 
|  | 3413 | ///   unsigned flags; | 
|  | 3414 | /// }; | 
|  | 3415 | enum ImageInfoFlags { | 
| Daniel Dunbar | fce176b | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 3416 | eImageInfo_FixAndContinue      = (1 << 0), | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3417 | eImageInfo_GarbageCollected    = (1 << 1), | 
|  | 3418 | eImageInfo_GCOnly              = (1 << 2), | 
| Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 3419 | eImageInfo_OptimizedByDyld     = (1 << 3), // FIXME: When is this set. | 
|  | 3420 |  | 
| Daniel Dunbar | fce176b | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 3421 | // A flag indicating that the module has no instances of a @synthesize of a | 
|  | 3422 | // superclass variable. <rdar://problem/6803242> | 
| Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 3423 | eImageInfo_CorrectedSynthesize = (1 << 4) | 
| Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3424 | }; | 
|  | 3425 |  | 
| Daniel Dunbar | fce176b | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 3426 | void CGObjCCommonMac::EmitImageInfo() { | 
| Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3427 | unsigned version = 0; // Version is unused? | 
|  | 3428 | unsigned flags = 0; | 
|  | 3429 |  | 
|  | 3430 | // FIXME: Fix and continue? | 
| Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 3431 | if (CGM.getLangOptions().getGC() != LangOptions::NonGC) | 
| Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3432 | flags |= eImageInfo_GarbageCollected; | 
| Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 3433 | if (CGM.getLangOptions().getGC() == LangOptions::GCOnly) | 
| Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3434 | flags |= eImageInfo_GCOnly; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3435 |  | 
| Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 3436 | // We never allow @synthesize of a superclass property. | 
|  | 3437 | flags |= eImageInfo_CorrectedSynthesize; | 
| Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3438 |  | 
| Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3439 | llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext); | 
| Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3440 |  | 
| Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3441 | // Emitted as int[2]; | 
|  | 3442 | llvm::Constant *values[2] = { | 
| Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3443 | llvm::ConstantInt::get(Int32Ty, version), | 
|  | 3444 | llvm::ConstantInt::get(Int32Ty, flags) | 
| Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3445 | }; | 
| Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 3446 | llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2); | 
| Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3447 |  | 
|  | 3448 | const char *Section; | 
|  | 3449 | if (ObjCABI == 1) | 
|  | 3450 | Section = "__OBJC, __image_info,regular"; | 
|  | 3451 | else | 
|  | 3452 | Section = "__DATA, __objc_imageinfo, regular, no_dead_strip"; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3453 | llvm::GlobalVariable *GV = | 
| Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3454 | CreateMetadataVar("\01L_OBJC_IMAGE_INFO", | 
| Jay Foad | 9735760 | 2011-06-22 09:24:39 +0000 | [diff] [blame] | 3455 | llvm::ConstantArray::get(AT, values), | 
| Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3456 | Section, | 
|  | 3457 | 0, | 
|  | 3458 | true); | 
|  | 3459 | GV->setConstant(true); | 
| Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3460 | } | 
|  | 3461 |  | 
| Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3462 |  | 
|  | 3463 | // struct objc_module { | 
|  | 3464 | //   unsigned long version; | 
|  | 3465 | //   unsigned long size; | 
|  | 3466 | //   const char *name; | 
|  | 3467 | //   Symtab symtab; | 
|  | 3468 | // }; | 
|  | 3469 |  | 
|  | 3470 | // FIXME: Get from somewhere | 
|  | 3471 | static const int ModuleVersion = 7; | 
|  | 3472 |  | 
|  | 3473 | void CGObjCMac::EmitModuleInfo() { | 
| Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3474 | uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3475 |  | 
| Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 3476 | llvm::Constant *Values[] = { | 
|  | 3477 | llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion), | 
|  | 3478 | llvm::ConstantInt::get(ObjCTypes.LongTy, Size), | 
|  | 3479 | // This used to be the filename, now it is unused. <rdr://4327263> | 
|  | 3480 | GetClassName(&CGM.getContext().Idents.get("")), | 
|  | 3481 | EmitModuleSymbols() | 
|  | 3482 | }; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3483 | CreateMetadataVar("\01L_OBJC_MODULES", | 
| Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 3484 | llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values), | 
| Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3485 | "__OBJC,__module_info,regular,no_dead_strip", | 
| Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 3486 | 4, true); | 
| Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3487 | } | 
|  | 3488 |  | 
|  | 3489 | llvm::Constant *CGObjCMac::EmitModuleSymbols() { | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3490 | unsigned NumClasses = DefinedClasses.size(); | 
|  | 3491 | unsigned NumCategories = DefinedCategories.size(); | 
|  | 3492 |  | 
| Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3493 | // Return null if no symbols were defined. | 
|  | 3494 | if (!NumClasses && !NumCategories) | 
| Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3495 | return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy); | 
| Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3496 |  | 
| Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3497 | llvm::Constant *Values[5]; | 
| Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3498 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); | 
| Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3499 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy); | 
| Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3500 | Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses); | 
|  | 3501 | Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3502 |  | 
| Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3503 | // The runtime expects exactly the list of defined classes followed | 
|  | 3504 | // by the list of defined categories, in a single array. | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3505 | std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories); | 
| Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3506 | for (unsigned i=0; i<NumClasses; i++) | 
| Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3507 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], | 
| Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3508 | ObjCTypes.Int8PtrTy); | 
|  | 3509 | for (unsigned i=0; i<NumCategories; i++) | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3510 | Symbols[NumClasses + i] = | 
| Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3511 | llvm::ConstantExpr::getBitCast(DefinedCategories[i], | 
| Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3512 | ObjCTypes.Int8PtrTy); | 
|  | 3513 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3514 | Values[4] = | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3515 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3516 | NumClasses + NumCategories), | 
|  | 3517 | Symbols); | 
|  | 3518 |  | 
| Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 3519 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3520 |  | 
| Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3521 | llvm::GlobalVariable *GV = | 
| Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3522 | CreateMetadataVar("\01L_OBJC_SYMBOLS", Init, | 
|  | 3523 | "__OBJC,__symbols,regular,no_dead_strip", | 
| Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 3524 | 4, true); | 
| Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3525 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3526 | } | 
|  | 3527 |  | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3528 | llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder, | 
|  | 3529 | IdentifierInfo *II) { | 
|  | 3530 | LazySymbols.insert(II); | 
|  | 3531 |  | 
|  | 3532 | llvm::GlobalVariable *&Entry = ClassReferences[II]; | 
|  | 3533 |  | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3534 | if (!Entry) { | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3535 | llvm::Constant *Casted = | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3536 | llvm::ConstantExpr::getBitCast(GetClassName(II), | 
|  | 3537 | ObjCTypes.ClassPtrTy); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3538 | Entry = | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3539 | CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted, | 
|  | 3540 | "__OBJC,__cls_refs,literal_pointers,no_dead_strip", | 
|  | 3541 | 4, true); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3542 | } | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3543 |  | 
| Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 3544 | return Builder.CreateLoad(Entry); | 
| Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3545 | } | 
|  | 3546 |  | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3547 | llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder, | 
|  | 3548 | const ObjCInterfaceDecl *ID) { | 
|  | 3549 | return EmitClassRefFromId(Builder, ID->getIdentifier()); | 
|  | 3550 | } | 
|  | 3551 |  | 
|  | 3552 | llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) { | 
|  | 3553 | IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool"); | 
|  | 3554 | return EmitClassRefFromId(Builder, II); | 
|  | 3555 | } | 
|  | 3556 |  | 
| Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 3557 | llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel, | 
|  | 3558 | bool lvalue) { | 
| Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3559 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3560 |  | 
| Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3561 | if (!Entry) { | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3562 | llvm::Constant *Casted = | 
| Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3563 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), | 
| Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3564 | ObjCTypes.SelectorPtrTy); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3565 | Entry = | 
| Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3566 | CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted, | 
|  | 3567 | "__OBJC,__message_refs,literal_pointers,no_dead_strip", | 
| Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 3568 | 4, true); | 
| Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3569 | } | 
|  | 3570 |  | 
| Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 3571 | if (lvalue) | 
|  | 3572 | return Entry; | 
| Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 3573 | return Builder.CreateLoad(Entry); | 
| Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3574 | } | 
|  | 3575 |  | 
| Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3576 | llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) { | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3577 | llvm::GlobalVariable *&Entry = ClassNames[Ident]; | 
| Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3578 |  | 
| Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3579 | if (!Entry) | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3580 | Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 3581 | llvm::ConstantArray::get(VMContext, | 
|  | 3582 | Ident->getNameStart()), | 
| Daniel Dunbar | af19ac4 | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 3583 | ((ObjCABI == 2) ? | 
|  | 3584 | "__TEXT,__objc_classname,cstring_literals" : | 
|  | 3585 | "__TEXT,__cstring,cstring_literals"), | 
| Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3586 | 1, true); | 
| Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3587 |  | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3588 | return getConstantGEP(VMContext, Entry, 0, 0); | 
| Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3589 | } | 
|  | 3590 |  | 
| Argyrios Kyrtzidis | 9d50c06 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 3591 | llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) { | 
|  | 3592 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator | 
|  | 3593 | I = MethodDefinitions.find(MD); | 
|  | 3594 | if (I != MethodDefinitions.end()) | 
|  | 3595 | return I->second; | 
|  | 3596 |  | 
| Argyrios Kyrtzidis | 9d50c06 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 3597 | return NULL; | 
|  | 3598 | } | 
|  | 3599 |  | 
| Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 3600 | /// GetIvarLayoutName - Returns a unique constant for the given | 
|  | 3601 | /// ivar layout bitmap. | 
|  | 3602 | llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident, | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3603 | const ObjCCommonTypesHelper &ObjCTypes) { | 
| Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3604 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); | 
| Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 3605 | } | 
|  | 3606 |  | 
| Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3607 | void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT, | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3608 | unsigned int BytePos, | 
| Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3609 | bool ForStrongLayout, | 
|  | 3610 | bool &HasUnion) { | 
|  | 3611 | const RecordDecl *RD = RT->getDecl(); | 
|  | 3612 | // FIXME - Use iterator. | 
| Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3613 | SmallVector<const FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end()); | 
| Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3614 | llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0)); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3615 | const llvm::StructLayout *RecLayout = | 
| Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3616 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty)); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3617 |  | 
| Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3618 | BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos, | 
|  | 3619 | ForStrongLayout, HasUnion); | 
|  | 3620 | } | 
|  | 3621 |  | 
| Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 3622 | void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI, | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3623 | const llvm::StructLayout *Layout, | 
|  | 3624 | const RecordDecl *RD, | 
| Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3625 | const SmallVectorImpl<const FieldDecl*> &RecFields, | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3626 | unsigned int BytePos, bool ForStrongLayout, | 
|  | 3627 | bool &HasUnion) { | 
| Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3628 | bool IsUnion = (RD && RD->isUnion()); | 
|  | 3629 | uint64_t MaxUnionIvarSize = 0; | 
|  | 3630 | uint64_t MaxSkippedUnionIvarSize = 0; | 
| Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3631 | const FieldDecl *MaxField = 0; | 
|  | 3632 | const FieldDecl *MaxSkippedField = 0; | 
|  | 3633 | const FieldDecl *LastFieldBitfieldOrUnnamed = 0; | 
| Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3634 | uint64_t MaxFieldOffset = 0; | 
|  | 3635 | uint64_t MaxSkippedFieldOffset = 0; | 
| Argyrios Kyrtzidis | 0dc7509 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 3636 | uint64_t LastBitfieldOrUnnamedOffset = 0; | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3637 | uint64_t FirstFieldDelta = 0; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3638 |  | 
| Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3639 | if (RecFields.empty()) | 
|  | 3640 | return; | 
| Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3641 | unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0); | 
|  | 3642 | unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth(); | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3643 | if (!RD && CGM.getLangOptions().ObjCAutoRefCount) { | 
| Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3644 | const FieldDecl *FirstField = RecFields[0]; | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3645 | FirstFieldDelta = | 
|  | 3646 | ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField)); | 
|  | 3647 | } | 
|  | 3648 |  | 
| Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3649 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { | 
| Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3650 | const FieldDecl *Field = RecFields[i]; | 
| Daniel Dunbar | e05cc98 | 2009-05-03 23:35:23 +0000 | [diff] [blame] | 3651 | uint64_t FieldOffset; | 
| Anders Carlsson | fc514ab | 2009-07-24 17:23:54 +0000 | [diff] [blame] | 3652 | if (RD) { | 
| Daniel Dunbar | f8f8eba | 2010-04-14 17:02:21 +0000 | [diff] [blame] | 3653 | // Note that 'i' here is actually the field index inside RD of Field, | 
|  | 3654 | // although this dependency is hidden. | 
|  | 3655 | const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD); | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3656 | FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta; | 
| Anders Carlsson | fc514ab | 2009-07-24 17:23:54 +0000 | [diff] [blame] | 3657 | } else | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3658 | FieldOffset = | 
|  | 3659 | ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta; | 
| Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3660 |  | 
| Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3661 | // Skip over unnamed or bitfields | 
| Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3662 | if (!Field->getIdentifier() || Field->isBitField()) { | 
| Argyrios Kyrtzidis | 0dc7509 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 3663 | LastFieldBitfieldOrUnnamed = Field; | 
|  | 3664 | LastBitfieldOrUnnamedOffset = FieldOffset; | 
| Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3665 | continue; | 
| Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3666 | } | 
| Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3667 |  | 
| Argyrios Kyrtzidis | 0dc7509 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 3668 | LastFieldBitfieldOrUnnamed = 0; | 
| Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3669 | QualType FQT = Field->getType(); | 
| Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3670 | if (FQT->isRecordType() || FQT->isUnionType()) { | 
| Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3671 | if (FQT->isUnionType()) | 
|  | 3672 | HasUnion = true; | 
| Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3673 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3674 | BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(), | 
| Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3675 | BytePos + FieldOffset, | 
| Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3676 | ForStrongLayout, HasUnion); | 
| Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3677 | continue; | 
|  | 3678 | } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3679 |  | 
| Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3680 | if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3681 | const ConstantArrayType *CArray = | 
| Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3682 | dyn_cast_or_null<ConstantArrayType>(Array); | 
| Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3683 | uint64_t ElCount = CArray->getSize().getZExtValue(); | 
| Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3684 | assert(CArray && "only array with known element size is supported"); | 
| Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3685 | FQT = CArray->getElementType(); | 
| Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3686 | while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { | 
|  | 3687 | const ConstantArrayType *CArray = | 
| Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3688 | dyn_cast_or_null<ConstantArrayType>(Array); | 
| Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3689 | ElCount *= CArray->getSize().getZExtValue(); | 
| Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3690 | FQT = CArray->getElementType(); | 
|  | 3691 | } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3692 |  | 
|  | 3693 | assert(!FQT->isUnionType() && | 
| Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3694 | "layout for array of unions not supported"); | 
| Fariborz Jahanian | f96bdf4 | 2011-01-03 19:23:18 +0000 | [diff] [blame] | 3695 | if (FQT->isRecordType() && ElCount) { | 
| Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3696 | int OldIndex = IvarsInfo.size() - 1; | 
|  | 3697 | int OldSkIndex = SkipIvars.size() -1; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3698 |  | 
| Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3699 | const RecordType *RT = FQT->getAs<RecordType>(); | 
| Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3700 | BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset, | 
| Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3701 | ForStrongLayout, HasUnion); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3702 |  | 
| Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3703 | // Replicate layout information for each array element. Note that | 
|  | 3704 | // one element is already done. | 
|  | 3705 | uint64_t ElIx = 1; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3706 | for (int FirstIndex = IvarsInfo.size() - 1, | 
|  | 3707 | FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) { | 
| Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3708 | uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits; | 
| Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3709 | for (int i = OldIndex+1; i <= FirstIndex; ++i) | 
|  | 3710 | IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx, | 
|  | 3711 | IvarsInfo[i].ivar_size)); | 
|  | 3712 | for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i) | 
|  | 3713 | SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx, | 
|  | 3714 | SkipIvars[i].ivar_size)); | 
| Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3715 | } | 
|  | 3716 | continue; | 
|  | 3717 | } | 
| Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3718 | } | 
| Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3719 | // At this point, we are done with Record/Union and array there of. | 
|  | 3720 | // For other arrays we are down to its element type. | 
| John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3721 | Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT); | 
| Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3722 |  | 
| Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3723 | unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType()); | 
| John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3724 | if ((ForStrongLayout && GCAttr == Qualifiers::Strong) | 
|  | 3725 | || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) { | 
| Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3726 | if (IsUnion) { | 
| Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3727 | uint64_t UnionIvarSize = FieldSize / WordSizeInBits; | 
| Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3728 | if (UnionIvarSize > MaxUnionIvarSize) { | 
| Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3729 | MaxUnionIvarSize = UnionIvarSize; | 
|  | 3730 | MaxField = Field; | 
| Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3731 | MaxFieldOffset = FieldOffset; | 
| Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3732 | } | 
| Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3733 | } else { | 
| Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3734 | IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset, | 
| Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3735 | FieldSize / WordSizeInBits)); | 
| Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3736 | } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3737 | } else if ((ForStrongLayout && | 
| John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3738 | (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)) | 
|  | 3739 | || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) { | 
| Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3740 | if (IsUnion) { | 
| Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 3741 | // FIXME: Why the asymmetry? We divide by word size in bits on other | 
|  | 3742 | // side. | 
| Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3743 | uint64_t UnionIvarSize = FieldSize; | 
| Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3744 | if (UnionIvarSize > MaxSkippedUnionIvarSize) { | 
| Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3745 | MaxSkippedUnionIvarSize = UnionIvarSize; | 
|  | 3746 | MaxSkippedField = Field; | 
| Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3747 | MaxSkippedFieldOffset = FieldOffset; | 
| Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3748 | } | 
| Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3749 | } else { | 
| Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3750 | // FIXME: Why the asymmetry, we divide by byte size in bits here? | 
| Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3751 | SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset, | 
| Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3752 | FieldSize / ByteSizeInBits)); | 
| Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3753 | } | 
|  | 3754 | } | 
|  | 3755 | } | 
| Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3756 |  | 
| Argyrios Kyrtzidis | 0dc7509 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 3757 | if (LastFieldBitfieldOrUnnamed) { | 
|  | 3758 | if (LastFieldBitfieldOrUnnamed->isBitField()) { | 
|  | 3759 | // Last field was a bitfield. Must update skip info. | 
| Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3760 | uint64_t BitFieldSize | 
|  | 3761 | = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext()); | 
| Argyrios Kyrtzidis | 0dc7509 | 2010-09-06 12:00:10 +0000 | [diff] [blame] | 3762 | GC_IVAR skivar; | 
|  | 3763 | skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset; | 
|  | 3764 | skivar.ivar_size = (BitFieldSize / ByteSizeInBits) | 
|  | 3765 | + ((BitFieldSize % ByteSizeInBits) != 0); | 
|  | 3766 | SkipIvars.push_back(skivar); | 
|  | 3767 | } else { | 
|  | 3768 | assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed"); | 
|  | 3769 | // Last field was unnamed. Must update skip info. | 
|  | 3770 | unsigned FieldSize | 
|  | 3771 | = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType()); | 
|  | 3772 | SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset, | 
|  | 3773 | FieldSize / ByteSizeInBits)); | 
|  | 3774 | } | 
| Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3775 | } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3776 |  | 
| Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3777 | if (MaxField) | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3778 | IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset, | 
| Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3779 | MaxUnionIvarSize)); | 
|  | 3780 | if (MaxSkippedField) | 
| Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3781 | SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset, | 
| Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3782 | MaxSkippedUnionIvarSize)); | 
| Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3783 | } | 
|  | 3784 |  | 
| Fariborz Jahanian | b8fd2eb | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 3785 | /// BuildIvarLayoutBitmap - This routine is the horsework for doing all | 
|  | 3786 | /// the computations and returning the layout bitmap (for ivar or blocks) in | 
|  | 3787 | /// the given argument BitMap string container. Routine reads | 
|  | 3788 | /// two containers, IvarsInfo and SkipIvars which are assumed to be | 
|  | 3789 | /// filled already by the caller. | 
|  | 3790 | llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) { | 
| Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3791 | unsigned int WordsToScan, WordsToSkip; | 
| Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3792 | llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext); | 
| Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3793 |  | 
| Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3794 | // Build the string of skip/scan nibbles | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3795 | SmallVector<SKIP_SCAN, 32> SkipScanIvars; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3796 | unsigned int WordSize = | 
| Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3797 | CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy); | 
| Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3798 | if (IvarsInfo[0].ivar_bytepos == 0) { | 
|  | 3799 | WordsToSkip = 0; | 
|  | 3800 | WordsToScan = IvarsInfo[0].ivar_size; | 
| Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3801 | } else { | 
| Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3802 | WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize; | 
|  | 3803 | WordsToScan = IvarsInfo[0].ivar_size; | 
|  | 3804 | } | 
| Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3805 | for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) { | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3806 | unsigned int TailPrevGCObjC = | 
| Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3807 | IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize; | 
| Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3808 | if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) { | 
| Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3809 | // consecutive 'scanned' object pointers. | 
|  | 3810 | WordsToScan += IvarsInfo[i].ivar_size; | 
| Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3811 | } else { | 
| Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3812 | // Skip over 'gc'able object pointer which lay over each other. | 
|  | 3813 | if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos) | 
|  | 3814 | continue; | 
|  | 3815 | // Must skip over 1 or more words. We save current skip/scan values | 
|  | 3816 | //  and start a new pair. | 
| Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3817 | SKIP_SCAN SkScan; | 
|  | 3818 | SkScan.skip = WordsToSkip; | 
|  | 3819 | SkScan.scan = WordsToScan; | 
| Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3820 | SkipScanIvars.push_back(SkScan); | 
| Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3821 |  | 
| Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3822 | // Skip the hole. | 
| Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3823 | SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize; | 
|  | 3824 | SkScan.scan = 0; | 
| Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3825 | SkipScanIvars.push_back(SkScan); | 
| Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3826 | WordsToSkip = 0; | 
|  | 3827 | WordsToScan = IvarsInfo[i].ivar_size; | 
|  | 3828 | } | 
|  | 3829 | } | 
| Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3830 | if (WordsToScan > 0) { | 
| Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3831 | SKIP_SCAN SkScan; | 
|  | 3832 | SkScan.skip = WordsToSkip; | 
|  | 3833 | SkScan.scan = WordsToScan; | 
| Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3834 | SkipScanIvars.push_back(SkScan); | 
| Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3835 | } | 
| Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3836 |  | 
| Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3837 | if (!SkipIvars.empty()) { | 
| Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3838 | unsigned int LastIndex = SkipIvars.size()-1; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3839 | int LastByteSkipped = | 
| Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3840 | SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size; | 
| Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3841 | LastIndex = IvarsInfo.size()-1; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3842 | int LastByteScanned = | 
| Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3843 | IvarsInfo[LastIndex].ivar_bytepos + | 
|  | 3844 | IvarsInfo[LastIndex].ivar_size * WordSize; | 
| Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3845 | // 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] | 3846 | if (LastByteSkipped > LastByteScanned) { | 
| Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3847 | unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize; | 
| Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3848 | SKIP_SCAN SkScan; | 
|  | 3849 | SkScan.skip = TotalWords - (LastByteScanned/WordSize); | 
|  | 3850 | SkScan.scan = 0; | 
| Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3851 | SkipScanIvars.push_back(SkScan); | 
| Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3852 | } | 
|  | 3853 | } | 
|  | 3854 | // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced | 
|  | 3855 | // as 0xMN. | 
| Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3856 | int SkipScan = SkipScanIvars.size()-1; | 
| Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3857 | for (int i = 0; i <= SkipScan; i++) { | 
| Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3858 | if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0 | 
|  | 3859 | && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) { | 
|  | 3860 | // 0xM0 followed by 0x0N detected. | 
|  | 3861 | SkipScanIvars[i].scan = SkipScanIvars[i+1].scan; | 
|  | 3862 | for (int j = i+1; j < SkipScan; j++) | 
|  | 3863 | SkipScanIvars[j] = SkipScanIvars[j+1]; | 
|  | 3864 | --SkipScan; | 
|  | 3865 | } | 
|  | 3866 | } | 
| Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3867 |  | 
| Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3868 | // Generate the string. | 
| Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3869 | for (int i = 0; i <= SkipScan; i++) { | 
| Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3870 | unsigned char byte; | 
|  | 3871 | unsigned int skip_small = SkipScanIvars[i].skip % 0xf; | 
|  | 3872 | unsigned int scan_small = SkipScanIvars[i].scan % 0xf; | 
|  | 3873 | unsigned int skip_big  = SkipScanIvars[i].skip / 0xf; | 
|  | 3874 | unsigned int scan_big  = SkipScanIvars[i].scan / 0xf; | 
| Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3875 |  | 
| Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3876 | // first skip big. | 
|  | 3877 | for (unsigned int ix = 0; ix < skip_big; ix++) | 
|  | 3878 | BitMap += (unsigned char)(0xf0); | 
| Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3879 |  | 
| Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3880 | // next (skip small, scan) | 
| Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3881 | if (skip_small) { | 
| Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3882 | byte = skip_small << 4; | 
| Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3883 | if (scan_big > 0) { | 
| Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3884 | byte |= 0xf; | 
|  | 3885 | --scan_big; | 
| Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3886 | } else if (scan_small) { | 
| Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3887 | byte |= scan_small; | 
|  | 3888 | scan_small = 0; | 
|  | 3889 | } | 
|  | 3890 | BitMap += byte; | 
|  | 3891 | } | 
|  | 3892 | // next scan big | 
|  | 3893 | for (unsigned int ix = 0; ix < scan_big; ix++) | 
|  | 3894 | BitMap += (unsigned char)(0x0f); | 
|  | 3895 | // last scan small | 
| Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3896 | if (scan_small) { | 
| Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3897 | byte = scan_small; | 
|  | 3898 | BitMap += byte; | 
|  | 3899 | } | 
|  | 3900 | } | 
|  | 3901 | // null terminate string. | 
| Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3902 | unsigned char zero = 0; | 
|  | 3903 | BitMap += zero; | 
| Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3904 |  | 
|  | 3905 | llvm::GlobalVariable * Entry = | 
|  | 3906 | CreateMetadataVar("\01L_OBJC_CLASS_NAME_", | 
|  | 3907 | llvm::ConstantArray::get(VMContext, BitMap.c_str()), | 
| Daniel Dunbar | af19ac4 | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 3908 | ((ObjCABI == 2) ? | 
|  | 3909 | "__TEXT,__objc_classname,cstring_literals" : | 
|  | 3910 | "__TEXT,__cstring,cstring_literals"), | 
| Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3911 | 1, true); | 
|  | 3912 | return getConstantGEP(VMContext, Entry, 0, 0); | 
|  | 3913 | } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3914 |  | 
| Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3915 | /// BuildIvarLayout - Builds ivar layout bitmap for the class | 
|  | 3916 | /// implementation for the __strong or __weak case. | 
|  | 3917 | /// The layout map displays which words in ivar list must be skipped | 
|  | 3918 | /// and which must be scanned by GC (see below). String is built of bytes. | 
|  | 3919 | /// Each byte is divided up in two nibbles (4-bit each). Left nibble is count | 
|  | 3920 | /// of words to skip and right nibble is count of words to scan. So, each | 
|  | 3921 | /// nibble represents up to 15 workds to skip or scan. Skipping the rest is | 
|  | 3922 | /// represented by a 0x00 byte which also ends the string. | 
|  | 3923 | /// 1. when ForStrongLayout is true, following ivars are scanned: | 
|  | 3924 | /// - id, Class | 
|  | 3925 | /// - object * | 
|  | 3926 | /// - __strong anything | 
|  | 3927 | /// | 
|  | 3928 | /// 2. When ForStrongLayout is false, following ivars are scanned: | 
|  | 3929 | /// - __weak anything | 
|  | 3930 | /// | 
|  | 3931 | llvm::Constant *CGObjCCommonMac::BuildIvarLayout( | 
|  | 3932 | const ObjCImplementationDecl *OMD, | 
|  | 3933 | bool ForStrongLayout) { | 
|  | 3934 | bool hasUnion = false; | 
|  | 3935 |  | 
| Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3936 | llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext); | 
| Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 3937 | if (CGM.getLangOptions().getGC() == LangOptions::NonGC && | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3938 | !CGM.getLangOptions().ObjCAutoRefCount) | 
| Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3939 | return llvm::Constant::getNullValue(PtrTy); | 
|  | 3940 |  | 
| Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3941 | const ObjCInterfaceDecl *OI = OMD->getClassInterface(); | 
|  | 3942 | SmallVector<const FieldDecl*, 32> RecFields; | 
| Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 3943 | if (CGM.getLangOptions().ObjCAutoRefCount) { | 
| Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3944 | for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin(); | 
| Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 3945 | IVD; IVD = IVD->getNextIvar()) | 
|  | 3946 | RecFields.push_back(cast<FieldDecl>(IVD)); | 
|  | 3947 | } | 
|  | 3948 | else { | 
| Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3949 | SmallVector<const ObjCIvarDecl*, 32> Ivars; | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3950 | CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars); | 
| Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3951 |  | 
| Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3952 | // FIXME: This is not ideal; we shouldn't have to do this copy. | 
|  | 3953 | RecFields.append(Ivars.begin(), Ivars.end()); | 
| Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 3954 | } | 
| Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3955 |  | 
|  | 3956 | if (RecFields.empty()) | 
|  | 3957 | return llvm::Constant::getNullValue(PtrTy); | 
|  | 3958 |  | 
|  | 3959 | SkipIvars.clear(); | 
|  | 3960 | IvarsInfo.clear(); | 
|  | 3961 |  | 
|  | 3962 | BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion); | 
|  | 3963 | if (IvarsInfo.empty()) | 
|  | 3964 | return llvm::Constant::getNullValue(PtrTy); | 
| Fariborz Jahanian | b8fd2eb | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 3965 | // Sort on byte position in case we encounterred a union nested in | 
|  | 3966 | // the ivar list. | 
|  | 3967 | if (hasUnion && !IvarsInfo.empty()) | 
|  | 3968 | std::sort(IvarsInfo.begin(), IvarsInfo.end()); | 
|  | 3969 | if (hasUnion && !SkipIvars.empty()) | 
|  | 3970 | std::sort(SkipIvars.begin(), SkipIvars.end()); | 
|  | 3971 |  | 
| Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3972 | std::string BitMap; | 
| Fariborz Jahanian | b8fd2eb | 2010-08-05 00:19:48 +0000 | [diff] [blame] | 3973 | llvm::Constant *C = BuildIvarLayoutBitmap(BitMap); | 
| Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3974 |  | 
|  | 3975 | if (CGM.getLangOptions().ObjCGCBitmapPrint) { | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3976 | printf("\n%s ivar layout for class '%s': ", | 
| Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 3977 | ForStrongLayout ? "strong" : "weak", | 
| Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 3978 | OMD->getClassInterface()->getName().data()); | 
| Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 3979 | const unsigned char *s = (unsigned char*)BitMap.c_str(); | 
|  | 3980 | for (unsigned i = 0; i < BitMap.size(); i++) | 
|  | 3981 | if (!(s[i] & 0xf0)) | 
|  | 3982 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); | 
|  | 3983 | else | 
|  | 3984 | printf("0x%x%s",  s[i], s[i] != 0 ? ", " : ""); | 
|  | 3985 | printf("\n"); | 
|  | 3986 | } | 
| Fariborz Jahanian | 93ce50d | 2010-08-04 23:55:24 +0000 | [diff] [blame] | 3987 | return C; | 
| Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3988 | } | 
|  | 3989 |  | 
| Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3990 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) { | 
| Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3991 | llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; | 
|  | 3992 |  | 
| Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3993 | // FIXME: Avoid std::string copying. | 
|  | 3994 | if (!Entry) | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3995 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_", | 
| Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3996 | llvm::ConstantArray::get(VMContext, Sel.getAsString()), | 
| Daniel Dunbar | af19ac4 | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 3997 | ((ObjCABI == 2) ? | 
|  | 3998 | "__TEXT,__objc_methname,cstring_literals" : | 
|  | 3999 | "__TEXT,__cstring,cstring_literals"), | 
| Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4000 | 1, true); | 
| Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4001 |  | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4002 | return getConstantGEP(VMContext, Entry, 0, 0); | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4003 | } | 
|  | 4004 |  | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4005 | // FIXME: Merge into a single cstring creation function. | 
| Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4006 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) { | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4007 | return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); | 
|  | 4008 | } | 
|  | 4009 |  | 
| Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4010 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) { | 
| Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4011 | std::string TypeStr; | 
|  | 4012 | CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); | 
|  | 4013 |  | 
|  | 4014 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4015 |  | 
| Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4016 | if (!Entry) | 
|  | 4017 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", | 
| Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 4018 | llvm::ConstantArray::get(VMContext, TypeStr), | 
| Daniel Dunbar | af19ac4 | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4019 | ((ObjCABI == 2) ? | 
|  | 4020 | "__TEXT,__objc_methtype,cstring_literals" : | 
|  | 4021 | "__TEXT,__cstring,cstring_literals"), | 
| Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4022 | 1, true); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4023 |  | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4024 | return getConstantGEP(VMContext, Entry, 0, 0); | 
| Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 4025 | } | 
|  | 4026 |  | 
| Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4027 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) { | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4028 | std::string TypeStr; | 
| Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 4029 | if (CGM.getContext().getObjCEncodingForMethodDecl( | 
|  | 4030 | const_cast<ObjCMethodDecl*>(D), | 
|  | 4031 | TypeStr)) | 
|  | 4032 | return 0; | 
| Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4033 |  | 
|  | 4034 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; | 
|  | 4035 |  | 
| Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4036 | if (!Entry) | 
|  | 4037 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", | 
| Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 4038 | llvm::ConstantArray::get(VMContext, TypeStr), | 
| Daniel Dunbar | af19ac4 | 2011-03-25 20:09:09 +0000 | [diff] [blame] | 4039 | ((ObjCABI == 2) ? | 
|  | 4040 | "__TEXT,__objc_methtype,cstring_literals" : | 
|  | 4041 | "__TEXT,__cstring,cstring_literals"), | 
| Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4042 | 1, true); | 
| Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +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 | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4045 | } | 
|  | 4046 |  | 
| Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +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::GetPropertyName(IdentifierInfo *Ident) { | 
| Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4049 | llvm::GlobalVariable *&Entry = PropertyNames[Ident]; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4050 |  | 
| Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4051 | if (!Entry) | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4052 | Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_", | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4053 | llvm::ConstantArray::get(VMContext, | 
|  | 4054 | Ident->getNameStart()), | 
| Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4055 | "__TEXT,__cstring,cstring_literals", | 
| Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 4056 | 1, true); | 
| Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4057 |  | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4058 | return getConstantGEP(VMContext, Entry, 0, 0); | 
| Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4059 | } | 
|  | 4060 |  | 
|  | 4061 | // FIXME: Merge into a single cstring creation function. | 
| Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4062 | // FIXME: This Decl should be more precise. | 
| Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 4063 | llvm::Constant * | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4064 | CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, | 
|  | 4065 | const Decl *Container) { | 
| Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 4066 | std::string TypeStr; | 
|  | 4067 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); | 
| Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 4068 | return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); | 
|  | 4069 | } | 
|  | 4070 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4071 | void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D, | 
| Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 4072 | const ObjCContainerDecl *CD, | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4073 | SmallVectorImpl<char> &Name) { | 
| Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4074 | llvm::raw_svector_ostream OS(Name); | 
| Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 4075 | assert (CD && "Missing container decl in GetNameForMethod"); | 
| Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4076 | OS << '\01' << (D->isInstanceMethod() ? '-' : '+') | 
|  | 4077 | << '[' << CD->getName(); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4078 | if (const ObjCCategoryImplDecl *CID = | 
| Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4079 | dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) | 
| Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 4080 | OS << '(' << CID << ')'; | 
| Daniel Dunbar | c575ce7 | 2009-10-19 01:21:19 +0000 | [diff] [blame] | 4081 | OS << ' ' << D->getSelector().getAsString() << ']'; | 
| Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 4082 | } | 
|  | 4083 |  | 
| Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4084 | void CGObjCMac::FinishModule() { | 
| Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4085 | EmitModuleInfo(); | 
|  | 4086 |  | 
| Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4087 | // Emit the dummy bodies for any protocols which were referenced but | 
|  | 4088 | // never defined. | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4089 | for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator | 
| Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4090 | I = Protocols.begin(), e = Protocols.end(); I != e; ++I) { | 
|  | 4091 | if (I->second->hasInitializer()) | 
| Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4092 | continue; | 
| Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4093 |  | 
| Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 4094 | llvm::Constant *Values[5]; | 
| Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4095 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); | 
| Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4096 | Values[1] = GetClassName(I->first); | 
| Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4097 | Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); | 
| Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4098 | Values[3] = Values[4] = | 
| Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4099 | llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); | 
| Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4100 | I->second->setLinkage(llvm::GlobalValue::InternalLinkage); | 
| Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 4101 | I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, | 
| Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4102 | Values)); | 
| Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4103 | CGM.AddUsedGlobal(I->second); | 
| Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4104 | } | 
|  | 4105 |  | 
| Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4106 | // Add assembler directives to add lazy undefined symbol references | 
|  | 4107 | // for classes which are referenced but not defined. This is | 
|  | 4108 | // important for correct linker interaction. | 
| Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4109 | // | 
|  | 4110 | // FIXME: It would be nice if we had an LLVM construct for this. | 
|  | 4111 | if (!LazySymbols.empty() || !DefinedSymbols.empty()) { | 
|  | 4112 | llvm::SmallString<256> Asm; | 
|  | 4113 | Asm += CGM.getModule().getModuleInlineAsm(); | 
|  | 4114 | if (!Asm.empty() && Asm.back() != '\n') | 
|  | 4115 | Asm += '\n'; | 
| Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4116 |  | 
| Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4117 | llvm::raw_svector_ostream OS(Asm); | 
| Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4118 | for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(), | 
|  | 4119 | e = DefinedSymbols.end(); I != e; ++I) | 
| Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 4120 | OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n" | 
|  | 4121 | << "\t.globl .objc_class_name_" << (*I)->getName() << "\n"; | 
| Chris Lattner | afd5eda | 2010-04-17 18:26:20 +0000 | [diff] [blame] | 4122 | for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(), | 
| Fariborz Jahanian | b9c5b3d | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 4123 | e = LazySymbols.end(); I != e; ++I) { | 
| Chris Lattner | afd5eda | 2010-04-17 18:26:20 +0000 | [diff] [blame] | 4124 | OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n"; | 
| Fariborz Jahanian | b9c5b3d | 2010-06-21 22:05:18 +0000 | [diff] [blame] | 4125 | } | 
|  | 4126 |  | 
|  | 4127 | for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) { | 
|  | 4128 | OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n" | 
|  | 4129 | << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n"; | 
|  | 4130 | } | 
| Chris Lattner | afd5eda | 2010-04-17 18:26:20 +0000 | [diff] [blame] | 4131 |  | 
| Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame] | 4132 | CGM.getModule().setModuleInlineAsm(OS.str()); | 
| Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 4133 | } | 
| Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4134 | } | 
|  | 4135 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4136 | CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm) | 
| Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4137 | : CGObjCCommonMac(cgm), | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4138 | ObjCTypes(cgm) { | 
| Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4139 | ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL; | 
| Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4140 | ObjCABI = 2; | 
|  | 4141 | } | 
|  | 4142 |  | 
| Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 4143 | /* *** */ | 
|  | 4144 |  | 
| Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4145 | ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm) | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4146 | : VMContext(cgm.getLLVMContext()), CGM(cgm) { | 
| Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4147 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); | 
|  | 4148 | ASTContext &Ctx = CGM.getContext(); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4149 |  | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4150 | ShortTy = Types.ConvertType(Ctx.ShortTy); | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4151 | IntTy = Types.ConvertType(Ctx.IntTy); | 
| Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4152 | LongTy = Types.ConvertType(Ctx.LongTy); | 
| Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 4153 | LongLongTy = Types.ConvertType(Ctx.LongLongTy); | 
| Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 4154 | Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4155 |  | 
| Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4156 | ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType()); | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4157 | PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy); | 
| Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4158 | SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType()); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4159 |  | 
| Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 4160 | // FIXME: It would be nice to unify this with the opaque type, so that the IR | 
|  | 4161 | // comes out a bit cleaner. | 
| Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 4162 | llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4163 | ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4164 |  | 
| Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4165 | // I'm not sure I like this. The implicit coordination is a bit | 
|  | 4166 | // gross. We should solve this in a reasonable fashion because this | 
|  | 4167 | // is a pretty common task (match some runtime data structure with | 
|  | 4168 | // an LLVM data structure). | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4169 |  | 
| Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4170 | // FIXME: This is leaked. | 
|  | 4171 | // FIXME: Merge with rewriter code? | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4172 |  | 
| Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4173 | // struct _objc_super { | 
|  | 4174 | //   id self; | 
|  | 4175 | //   Class cls; | 
|  | 4176 | // } | 
| Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4177 | RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct, | 
| Daniel Dunbar | daa3ac5 | 2010-04-29 16:29:11 +0000 | [diff] [blame] | 4178 | Ctx.getTranslationUnitDecl(), | 
| Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4179 | SourceLocation(), SourceLocation(), | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4180 | &Ctx.Idents.get("_objc_super")); | 
| Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4181 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, | 
| Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4182 | Ctx.getObjCIdType(), 0, 0, false, false)); | 
| Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4183 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, | 
| Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4184 | Ctx.getObjCClassType(), 0, 0, false, false)); | 
| Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4185 | RD->completeDefinition(); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4186 |  | 
| Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4187 | SuperCTy = Ctx.getTagDeclType(RD); | 
|  | 4188 | SuperPtrCTy = Ctx.getPointerType(SuperCTy); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4189 |  | 
| Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4190 | SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy)); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4191 | SuperPtrTy = llvm::PointerType::getUnqual(SuperTy); | 
|  | 4192 |  | 
| Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4193 | // struct _prop_t { | 
|  | 4194 | //   char *name; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4195 | //   char *attributes; | 
| Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4196 | // } | 
| Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4197 | PropertyTy = llvm::StructType::create("struct._prop_t", | 
|  | 4198 | Int8PtrTy, Int8PtrTy, NULL); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4199 |  | 
| Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4200 | // struct _prop_list_t { | 
| Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4201 | //   uint32_t entsize;      // sizeof(struct _prop_t) | 
| Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4202 | //   uint32_t count_of_properties; | 
| Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4203 | //   struct _prop_t prop_list[count_of_properties]; | 
| Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4204 | // } | 
| Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4205 | PropertyListTy = | 
| Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4206 | llvm::StructType::create("struct._prop_list_t", IntTy, IntTy, | 
|  | 4207 | llvm::ArrayType::get(PropertyTy, 0), NULL); | 
| Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4208 | // struct _prop_list_t * | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4209 | PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4210 |  | 
| Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4211 | // struct _objc_method { | 
|  | 4212 | //   SEL _cmd; | 
|  | 4213 | //   char *method_type; | 
|  | 4214 | //   char *_imp; | 
|  | 4215 | // } | 
| Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4216 | MethodTy = llvm::StructType::create("struct._objc_method", | 
|  | 4217 | SelectorPtrTy, Int8PtrTy, Int8PtrTy, | 
|  | 4218 | NULL); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4219 |  | 
| Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4220 | // struct _objc_cache * | 
| Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4221 | CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache"); | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4222 | CachePtrTy = llvm::PointerType::getUnqual(CacheTy); | 
| Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 4223 |  | 
| Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4224 | } | 
| Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 4225 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4226 | ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4227 | : ObjCCommonTypesHelper(cgm) { | 
| Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4228 | // struct _objc_method_description { | 
|  | 4229 | //   SEL name; | 
|  | 4230 | //   char *types; | 
|  | 4231 | // } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4232 | MethodDescriptionTy = | 
| Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4233 | llvm::StructType::create("struct._objc_method_description", | 
|  | 4234 | SelectorPtrTy, Int8PtrTy, NULL); | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4235 |  | 
| Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4236 | // struct _objc_method_description_list { | 
|  | 4237 | //   int count; | 
|  | 4238 | //   struct _objc_method_description[1]; | 
|  | 4239 | // } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4240 | MethodDescriptionListTy = | 
| Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4241 | llvm::StructType::create("struct._objc_method_description_list", | 
|  | 4242 | IntTy, | 
|  | 4243 | llvm::ArrayType::get(MethodDescriptionTy, 0),NULL); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4244 |  | 
| Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4245 | // struct _objc_method_description_list * | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4246 | MethodDescriptionListPtrTy = | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4247 | llvm::PointerType::getUnqual(MethodDescriptionListTy); | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4248 |  | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4249 | // Protocol description structures | 
|  | 4250 |  | 
| Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4251 | // struct _objc_protocol_extension { | 
|  | 4252 | //   uint32_t size;  // sizeof(struct _objc_protocol_extension) | 
|  | 4253 | //   struct _objc_method_description_list *optional_instance_methods; | 
|  | 4254 | //   struct _objc_method_description_list *optional_class_methods; | 
|  | 4255 | //   struct _objc_property_list *instance_properties; | 
|  | 4256 | // } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4257 | ProtocolExtensionTy = | 
| Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4258 | llvm::StructType::create("struct._objc_protocol_extension", | 
|  | 4259 | IntTy, MethodDescriptionListPtrTy, | 
|  | 4260 | MethodDescriptionListPtrTy, PropertyListPtrTy, | 
|  | 4261 | NULL); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4262 |  | 
| Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4263 | // struct _objc_protocol_extension * | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4264 | ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy); | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4265 |  | 
| Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 4266 | // Handle recursive construction of Protocol and ProtocolList types | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4267 |  | 
| Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4268 | ProtocolTy = | 
| Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4269 | llvm::StructType::create(VMContext, "struct._objc_protocol"); | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4270 |  | 
| Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4271 | ProtocolListTy = | 
| Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4272 | llvm::StructType::create(VMContext, "struct._objc_protocol_list"); | 
| Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4273 | ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy), | 
| Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4274 | LongTy, | 
| Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4275 | llvm::ArrayType::get(ProtocolTy, 0), | 
| Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4276 | NULL); | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4277 |  | 
| Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4278 | // struct _objc_protocol { | 
|  | 4279 | //   struct _objc_protocol_extension *isa; | 
|  | 4280 | //   char *protocol_name; | 
|  | 4281 | //   struct _objc_protocol **_objc_protocol_list; | 
|  | 4282 | //   struct _objc_method_description_list *instance_methods; | 
|  | 4283 | //   struct _objc_method_description_list *class_methods; | 
|  | 4284 | // } | 
| Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4285 | ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy, | 
|  | 4286 | llvm::PointerType::getUnqual(ProtocolListTy), | 
|  | 4287 | MethodDescriptionListPtrTy, | 
|  | 4288 | MethodDescriptionListPtrTy, | 
|  | 4289 | NULL); | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4290 |  | 
| Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4291 | // struct _objc_protocol_list * | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4292 | ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy); | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4293 |  | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4294 | ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4295 |  | 
|  | 4296 | // Class description structures | 
|  | 4297 |  | 
| Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4298 | // struct _objc_ivar { | 
|  | 4299 | //   char *ivar_name; | 
|  | 4300 | //   char *ivar_type; | 
|  | 4301 | //   int  ivar_offset; | 
|  | 4302 | // } | 
| Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4303 | IvarTy = llvm::StructType::create("struct._objc_ivar", | 
|  | 4304 | Int8PtrTy, Int8PtrTy, IntTy, NULL); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4305 |  | 
| Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4306 | // struct _objc_ivar_list * | 
| Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4307 | IvarListTy = | 
| Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4308 | llvm::StructType::create(VMContext, "struct._objc_ivar_list"); | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4309 | IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4310 |  | 
| Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4311 | // struct _objc_method_list * | 
| Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4312 | MethodListTy = | 
| Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4313 | llvm::StructType::create(VMContext, "struct._objc_method_list"); | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4314 | MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4315 |  | 
| Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4316 | // struct _objc_class_extension * | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4317 | ClassExtensionTy = | 
| Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4318 | llvm::StructType::create("struct._objc_class_extension", | 
|  | 4319 | IntTy, Int8PtrTy, PropertyListPtrTy, NULL); | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4320 | ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4321 |  | 
| Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4322 | ClassTy = llvm::StructType::create(VMContext, "struct._objc_class"); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4323 |  | 
| Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4324 | // struct _objc_class { | 
|  | 4325 | //   Class isa; | 
|  | 4326 | //   Class super_class; | 
|  | 4327 | //   char *name; | 
|  | 4328 | //   long version; | 
|  | 4329 | //   long info; | 
|  | 4330 | //   long instance_size; | 
|  | 4331 | //   struct _objc_ivar_list *ivars; | 
|  | 4332 | //   struct _objc_method_list *methods; | 
|  | 4333 | //   struct _objc_cache *cache; | 
|  | 4334 | //   struct _objc_protocol_list *protocols; | 
|  | 4335 | //   char *ivar_layout; | 
|  | 4336 | //   struct _objc_class_ext *ext; | 
|  | 4337 | // }; | 
| Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4338 | ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy), | 
|  | 4339 | llvm::PointerType::getUnqual(ClassTy), | 
|  | 4340 | Int8PtrTy, | 
|  | 4341 | LongTy, | 
|  | 4342 | LongTy, | 
|  | 4343 | LongTy, | 
|  | 4344 | IvarListPtrTy, | 
|  | 4345 | MethodListPtrTy, | 
|  | 4346 | CachePtrTy, | 
|  | 4347 | ProtocolListPtrTy, | 
|  | 4348 | Int8PtrTy, | 
|  | 4349 | ClassExtensionPtrTy, | 
|  | 4350 | NULL); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4351 |  | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4352 | ClassPtrTy = llvm::PointerType::getUnqual(ClassTy); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4353 |  | 
| Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4354 | // struct _objc_category { | 
|  | 4355 | //   char *category_name; | 
|  | 4356 | //   char *class_name; | 
|  | 4357 | //   struct _objc_method_list *instance_method; | 
|  | 4358 | //   struct _objc_method_list *class_method; | 
|  | 4359 | //   uint32_t size;  // sizeof(struct _objc_category) | 
|  | 4360 | //   struct _objc_property_list *instance_properties;// category's @property | 
|  | 4361 | // } | 
| Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4362 | CategoryTy = | 
| Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4363 | llvm::StructType::create("struct._objc_category", | 
|  | 4364 | Int8PtrTy, Int8PtrTy, MethodListPtrTy, | 
|  | 4365 | MethodListPtrTy, ProtocolListPtrTy, | 
|  | 4366 | IntTy, PropertyListPtrTy, NULL); | 
| Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 4367 |  | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4368 | // Global metadata structures | 
|  | 4369 |  | 
| Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 4370 | // struct _objc_symtab { | 
|  | 4371 | //   long sel_ref_cnt; | 
|  | 4372 | //   SEL *refs; | 
|  | 4373 | //   short cls_def_cnt; | 
|  | 4374 | //   short cat_def_cnt; | 
|  | 4375 | //   char *defs[cls_def_cnt + cat_def_cnt]; | 
|  | 4376 | // } | 
| Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4377 | SymtabTy = | 
| Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4378 | llvm::StructType::create("struct._objc_symtab", | 
|  | 4379 | LongTy, SelectorPtrTy, ShortTy, ShortTy, | 
|  | 4380 | llvm::ArrayType::get(Int8PtrTy, 0), NULL); | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4381 | SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy); | 
| Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 4382 |  | 
| Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 4383 | // struct _objc_module { | 
|  | 4384 | //   long version; | 
|  | 4385 | //   long size;   // sizeof(struct _objc_module) | 
|  | 4386 | //   char *name; | 
|  | 4387 | //   struct _objc_symtab* symtab; | 
|  | 4388 | //  } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4389 | ModuleTy = | 
| Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4390 | llvm::StructType::create("struct._objc_module", | 
|  | 4391 | LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL); | 
| Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 4392 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4393 |  | 
| Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 4394 | // FIXME: This is the size of the setjmp buffer and should be target | 
|  | 4395 | // specific. 18 is what's used on 32-bit X86. | 
| Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 4396 | uint64_t SetJmpBufferSize = 18; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4397 |  | 
| Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 4398 | // Exceptions | 
| Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4399 | llvm::Type *StackPtrTy = llvm::ArrayType::get( | 
| Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 4400 | llvm::Type::getInt8PtrTy(VMContext), 4); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4401 |  | 
|  | 4402 | ExceptionDataTy = | 
| Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4403 | llvm::StructType::create("struct._objc_exception_data", | 
| Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4404 | llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext), | 
|  | 4405 | SetJmpBufferSize), | 
|  | 4406 | StackPtrTy, NULL); | 
| Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 4407 |  | 
| Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 4408 | } | 
|  | 4409 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4410 | ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm) | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4411 | : ObjCCommonTypesHelper(cgm) { | 
| Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4412 | // struct _method_list_t { | 
|  | 4413 | //   uint32_t entsize;  // sizeof(struct _objc_method) | 
|  | 4414 | //   uint32_t method_count; | 
|  | 4415 | //   struct _objc_method method_list[method_count]; | 
|  | 4416 | // } | 
| Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4417 | MethodListnfABITy = | 
| Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4418 | llvm::StructType::create("struct.__method_list_t", IntTy, IntTy, | 
|  | 4419 | llvm::ArrayType::get(MethodTy, 0), NULL); | 
| Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4420 | // struct method_list_t * | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4421 | MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4422 |  | 
| Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4423 | // struct _protocol_t { | 
|  | 4424 | //   id isa;  // NULL | 
|  | 4425 | //   const char * const protocol_name; | 
| Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4426 | //   const struct _protocol_list_t * protocol_list; // super protocols | 
| Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4427 | //   const struct method_list_t * const instance_methods; | 
|  | 4428 | //   const struct method_list_t * const class_methods; | 
|  | 4429 | //   const struct method_list_t *optionalInstanceMethods; | 
|  | 4430 | //   const struct method_list_t *optionalClassMethods; | 
| Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4431 | //   const struct _prop_list_t * properties; | 
| Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4432 | //   const uint32_t size;  // sizeof(struct _protocol_t) | 
|  | 4433 | //   const uint32_t flags;  // = 0 | 
|  | 4434 | // } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4435 |  | 
| Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4436 | // Holder for struct _protocol_list_t * | 
| Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4437 | ProtocolListnfABITy = | 
| Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4438 | llvm::StructType::create(VMContext, "struct._objc_protocol_list"); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4439 |  | 
| Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4440 | ProtocolnfABITy = | 
| Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4441 | llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy, | 
|  | 4442 | llvm::PointerType::getUnqual(ProtocolListnfABITy), | 
|  | 4443 | MethodListnfABIPtrTy, MethodListnfABIPtrTy, | 
|  | 4444 | MethodListnfABIPtrTy, MethodListnfABIPtrTy, | 
|  | 4445 | PropertyListPtrTy, IntTy, IntTy, NULL); | 
| Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4446 |  | 
|  | 4447 | // struct _protocol_t* | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4448 | ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4449 |  | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4450 | // struct _protocol_list_t { | 
| Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4451 | //   long protocol_count;   // Note, this is 32/64 bit | 
| Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4452 | //   struct _protocol_t *[protocol_count]; | 
| Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4453 | // } | 
| Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4454 | ProtocolListnfABITy->setBody(LongTy, | 
|  | 4455 | llvm::ArrayType::get(ProtocolnfABIPtrTy, 0), | 
|  | 4456 | NULL); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4457 |  | 
| Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4458 | // struct _objc_protocol_list* | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4459 | ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4460 |  | 
| Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4461 | // struct _ivar_t { | 
|  | 4462 | //   unsigned long int *offset;  // pointer to ivar offset location | 
|  | 4463 | //   char *name; | 
|  | 4464 | //   char *type; | 
|  | 4465 | //   uint32_t alignment; | 
|  | 4466 | //   uint32_t size; | 
|  | 4467 | // } | 
| Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4468 | IvarnfABITy = | 
| Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4469 | llvm::StructType::create("struct._ivar_t", | 
|  | 4470 | llvm::PointerType::getUnqual(LongTy), | 
|  | 4471 | Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4472 |  | 
| Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4473 | // struct _ivar_list_t { | 
|  | 4474 | //   uint32 entsize;  // sizeof(struct _ivar_t) | 
|  | 4475 | //   uint32 count; | 
|  | 4476 | //   struct _iver_t list[count]; | 
|  | 4477 | // } | 
| Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4478 | IvarListnfABITy = | 
| Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4479 | llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy, | 
|  | 4480 | llvm::ArrayType::get(IvarnfABITy, 0), NULL); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4481 |  | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4482 | IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4483 |  | 
| Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4484 | // struct _class_ro_t { | 
| Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4485 | //   uint32_t const flags; | 
|  | 4486 | //   uint32_t const instanceStart; | 
|  | 4487 | //   uint32_t const instanceSize; | 
|  | 4488 | //   uint32_t const reserved;  // only when building for 64bit targets | 
|  | 4489 | //   const uint8_t * const ivarLayout; | 
|  | 4490 | //   const char *const name; | 
|  | 4491 | //   const struct _method_list_t * const baseMethods; | 
|  | 4492 | //   const struct _objc_protocol_list *const baseProtocols; | 
|  | 4493 | //   const struct _ivar_list_t *const ivars; | 
|  | 4494 | //   const uint8_t * const weakIvarLayout; | 
|  | 4495 | //   const struct _prop_list_t * const properties; | 
|  | 4496 | // } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4497 |  | 
| Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4498 | // FIXME. Add 'reserved' field in 64bit abi mode! | 
| Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4499 | ClassRonfABITy = llvm::StructType::create("struct._class_ro_t", | 
|  | 4500 | IntTy, IntTy, IntTy, Int8PtrTy, | 
|  | 4501 | Int8PtrTy, MethodListnfABIPtrTy, | 
|  | 4502 | ProtocolListnfABIPtrTy, | 
|  | 4503 | IvarListnfABIPtrTy, | 
|  | 4504 | Int8PtrTy, PropertyListPtrTy, NULL); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4505 |  | 
| Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4506 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) | 
| Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4507 | llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy }; | 
| John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 4508 | ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false) | 
|  | 4509 | ->getPointerTo(); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4510 |  | 
| Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4511 | // struct _class_t { | 
|  | 4512 | //   struct _class_t *isa; | 
|  | 4513 | //   struct _class_t * const superclass; | 
|  | 4514 | //   void *cache; | 
|  | 4515 | //   IMP *vtable; | 
|  | 4516 | //   struct class_ro_t *ro; | 
|  | 4517 | // } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4518 |  | 
| Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4519 | ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t"); | 
| Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4520 | ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy), | 
|  | 4521 | llvm::PointerType::getUnqual(ClassnfABITy), | 
|  | 4522 | CachePtrTy, | 
|  | 4523 | llvm::PointerType::getUnqual(ImpnfABITy), | 
|  | 4524 | llvm::PointerType::getUnqual(ClassRonfABITy), | 
|  | 4525 | NULL); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4526 |  | 
| Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4527 | // LLVM for struct _class_t * | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4528 | ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4529 |  | 
| Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 4530 | // struct _category_t { | 
|  | 4531 | //   const char * const name; | 
|  | 4532 | //   struct _class_t *const cls; | 
|  | 4533 | //   const struct _method_list_t * const instance_methods; | 
|  | 4534 | //   const struct _method_list_t * const class_methods; | 
|  | 4535 | //   const struct _protocol_list_t * const protocols; | 
|  | 4536 | //   const struct _prop_list_t * const properties; | 
| Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 4537 | // } | 
| Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4538 | CategorynfABITy = llvm::StructType::create("struct._category_t", | 
|  | 4539 | Int8PtrTy, ClassnfABIPtrTy, | 
|  | 4540 | MethodListnfABIPtrTy, | 
|  | 4541 | MethodListnfABIPtrTy, | 
|  | 4542 | ProtocolListnfABIPtrTy, | 
|  | 4543 | PropertyListPtrTy, | 
|  | 4544 | NULL); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4545 |  | 
| Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4546 | // New types for nonfragile abi messaging. | 
| Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4547 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); | 
|  | 4548 | ASTContext &Ctx = CGM.getContext(); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4549 |  | 
| Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4550 | // MessageRefTy - LLVM for: | 
|  | 4551 | // struct _message_ref_t { | 
|  | 4552 | //   IMP messenger; | 
|  | 4553 | //   SEL name; | 
|  | 4554 | // }; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4555 |  | 
| Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4556 | // First the clang type for struct _message_ref_t | 
| Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 4557 | RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct, | 
| Daniel Dunbar | daa3ac5 | 2010-04-29 16:29:11 +0000 | [diff] [blame] | 4558 | Ctx.getTranslationUnitDecl(), | 
| Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4559 | SourceLocation(), SourceLocation(), | 
| Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4560 | &Ctx.Idents.get("_message_ref_t")); | 
| Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4561 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, | 
| Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4562 | Ctx.VoidPtrTy, 0, 0, false, false)); | 
| Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4563 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0, | 
| Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4564 | Ctx.getObjCSelType(), 0, 0, false, false)); | 
| Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4565 | RD->completeDefinition(); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4566 |  | 
| Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4567 | MessageRefCTy = Ctx.getTagDeclType(RD); | 
|  | 4568 | MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy); | 
|  | 4569 | MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy)); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4570 |  | 
| Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4571 | // MessageRefPtrTy - LLVM for struct _message_ref_t* | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4572 | MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4573 |  | 
| Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4574 | // SuperMessageRefTy - LLVM for: | 
|  | 4575 | // struct _super_message_ref_t { | 
|  | 4576 | //   SUPER_IMP messenger; | 
|  | 4577 | //   SEL name; | 
|  | 4578 | // }; | 
| Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4579 | SuperMessageRefTy = | 
| Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4580 | llvm::StructType::create("struct._super_message_ref_t", | 
|  | 4581 | ImpnfABITy, SelectorPtrTy, NULL); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4582 |  | 
| Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4583 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4584 | SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy); | 
| Fariborz Jahanian | 9d96bce | 2011-06-22 20:21:51 +0000 | [diff] [blame] | 4585 |  | 
| Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 4586 |  | 
|  | 4587 | // struct objc_typeinfo { | 
|  | 4588 | //   const void** vtable; // objc_ehtype_vtable + 2 | 
|  | 4589 | //   const char*  name;    // c++ typeinfo string | 
|  | 4590 | //   Class        cls; | 
|  | 4591 | // }; | 
| Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 4592 | EHTypeTy = | 
| Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 4593 | llvm::StructType::create("struct._objc_typeinfo", | 
|  | 4594 | llvm::PointerType::getUnqual(Int8PtrTy), | 
|  | 4595 | Int8PtrTy, ClassnfABIPtrTy, NULL); | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4596 | EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy); | 
| Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 4597 | } | 
|  | 4598 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4599 | llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() { | 
| Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4600 | FinishNonFragileABIModule(); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4601 |  | 
| Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4602 | return NULL; | 
|  | 4603 | } | 
|  | 4604 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4605 | void CGObjCNonFragileABIMac::AddModuleClassList(const | 
|  | 4606 | std::vector<llvm::GlobalValue*> | 
|  | 4607 | &Container, | 
| Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4608 | const char *SymbolName, | 
|  | 4609 | const char *SectionName) { | 
|  | 4610 | unsigned NumClasses = Container.size(); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4611 |  | 
| Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4612 | if (!NumClasses) | 
|  | 4613 | return; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4614 |  | 
| Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4615 | std::vector<llvm::Constant*> Symbols(NumClasses); | 
|  | 4616 | for (unsigned i=0; i<NumClasses; i++) | 
| Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4617 | Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i], | 
| Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4618 | ObjCTypes.Int8PtrTy); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4619 | llvm::Constant* Init = | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4620 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, | 
| Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4621 | NumClasses), | 
|  | 4622 | Symbols); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4623 |  | 
| Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4624 | llvm::GlobalVariable *GV = | 
| Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4625 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, | 
| Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4626 | llvm::GlobalValue::InternalLinkage, | 
|  | 4627 | Init, | 
| Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4628 | SymbolName); | 
| Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 4629 | GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType())); | 
| Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4630 | GV->setSection(SectionName); | 
| Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4631 | CGM.AddUsedGlobal(GV); | 
| Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4632 | } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4633 |  | 
| Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4634 | void CGObjCNonFragileABIMac::FinishNonFragileABIModule() { | 
|  | 4635 | // nonfragile abi has no module definition. | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4636 |  | 
| Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4637 | // Build list of all implemented class addresses in array | 
| Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4638 | // L_OBJC_LABEL_CLASS_$. | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4639 | AddModuleClassList(DefinedClasses, | 
| Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4640 | "\01L_OBJC_LABEL_CLASS_$", | 
|  | 4641 | "__DATA, __objc_classlist, regular, no_dead_strip"); | 
| Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4642 |  | 
| Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4643 | for (unsigned i = 0; i < DefinedClasses.size(); i++) { | 
|  | 4644 | llvm::GlobalValue *IMPLGV = DefinedClasses[i]; | 
|  | 4645 | if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage) | 
|  | 4646 | continue; | 
|  | 4647 | IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage); | 
| Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4648 | } | 
|  | 4649 |  | 
| Fariborz Jahanian | 0e93d25 | 2009-12-01 18:25:24 +0000 | [diff] [blame] | 4650 | for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) { | 
|  | 4651 | llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i]; | 
|  | 4652 | if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage) | 
|  | 4653 | continue; | 
|  | 4654 | IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage); | 
|  | 4655 | } | 
| Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4656 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4657 | AddModuleClassList(DefinedNonLazyClasses, | 
| Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4658 | "\01L_OBJC_LABEL_NONLAZY_CLASS_$", | 
|  | 4659 | "__DATA, __objc_nlclslist, regular, no_dead_strip"); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4660 |  | 
| Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4661 | // Build list of all implemented category addresses in array | 
|  | 4662 | // L_OBJC_LABEL_CATEGORY_$. | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4663 | AddModuleClassList(DefinedCategories, | 
| Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4664 | "\01L_OBJC_LABEL_CATEGORY_$", | 
|  | 4665 | "__DATA, __objc_catlist, regular, no_dead_strip"); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4666 | AddModuleClassList(DefinedNonLazyCategories, | 
| Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4667 | "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$", | 
|  | 4668 | "__DATA, __objc_nlcatlist, regular, no_dead_strip"); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4669 |  | 
| Daniel Dunbar | fce176b | 2010-04-25 20:39:01 +0000 | [diff] [blame] | 4670 | EmitImageInfo(); | 
| Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4671 | } | 
|  | 4672 |  | 
| John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4673 | /// isVTableDispatchedSelector - Returns true if SEL is not in the list of | 
|  | 4674 | /// VTableDispatchMethods; false otherwise. What this means is that | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4675 | /// except for the 19 selectors in the list, we generate 32bit-style | 
| Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4676 | /// message dispatch call for all the rest. | 
| John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4677 | bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) { | 
|  | 4678 | // At various points we've experimented with using vtable-based | 
|  | 4679 | // dispatch for all methods. | 
| Daniel Dunbar | f643b9b | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 4680 | switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) { | 
|  | 4681 | default: | 
| John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4682 | llvm_unreachable("Invalid dispatch method!"); | 
| Daniel Dunbar | f643b9b | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 4683 | case CodeGenOptions::Legacy: | 
| Fariborz Jahanian | 776dbf9 | 2010-04-19 17:53:30 +0000 | [diff] [blame] | 4684 | return false; | 
| John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4685 | case CodeGenOptions::NonLegacy: | 
|  | 4686 | return true; | 
| Daniel Dunbar | f643b9b | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 4687 | case CodeGenOptions::Mixed: | 
|  | 4688 | break; | 
|  | 4689 | } | 
|  | 4690 |  | 
|  | 4691 | // If so, see whether this selector is in the white-list of things which must | 
|  | 4692 | // 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] | 4693 | if (VTableDispatchMethods.empty()) { | 
|  | 4694 | VTableDispatchMethods.insert(GetNullarySelector("alloc")); | 
|  | 4695 | VTableDispatchMethods.insert(GetNullarySelector("class")); | 
|  | 4696 | VTableDispatchMethods.insert(GetNullarySelector("self")); | 
|  | 4697 | VTableDispatchMethods.insert(GetNullarySelector("isFlipped")); | 
|  | 4698 | VTableDispatchMethods.insert(GetNullarySelector("length")); | 
|  | 4699 | VTableDispatchMethods.insert(GetNullarySelector("count")); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4700 |  | 
| John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4701 | // These are vtable-based if GC is disabled. | 
|  | 4702 | // Optimistically use vtable dispatch for hybrid compiles. | 
| Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 4703 | if (CGM.getLangOptions().getGC() != LangOptions::GCOnly) { | 
| John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4704 | VTableDispatchMethods.insert(GetNullarySelector("retain")); | 
|  | 4705 | VTableDispatchMethods.insert(GetNullarySelector("release")); | 
|  | 4706 | VTableDispatchMethods.insert(GetNullarySelector("autorelease")); | 
|  | 4707 | } | 
|  | 4708 |  | 
|  | 4709 | VTableDispatchMethods.insert(GetUnarySelector("allocWithZone")); | 
|  | 4710 | VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass")); | 
|  | 4711 | VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector")); | 
|  | 4712 | VTableDispatchMethods.insert(GetUnarySelector("objectForKey")); | 
|  | 4713 | VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex")); | 
|  | 4714 | VTableDispatchMethods.insert(GetUnarySelector("isEqualToString")); | 
|  | 4715 | VTableDispatchMethods.insert(GetUnarySelector("isEqual")); | 
|  | 4716 |  | 
|  | 4717 | // These are vtable-based if GC is enabled. | 
|  | 4718 | // Optimistically use vtable dispatch for hybrid compiles. | 
| Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 4719 | if (CGM.getLangOptions().getGC() != LangOptions::NonGC) { | 
| John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4720 | VTableDispatchMethods.insert(GetNullarySelector("hash")); | 
|  | 4721 | VTableDispatchMethods.insert(GetUnarySelector("addObject")); | 
|  | 4722 |  | 
|  | 4723 | // "countByEnumeratingWithState:objects:count" | 
|  | 4724 | IdentifierInfo *KeyIdents[] = { | 
|  | 4725 | &CGM.getContext().Idents.get("countByEnumeratingWithState"), | 
|  | 4726 | &CGM.getContext().Idents.get("objects"), | 
|  | 4727 | &CGM.getContext().Idents.get("count") | 
|  | 4728 | }; | 
|  | 4729 | VTableDispatchMethods.insert( | 
|  | 4730 | CGM.getContext().Selectors.getSelector(3, KeyIdents)); | 
|  | 4731 | } | 
| Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4732 | } | 
| Daniel Dunbar | f643b9b | 2010-04-24 17:56:46 +0000 | [diff] [blame] | 4733 |  | 
| John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 4734 | return VTableDispatchMethods.count(Sel); | 
| Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4735 | } | 
|  | 4736 |  | 
| Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4737 | // Metadata flags | 
|  | 4738 | enum MetaDataDlags { | 
|  | 4739 | CLS = 0x0, | 
|  | 4740 | CLS_META = 0x1, | 
|  | 4741 | CLS_ROOT = 0x2, | 
| Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4742 | OBJC2_CLS_HIDDEN = 0x10, | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4743 | CLS_EXCEPTION = 0x20, | 
|  | 4744 |  | 
|  | 4745 | /// (Obsolete) ARC-specific: this class has a .release_ivars method | 
|  | 4746 | CLS_HAS_IVAR_RELEASER = 0x40, | 
|  | 4747 | /// class was compiled with -fobjc-arr | 
|  | 4748 | CLS_COMPILED_BY_ARC = 0x80  // (1<<7) | 
| Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4749 | }; | 
|  | 4750 | /// BuildClassRoTInitializer - generate meta-data for: | 
|  | 4751 | /// struct _class_ro_t { | 
|  | 4752 | ///   uint32_t const flags; | 
|  | 4753 | ///   uint32_t const instanceStart; | 
|  | 4754 | ///   uint32_t const instanceSize; | 
|  | 4755 | ///   uint32_t const reserved;  // only when building for 64bit targets | 
|  | 4756 | ///   const uint8_t * const ivarLayout; | 
|  | 4757 | ///   const char *const name; | 
|  | 4758 | ///   const struct _method_list_t * const baseMethods; | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4759 | ///   const struct _protocol_list_t *const baseProtocols; | 
| Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4760 | ///   const struct _ivar_list_t *const ivars; | 
|  | 4761 | ///   const uint8_t * const weakIvarLayout; | 
|  | 4762 | ///   const struct _prop_list_t * const properties; | 
|  | 4763 | /// } | 
|  | 4764 | /// | 
|  | 4765 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer( | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4766 | unsigned flags, | 
|  | 4767 | unsigned InstanceStart, | 
|  | 4768 | unsigned InstanceSize, | 
|  | 4769 | const ObjCImplementationDecl *ID) { | 
| Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4770 | std::string ClassName = ID->getNameAsString(); | 
| Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 4771 | llvm::Constant *Values[10]; // 11 for 64bit targets! | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4772 |  | 
|  | 4773 | if (CGM.getLangOptions().ObjCAutoRefCount) | 
|  | 4774 | flags |= CLS_COMPILED_BY_ARC; | 
|  | 4775 |  | 
| Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4776 | Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); | 
|  | 4777 | Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart); | 
|  | 4778 | Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize); | 
| Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4779 | // FIXME. For 64bit targets add 0 here. | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4780 | Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) | 
|  | 4781 | : BuildIvarLayout(ID, true); | 
| Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4782 | Values[ 4] = GetClassName(ID->getIdentifier()); | 
| Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4783 | // const struct _method_list_t * const baseMethods; | 
|  | 4784 | std::vector<llvm::Constant*> Methods; | 
|  | 4785 | std::string MethodListName("\01l_OBJC_$_"); | 
|  | 4786 | if (flags & CLS_META) { | 
|  | 4787 | MethodListName += "CLASS_METHODS_" + ID->getNameAsString(); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4788 | for (ObjCImplementationDecl::classmeth_iterator | 
| Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4789 | i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) { | 
| Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4790 | // Class methods should always be defined. | 
|  | 4791 | Methods.push_back(GetMethodConstant(*i)); | 
|  | 4792 | } | 
|  | 4793 | } else { | 
|  | 4794 | MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString(); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4795 | for (ObjCImplementationDecl::instmeth_iterator | 
| Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4796 | i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) { | 
| Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4797 | // Instance methods should always be defined. | 
|  | 4798 | Methods.push_back(GetMethodConstant(*i)); | 
|  | 4799 | } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4800 | for (ObjCImplementationDecl::propimpl_iterator | 
| Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4801 | i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) { | 
| Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4802 | ObjCPropertyImplDecl *PID = *i; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4803 |  | 
| Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4804 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){ | 
|  | 4805 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4806 |  | 
| Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4807 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) | 
|  | 4808 | if (llvm::Constant *C = GetMethodConstant(MD)) | 
|  | 4809 | Methods.push_back(C); | 
|  | 4810 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) | 
|  | 4811 | if (llvm::Constant *C = GetMethodConstant(MD)) | 
|  | 4812 | Methods.push_back(C); | 
|  | 4813 | } | 
|  | 4814 | } | 
| Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4815 | } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4816 | Values[ 5] = EmitMethodList(MethodListName, | 
|  | 4817 | "__DATA, __objc_const", Methods); | 
|  | 4818 |  | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4819 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); | 
|  | 4820 | assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer"); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4821 | Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_" | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4822 | + OID->getName(), | 
| Ted Kremenek | 53b9441 | 2010-09-01 01:21:15 +0000 | [diff] [blame] | 4823 | OID->all_referenced_protocol_begin(), | 
|  | 4824 | OID->all_referenced_protocol_end()); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4825 |  | 
| Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4826 | if (flags & CLS_META) | 
| Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4827 | Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); | 
| Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4828 | else | 
|  | 4829 | Values[ 7] = EmitIvarList(ID); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4830 | Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) | 
|  | 4831 | : BuildIvarLayout(ID, false); | 
| Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4832 | if (flags & CLS_META) | 
| Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4833 | Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); | 
| Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4834 | else | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 4835 | Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(), | 
|  | 4836 | ID, ID->getClassInterface(), ObjCTypes); | 
| Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 4837 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy, | 
| Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4838 | Values); | 
|  | 4839 | llvm::GlobalVariable *CLASS_RO_GV = | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4840 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false, | 
|  | 4841 | llvm::GlobalValue::InternalLinkage, | 
|  | 4842 | Init, | 
|  | 4843 | (flags & CLS_META) ? | 
|  | 4844 | std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName : | 
|  | 4845 | std::string("\01l_OBJC_CLASS_RO_$_")+ClassName); | 
| Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4846 | CLASS_RO_GV->setAlignment( | 
| Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 4847 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy)); | 
| Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4848 | CLASS_RO_GV->setSection("__DATA, __objc_const"); | 
| Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4849 | return CLASS_RO_GV; | 
| Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4850 |  | 
| Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4851 | } | 
|  | 4852 |  | 
|  | 4853 | /// BuildClassMetaData - This routine defines that to-level meta-data | 
|  | 4854 | /// for the given ClassName for: | 
|  | 4855 | /// struct _class_t { | 
|  | 4856 | ///   struct _class_t *isa; | 
|  | 4857 | ///   struct _class_t * const superclass; | 
|  | 4858 | ///   void *cache; | 
|  | 4859 | ///   IMP *vtable; | 
|  | 4860 | ///   struct class_ro_t *ro; | 
|  | 4861 | /// } | 
|  | 4862 | /// | 
| Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4863 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData( | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4864 | std::string &ClassName, | 
|  | 4865 | llvm::Constant *IsAGV, | 
|  | 4866 | llvm::Constant *SuperClassGV, | 
|  | 4867 | llvm::Constant *ClassRoGV, | 
|  | 4868 | bool HiddenVisibility) { | 
| Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 4869 | llvm::Constant *Values[] = { | 
|  | 4870 | IsAGV, | 
|  | 4871 | SuperClassGV, | 
|  | 4872 | ObjCEmptyCacheVar,  // &ObjCEmptyCacheVar | 
|  | 4873 | ObjCEmptyVtableVar, // &ObjCEmptyVtableVar | 
|  | 4874 | ClassRoGV           // &CLASS_RO_GV | 
|  | 4875 | }; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4876 | if (!Values[1]) | 
|  | 4877 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4878 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy, | 
| Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4879 | Values); | 
| Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4880 | llvm::GlobalVariable *GV = GetClassGlobal(ClassName); | 
|  | 4881 | GV->setInitializer(Init); | 
| Fariborz Jahanian | dd0db2a | 2009-01-31 01:07:39 +0000 | [diff] [blame] | 4882 | GV->setSection("__DATA, __objc_data"); | 
| Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4883 | GV->setAlignment( | 
| Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 4884 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy)); | 
| Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4885 | if (HiddenVisibility) | 
|  | 4886 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); | 
| Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4887 | return GV; | 
| Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4888 | } | 
|  | 4889 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4890 | bool | 
| Fariborz Jahanian | ecfbdcb | 2009-05-21 01:03:45 +0000 | [diff] [blame] | 4891 | CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const { | 
| Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4892 | return OD->getClassMethod(GetNullarySelector("load")) != 0; | 
| Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4893 | } | 
|  | 4894 |  | 
| Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 4895 | void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID, | 
| Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4896 | uint32_t &InstanceStart, | 
|  | 4897 | uint32_t &InstanceSize) { | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4898 | const ASTRecordLayout &RL = | 
| Daniel Dunbar | b4c79e0 | 2009-05-04 21:26:30 +0000 | [diff] [blame] | 4899 | CGM.getContext().getASTObjCImplementationLayout(OID); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4900 |  | 
| Daniel Dunbar | 6e8575b | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 4901 | // InstanceSize is really instance end. | 
| Ken Dyck | ec29903 | 2011-02-11 02:20:09 +0000 | [diff] [blame] | 4902 | InstanceSize = RL.getDataSize().getQuantity(); | 
| Daniel Dunbar | 6e8575b | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 4903 |  | 
|  | 4904 | // If there are no fields, the start is the same as the end. | 
|  | 4905 | if (!RL.getFieldCount()) | 
|  | 4906 | InstanceStart = InstanceSize; | 
|  | 4907 | else | 
| Ken Dyck | fb67ccd | 2011-04-14 00:43:09 +0000 | [diff] [blame] | 4908 | InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth(); | 
| Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4909 | } | 
|  | 4910 |  | 
| Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4911 | void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) { | 
|  | 4912 | std::string ClassName = ID->getNameAsString(); | 
|  | 4913 | if (!ObjCEmptyCacheVar) { | 
|  | 4914 | ObjCEmptyCacheVar = new llvm::GlobalVariable( | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4915 | CGM.getModule(), | 
|  | 4916 | ObjCTypes.CacheTy, | 
|  | 4917 | false, | 
|  | 4918 | llvm::GlobalValue::ExternalLinkage, | 
|  | 4919 | 0, | 
|  | 4920 | "_objc_empty_cache"); | 
|  | 4921 |  | 
| Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4922 | ObjCEmptyVtableVar = new llvm::GlobalVariable( | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4923 | CGM.getModule(), | 
|  | 4924 | ObjCTypes.ImpnfABITy, | 
|  | 4925 | false, | 
|  | 4926 | llvm::GlobalValue::ExternalLinkage, | 
|  | 4927 | 0, | 
|  | 4928 | "_objc_empty_vtable"); | 
| Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4929 | } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4930 | assert(ID->getClassInterface() && | 
| Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4931 | "CGObjCNonFragileABIMac::GenerateClass - class is 0"); | 
| Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 4932 | // FIXME: Is this correct (that meta class size is never computed)? | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4933 | uint32_t InstanceStart = | 
| Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4934 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy); | 
| Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4935 | uint32_t InstanceSize = InstanceStart; | 
|  | 4936 | uint32_t flags = CLS_META; | 
| Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4937 | std::string ObjCMetaClassName(getMetaclassSymbolPrefix()); | 
|  | 4938 | std::string ObjCClassName(getClassSymbolPrefix()); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4939 |  | 
| Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4940 | llvm::GlobalVariable *SuperClassGV, *IsAGV; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4941 |  | 
|  | 4942 | bool classIsHidden = | 
| John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 4943 | ID->getClassInterface()->getVisibility() == HiddenVisibility; | 
| Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4944 | if (classIsHidden) | 
| Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4945 | flags |= OBJC2_CLS_HIDDEN; | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4946 | if (ID->hasCXXStructors()) | 
| Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 4947 | flags |= eClassFlags_ABI2_HasCXXStructors; | 
| Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4948 | if (!ID->getClassInterface()->getSuperClass()) { | 
| Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4949 | // class is root | 
|  | 4950 | flags |= CLS_ROOT; | 
| Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4951 | SuperClassGV = GetClassGlobal(ObjCClassName + ClassName); | 
| Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4952 | IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName); | 
| Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4953 | } else { | 
| Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4954 | // Has a root. Current class is not a root. | 
| Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4955 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); | 
|  | 4956 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) | 
|  | 4957 | Root = Super; | 
| Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4958 | IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString()); | 
| Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 4959 | if (Root->isWeakImported()) | 
| Fariborz Jahanian | 0e93d25 | 2009-12-01 18:25:24 +0000 | [diff] [blame] | 4960 | IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); | 
| Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4961 | // work on super class metadata symbol. | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4962 | std::string SuperClassName = | 
| Fariborz Jahanian | 0e93d25 | 2009-12-01 18:25:24 +0000 | [diff] [blame] | 4963 | ObjCMetaClassName + | 
|  | 4964 | ID->getClassInterface()->getSuperClass()->getNameAsString(); | 
| Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4965 | SuperClassGV = GetClassGlobal(SuperClassName); | 
| Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 4966 | if (ID->getClassInterface()->getSuperClass()->isWeakImported()) | 
| Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4967 | SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); | 
| Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4968 | } | 
|  | 4969 | llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags, | 
|  | 4970 | InstanceStart, | 
|  | 4971 | InstanceSize,ID); | 
| Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4972 | std::string TClassName = ObjCMetaClassName + ClassName; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4973 | llvm::GlobalVariable *MetaTClass = | 
| Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4974 | BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV, | 
|  | 4975 | classIsHidden); | 
| Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4976 | DefinedMetaClasses.push_back(MetaTClass); | 
| Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4977 |  | 
| Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4978 | // Metadata for the class | 
|  | 4979 | flags = CLS; | 
| Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4980 | if (classIsHidden) | 
| Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4981 | flags |= OBJC2_CLS_HIDDEN; | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4982 | if (ID->hasCXXStructors()) | 
| Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 4983 | flags |= eClassFlags_ABI2_HasCXXStructors; | 
| Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4984 |  | 
| Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 4985 | if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface())) | 
| Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4986 | flags |= CLS_EXCEPTION; | 
|  | 4987 |  | 
| Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4988 | if (!ID->getClassInterface()->getSuperClass()) { | 
| Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4989 | flags |= CLS_ROOT; | 
|  | 4990 | SuperClassGV = 0; | 
| Chris Lattner | b7b58b1 | 2009-04-19 06:02:28 +0000 | [diff] [blame] | 4991 | } else { | 
| Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4992 | // Has a root. Current class is not a root. | 
| Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4993 | std::string RootClassName = | 
| Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4994 | ID->getClassInterface()->getSuperClass()->getNameAsString(); | 
| Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4995 | SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName); | 
| Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 4996 | if (ID->getClassInterface()->getSuperClass()->isWeakImported()) | 
| Fariborz Jahanian | a03d0dd | 2009-11-17 21:37:35 +0000 | [diff] [blame] | 4997 | SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); | 
| Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4998 | } | 
| Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 4999 | GetClassSizeInfo(ID, InstanceStart, InstanceSize); | 
| Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5000 | CLASS_RO_GV = BuildClassRoTInitializer(flags, | 
| Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 5001 | InstanceStart, | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5002 | InstanceSize, | 
| Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 5003 | ID); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5004 |  | 
| Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 5005 | TClassName = ObjCClassName + ClassName; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5006 | llvm::GlobalVariable *ClassMD = | 
| Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 5007 | BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV, | 
|  | 5008 | classIsHidden); | 
| Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 5009 | DefinedClasses.push_back(ClassMD); | 
| Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5010 |  | 
| Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5011 | // Determine if this class is also "non-lazy". | 
|  | 5012 | if (ImplementationIsNonLazy(ID)) | 
|  | 5013 | DefinedNonLazyClasses.push_back(ClassMD); | 
|  | 5014 |  | 
| Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5015 | // Force the definition of the EHType if necessary. | 
|  | 5016 | if (flags & CLS_EXCEPTION) | 
|  | 5017 | GetInterfaceEHType(ID->getClassInterface(), true); | 
| Fariborz Jahanian | 64089ce | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 5018 | // Make sure method definition entries are all clear for next implementation. | 
|  | 5019 | MethodDefinitions.clear(); | 
| Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5020 | } | 
|  | 5021 |  | 
| Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5022 | /// GenerateProtocolRef - This routine is called to generate code for | 
|  | 5023 | /// a protocol reference expression; as in: | 
|  | 5024 | /// @code | 
|  | 5025 | ///   @protocol(Proto1); | 
|  | 5026 | /// @endcode | 
|  | 5027 | /// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1 | 
|  | 5028 | /// which will hold address of the protocol meta-data. | 
|  | 5029 | /// | 
|  | 5030 | llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder, | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5031 | const ObjCProtocolDecl *PD) { | 
|  | 5032 |  | 
| Fariborz Jahanian | 960cd06 | 2009-04-10 18:47:34 +0000 | [diff] [blame] | 5033 | // This routine is called for @protocol only. So, we must build definition | 
|  | 5034 | // of protocol's meta-data (not a reference to it!) | 
|  | 5035 | // | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5036 | llvm::Constant *Init = | 
|  | 5037 | llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD), | 
|  | 5038 | ObjCTypes.ExternalProtocolPtrTy); | 
|  | 5039 |  | 
| Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5040 | std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_"); | 
| Daniel Dunbar | 4087f27 | 2010-08-17 22:39:59 +0000 | [diff] [blame] | 5041 | ProtocolName += PD->getName(); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5042 |  | 
| Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5043 | llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName); | 
|  | 5044 | if (PTGV) | 
| Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 5045 | return Builder.CreateLoad(PTGV); | 
| Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5046 | PTGV = new llvm::GlobalVariable( | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5047 | CGM.getModule(), | 
|  | 5048 | Init->getType(), false, | 
|  | 5049 | llvm::GlobalValue::WeakAnyLinkage, | 
|  | 5050 | Init, | 
|  | 5051 | ProtocolName); | 
| Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5052 | PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip"); | 
|  | 5053 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); | 
| Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5054 | CGM.AddUsedGlobal(PTGV); | 
| Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 5055 | return Builder.CreateLoad(PTGV); | 
| Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5056 | } | 
|  | 5057 |  | 
| Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5058 | /// GenerateCategory - Build metadata for a category implementation. | 
|  | 5059 | /// struct _category_t { | 
|  | 5060 | ///   const char * const name; | 
|  | 5061 | ///   struct _class_t *const cls; | 
|  | 5062 | ///   const struct _method_list_t * const instance_methods; | 
|  | 5063 | ///   const struct _method_list_t * const class_methods; | 
|  | 5064 | ///   const struct _protocol_list_t * const protocols; | 
|  | 5065 | ///   const struct _prop_list_t * const properties; | 
|  | 5066 | /// } | 
|  | 5067 | /// | 
| Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5068 | void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { | 
| Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5069 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); | 
| Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5070 | const char *Prefix = "\01l_OBJC_$_CATEGORY_"; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5071 | std::string ExtCatName(Prefix + Interface->getNameAsString()+ | 
|  | 5072 | "_$_" + OCD->getNameAsString()); | 
|  | 5073 | std::string ExtClassName(getClassSymbolPrefix() + | 
| Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5074 | Interface->getNameAsString()); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5075 |  | 
| Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5076 | llvm::Constant *Values[6]; | 
| Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5077 | Values[0] = GetClassName(OCD->getIdentifier()); | 
|  | 5078 | // meta-class entry symbol | 
| Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5079 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName); | 
| Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5080 | if (Interface->isWeakImported()) | 
| Fariborz Jahanian | 2cdcc4c | 2009-11-17 22:02:21 +0000 | [diff] [blame] | 5081 | ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); | 
|  | 5082 |  | 
| Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5083 | Values[1] = ClassGV; | 
| Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5084 | std::vector<llvm::Constant*> Methods; | 
|  | 5085 | std::string MethodListName(Prefix); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5086 | MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() + | 
| Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5087 | "_$_" + OCD->getNameAsString(); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5088 |  | 
|  | 5089 | for (ObjCCategoryImplDecl::instmeth_iterator | 
| Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5090 | i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) { | 
| Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5091 | // Instance methods should always be defined. | 
|  | 5092 | Methods.push_back(GetMethodConstant(*i)); | 
|  | 5093 | } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5094 |  | 
|  | 5095 | Values[2] = EmitMethodList(MethodListName, | 
|  | 5096 | "__DATA, __objc_const", | 
| Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5097 | Methods); | 
|  | 5098 |  | 
|  | 5099 | MethodListName = Prefix; | 
|  | 5100 | MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" + | 
|  | 5101 | OCD->getNameAsString(); | 
|  | 5102 | Methods.clear(); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5103 | for (ObjCCategoryImplDecl::classmeth_iterator | 
| Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5104 | i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) { | 
| Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5105 | // Class methods should always be defined. | 
|  | 5106 | Methods.push_back(GetMethodConstant(*i)); | 
|  | 5107 | } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5108 |  | 
|  | 5109 | Values[3] = EmitMethodList(MethodListName, | 
|  | 5110 | "__DATA, __objc_const", | 
| Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 5111 | Methods); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5112 | const ObjCCategoryDecl *Category = | 
| Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 5113 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); | 
| Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5114 | if (Category) { | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5115 | llvm::SmallString<256> ExtName; | 
|  | 5116 | llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_" | 
|  | 5117 | << OCD->getName(); | 
| Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5118 | Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_" | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5119 | + Interface->getName() + "_$_" | 
|  | 5120 | + Category->getName(), | 
| Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5121 | Category->protocol_begin(), | 
|  | 5122 | Category->protocol_end()); | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5123 | Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(), | 
|  | 5124 | OCD, Category, ObjCTypes); | 
| Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5125 | } else { | 
| Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5126 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); | 
|  | 5127 | Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); | 
| Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 5128 | } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5129 |  | 
|  | 5130 | llvm::Constant *Init = | 
|  | 5131 | llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy, | 
| Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5132 | Values); | 
|  | 5133 | llvm::GlobalVariable *GCATV | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5134 | = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy, | 
| Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5135 | false, | 
|  | 5136 | llvm::GlobalValue::InternalLinkage, | 
|  | 5137 | Init, | 
| Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5138 | ExtCatName); | 
| Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5139 | GCATV->setAlignment( | 
| Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5140 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy)); | 
| Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5141 | GCATV->setSection("__DATA, __objc_const"); | 
| Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5142 | CGM.AddUsedGlobal(GCATV); | 
| Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5143 | DefinedCategories.push_back(GCATV); | 
| Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 5144 |  | 
|  | 5145 | // Determine if this category is also "non-lazy". | 
|  | 5146 | if (ImplementationIsNonLazy(OCD)) | 
|  | 5147 | DefinedNonLazyCategories.push_back(GCATV); | 
| Fariborz Jahanian | 64089ce | 2011-04-22 22:02:28 +0000 | [diff] [blame] | 5148 | // method definition entries must be clear for next implementation. | 
|  | 5149 | MethodDefinitions.clear(); | 
| Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 5150 | } | 
| Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5151 |  | 
|  | 5152 | /// GetMethodConstant - Return a struct objc_method constant for the | 
|  | 5153 | /// given method if it has been defined. The result is null if the | 
|  | 5154 | /// method has not been defined. The return value has type MethodPtrTy. | 
|  | 5155 | llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant( | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5156 | const ObjCMethodDecl *MD) { | 
| Argyrios Kyrtzidis | 9d50c06 | 2010-08-09 10:54:20 +0000 | [diff] [blame] | 5157 | llvm::Function *Fn = GetMethodDefinition(MD); | 
| Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5158 | if (!Fn) | 
|  | 5159 | return 0; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5160 |  | 
| Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5161 | llvm::Constant *Method[] = { | 
| Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 5162 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), | 
| Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5163 | ObjCTypes.SelectorPtrTy), | 
|  | 5164 | GetMethodVarType(MD), | 
|  | 5165 | llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy) | 
|  | 5166 | }; | 
| Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5167 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); | 
| Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5168 | } | 
|  | 5169 |  | 
|  | 5170 | /// EmitMethodList - Build meta-data for method declarations | 
|  | 5171 | /// struct _method_list_t { | 
|  | 5172 | ///   uint32_t entsize;  // sizeof(struct _objc_method) | 
|  | 5173 | ///   uint32_t method_count; | 
|  | 5174 | ///   struct _objc_method method_list[method_count]; | 
|  | 5175 | /// } | 
|  | 5176 | /// | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5177 | llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(Twine Name, | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5178 | const char *Section, | 
|  | 5179 | const ConstantVector &Methods) { | 
| Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5180 | // Return null for empty list. | 
|  | 5181 | if (Methods.empty()) | 
| Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5182 | return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5183 |  | 
| Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5184 | llvm::Constant *Values[3]; | 
| Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5185 | // sizeof(struct _objc_method) | 
| Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5186 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy); | 
| Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5187 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); | 
| Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5188 | // method_count | 
| Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5189 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5190 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, | 
| Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5191 | Methods.size()); | 
| Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 5192 | Values[2] = llvm::ConstantArray::get(AT, Methods); | 
| Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5193 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5194 |  | 
| Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5195 | llvm::GlobalVariable *GV = | 
| Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5196 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, | 
| Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5197 | llvm::GlobalValue::InternalLinkage, Init, Name); | 
|  | 5198 | GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType())); | 
| Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5199 | GV->setSection(Section); | 
| Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5200 | CGM.AddUsedGlobal(GV); | 
| Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5201 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy); | 
| Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 5202 | } | 
| Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5203 |  | 
| Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 5204 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for | 
|  | 5205 | /// the given ivar. | 
| Daniel Dunbar | e83be12 | 2010-04-02 21:14:02 +0000 | [diff] [blame] | 5206 | llvm::GlobalVariable * | 
|  | 5207 | CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID, | 
|  | 5208 | const ObjCIvarDecl *Ivar) { | 
|  | 5209 | const ObjCInterfaceDecl *Container = Ivar->getContainingInterface(); | 
| Daniel Dunbar | a81419d | 2009-05-05 00:36:57 +0000 | [diff] [blame] | 5210 | std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() + | 
| Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 5211 | '.' + Ivar->getNameAsString(); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5212 | llvm::GlobalVariable *IvarOffsetGV = | 
| Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 5213 | CGM.getModule().getGlobalVariable(Name); | 
|  | 5214 | if (!IvarOffsetGV) | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5215 | IvarOffsetGV = | 
| Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5216 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy, | 
| Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 5217 | false, | 
|  | 5218 | llvm::GlobalValue::ExternalLinkage, | 
|  | 5219 | 0, | 
| Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5220 | Name); | 
| Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 5221 | return IvarOffsetGV; | 
|  | 5222 | } | 
|  | 5223 |  | 
| Daniel Dunbar | e83be12 | 2010-04-02 21:14:02 +0000 | [diff] [blame] | 5224 | llvm::Constant * | 
|  | 5225 | CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, | 
|  | 5226 | const ObjCIvarDecl *Ivar, | 
|  | 5227 | unsigned long int Offset) { | 
| Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 5228 | llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5229 | IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy, | 
| Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 5230 | Offset)); | 
| Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5231 | IvarOffsetGV->setAlignment( | 
| Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5232 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy)); | 
| Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 5233 |  | 
| Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 5234 | // FIXME: This matches gcc, but shouldn't the visibility be set on the use as | 
|  | 5235 | // well (i.e., in ObjCIvarOffsetVariable). | 
| Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 5236 | if (Ivar->getAccessControl() == ObjCIvarDecl::Private || | 
|  | 5237 | Ivar->getAccessControl() == ObjCIvarDecl::Package || | 
| John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 5238 | ID->getVisibility() == HiddenVisibility) | 
| Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 5239 | IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); | 
| Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 5240 | else | 
| Fariborz Jahanian | 77c9fd2 | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 5241 | IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility); | 
| Bill Wendling | 1f38251 | 2011-05-04 21:37:25 +0000 | [diff] [blame] | 5242 | IvarOffsetGV->setSection("__DATA, __objc_ivar"); | 
| Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 5243 | return IvarOffsetGV; | 
| Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5244 | } | 
|  | 5245 |  | 
| Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5246 | /// EmitIvarList - Emit the ivar list for the given | 
| Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5247 | /// implementation. The return value has type | 
| Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5248 | /// IvarListnfABIPtrTy. | 
|  | 5249 | ///  struct _ivar_t { | 
|  | 5250 | ///   unsigned long int *offset;  // pointer to ivar offset location | 
|  | 5251 | ///   char *name; | 
|  | 5252 | ///   char *type; | 
|  | 5253 | ///   uint32_t alignment; | 
|  | 5254 | ///   uint32_t size; | 
|  | 5255 | /// } | 
|  | 5256 | /// struct _ivar_list_t { | 
|  | 5257 | ///   uint32 entsize;  // sizeof(struct _ivar_t) | 
|  | 5258 | ///   uint32 count; | 
|  | 5259 | ///   struct _iver_t list[count]; | 
|  | 5260 | /// } | 
|  | 5261 | /// | 
| Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 5262 |  | 
| Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5263 | llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList( | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5264 | const ObjCImplementationDecl *ID) { | 
|  | 5265 |  | 
| Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5266 | std::vector<llvm::Constant*> Ivars; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5267 |  | 
| Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 5268 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); | 
| Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5269 | assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface"); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5270 |  | 
| Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5271 | // FIXME. Consolidate this with similar code in GenerateClass. | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5272 |  | 
| Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 5273 | for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin(); | 
| Fariborz Jahanian | bf9eb88 | 2011-06-28 18:05:25 +0000 | [diff] [blame] | 5274 | IVD; IVD = IVD->getNextIvar()) { | 
| Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 5275 | // Ignore unnamed bit-fields. | 
|  | 5276 | if (!IVD->getDeclName()) | 
|  | 5277 | continue; | 
| Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5278 | llvm::Constant *Ivar[5]; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5279 | Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD, | 
| Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 5280 | ComputeIvarBaseOffset(CGM, ID, IVD)); | 
| Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 5281 | Ivar[1] = GetMethodVarName(IVD->getIdentifier()); | 
|  | 5282 | Ivar[2] = GetMethodVarType(IVD); | 
| Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5283 | llvm::Type *FieldTy = | 
| Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 5284 | CGM.getTypes().ConvertTypeForMem(IVD->getType()); | 
| Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5285 | unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy); | 
| Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5286 | unsigned Align = CGM.getContext().getPreferredTypeAlign( | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5287 | IVD->getType().getTypePtr()) >> 3; | 
| Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5288 | Align = llvm::Log2_32(Align); | 
| Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5289 | Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align); | 
| Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 5290 | // NOTE. Size of a bitfield does not match gcc's, because of the | 
|  | 5291 | // way bitfields are treated special in each. But I am told that | 
|  | 5292 | // 'size' for bitfield ivars is ignored by the runtime so it does | 
|  | 5293 | // not matter.  If it matters, there is enough info to get the | 
|  | 5294 | // bitfield right! | 
| Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5295 | Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); | 
| Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5296 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar)); | 
| Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5297 | } | 
|  | 5298 | // Return null for empty list. | 
|  | 5299 | if (Ivars.empty()) | 
| Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5300 | return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); | 
| Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5301 |  | 
|  | 5302 | llvm::Constant *Values[3]; | 
| Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5303 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy); | 
| Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5304 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); | 
|  | 5305 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5306 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy, | 
| Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5307 | Ivars.size()); | 
| Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 5308 | Values[2] = llvm::ConstantArray::get(AT, Ivars); | 
| Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5309 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); | 
| Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5310 | const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_"; | 
|  | 5311 | llvm::GlobalVariable *GV = | 
| Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5312 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, | 
| Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 5313 | llvm::GlobalValue::InternalLinkage, | 
|  | 5314 | Init, | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5315 | Prefix + OID->getName()); | 
| Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5316 | GV->setAlignment( | 
| Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5317 | CGM.getTargetData().getABITypeAlignment(Init->getType())); | 
| Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 5318 | GV->setSection("__DATA, __objc_const"); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5319 |  | 
| Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5320 | CGM.AddUsedGlobal(GV); | 
| Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 5321 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy); | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5322 | } | 
|  | 5323 |  | 
|  | 5324 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef( | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5325 | const ObjCProtocolDecl *PD) { | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5326 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5327 |  | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5328 | if (!Entry) { | 
|  | 5329 | // We use the initializer as a marker of whether this is a forward | 
|  | 5330 | // reference or not. At module finalization we add the empty | 
|  | 5331 | // contents for protocols which were referenced but never defined. | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5332 | Entry = | 
|  | 5333 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false, | 
|  | 5334 | llvm::GlobalValue::ExternalLinkage, | 
|  | 5335 | 0, | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5336 | "\01l_OBJC_PROTOCOL_$_" + PD->getName()); | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5337 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5338 | } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5339 |  | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5340 | return Entry; | 
|  | 5341 | } | 
|  | 5342 |  | 
|  | 5343 | /// GetOrEmitProtocol - Generate the protocol meta-data: | 
|  | 5344 | /// @code | 
|  | 5345 | /// struct _protocol_t { | 
|  | 5346 | ///   id isa;  // NULL | 
|  | 5347 | ///   const char * const protocol_name; | 
|  | 5348 | ///   const struct _protocol_list_t * protocol_list; // super protocols | 
|  | 5349 | ///   const struct method_list_t * const instance_methods; | 
|  | 5350 | ///   const struct method_list_t * const class_methods; | 
|  | 5351 | ///   const struct method_list_t *optionalInstanceMethods; | 
|  | 5352 | ///   const struct method_list_t *optionalClassMethods; | 
|  | 5353 | ///   const struct _prop_list_t * properties; | 
|  | 5354 | ///   const uint32_t size;  // sizeof(struct _protocol_t) | 
|  | 5355 | ///   const uint32_t flags;  // = 0 | 
|  | 5356 | /// } | 
|  | 5357 | /// @endcode | 
|  | 5358 | /// | 
|  | 5359 |  | 
|  | 5360 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol( | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5361 | const ObjCProtocolDecl *PD) { | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5362 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5363 |  | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5364 | // Early exit if a defining object has already been generated. | 
|  | 5365 | if (Entry && Entry->hasInitializer()) | 
|  | 5366 | return Entry; | 
|  | 5367 |  | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5368 | // Construct method lists. | 
|  | 5369 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; | 
|  | 5370 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5371 | for (ObjCProtocolDecl::instmeth_iterator | 
| Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5372 | i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) { | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5373 | ObjCMethodDecl *MD = *i; | 
| Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5374 | llvm::Constant *C = GetMethodDescriptionConstant(MD); | 
| Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5375 | if (!C) | 
|  | 5376 | return GetOrEmitProtocolRef(PD); | 
|  | 5377 |  | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5378 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { | 
|  | 5379 | OptInstanceMethods.push_back(C); | 
|  | 5380 | } else { | 
|  | 5381 | InstanceMethods.push_back(C); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5382 | } | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5383 | } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5384 |  | 
|  | 5385 | for (ObjCProtocolDecl::classmeth_iterator | 
| Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5386 | i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) { | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5387 | ObjCMethodDecl *MD = *i; | 
| Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5388 | llvm::Constant *C = GetMethodDescriptionConstant(MD); | 
| Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5389 | if (!C) | 
|  | 5390 | return GetOrEmitProtocolRef(PD); | 
|  | 5391 |  | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5392 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { | 
|  | 5393 | OptClassMethods.push_back(C); | 
|  | 5394 | } else { | 
|  | 5395 | ClassMethods.push_back(C); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5396 | } | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5397 | } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5398 |  | 
| Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5399 | llvm::Constant *Values[10]; | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5400 | // isa is NULL | 
| Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5401 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy); | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5402 | Values[1] = GetClassName(PD->getIdentifier()); | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5403 | Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(), | 
|  | 5404 | PD->protocol_begin(), | 
|  | 5405 | PD->protocol_end()); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5406 |  | 
| Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5407 | Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_" | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5408 | + PD->getName(), | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5409 | "__DATA, __objc_const", | 
|  | 5410 | InstanceMethods); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5411 | Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_" | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5412 | + PD->getName(), | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5413 | "__DATA, __objc_const", | 
|  | 5414 | ClassMethods); | 
| Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5415 | Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_" | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5416 | + PD->getName(), | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5417 | "__DATA, __objc_const", | 
|  | 5418 | OptInstanceMethods); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5419 | Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_" | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5420 | + PD->getName(), | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5421 | "__DATA, __objc_const", | 
|  | 5422 | OptClassMethods); | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5423 | Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(), | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5424 | 0, PD, ObjCTypes); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5425 | uint32_t Size = | 
| Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5426 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy); | 
| Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5427 | Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); | 
| Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5428 | Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy); | 
| Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5429 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy, | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5430 | Values); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5431 |  | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5432 | if (Entry) { | 
|  | 5433 | // Already created, fix the linkage and update the initializer. | 
| Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 5434 | Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage); | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5435 | Entry->setInitializer(Init); | 
|  | 5436 | } else { | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5437 | Entry = | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5438 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, | 
|  | 5439 | false, llvm::GlobalValue::WeakAnyLinkage, Init, | 
|  | 5440 | "\01l_OBJC_PROTOCOL_$_" + PD->getName()); | 
| Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5441 | Entry->setAlignment( | 
| Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5442 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy)); | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5443 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5444 | } | 
| Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 5445 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); | 
| Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5446 | CGM.AddUsedGlobal(Entry); | 
|  | 5447 |  | 
| Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 5448 | // Use this protocol meta-data to build protocol list table in section | 
|  | 5449 | // __DATA, __objc_protolist | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5450 | llvm::GlobalVariable *PTGV = | 
|  | 5451 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy, | 
|  | 5452 | false, llvm::GlobalValue::WeakAnyLinkage, Entry, | 
|  | 5453 | "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName()); | 
| Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5454 | PTGV->setAlignment( | 
| Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5455 | CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy)); | 
| Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 5456 | PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip"); | 
| Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 5457 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); | 
| Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5458 | CGM.AddUsedGlobal(PTGV); | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5459 | return Entry; | 
|  | 5460 | } | 
|  | 5461 |  | 
|  | 5462 | /// EmitProtocolList - Generate protocol list meta-data: | 
|  | 5463 | /// @code | 
|  | 5464 | /// struct _protocol_list_t { | 
|  | 5465 | ///   long protocol_count;   // Note, this is 32/64 bit | 
|  | 5466 | ///   struct _protocol_t[protocol_count]; | 
|  | 5467 | /// } | 
|  | 5468 | /// @endcode | 
|  | 5469 | /// | 
|  | 5470 | llvm::Constant * | 
| Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5471 | CGObjCNonFragileABIMac::EmitProtocolList(Twine Name, | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5472 | ObjCProtocolDecl::protocol_iterator begin, | 
|  | 5473 | ObjCProtocolDecl::protocol_iterator end) { | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5474 | std::vector<llvm::Constant*> ProtocolRefs; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5475 |  | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5476 | // Just return null for empty protocol lists | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5477 | if (begin == end) | 
| Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5478 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5479 |  | 
| Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5480 | // FIXME: We shouldn't need to do this lookup here, should we? | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5481 | llvm::SmallString<256> TmpName; | 
|  | 5482 | Name.toVector(TmpName); | 
|  | 5483 | llvm::GlobalVariable *GV = | 
|  | 5484 | CGM.getModule().getGlobalVariable(TmpName.str(), true); | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5485 | if (GV) | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 5486 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5487 |  | 
| Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5488 | for (; begin != end; ++begin) | 
|  | 5489 | ProtocolRefs.push_back(GetProtocolRef(*begin));  // Implemented??? | 
|  | 5490 |  | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5491 | // This list is null terminated. | 
| Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5492 | ProtocolRefs.push_back(llvm::Constant::getNullValue( | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5493 | ObjCTypes.ProtocolnfABIPtrTy)); | 
|  | 5494 |  | 
| Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5495 | llvm::Constant *Values[2]; | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 5496 | Values[0] = | 
| Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5497 | llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5498 | Values[1] = | 
| Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 5499 | llvm::ConstantArray::get( | 
| Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5500 | llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy, | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5501 | ProtocolRefs.size()), | 
|  | 5502 | ProtocolRefs); | 
|  | 5503 |  | 
| Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5504 | llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values); | 
| Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5505 | GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5506 | llvm::GlobalValue::InternalLinkage, | 
| Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5507 | Init, Name); | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5508 | GV->setSection("__DATA, __objc_const"); | 
| Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 5509 | GV->setAlignment( | 
| Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5510 | CGM.getTargetData().getABITypeAlignment(Init->getType())); | 
| Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5511 | CGM.AddUsedGlobal(GV); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5512 | return llvm::ConstantExpr::getBitCast(GV, | 
| Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 5513 | ObjCTypes.ProtocolListnfABIPtrTy); | 
| Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 5514 | } | 
|  | 5515 |  | 
| Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5516 | /// GetMethodDescriptionConstant - This routine build following meta-data: | 
|  | 5517 | /// struct _objc_method { | 
|  | 5518 | ///   SEL _cmd; | 
|  | 5519 | ///   char *method_type; | 
|  | 5520 | ///   char *_imp; | 
|  | 5521 | /// } | 
|  | 5522 |  | 
|  | 5523 | llvm::Constant * | 
|  | 5524 | CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { | 
| Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 5525 | llvm::Constant *Desc[3]; | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 5526 | Desc[0] = | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5527 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), | 
|  | 5528 | ObjCTypes.SelectorPtrTy); | 
| Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5529 | Desc[1] = GetMethodVarType(MD); | 
| Douglas Gregor | f968d83 | 2011-05-27 01:19:52 +0000 | [diff] [blame] | 5530 | if (!Desc[1]) | 
|  | 5531 | return 0; | 
|  | 5532 |  | 
| Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 5533 | // Protocol methods have no implementation. So, this entry is always NULL. | 
| Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5534 | Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); | 
| Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5535 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc); | 
| Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 5536 | } | 
| Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 5537 |  | 
|  | 5538 | /// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference. | 
|  | 5539 | /// This code gen. amounts to generating code for: | 
|  | 5540 | /// @code | 
|  | 5541 | /// (type *)((char *)base + _OBJC_IVAR_$_.ivar; | 
|  | 5542 | /// @encode | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5543 | /// | 
| Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 5544 | LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar( | 
| John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 5545 | CodeGen::CodeGenFunction &CGF, | 
|  | 5546 | QualType ObjectTy, | 
|  | 5547 | llvm::Value *BaseValue, | 
|  | 5548 | const ObjCIvarDecl *Ivar, | 
|  | 5549 | unsigned CVRQualifiers) { | 
|  | 5550 | ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface(); | 
| Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 5551 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, | 
|  | 5552 | EmitIvarOffset(CGF, ID, Ivar)); | 
| Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 5553 | } | 
|  | 5554 |  | 
| Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 5555 | llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset( | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5556 | CodeGen::CodeGenFunction &CGF, | 
|  | 5557 | const ObjCInterfaceDecl *Interface, | 
|  | 5558 | const ObjCIvarDecl *Ivar) { | 
| Daniel Dunbar | 2da84ff | 2009-11-29 21:23:36 +0000 | [diff] [blame] | 5559 | return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar"); | 
| Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 5560 | } | 
|  | 5561 |  | 
| John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5562 | static void appendSelectorForMessageRefTable(std::string &buffer, | 
|  | 5563 | Selector selector) { | 
|  | 5564 | if (selector.isUnarySelector()) { | 
|  | 5565 | buffer += selector.getNameForSlot(0); | 
|  | 5566 | return; | 
|  | 5567 | } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5568 |  | 
| John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5569 | for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) { | 
|  | 5570 | buffer += selector.getNameForSlot(i); | 
|  | 5571 | buffer += '_'; | 
|  | 5572 | } | 
|  | 5573 | } | 
|  | 5574 |  | 
| John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5575 | /// Emit a "v-table" message send.  We emit a weak hidden-visibility | 
|  | 5576 | /// struct, initially containing the selector pointer and a pointer to | 
|  | 5577 | /// a "fixup" variant of the appropriate objc_msgSend.  To call, we | 
|  | 5578 | /// load and call the function pointer, passing the address of the | 
|  | 5579 | /// struct as the second parameter.  The runtime determines whether | 
|  | 5580 | /// the selector is currently emitted using vtable dispatch; if so, it | 
|  | 5581 | /// substitutes a stub function which simply tail-calls through the | 
|  | 5582 | /// appropriate vtable slot, and if not, it substitues a stub function | 
|  | 5583 | /// which tail-calls objc_msgSend.  Both stubs adjust the selector | 
|  | 5584 | /// argument to correctly point to the selector. | 
|  | 5585 | RValue | 
|  | 5586 | CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF, | 
|  | 5587 | ReturnValueSlot returnSlot, | 
|  | 5588 | QualType resultType, | 
|  | 5589 | Selector selector, | 
|  | 5590 | llvm::Value *arg0, | 
|  | 5591 | QualType arg0Type, | 
|  | 5592 | bool isSuper, | 
|  | 5593 | const CallArgList &formalArgs, | 
|  | 5594 | const ObjCMethodDecl *method) { | 
| John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5595 | // Compute the actual arguments. | 
|  | 5596 | CallArgList args; | 
|  | 5597 |  | 
| John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5598 | // First argument: the receiver / super-call structure. | 
| John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5599 | if (!isSuper) | 
| John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5600 | arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy); | 
|  | 5601 | args.add(RValue::get(arg0), arg0Type); | 
| John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5602 |  | 
| John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5603 | // Second argument: a pointer to the message ref structure.  Leave | 
|  | 5604 | // the actual argument value blank for now. | 
| John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5605 | args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy); | 
|  | 5606 |  | 
|  | 5607 | args.insert(args.end(), formalArgs.begin(), formalArgs.end()); | 
|  | 5608 |  | 
|  | 5609 | const CGFunctionInfo &fnInfo = | 
|  | 5610 | CGM.getTypes().getFunctionInfo(resultType, args, | 
|  | 5611 | FunctionType::ExtInfo()); | 
|  | 5612 |  | 
| John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 5613 | NullReturnState nullReturn; | 
|  | 5614 |  | 
| John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5615 | // Find the function to call and the mangled name for the message | 
|  | 5616 | // ref structure.  Using a different mangled name wouldn't actually | 
|  | 5617 | // be a problem; it would just be a waste. | 
|  | 5618 | // | 
|  | 5619 | // The runtime currently never uses vtable dispatch for anything | 
|  | 5620 | // except normal, non-super message-sends. | 
|  | 5621 | // FIXME: don't use this for that. | 
| John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5622 | llvm::Constant *fn = 0; | 
|  | 5623 | std::string messageRefName("\01l_"); | 
|  | 5624 | if (CGM.ReturnTypeUsesSRet(fnInfo)) { | 
| John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5625 | if (isSuper) { | 
|  | 5626 | fn = ObjCTypes.getMessageSendSuper2StretFixupFn(); | 
|  | 5627 | messageRefName += "objc_msgSendSuper2_stret_fixup"; | 
| Chris Lattner | 9b7d670 | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 5628 | } else { | 
| John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 5629 | nullReturn.init(CGF, arg0); | 
| John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5630 | fn = ObjCTypes.getMessageSendStretFixupFn(); | 
|  | 5631 | messageRefName += "objc_msgSend_stret_fixup"; | 
| Chris Lattner | 9b7d670 | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 5632 | } | 
| John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5633 | } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) { | 
|  | 5634 | fn = ObjCTypes.getMessageSendFpretFixupFn(); | 
|  | 5635 | messageRefName += "objc_msgSend_fpret_fixup"; | 
| Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5636 | } else { | 
| John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5637 | if (isSuper) { | 
|  | 5638 | fn = ObjCTypes.getMessageSendSuper2FixupFn(); | 
|  | 5639 | messageRefName += "objc_msgSendSuper2_fixup"; | 
| Chris Lattner | 9b7d670 | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 5640 | } else { | 
| John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5641 | fn = ObjCTypes.getMessageSendFixupFn(); | 
|  | 5642 | messageRefName += "objc_msgSend_fixup"; | 
| Chris Lattner | 9b7d670 | 2010-08-18 16:09:06 +0000 | [diff] [blame] | 5643 | } | 
| Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5644 | } | 
| John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5645 | assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend"); | 
|  | 5646 | messageRefName += '_'; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5647 |  | 
| John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5648 | // Append the selector name, except use underscores anywhere we | 
|  | 5649 | // would have used colons. | 
|  | 5650 | appendSelectorForMessageRefTable(messageRefName, selector); | 
|  | 5651 |  | 
|  | 5652 | llvm::GlobalVariable *messageRef | 
|  | 5653 | = CGM.getModule().getGlobalVariable(messageRefName); | 
|  | 5654 | if (!messageRef) { | 
| John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5655 | // Build the message ref structure. | 
| John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5656 | llvm::Constant *values[] = { fn, GetMethodVarName(selector) }; | 
| Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 5657 | llvm::Constant *init = llvm::ConstantStruct::getAnon(values); | 
| John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5658 | messageRef = new llvm::GlobalVariable(CGM.getModule(), | 
|  | 5659 | init->getType(), | 
|  | 5660 | /*constant*/ false, | 
|  | 5661 | llvm::GlobalValue::WeakAnyLinkage, | 
|  | 5662 | init, | 
|  | 5663 | messageRefName); | 
|  | 5664 | messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility); | 
|  | 5665 | messageRef->setAlignment(16); | 
|  | 5666 | messageRef->setSection("__DATA, __objc_msgrefs, coalesced"); | 
|  | 5667 | } | 
|  | 5668 | llvm::Value *mref = | 
|  | 5669 | CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy); | 
|  | 5670 |  | 
| John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5671 | // Update the message ref argument. | 
| John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5672 | args[1].RV = RValue::get(mref); | 
|  | 5673 |  | 
|  | 5674 | // Load the function to call from the message ref table. | 
|  | 5675 | llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0); | 
|  | 5676 | callee = CGF.Builder.CreateLoad(callee, "msgSend_fn"); | 
|  | 5677 |  | 
|  | 5678 | bool variadic = method ? method->isVariadic() : false; | 
| Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5679 | llvm::FunctionType *fnType = | 
| John McCall | b1e8144 | 2011-05-13 23:16:18 +0000 | [diff] [blame] | 5680 | CGF.getTypes().GetFunctionType(fnInfo, variadic); | 
|  | 5681 | callee = CGF.Builder.CreateBitCast(callee, | 
|  | 5682 | llvm::PointerType::getUnqual(fnType)); | 
|  | 5683 |  | 
| John McCall | cba681a | 2011-05-14 21:12:11 +0000 | [diff] [blame] | 5684 | RValue result = CGF.EmitCall(fnInfo, callee, returnSlot, args); | 
|  | 5685 | return nullReturn.complete(CGF, result, resultType); | 
| Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5686 | } | 
|  | 5687 |  | 
|  | 5688 | /// Generate code for a message send expression in the nonfragile abi. | 
| Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5689 | CodeGen::RValue | 
|  | 5690 | CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, | 
| John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 5691 | ReturnValueSlot Return, | 
| Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5692 | QualType ResultType, | 
|  | 5693 | Selector Sel, | 
|  | 5694 | llvm::Value *Receiver, | 
| Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5695 | const CallArgList &CallArgs, | 
| David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 5696 | const ObjCInterfaceDecl *Class, | 
| Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5697 | const ObjCMethodDecl *Method) { | 
| John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5698 | return isVTableDispatchedSelector(Sel) | 
|  | 5699 | ? EmitVTableMessageSend(CGF, Return, ResultType, Sel, | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5700 | Receiver, CGF.getContext().getObjCIdType(), | 
| John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5701 | false, CallArgs, Method) | 
|  | 5702 | : EmitMessageSend(CGF, Return, ResultType, | 
|  | 5703 | EmitSelector(CGF.Builder, Sel), | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5704 | Receiver, CGF.getContext().getObjCIdType(), | 
| John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5705 | false, CallArgs, Method, ObjCTypes); | 
| Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5706 | } | 
|  | 5707 |  | 
| Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5708 | llvm::GlobalVariable * | 
| Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5709 | CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) { | 
| Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5710 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); | 
|  | 5711 |  | 
| Daniel Dunbar | dfff230 | 2009-03-02 05:18:14 +0000 | [diff] [blame] | 5712 | if (!GV) { | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5713 | GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy, | 
| Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5714 | false, llvm::GlobalValue::ExternalLinkage, | 
|  | 5715 | 0, Name); | 
| Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5716 | } | 
|  | 5717 |  | 
|  | 5718 | return GV; | 
|  | 5719 | } | 
|  | 5720 |  | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5721 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder, | 
|  | 5722 | IdentifierInfo *II) { | 
|  | 5723 | llvm::GlobalVariable *&Entry = ClassReferences[II]; | 
|  | 5724 |  | 
| Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5725 | if (!Entry) { | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5726 | std::string ClassName(getClassSymbolPrefix() + II->getName().str()); | 
| Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5727 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5728 | Entry = | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5729 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, | 
|  | 5730 | false, llvm::GlobalValue::InternalLinkage, | 
|  | 5731 | ClassGV, | 
|  | 5732 | "\01L_OBJC_CLASSLIST_REFERENCES_$_"); | 
| Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5733 | Entry->setAlignment( | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5734 | CGM.getTargetData().getABITypeAlignment( | 
|  | 5735 | ObjCTypes.ClassnfABIPtrTy)); | 
| Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5736 | Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip"); | 
| Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5737 | CGM.AddUsedGlobal(Entry); | 
| Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5738 | } | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5739 |  | 
| Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 5740 | return Builder.CreateLoad(Entry); | 
| Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5741 | } | 
| Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5742 |  | 
| John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5743 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder, | 
|  | 5744 | const ObjCInterfaceDecl *ID) { | 
|  | 5745 | return EmitClassRefFromId(Builder, ID->getIdentifier()); | 
|  | 5746 | } | 
|  | 5747 |  | 
|  | 5748 | llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef( | 
|  | 5749 | CGBuilderTy &Builder) { | 
|  | 5750 | IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool"); | 
|  | 5751 | return EmitClassRefFromId(Builder, II); | 
|  | 5752 | } | 
|  | 5753 |  | 
| Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5754 | llvm::Value * | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5755 | CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder, | 
| Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5756 | const ObjCInterfaceDecl *ID) { | 
|  | 5757 | llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()]; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5758 |  | 
| Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5759 | if (!Entry) { | 
|  | 5760 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); | 
|  | 5761 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5762 | Entry = | 
|  | 5763 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, | 
| Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5764 | false, llvm::GlobalValue::InternalLinkage, | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5765 | ClassGV, | 
| Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5766 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_"); | 
| Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5767 | Entry->setAlignment( | 
| Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5768 | CGM.getTargetData().getABITypeAlignment( | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5769 | ObjCTypes.ClassnfABIPtrTy)); | 
| Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5770 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); | 
| Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5771 | CGM.AddUsedGlobal(Entry); | 
| Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5772 | } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5773 |  | 
| Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 5774 | return Builder.CreateLoad(Entry); | 
| Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5775 | } | 
|  | 5776 |  | 
| Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5777 | /// EmitMetaClassRef - Return a Value * of the address of _class_t | 
|  | 5778 | /// meta-data | 
|  | 5779 | /// | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5780 | llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder, | 
|  | 5781 | const ObjCInterfaceDecl *ID) { | 
| Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5782 | llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()]; | 
|  | 5783 | if (Entry) | 
| Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 5784 | return Builder.CreateLoad(Entry); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5785 |  | 
| Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5786 | std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString()); | 
| Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5787 | llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5788 | Entry = | 
| Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5789 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false, | 
| Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5790 | llvm::GlobalValue::InternalLinkage, | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5791 | MetaClassGV, | 
| Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5792 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_"); | 
| Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5793 | Entry->setAlignment( | 
| Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 5794 | CGM.getTargetData().getABITypeAlignment( | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5795 | ObjCTypes.ClassnfABIPtrTy)); | 
|  | 5796 |  | 
| Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5797 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); | 
| Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5798 | CGM.AddUsedGlobal(Entry); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5799 |  | 
| Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 5800 | return Builder.CreateLoad(Entry); | 
| Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5801 | } | 
|  | 5802 |  | 
| Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5803 | /// GetClass - Return a reference to the class for the given interface | 
|  | 5804 | /// decl. | 
|  | 5805 | llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder, | 
|  | 5806 | const ObjCInterfaceDecl *ID) { | 
| Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5807 | if (ID->isWeakImported()) { | 
| Fariborz Jahanian | 269f8bc | 2009-11-17 22:42:00 +0000 | [diff] [blame] | 5808 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); | 
|  | 5809 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); | 
|  | 5810 | ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage); | 
|  | 5811 | } | 
|  | 5812 |  | 
| Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5813 | return EmitClassRef(Builder, ID); | 
|  | 5814 | } | 
|  | 5815 |  | 
| Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5816 | /// Generates a message send where the super is the receiver.  This is | 
|  | 5817 | /// a message send to self with special delivery semantics indicating | 
|  | 5818 | /// which class's method should be called. | 
|  | 5819 | CodeGen::RValue | 
|  | 5820 | CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, | 
| John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 5821 | ReturnValueSlot Return, | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5822 | QualType ResultType, | 
|  | 5823 | Selector Sel, | 
|  | 5824 | const ObjCInterfaceDecl *Class, | 
|  | 5825 | bool isCategoryImpl, | 
|  | 5826 | llvm::Value *Receiver, | 
|  | 5827 | bool IsClassMessage, | 
| Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 5828 | const CodeGen::CallArgList &CallArgs, | 
|  | 5829 | const ObjCMethodDecl *Method) { | 
| Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5830 | // ... | 
|  | 5831 | // Create and init a super structure; this is a (receiver, class) | 
|  | 5832 | // pair we will pass to objc_msgSendSuper. | 
|  | 5833 | llvm::Value *ObjCSuper = | 
| John McCall | 604da29 | 2011-03-04 08:00:29 +0000 | [diff] [blame] | 5834 | CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super"); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5835 |  | 
| Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5836 | llvm::Value *ReceiverAsObject = | 
|  | 5837 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); | 
|  | 5838 | CGF.Builder.CreateStore(ReceiverAsObject, | 
|  | 5839 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5840 |  | 
| Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5841 | // 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] | 5842 | llvm::Value *Target; | 
|  | 5843 | if (IsClassMessage) { | 
|  | 5844 | if (isCategoryImpl) { | 
|  | 5845 | // Message sent to "super' in a class method defined in | 
|  | 5846 | // a category implementation. | 
| Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5847 | Target = EmitClassRef(CGF.Builder, Class); | 
| Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5848 | Target = CGF.Builder.CreateStructGEP(Target, 0); | 
|  | 5849 | Target = CGF.Builder.CreateLoad(Target); | 
| Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5850 | } else | 
| Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5851 | Target = EmitMetaClassRef(CGF.Builder, Class); | 
| Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5852 | } else | 
| Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5853 | Target = EmitSuperClassRef(CGF.Builder, Class); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5854 |  | 
| Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 5855 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext and | 
|  | 5856 | // ObjCTypes types. | 
| Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5857 | llvm::Type *ClassTy = | 
| Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5858 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); | 
|  | 5859 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); | 
|  | 5860 | CGF.Builder.CreateStore(Target, | 
|  | 5861 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5862 |  | 
| John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5863 | return (isVTableDispatchedSelector(Sel)) | 
|  | 5864 | ? EmitVTableMessageSend(CGF, Return, ResultType, Sel, | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5865 | ObjCSuper, ObjCTypes.SuperPtrCTy, | 
| John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5866 | true, CallArgs, Method) | 
|  | 5867 | : EmitMessageSend(CGF, Return, ResultType, | 
|  | 5868 | EmitSelector(CGF.Builder, Sel), | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5869 | ObjCSuper, ObjCTypes.SuperPtrCTy, | 
| John McCall | 944c843 | 2011-05-14 03:10:52 +0000 | [diff] [blame] | 5870 | true, CallArgs, Method, ObjCTypes); | 
| Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5871 | } | 
| Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5872 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5873 | llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder, | 
| Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 5874 | Selector Sel, bool lval) { | 
| Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5875 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5876 |  | 
| Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5877 | if (!Entry) { | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5878 | llvm::Constant *Casted = | 
|  | 5879 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), | 
|  | 5880 | ObjCTypes.SelectorPtrTy); | 
|  | 5881 | Entry = | 
|  | 5882 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false, | 
|  | 5883 | llvm::GlobalValue::InternalLinkage, | 
|  | 5884 | Casted, "\01L_OBJC_SELECTOR_REFERENCES_"); | 
| Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5885 | Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip"); | 
| Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5886 | CGM.AddUsedGlobal(Entry); | 
| Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5887 | } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5888 |  | 
| Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 5889 | if (lval) | 
|  | 5890 | return Entry; | 
| Pete Cooper | b6d7114 | 2011-11-10 21:45:06 +0000 | [diff] [blame] | 5891 | llvm::LoadInst* LI = Builder.CreateLoad(Entry); | 
|  | 5892 |  | 
|  | 5893 | LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"), | 
|  | 5894 | llvm::MDNode::get(VMContext, | 
|  | 5895 | ArrayRef<llvm::Value*>())); | 
|  | 5896 | return LI; | 
| Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5897 | } | 
| Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5898 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. | 
| Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 5899 | /// objc_assign_ivar (id src, id *dst, ptrdiff_t) | 
| Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5900 | /// | 
|  | 5901 | void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5902 | llvm::Value *src, | 
| Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 5903 | llvm::Value *dst, | 
|  | 5904 | llvm::Value *ivarOffset) { | 
| Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5905 | llvm::Type * SrcTy = src->getType(); | 
| Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5906 | if (!isa<llvm::PointerType>(SrcTy)) { | 
| Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5907 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); | 
| Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5908 | assert(Size <= 8 && "does not support size > 8"); | 
|  | 5909 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) | 
|  | 5910 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); | 
| Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5911 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); | 
|  | 5912 | } | 
| Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5913 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); | 
|  | 5914 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); | 
| Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 5915 | CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(), | 
|  | 5916 | src, dst, ivarOffset); | 
| Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5917 | return; | 
|  | 5918 | } | 
|  | 5919 |  | 
|  | 5920 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. | 
|  | 5921 | /// objc_assign_strongCast (id src, id *dst) | 
|  | 5922 | /// | 
|  | 5923 | void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign( | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5924 | CodeGen::CodeGenFunction &CGF, | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5925 | llvm::Value *src, llvm::Value *dst) { | 
| Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5926 | llvm::Type * SrcTy = src->getType(); | 
| Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5927 | if (!isa<llvm::PointerType>(SrcTy)) { | 
| Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5928 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); | 
| Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5929 | assert(Size <= 8 && "does not support size > 8"); | 
|  | 5930 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5931 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); | 
| Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5932 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); | 
|  | 5933 | } | 
| Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5934 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); | 
|  | 5935 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); | 
| Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5936 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), | 
| Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5937 | src, dst, "weakassign"); | 
|  | 5938 | return; | 
|  | 5939 | } | 
|  | 5940 |  | 
| Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 5941 | void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable( | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5942 | CodeGen::CodeGenFunction &CGF, | 
|  | 5943 | llvm::Value *DestPtr, | 
|  | 5944 | llvm::Value *SrcPtr, | 
| Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 5945 | llvm::Value *Size) { | 
| Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 5946 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy); | 
|  | 5947 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy); | 
| Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 5948 | CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(), | 
| Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 5949 | DestPtr, SrcPtr, Size); | 
| Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 5950 | return; | 
|  | 5951 | } | 
|  | 5952 |  | 
| Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5953 | /// EmitObjCWeakRead - Code gen for loading value of a __weak | 
|  | 5954 | /// object: objc_read_weak (id *src) | 
|  | 5955 | /// | 
|  | 5956 | llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead( | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5957 | CodeGen::CodeGenFunction &CGF, | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5958 | llvm::Value *AddrWeakObj) { | 
| Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5959 | llvm::Type* DestTy = | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5960 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); | 
|  | 5961 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); | 
| Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 5962 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), | 
| Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5963 | AddrWeakObj, "weakread"); | 
| Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5964 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); | 
| Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5965 | return read_weak; | 
|  | 5966 | } | 
|  | 5967 |  | 
|  | 5968 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. | 
|  | 5969 | /// objc_assign_weak (id src, id *dst) | 
|  | 5970 | /// | 
|  | 5971 | void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, | 
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5972 | llvm::Value *src, llvm::Value *dst) { | 
| Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5973 | llvm::Type * SrcTy = src->getType(); | 
| Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5974 | if (!isa<llvm::PointerType>(SrcTy)) { | 
| Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5975 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); | 
| Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5976 | assert(Size <= 8 && "does not support size > 8"); | 
|  | 5977 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) | 
|  | 5978 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); | 
| Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5979 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); | 
|  | 5980 | } | 
| Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5981 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); | 
|  | 5982 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); | 
| Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 5983 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), | 
| Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5984 | src, dst, "weakassign"); | 
|  | 5985 | return; | 
|  | 5986 | } | 
|  | 5987 |  | 
|  | 5988 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. | 
|  | 5989 | /// objc_assign_global (id src, id *dst) | 
|  | 5990 | /// | 
|  | 5991 | void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, | 
| Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 5992 | llvm::Value *src, llvm::Value *dst, | 
|  | 5993 | bool threadlocal) { | 
| Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 5994 | llvm::Type * SrcTy = src->getType(); | 
| Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5995 | if (!isa<llvm::PointerType>(SrcTy)) { | 
| Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5996 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); | 
| Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5997 | assert(Size <= 8 && "does not support size > 8"); | 
|  | 5998 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) | 
|  | 5999 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); | 
| Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 6000 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); | 
|  | 6001 | } | 
| Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6002 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); | 
|  | 6003 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); | 
| Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 6004 | if (!threadlocal) | 
|  | 6005 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), | 
|  | 6006 | src, dst, "globalassign"); | 
|  | 6007 | else | 
|  | 6008 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(), | 
|  | 6009 | src, dst, "threadlocalassign"); | 
| Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 6010 | return; | 
|  | 6011 | } | 
| Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 6012 |  | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6013 | void | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6014 | CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, | 
|  | 6015 | const ObjCAtSynchronizedStmt &S) { | 
| David Chisnall | 05dc91b | 2011-03-25 17:46:35 +0000 | [diff] [blame] | 6016 | EmitAtSynchronizedStmt(CGF, S, | 
|  | 6017 | cast<llvm::Function>(ObjCTypes.getSyncEnterFn()), | 
|  | 6018 | cast<llvm::Function>(ObjCTypes.getSyncExitFn())); | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6019 | } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6020 |  | 
| John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6021 | llvm::Constant * | 
| Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 6022 | CGObjCNonFragileABIMac::GetEHType(QualType T) { | 
| John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6023 | // There's a particular fixed type info for 'id'. | 
|  | 6024 | if (T->isObjCIdType() || | 
|  | 6025 | T->isObjCQualifiedIdType()) { | 
|  | 6026 | llvm::Constant *IDEHType = | 
|  | 6027 | CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id"); | 
|  | 6028 | if (!IDEHType) | 
|  | 6029 | IDEHType = | 
|  | 6030 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, | 
|  | 6031 | false, | 
|  | 6032 | llvm::GlobalValue::ExternalLinkage, | 
|  | 6033 | 0, "OBJC_EHTYPE_id"); | 
|  | 6034 | return IDEHType; | 
|  | 6035 | } | 
|  | 6036 |  | 
|  | 6037 | // All other types should be Objective-C interface pointer types. | 
|  | 6038 | const ObjCObjectPointerType *PT = | 
|  | 6039 | T->getAs<ObjCObjectPointerType>(); | 
|  | 6040 | assert(PT && "Invalid @catch type."); | 
|  | 6041 | const ObjCInterfaceType *IT = PT->getInterfaceType(); | 
|  | 6042 | assert(IT && "Invalid @catch type."); | 
|  | 6043 | return GetInterfaceEHType(IT->getDecl(), false); | 
|  | 6044 | } | 
|  | 6045 |  | 
| John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 6046 | void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF, | 
|  | 6047 | const ObjCAtTryStmt &S) { | 
| David Chisnall | 05dc91b | 2011-03-25 17:46:35 +0000 | [diff] [blame] | 6048 | EmitTryCatchStmt(CGF, S, | 
|  | 6049 | cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()), | 
|  | 6050 | cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()), | 
|  | 6051 | cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn())); | 
| Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 6052 | } | 
|  | 6053 |  | 
| Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6054 | /// EmitThrowStmt - Generate code for a throw statement. | 
|  | 6055 | void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, | 
|  | 6056 | const ObjCAtThrowStmt &S) { | 
| Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6057 | if (const Expr *ThrowExpr = S.getThrowExpr()) { | 
| John McCall | 2b014d6 | 2011-10-01 10:32:24 +0000 | [diff] [blame] | 6058 | llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr); | 
| Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 6059 | Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy); | 
| Jay Foad | 4c7d9f1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 6060 | CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception) | 
| John McCall | 7ec404c | 2010-10-16 08:21:07 +0000 | [diff] [blame] | 6061 | .setDoesNotReturn(); | 
| Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6062 | } else { | 
| Jay Foad | 4c7d9f1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 6063 | CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn()) | 
| John McCall | 7ec404c | 2010-10-16 08:21:07 +0000 | [diff] [blame] | 6064 | .setDoesNotReturn(); | 
| Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6065 | } | 
|  | 6066 |  | 
| John McCall | 7ec404c | 2010-10-16 08:21:07 +0000 | [diff] [blame] | 6067 | CGF.Builder.CreateUnreachable(); | 
| Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 6068 | CGF.Builder.ClearInsertionPoint(); | 
|  | 6069 | } | 
| Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6070 |  | 
| John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 6071 | llvm::Constant * | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6072 | CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID, | 
| Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6073 | bool ForDefinition) { | 
| Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6074 | llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()]; | 
| Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6075 |  | 
| Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6076 | // If we don't need a definition, return the entry if found or check | 
|  | 6077 | // if we use an external reference. | 
|  | 6078 | if (!ForDefinition) { | 
|  | 6079 | if (Entry) | 
|  | 6080 | return Entry; | 
| Daniel Dunbar | 7e075cb | 2009-04-07 06:43:45 +0000 | [diff] [blame] | 6081 |  | 
| Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6082 | // If this type (or a super class) has the __objc_exception__ | 
|  | 6083 | // attribute, emit an external reference. | 
| Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 6084 | if (hasObjCExceptionAttribute(CGM.getContext(), ID)) | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6085 | return Entry = | 
| Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6086 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false, | 
| Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6087 | llvm::GlobalValue::ExternalLinkage, | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6088 | 0, | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6089 | ("OBJC_EHTYPE_$_" + | 
| Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 6090 | ID->getIdentifier()->getName())); | 
| Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6091 | } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6092 |  | 
| Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6093 | // Otherwise we need to either make a new entry or fill in the | 
|  | 6094 | // initializer. | 
|  | 6095 | assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition"); | 
| Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 6096 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); | 
| Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6097 | std::string VTableName = "objc_ehtype_vtable"; | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6098 | llvm::GlobalVariable *VTableGV = | 
| Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6099 | CGM.getModule().getGlobalVariable(VTableName); | 
|  | 6100 | if (!VTableGV) | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6101 | VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy, | 
|  | 6102 | false, | 
| Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6103 | llvm::GlobalValue::ExternalLinkage, | 
| Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6104 | 0, VTableName); | 
| Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6105 |  | 
| Chris Lattner | 77b89b8 | 2010-06-27 07:15:29 +0000 | [diff] [blame] | 6106 | llvm::Value *VTableIdx = | 
|  | 6107 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2); | 
| Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6108 |  | 
| Benjamin Kramer | 1d236ab | 2011-10-15 12:20:02 +0000 | [diff] [blame] | 6109 | llvm::Constant *Values[] = { | 
|  | 6110 | llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx), | 
|  | 6111 | GetClassName(ID->getIdentifier()), | 
|  | 6112 | GetClassGlobal(ClassName) | 
|  | 6113 | }; | 
| Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 6114 | llvm::Constant *Init = | 
| Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 6115 | llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values); | 
| Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6116 |  | 
| Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6117 | if (Entry) { | 
|  | 6118 | Entry->setInitializer(Init); | 
|  | 6119 | } else { | 
| Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 6120 | Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false, | 
| Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6121 | llvm::GlobalValue::WeakAnyLinkage, | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6122 | Init, | 
| Daniel Dunbar | 9c29bf5 | 2009-10-18 20:48:59 +0000 | [diff] [blame] | 6123 | ("OBJC_EHTYPE_$_" + | 
| Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 6124 | ID->getIdentifier()->getName())); | 
| Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6125 | } | 
|  | 6126 |  | 
| John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 6127 | if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility) | 
| Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 6128 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); | 
| Daniel Dunbar | 4b429ae | 2010-04-25 20:39:32 +0000 | [diff] [blame] | 6129 | Entry->setAlignment(CGM.getTargetData().getABITypeAlignment( | 
|  | 6130 | ObjCTypes.EHTypeTy)); | 
| Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 6131 |  | 
|  | 6132 | if (ForDefinition) { | 
|  | 6133 | Entry->setSection("__DATA,__objc_const"); | 
|  | 6134 | Entry->setLinkage(llvm::GlobalValue::ExternalLinkage); | 
|  | 6135 | } else { | 
|  | 6136 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); | 
|  | 6137 | } | 
| Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 6138 |  | 
|  | 6139 | return Entry; | 
|  | 6140 | } | 
| Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 6141 |  | 
| Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 6142 | /* *** */ | 
|  | 6143 |  | 
| Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 6144 | CodeGen::CGObjCRuntime * | 
|  | 6145 | CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { | 
| David Chisnall | 60be607 | 2011-03-22 21:21:24 +0000 | [diff] [blame] | 6146 | if (CGM.getLangOptions().ObjCNonFragileABI) | 
|  | 6147 | return new CGObjCNonFragileABIMac(CGM); | 
| Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 6148 | return new CGObjCMac(CGM); | 
|  | 6149 | } |