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 | // |
| 10 | // This provides Objective-C code generation targetting the Apple runtime. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CGObjCRuntime.h" |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 15 | |
| 16 | #include "CodeGenModule.h" |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 17 | #include "CodeGenFunction.h" |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 19 | #include "clang/AST/Decl.h" |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclObjC.h" |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 21 | #include "clang/Basic/LangOptions.h" |
| 22 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 23 | #include "llvm/Intrinsics.h" |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 24 | #include "llvm/Module.h" |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/DenseSet.h" |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetData.h" |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 27 | #include <sstream> |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace clang; |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 30 | using namespace CodeGen; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 31 | |
| 32 | namespace { |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 33 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 34 | typedef std::vector<llvm::Constant*> ConstantVector; |
| 35 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 36 | // FIXME: We should find a nicer way to make the labels for |
| 37 | // metadata, string concatenation is lame. |
| 38 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 39 | class ObjCCommonTypesHelper { |
| 40 | protected: |
| 41 | CodeGen::CodeGenModule &CGM; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 42 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 43 | public: |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 44 | const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 45 | const llvm::Type *Int8PtrTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 46 | |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 47 | /// ObjectPtrTy - LLVM type for object handles (typeof(id)) |
| 48 | const llvm::Type *ObjectPtrTy; |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 49 | |
| 50 | /// PtrObjectPtrTy - LLVM type for id * |
| 51 | const llvm::Type *PtrObjectPtrTy; |
| 52 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 53 | /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL)) |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 54 | const llvm::Type *SelectorPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 55 | /// ProtocolPtrTy - LLVM type for external protocol handles |
| 56 | /// (typeof(Protocol)) |
| 57 | const llvm::Type *ExternalProtocolPtrTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 58 | |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 59 | // SuperCTy - clang type for struct objc_super. |
| 60 | QualType SuperCTy; |
| 61 | // SuperPtrCTy - clang type for struct objc_super *. |
| 62 | QualType SuperPtrCTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 63 | |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 64 | /// SuperTy - LLVM type for struct objc_super. |
| 65 | const llvm::StructType *SuperTy; |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 66 | /// SuperPtrTy - LLVM type for struct objc_super *. |
| 67 | const llvm::Type *SuperPtrTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 68 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 69 | /// PropertyTy - LLVM type for struct objc_property (struct _prop_t |
| 70 | /// in GCC parlance). |
| 71 | const llvm::StructType *PropertyTy; |
| 72 | |
| 73 | /// PropertyListTy - LLVM type for struct objc_property_list |
| 74 | /// (_prop_list_t in GCC parlance). |
| 75 | const llvm::StructType *PropertyListTy; |
| 76 | /// PropertyListPtrTy - LLVM type for struct objc_property_list*. |
| 77 | const llvm::Type *PropertyListPtrTy; |
| 78 | |
| 79 | // MethodTy - LLVM type for struct objc_method. |
| 80 | const llvm::StructType *MethodTy; |
| 81 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 82 | /// CacheTy - LLVM type for struct objc_cache. |
| 83 | const llvm::Type *CacheTy; |
| 84 | /// CachePtrTy - LLVM type for struct objc_cache *. |
| 85 | const llvm::Type *CachePtrTy; |
| 86 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 87 | llvm::Constant *GetPropertyFn, *SetPropertyFn; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 88 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 89 | llvm::Constant *EnumerationMutationFn; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 90 | |
| 91 | /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 92 | llvm::Constant *GcReadWeakFn; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 93 | |
| 94 | /// GcAssignWeakFn -- LLVM objc_assign_weak function. |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 95 | llvm::Constant *getGcAssignWeakFn() { |
| 96 | // id objc_assign_weak (id, id *) |
| 97 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 98 | Args.push_back(ObjectPtrTy->getPointerTo()); |
| 99 | llvm::FunctionType *FTy = |
| 100 | llvm::FunctionType::get(ObjectPtrTy, Args, false); |
| 101 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak"); |
| 102 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 103 | |
| 104 | /// GcAssignGlobalFn -- LLVM objc_assign_global function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 105 | llvm::Constant *GcAssignGlobalFn; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 106 | |
| 107 | /// GcAssignIvarFn -- LLVM objc_assign_ivar function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 108 | llvm::Constant *GcAssignIvarFn; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 109 | |
| 110 | /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 111 | llvm::Constant *GcAssignStrongCastFn; |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 112 | |
| 113 | /// ExceptionThrowFn - LLVM objc_exception_throw function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 114 | llvm::Constant *ExceptionThrowFn; |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 115 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 116 | /// SyncEnterFn - LLVM object_sync_enter function. |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 117 | llvm::Constant *getSyncEnterFn() { |
| 118 | // void objc_sync_enter (id) |
| 119 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 120 | llvm::FunctionType *FTy = |
| 121 | llvm::FunctionType::get(llvm::Type::VoidTy, Args, false); |
| 122 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter"); |
| 123 | } |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 124 | |
| 125 | /// SyncExitFn - LLVM object_sync_exit function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 126 | llvm::Constant *SyncExitFn; |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 127 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 128 | ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm); |
| 129 | ~ObjCCommonTypesHelper(){} |
| 130 | }; |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 131 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 132 | /// ObjCTypesHelper - Helper class that encapsulates lazy |
| 133 | /// construction of varies types used during ObjC generation. |
| 134 | class ObjCTypesHelper : public ObjCCommonTypesHelper { |
| 135 | private: |
| 136 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 137 | llvm::Constant *MessageSendFn, *MessageSendStretFn, *MessageSendFpretFn; |
| 138 | llvm::Constant *MessageSendSuperFn, *MessageSendSuperStretFn, |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 139 | *MessageSendSuperFpretFn; |
| 140 | |
| 141 | public: |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 142 | /// SymtabTy - LLVM type for struct objc_symtab. |
| 143 | const llvm::StructType *SymtabTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 144 | /// SymtabPtrTy - LLVM type for struct objc_symtab *. |
| 145 | const llvm::Type *SymtabPtrTy; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 146 | /// ModuleTy - LLVM type for struct objc_module. |
| 147 | const llvm::StructType *ModuleTy; |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 148 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 149 | /// ProtocolTy - LLVM type for struct objc_protocol. |
| 150 | const llvm::StructType *ProtocolTy; |
| 151 | /// ProtocolPtrTy - LLVM type for struct objc_protocol *. |
| 152 | const llvm::Type *ProtocolPtrTy; |
| 153 | /// ProtocolExtensionTy - LLVM type for struct |
| 154 | /// objc_protocol_extension. |
| 155 | const llvm::StructType *ProtocolExtensionTy; |
| 156 | /// ProtocolExtensionTy - LLVM type for struct |
| 157 | /// objc_protocol_extension *. |
| 158 | const llvm::Type *ProtocolExtensionPtrTy; |
| 159 | /// MethodDescriptionTy - LLVM type for struct |
| 160 | /// objc_method_description. |
| 161 | const llvm::StructType *MethodDescriptionTy; |
| 162 | /// MethodDescriptionListTy - LLVM type for struct |
| 163 | /// objc_method_description_list. |
| 164 | const llvm::StructType *MethodDescriptionListTy; |
| 165 | /// MethodDescriptionListPtrTy - LLVM type for struct |
| 166 | /// objc_method_description_list *. |
| 167 | const llvm::Type *MethodDescriptionListPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 168 | /// ProtocolListTy - LLVM type for struct objc_property_list. |
| 169 | const llvm::Type *ProtocolListTy; |
| 170 | /// ProtocolListPtrTy - LLVM type for struct objc_property_list*. |
| 171 | const llvm::Type *ProtocolListPtrTy; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 172 | /// CategoryTy - LLVM type for struct objc_category. |
| 173 | const llvm::StructType *CategoryTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 174 | /// ClassTy - LLVM type for struct objc_class. |
| 175 | const llvm::StructType *ClassTy; |
| 176 | /// ClassPtrTy - LLVM type for struct objc_class *. |
| 177 | const llvm::Type *ClassPtrTy; |
| 178 | /// ClassExtensionTy - LLVM type for struct objc_class_ext. |
| 179 | const llvm::StructType *ClassExtensionTy; |
| 180 | /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *. |
| 181 | const llvm::Type *ClassExtensionPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 182 | // IvarTy - LLVM type for struct objc_ivar. |
| 183 | const llvm::StructType *IvarTy; |
| 184 | /// IvarListTy - LLVM type for struct objc_ivar_list. |
| 185 | const llvm::Type *IvarListTy; |
| 186 | /// IvarListPtrTy - LLVM type for struct objc_ivar_list *. |
| 187 | const llvm::Type *IvarListPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 188 | /// MethodListTy - LLVM type for struct objc_method_list. |
| 189 | const llvm::Type *MethodListTy; |
| 190 | /// MethodListPtrTy - LLVM type for struct objc_method_list *. |
| 191 | const llvm::Type *MethodListPtrTy; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 192 | |
| 193 | /// ExceptionDataTy - LLVM type for struct _objc_exception_data. |
| 194 | const llvm::Type *ExceptionDataTy; |
| 195 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 196 | /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame^] | 197 | llvm::Constant *getExceptionTryEnterFn() { |
| 198 | std::vector<const llvm::Type*> Params; |
| 199 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
| 200 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 201 | Params, false), |
| 202 | "objc_exception_try_enter"); |
| 203 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 204 | |
| 205 | /// ExceptionTryExitFn - LLVM objc_exception_try_exit function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame^] | 206 | llvm::Constant *getExceptionTryExitFn() { |
| 207 | std::vector<const llvm::Type*> Params; |
| 208 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
| 209 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 210 | Params, false), |
| 211 | "objc_exception_try_exit"); |
| 212 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 213 | |
| 214 | /// ExceptionExtractFn - LLVM objc_exception_extract function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame^] | 215 | llvm::Constant *getExceptionExtractFn() { |
| 216 | std::vector<const llvm::Type*> Params; |
| 217 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
| 218 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 219 | Params, false), |
| 220 | "objc_exception_extract"); |
| 221 | |
| 222 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 223 | |
| 224 | /// ExceptionMatchFn - LLVM objc_exception_match function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame^] | 225 | llvm::Constant *getExceptionMatchFn() { |
| 226 | std::vector<const llvm::Type*> Params; |
| 227 | Params.push_back(ClassPtrTy); |
| 228 | Params.push_back(ObjectPtrTy); |
| 229 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 230 | Params, false), |
| 231 | "objc_exception_match"); |
| 232 | |
| 233 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 234 | |
| 235 | /// SetJmpFn - LLVM _setjmp function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame^] | 236 | llvm::Constant *getSetJmpFn() { |
| 237 | std::vector<const llvm::Type*> Params; |
| 238 | Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty)); |
| 239 | return |
| 240 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 241 | Params, false), |
| 242 | "_setjmp"); |
| 243 | |
| 244 | } |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 245 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 246 | public: |
| 247 | ObjCTypesHelper(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 248 | ~ObjCTypesHelper() {} |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 249 | |
| 250 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 251 | llvm::Constant *getSendFn(bool IsSuper) { |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 252 | return IsSuper ? MessageSendSuperFn : MessageSendFn; |
| 253 | } |
| 254 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 255 | llvm::Constant *getSendStretFn(bool IsSuper) { |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 256 | return IsSuper ? MessageSendSuperStretFn : MessageSendStretFn; |
| 257 | } |
| 258 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 259 | llvm::Constant *getSendFpretFn(bool IsSuper) { |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 260 | return IsSuper ? MessageSendSuperFpretFn : MessageSendFpretFn; |
| 261 | } |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 262 | }; |
| 263 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 264 | /// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 265 | /// modern abi |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 266 | class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 267 | public: |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 268 | llvm::Constant *MessageSendFixupFn, *MessageSendFpretFixupFn, |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 269 | *MessageSendStretFixupFn, *MessageSendIdFixupFn, |
| 270 | *MessageSendIdStretFixupFn, *MessageSendSuper2FixupFn, |
| 271 | *MessageSendSuper2StretFixupFn; |
| 272 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 273 | // MethodListnfABITy - LLVM for struct _method_list_t |
| 274 | const llvm::StructType *MethodListnfABITy; |
| 275 | |
| 276 | // MethodListnfABIPtrTy - LLVM for struct _method_list_t* |
| 277 | const llvm::Type *MethodListnfABIPtrTy; |
| 278 | |
| 279 | // ProtocolnfABITy = LLVM for struct _protocol_t |
| 280 | const llvm::StructType *ProtocolnfABITy; |
| 281 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 282 | // ProtocolnfABIPtrTy = LLVM for struct _protocol_t* |
| 283 | const llvm::Type *ProtocolnfABIPtrTy; |
| 284 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 285 | // ProtocolListnfABITy - LLVM for struct _objc_protocol_list |
| 286 | const llvm::StructType *ProtocolListnfABITy; |
| 287 | |
| 288 | // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list* |
| 289 | const llvm::Type *ProtocolListnfABIPtrTy; |
| 290 | |
| 291 | // ClassnfABITy - LLVM for struct _class_t |
| 292 | const llvm::StructType *ClassnfABITy; |
| 293 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 294 | // ClassnfABIPtrTy - LLVM for struct _class_t* |
| 295 | const llvm::Type *ClassnfABIPtrTy; |
| 296 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 297 | // IvarnfABITy - LLVM for struct _ivar_t |
| 298 | const llvm::StructType *IvarnfABITy; |
| 299 | |
| 300 | // IvarListnfABITy - LLVM for struct _ivar_list_t |
| 301 | const llvm::StructType *IvarListnfABITy; |
| 302 | |
| 303 | // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t* |
| 304 | const llvm::Type *IvarListnfABIPtrTy; |
| 305 | |
| 306 | // ClassRonfABITy - LLVM for struct _class_ro_t |
| 307 | const llvm::StructType *ClassRonfABITy; |
| 308 | |
| 309 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 310 | const llvm::Type *ImpnfABITy; |
| 311 | |
| 312 | // CategorynfABITy - LLVM for struct _category_t |
| 313 | const llvm::StructType *CategorynfABITy; |
| 314 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 315 | // New types for nonfragile abi messaging. |
| 316 | |
| 317 | // MessageRefTy - LLVM for: |
| 318 | // struct _message_ref_t { |
| 319 | // IMP messenger; |
| 320 | // SEL name; |
| 321 | // }; |
| 322 | const llvm::StructType *MessageRefTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 323 | // MessageRefCTy - clang type for struct _message_ref_t |
| 324 | QualType MessageRefCTy; |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 325 | |
| 326 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
| 327 | const llvm::Type *MessageRefPtrTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 328 | // MessageRefCPtrTy - clang type for struct _message_ref_t* |
| 329 | QualType MessageRefCPtrTy; |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 330 | |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 331 | // MessengerTy - Type of the messenger (shown as IMP above) |
| 332 | const llvm::FunctionType *MessengerTy; |
| 333 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 334 | // SuperMessageRefTy - LLVM for: |
| 335 | // struct _super_message_ref_t { |
| 336 | // SUPER_IMP messenger; |
| 337 | // SEL name; |
| 338 | // }; |
| 339 | const llvm::StructType *SuperMessageRefTy; |
| 340 | |
| 341 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
| 342 | const llvm::Type *SuperMessageRefPtrTy; |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 343 | |
| 344 | /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C |
| 345 | /// exception personality function. |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 346 | llvm::Value *getEHPersonalityPtr() { |
| 347 | llvm::Constant *Personality = |
| 348 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 349 | std::vector<const llvm::Type*>(), |
| 350 | true), |
| 351 | "__objc_personality_v0"); |
| 352 | return llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy); |
| 353 | } |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 354 | |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 355 | llvm::Constant *getUnwindResumeOrRethrowFn() { |
| 356 | std::vector<const llvm::Type*> Params; |
| 357 | Params.push_back(Int8PtrTy); |
| 358 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 359 | Params, false), |
| 360 | "_Unwind_Resume_or_Rethrow"); |
| 361 | } |
| 362 | |
| 363 | llvm::Constant *getObjCEndCatchFn() { |
| 364 | std::vector<const llvm::Type*> Params; |
| 365 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 366 | Params, false), |
| 367 | "objc_end_catch"); |
| 368 | |
| 369 | } |
| 370 | |
| 371 | llvm::Constant *getObjCBeginCatchFn() { |
| 372 | std::vector<const llvm::Type*> Params; |
| 373 | Params.push_back(Int8PtrTy); |
| 374 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy, |
| 375 | Params, false), |
| 376 | "objc_begin_catch"); |
| 377 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 378 | |
| 379 | const llvm::StructType *EHTypeTy; |
| 380 | const llvm::Type *EHTypePtrTy; |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 381 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 382 | ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm); |
| 383 | ~ObjCNonFragileABITypesHelper(){} |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 384 | }; |
| 385 | |
| 386 | class CGObjCCommonMac : public CodeGen::CGObjCRuntime { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 387 | public: |
| 388 | // FIXME - accessibility |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 389 | class GC_IVAR { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 390 | public: |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 391 | unsigned int ivar_bytepos; |
| 392 | unsigned int ivar_size; |
| 393 | GC_IVAR() : ivar_bytepos(0), ivar_size(0) {} |
| 394 | }; |
| 395 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 396 | class SKIP_SCAN { |
| 397 | public: |
| 398 | unsigned int skip; |
| 399 | unsigned int scan; |
| 400 | SKIP_SCAN() : skip(0), scan(0) {} |
| 401 | }; |
| 402 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 403 | protected: |
| 404 | CodeGen::CodeGenModule &CGM; |
| 405 | // FIXME! May not be needing this after all. |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 406 | unsigned ObjCABI; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 407 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 408 | // gc ivar layout bitmap calculation helper caches. |
| 409 | llvm::SmallVector<GC_IVAR, 16> SkipIvars; |
| 410 | llvm::SmallVector<GC_IVAR, 16> IvarsInfo; |
| 411 | llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 412 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 413 | /// LazySymbols - Symbols to generate a lazy reference for. See |
| 414 | /// DefinedSymbols and FinishModule(). |
| 415 | std::set<IdentifierInfo*> LazySymbols; |
| 416 | |
| 417 | /// DefinedSymbols - External symbols which are defined by this |
| 418 | /// module. The symbols in this list and LazySymbols are used to add |
| 419 | /// special linker symbols which ensure that Objective-C modules are |
| 420 | /// linked properly. |
| 421 | std::set<IdentifierInfo*> DefinedSymbols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 422 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 423 | /// ClassNames - uniqued class names. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 424 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 425 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 426 | /// MethodVarNames - uniqued method variable names. |
| 427 | llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 428 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 429 | /// MethodVarTypes - uniqued method type signatures. We have to use |
| 430 | /// a StringMap here because have no other unique reference. |
| 431 | llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 432 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 433 | /// MethodDefinitions - map of methods which have been defined in |
| 434 | /// this translation unit. |
| 435 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 436 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 437 | /// PropertyNames - uniqued method variable names. |
| 438 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 439 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 440 | /// ClassReferences - uniqued class references. |
| 441 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 442 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 443 | /// SelectorReferences - uniqued selector references. |
| 444 | llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 445 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 446 | /// Protocols - Protocols for which an objc_protocol structure has |
| 447 | /// been emitted. Forward declarations are handled by creating an |
| 448 | /// empty structure whose initializer is filled in when/if defined. |
| 449 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 450 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 451 | /// DefinedProtocols - Protocols which have actually been |
| 452 | /// defined. We should not need this, see FIXME in GenerateProtocol. |
| 453 | llvm::DenseSet<IdentifierInfo*> DefinedProtocols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 454 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 455 | /// DefinedClasses - List of defined classes. |
| 456 | std::vector<llvm::GlobalValue*> DefinedClasses; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 457 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 458 | /// DefinedCategories - List of defined categories. |
| 459 | std::vector<llvm::GlobalValue*> DefinedCategories; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 460 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 461 | /// UsedGlobals - List of globals to pack into the llvm.used metadata |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 462 | /// to prevent them from being clobbered. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 463 | std::vector<llvm::GlobalVariable*> UsedGlobals; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 464 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 465 | /// GetNameForMethod - Return a name for the given method. |
| 466 | /// \param[out] NameOut - The return value. |
| 467 | void GetNameForMethod(const ObjCMethodDecl *OMD, |
| 468 | const ObjCContainerDecl *CD, |
| 469 | std::string &NameOut); |
| 470 | |
| 471 | /// GetMethodVarName - Return a unique constant for the given |
| 472 | /// selector's name. The return value has type char *. |
| 473 | llvm::Constant *GetMethodVarName(Selector Sel); |
| 474 | llvm::Constant *GetMethodVarName(IdentifierInfo *Ident); |
| 475 | llvm::Constant *GetMethodVarName(const std::string &Name); |
| 476 | |
| 477 | /// GetMethodVarType - Return a unique constant for the given |
| 478 | /// selector's name. The return value has type char *. |
| 479 | |
| 480 | // FIXME: This is a horrible name. |
| 481 | llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 482 | llvm::Constant *GetMethodVarType(const FieldDecl *D); |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 483 | |
| 484 | /// GetPropertyName - Return a unique constant for the given |
| 485 | /// name. The return value has type char *. |
| 486 | llvm::Constant *GetPropertyName(IdentifierInfo *Ident); |
| 487 | |
| 488 | // FIXME: This can be dropped once string functions are unified. |
| 489 | llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 490 | const Decl *Container); |
| 491 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 492 | /// GetClassName - Return a unique constant for the given selector's |
| 493 | /// name. The return value has type char *. |
| 494 | llvm::Constant *GetClassName(IdentifierInfo *Ident); |
| 495 | |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 496 | /// GetInterfaceDeclStructLayout - Get layout for ivars of given |
| 497 | /// interface declaration. |
| 498 | const llvm::StructLayout *GetInterfaceDeclStructLayout( |
| 499 | const ObjCInterfaceDecl *ID) const; |
| 500 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 501 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 502 | /// implementation for the __strong or __weak case. |
| 503 | /// |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 504 | llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI, |
| 505 | bool ForStrongLayout); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 506 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 507 | void BuildAggrIvarLayout(const ObjCInterfaceDecl *OI, |
| 508 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 509 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 510 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 511 | unsigned int BytePos, bool ForStrongLayout, |
| 512 | int &Index, int &SkIndex, bool &HasUnion); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 513 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 514 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 515 | /// ivar layout bitmap. |
| 516 | llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident, |
| 517 | const ObjCCommonTypesHelper &ObjCTypes); |
| 518 | |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 519 | /// EmitPropertyList - Emit the given property list. The return |
| 520 | /// value has type PropertyListPtrTy. |
| 521 | llvm::Constant *EmitPropertyList(const std::string &Name, |
| 522 | const Decl *Container, |
| 523 | const ObjCContainerDecl *OCD, |
| 524 | const ObjCCommonTypesHelper &ObjCTypes); |
| 525 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 526 | /// GetProtocolRef - Return a reference to the internal protocol |
| 527 | /// description, creating an empty one if it has not been |
| 528 | /// defined. The return value has type ProtocolPtrTy. |
| 529 | llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD); |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 530 | |
| 531 | /// GetIvarBaseOffset - returns ivars byte offset. |
| 532 | uint64_t GetIvarBaseOffset(const llvm::StructLayout *Layout, |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 533 | const FieldDecl *Field); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 534 | |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 535 | /// GetFieldBaseOffset - return's field byte offset. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 536 | uint64_t GetFieldBaseOffset(const ObjCInterfaceDecl *OI, |
| 537 | const llvm::StructLayout *Layout, |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 538 | const FieldDecl *Field); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 539 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 540 | /// CreateMetadataVar - Create a global variable with internal |
| 541 | /// linkage for use by the Objective-C runtime. |
| 542 | /// |
| 543 | /// This is a convenience wrapper which not only creates the |
| 544 | /// variable, but also sets the section and alignment and adds the |
| 545 | /// global to the UsedGlobals list. |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 546 | /// |
| 547 | /// \param Name - The variable name. |
| 548 | /// \param Init - The variable initializer; this is also used to |
| 549 | /// define the type of the variable. |
| 550 | /// \param Section - The section the variable should go into, or 0. |
| 551 | /// \param Align - The alignment for the variable, or 0. |
| 552 | /// \param AddToUsed - Whether the variable should be added to |
Daniel Dunbar | c158306 | 2009-04-14 17:42:51 +0000 | [diff] [blame] | 553 | /// "llvm.used". |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 554 | llvm::GlobalVariable *CreateMetadataVar(const std::string &Name, |
| 555 | llvm::Constant *Init, |
| 556 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 557 | unsigned Align, |
| 558 | bool AddToUsed); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 559 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 560 | /// GetNamedIvarList - Return the list of ivars in the interface |
| 561 | /// itself (not including super classes and not including unnamed |
| 562 | /// bitfields). |
| 563 | /// |
| 564 | /// For the non-fragile ABI, this also includes synthesized property |
| 565 | /// ivars. |
| 566 | void GetNamedIvarList(const ObjCInterfaceDecl *OID, |
| 567 | llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const; |
| 568 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 569 | public: |
| 570 | CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm) |
| 571 | { } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 572 | |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 573 | virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *SL); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 574 | |
| 575 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
| 576 | const ObjCContainerDecl *CD=0); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 577 | |
| 578 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); |
| 579 | |
| 580 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 581 | /// declaration, emitting it if necessary. The return value has type |
| 582 | /// ProtocolPtrTy. |
| 583 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0; |
| 584 | |
| 585 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 586 | /// object for the given declaration, emitting it if needed. These |
| 587 | /// forward references will be filled in with empty bodies if no |
| 588 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 589 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 590 | }; |
| 591 | |
| 592 | class CGObjCMac : public CGObjCCommonMac { |
| 593 | private: |
| 594 | ObjCTypesHelper ObjCTypes; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 595 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 596 | /// level information. |
| 597 | void EmitImageInfo(); |
| 598 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 599 | /// EmitModuleInfo - Another marker encoding module level |
| 600 | /// information. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 601 | void EmitModuleInfo(); |
| 602 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 603 | /// EmitModuleSymols - Emit module symbols, the list of defined |
| 604 | /// classes and categories. The result has type SymtabPtrTy. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 605 | llvm::Constant *EmitModuleSymbols(); |
| 606 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 607 | /// FinishModule - Write out global data structures at the end of |
| 608 | /// processing a translation unit. |
| 609 | void FinishModule(); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 610 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 611 | /// EmitClassExtension - Generate the class extension structure used |
| 612 | /// to store the weak ivar layout and properties. The return value |
| 613 | /// has type ClassExtensionPtrTy. |
| 614 | llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID); |
| 615 | |
| 616 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 617 | /// for the given class. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 618 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 619 | const ObjCInterfaceDecl *ID); |
| 620 | |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 621 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 622 | QualType ResultType, |
| 623 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 624 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 625 | QualType Arg0Ty, |
| 626 | bool IsSuper, |
| 627 | const CallArgList &CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 628 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 629 | /// EmitIvarList - Emit the ivar list for the given |
| 630 | /// implementation. If ForClass is true the list of class ivars |
| 631 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 632 | /// interface ivars will be emitted. The return value has type |
| 633 | /// IvarListPtrTy. |
| 634 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 635 | bool ForClass); |
| 636 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 637 | /// EmitMetaClass - Emit a forward reference to the class structure |
| 638 | /// for the metaclass of the given interface. The return value has |
| 639 | /// type ClassPtrTy. |
| 640 | llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID); |
| 641 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 642 | /// EmitMetaClass - Emit a class structure for the metaclass of the |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 643 | /// given implementation. The return value has type ClassPtrTy. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 644 | llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID, |
| 645 | llvm::Constant *Protocols, |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 646 | const llvm::Type *InterfaceTy, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 647 | const ConstantVector &Methods); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 648 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 649 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 650 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 651 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 652 | |
| 653 | /// EmitMethodList - Emit the method list for the given |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 654 | /// implementation. The return value has type MethodListPtrTy. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 655 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 656 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 657 | const ConstantVector &Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 658 | |
| 659 | /// EmitMethodDescList - Emit a method description list for a list of |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 660 | /// method declarations. |
| 661 | /// - TypeName: The name for the type containing the methods. |
| 662 | /// - IsProtocol: True iff these methods are for a protocol. |
| 663 | /// - ClassMethds: True iff these are class methods. |
| 664 | /// - Required: When true, only "required" methods are |
| 665 | /// listed. Similarly, when false only "optional" methods are |
| 666 | /// listed. For classes this should always be true. |
| 667 | /// - begin, end: The method list to output. |
| 668 | /// |
| 669 | /// The return value has type MethodDescriptionListPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 670 | llvm::Constant *EmitMethodDescList(const std::string &Name, |
| 671 | const char *Section, |
| 672 | const ConstantVector &Methods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 673 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 674 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 675 | /// declaration, emitting it if necessary. The return value has type |
| 676 | /// ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 677 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 678 | |
| 679 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 680 | /// object for the given declaration, emitting it if needed. These |
| 681 | /// forward references will be filled in with empty bodies if no |
| 682 | /// definition is seen. The return value has type ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 683 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 684 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 685 | /// EmitProtocolExtension - Generate the protocol extension |
| 686 | /// structure used to store optional instance and class methods, and |
| 687 | /// protocol properties. The return value has type |
| 688 | /// ProtocolExtensionPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 689 | llvm::Constant * |
| 690 | EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 691 | const ConstantVector &OptInstanceMethods, |
| 692 | const ConstantVector &OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 693 | |
| 694 | /// EmitProtocolList - Generate the list of referenced |
| 695 | /// protocols. The return value has type ProtocolListPtrTy. |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 696 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 697 | ObjCProtocolDecl::protocol_iterator begin, |
| 698 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 699 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 700 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 701 | /// for the given selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 702 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 703 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 704 | public: |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 705 | CGObjCMac(CodeGen::CodeGenModule &cgm); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 706 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 707 | virtual llvm::Function *ModuleInitFunction(); |
| 708 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 709 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 710 | QualType ResultType, |
| 711 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 712 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 713 | bool IsClassMessage, |
| 714 | const CallArgList &CallArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 715 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 716 | virtual CodeGen::RValue |
| 717 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 718 | QualType ResultType, |
| 719 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 720 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 721 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 722 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 723 | bool IsClassMessage, |
| 724 | const CallArgList &CallArgs); |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 725 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 726 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 727 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 728 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 729 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 730 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 731 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 732 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 733 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 734 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 735 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 736 | const ObjCProtocolDecl *PD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 737 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 738 | virtual llvm::Constant *GetPropertyGetFunction(); |
| 739 | virtual llvm::Constant *GetPropertySetFunction(); |
| 740 | virtual llvm::Constant *EnumerationMutationFunction(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 741 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 742 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 743 | const Stmt &S); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 744 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 745 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 746 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 747 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 748 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 749 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 750 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 751 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 752 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 753 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 754 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 755 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 756 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 757 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 758 | QualType ObjectTy, |
| 759 | llvm::Value *BaseValue, |
| 760 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 761 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 762 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
| 763 | ObjCInterfaceDecl *Interface, |
| 764 | const ObjCIvarDecl *Ivar); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 765 | }; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 766 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 767 | class CGObjCNonFragileABIMac : public CGObjCCommonMac { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 768 | private: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 769 | ObjCNonFragileABITypesHelper ObjCTypes; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 770 | llvm::GlobalVariable* ObjCEmptyCacheVar; |
| 771 | llvm::GlobalVariable* ObjCEmptyVtableVar; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 772 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 773 | /// SuperClassReferences - uniqued super class references. |
| 774 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences; |
| 775 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 776 | /// MetaClassReferences - uniqued meta class references. |
| 777 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 778 | |
| 779 | /// EHTypeReferences - uniqued class ehtype references. |
| 780 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 781 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 782 | /// FinishNonFragileABIModule - Write out global data structures at the end of |
| 783 | /// processing a translation unit. |
| 784 | void FinishNonFragileABIModule(); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 785 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 786 | llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags, |
| 787 | unsigned InstanceStart, |
| 788 | unsigned InstanceSize, |
| 789 | const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 790 | llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName, |
| 791 | llvm::Constant *IsAGV, |
| 792 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 793 | llvm::Constant *ClassRoGV, |
| 794 | bool HiddenVisibility); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 795 | |
| 796 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
| 797 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 798 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
| 799 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 800 | /// EmitMethodList - Emit the method list for the given |
| 801 | /// implementation. The return value has type MethodListnfABITy. |
| 802 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 803 | const char *Section, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 804 | const ConstantVector &Methods); |
| 805 | /// EmitIvarList - Emit the ivar list for the given |
| 806 | /// implementation. If ForClass is true the list of class ivars |
| 807 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 808 | /// interface ivars will be emitted. The return value has type |
| 809 | /// IvarListnfABIPtrTy. |
| 810 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 811 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 812 | llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 813 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 814 | unsigned long int offset); |
| 815 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 816 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 817 | /// declaration, emitting it if necessary. The return value has type |
| 818 | /// ProtocolPtrTy. |
| 819 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
| 820 | |
| 821 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 822 | /// object for the given declaration, emitting it if needed. These |
| 823 | /// forward references will be filled in with empty bodies if no |
| 824 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 825 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
| 826 | |
| 827 | /// EmitProtocolList - Generate the list of referenced |
| 828 | /// protocols. The return value has type ProtocolListPtrTy. |
| 829 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 830 | ObjCProtocolDecl::protocol_iterator begin, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 831 | ObjCProtocolDecl::protocol_iterator end); |
| 832 | |
| 833 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 834 | QualType ResultType, |
| 835 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 836 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 837 | QualType Arg0Ty, |
| 838 | bool IsSuper, |
| 839 | const CallArgList &CallArgs); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 840 | |
| 841 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 842 | /// class of the given name. |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 843 | llvm::GlobalVariable *GetClassGlobal(const std::string &Name); |
| 844 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 845 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 846 | /// for the given class reference. |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 847 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 848 | const ObjCInterfaceDecl *ID); |
| 849 | |
| 850 | /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 851 | /// for the given super class reference. |
| 852 | llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder, |
| 853 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 854 | |
| 855 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 856 | /// meta-data |
| 857 | llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder, |
| 858 | const ObjCInterfaceDecl *ID); |
| 859 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 860 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 861 | /// the given ivar. |
| 862 | /// |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 863 | llvm::GlobalVariable * ObjCIvarOffsetVariable( |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 864 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 865 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 866 | |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 867 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 868 | /// for the given selector. |
| 869 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 870 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 871 | /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 872 | /// interface. The return value has type EHTypePtrTy. |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 873 | llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
| 874 | bool ForDefinition); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 875 | |
| 876 | const char *getMetaclassSymbolPrefix() const { |
| 877 | return "OBJC_METACLASS_$_"; |
| 878 | } |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 879 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 880 | const char *getClassSymbolPrefix() const { |
| 881 | return "OBJC_CLASS_$_"; |
| 882 | } |
| 883 | |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 884 | void GetClassSizeInfo(const ObjCInterfaceDecl *OID, |
| 885 | uint32_t &InstanceStart, |
| 886 | uint32_t &InstanceSize); |
| 887 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 888 | public: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 889 | CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 890 | // FIXME. All stubs for now! |
| 891 | virtual llvm::Function *ModuleInitFunction(); |
| 892 | |
| 893 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
| 894 | QualType ResultType, |
| 895 | Selector Sel, |
| 896 | llvm::Value *Receiver, |
| 897 | bool IsClassMessage, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 898 | const CallArgList &CallArgs); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 899 | |
| 900 | virtual CodeGen::RValue |
| 901 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 902 | QualType ResultType, |
| 903 | Selector Sel, |
| 904 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 905 | bool isCategoryImpl, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 906 | llvm::Value *Receiver, |
| 907 | bool IsClassMessage, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 908 | const CallArgList &CallArgs); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 909 | |
| 910 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 911 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 912 | |
| 913 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel) |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 914 | { return EmitSelector(Builder, Sel); } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 915 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 916 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 917 | |
| 918 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 919 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 920 | const ObjCProtocolDecl *PD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 921 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 922 | virtual llvm::Constant *GetPropertyGetFunction() { |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 923 | return ObjCTypes.GetPropertyFn; |
| 924 | } |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 925 | virtual llvm::Constant *GetPropertySetFunction() { |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 926 | return ObjCTypes.SetPropertyFn; |
| 927 | } |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 928 | virtual llvm::Constant *EnumerationMutationFunction() { |
Daniel Dunbar | 28ed084 | 2009-02-16 18:48:45 +0000 | [diff] [blame] | 929 | return ObjCTypes.EnumerationMutationFn; |
| 930 | } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 931 | |
| 932 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 933 | const Stmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 934 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 935 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 936 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 937 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 938 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 939 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 940 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 941 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 942 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 943 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 944 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 945 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 946 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 947 | QualType ObjectTy, |
| 948 | llvm::Value *BaseValue, |
| 949 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 950 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 951 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
| 952 | ObjCInterfaceDecl *Interface, |
| 953 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 954 | }; |
| 955 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 956 | } // end anonymous namespace |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 957 | |
| 958 | /* *** Helper Functions *** */ |
| 959 | |
| 960 | /// getConstantGEP() - Help routine to construct simple GEPs. |
| 961 | static llvm::Constant *getConstantGEP(llvm::Constant *C, |
| 962 | unsigned idx0, |
| 963 | unsigned idx1) { |
| 964 | llvm::Value *Idxs[] = { |
| 965 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0), |
| 966 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1) |
| 967 | }; |
| 968 | return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2); |
| 969 | } |
| 970 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 971 | /// hasObjCExceptionAttribute - Return true if this class or any super |
| 972 | /// class has the __objc_exception__ attribute. |
| 973 | static bool hasObjCExceptionAttribute(const ObjCInterfaceDecl *OID) { |
Daniel Dunbar | b11fa0d | 2009-04-13 21:08:27 +0000 | [diff] [blame] | 974 | if (OID->hasAttr<ObjCExceptionAttr>()) |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 975 | return true; |
| 976 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
| 977 | return hasObjCExceptionAttribute(Super); |
| 978 | return false; |
| 979 | } |
| 980 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 981 | /* *** CGObjCMac Public Interface *** */ |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 982 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 983 | CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm), |
| 984 | ObjCTypes(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 985 | { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 986 | ObjCABI = 1; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 987 | EmitImageInfo(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 988 | } |
| 989 | |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 990 | /// GetClass - Return a reference to the class for the given interface |
| 991 | /// decl. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 992 | llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 993 | const ObjCInterfaceDecl *ID) { |
| 994 | return EmitClassRef(Builder, ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | /// GetSelector - Return the pointer to the unique'd string for this selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 998 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 999 | return EmitSelector(Builder, Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1002 | /// Generate a constant CFString object. |
| 1003 | /* |
| 1004 | struct __builtin_CFString { |
| 1005 | const int *isa; // point to __CFConstantStringClassReference |
| 1006 | int flags; |
| 1007 | const char *str; |
| 1008 | long length; |
| 1009 | }; |
| 1010 | */ |
| 1011 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1012 | llvm::Constant *CGObjCCommonMac::GenerateConstantString( |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 1013 | const ObjCStringLiteral *SL) { |
Steve Naroff | 8d4141f | 2009-04-01 13:55:36 +0000 | [diff] [blame] | 1014 | return CGM.GetAddrOfConstantCFString(SL->getString()); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | /// Generates a message send where the super is the receiver. This is |
| 1018 | /// a message send to self with special delivery semantics indicating |
| 1019 | /// which class's method should be called. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1020 | CodeGen::RValue |
| 1021 | CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1022 | QualType ResultType, |
| 1023 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1024 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1025 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1026 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1027 | bool IsClassMessage, |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 1028 | const CodeGen::CallArgList &CallArgs) { |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1029 | // Create and init a super structure; this is a (receiver, class) |
| 1030 | // pair we will pass to objc_msgSendSuper. |
| 1031 | llvm::Value *ObjCSuper = |
| 1032 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 1033 | llvm::Value *ReceiverAsObject = |
| 1034 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 1035 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 1036 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1037 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1038 | // If this is a class message the metaclass is passed as the target. |
| 1039 | llvm::Value *Target; |
| 1040 | if (IsClassMessage) { |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1041 | if (isCategoryImpl) { |
| 1042 | // Message sent to 'super' in a class method defined in a category |
| 1043 | // implementation requires an odd treatment. |
| 1044 | // If we are in a class method, we must retrieve the |
| 1045 | // _metaclass_ for the current class, pointed at by |
| 1046 | // the class's "isa" pointer. The following assumes that |
| 1047 | // isa" is the first ivar in a class (which it must be). |
| 1048 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1049 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 1050 | Target = CGF.Builder.CreateLoad(Target); |
| 1051 | } |
| 1052 | else { |
| 1053 | llvm::Value *MetaClassPtr = EmitMetaClassRef(Class); |
| 1054 | llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1); |
| 1055 | llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr); |
| 1056 | Target = Super; |
| 1057 | } |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1058 | } else { |
| 1059 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1060 | } |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1061 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext |
| 1062 | // and ObjCTypes types. |
| 1063 | const llvm::Type *ClassTy = |
| 1064 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1065 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1066 | CGF.Builder.CreateStore(Target, |
| 1067 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
| 1068 | |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1069 | return EmitMessageSend(CGF, ResultType, Sel, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1070 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 1071 | true, CallArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1072 | } |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1073 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1074 | /// Generate code for a message send expression. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1075 | CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1076 | QualType ResultType, |
| 1077 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1078 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1079 | bool IsClassMessage, |
| 1080 | const CallArgList &CallArgs) { |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1081 | llvm::Value *Arg0 = |
| 1082 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp"); |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1083 | return EmitMessageSend(CGF, ResultType, Sel, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1084 | Arg0, CGF.getContext().getObjCIdType(), |
| 1085 | false, CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1089 | QualType ResultType, |
| 1090 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1091 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1092 | QualType Arg0Ty, |
| 1093 | bool IsSuper, |
| 1094 | const CallArgList &CallArgs) { |
| 1095 | CallArgList ActualArgs; |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 1096 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
| 1097 | ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder, |
| 1098 | Sel)), |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1099 | CGF.getContext().getObjCSelType())); |
| 1100 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1101 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1102 | CodeGenTypes &Types = CGM.getTypes(); |
| 1103 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs); |
| 1104 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1105 | |
| 1106 | llvm::Constant *Fn; |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1107 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1108 | Fn = ObjCTypes.getSendStretFn(IsSuper); |
| 1109 | } else if (ResultType->isFloatingType()) { |
| 1110 | // FIXME: Sadly, this is wrong. This actually depends on the |
| 1111 | // architecture. This happens to be right for x86-32 though. |
| 1112 | Fn = ObjCTypes.getSendFpretFn(IsSuper); |
| 1113 | } else { |
| 1114 | Fn = ObjCTypes.getSendFn(IsSuper); |
| 1115 | } |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 1116 | Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy)); |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1117 | return CGF.EmitCall(FnInfo, Fn, ActualArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1120 | llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1121 | const ObjCProtocolDecl *PD) { |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1122 | // FIXME: I don't understand why gcc generates this, or where it is |
| 1123 | // resolved. Investigate. Its also wasteful to look this up over and |
| 1124 | // over. |
| 1125 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1126 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1127 | return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), |
| 1128 | ObjCTypes.ExternalProtocolPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1131 | void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1132 | // FIXME: We shouldn't need this, the protocol decl should contain |
| 1133 | // enough information to tell us whether this was a declaration or a |
| 1134 | // definition. |
| 1135 | DefinedProtocols.insert(PD->getIdentifier()); |
| 1136 | |
| 1137 | // If we have generated a forward reference to this protocol, emit |
| 1138 | // it now. Otherwise do nothing, the protocol objects are lazily |
| 1139 | // emitted. |
| 1140 | if (Protocols.count(PD->getIdentifier())) |
| 1141 | GetOrEmitProtocol(PD); |
| 1142 | } |
| 1143 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1144 | llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1145 | if (DefinedProtocols.count(PD->getIdentifier())) |
| 1146 | return GetOrEmitProtocol(PD); |
| 1147 | return GetOrEmitProtocolRef(PD); |
| 1148 | } |
| 1149 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1150 | /* |
| 1151 | // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions |
| 1152 | struct _objc_protocol { |
| 1153 | struct _objc_protocol_extension *isa; |
| 1154 | char *protocol_name; |
| 1155 | struct _objc_protocol_list *protocol_list; |
| 1156 | struct _objc__method_prototype_list *instance_methods; |
| 1157 | struct _objc__method_prototype_list *class_methods |
| 1158 | }; |
| 1159 | |
| 1160 | See EmitProtocolExtension(). |
| 1161 | */ |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1162 | llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) { |
| 1163 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1164 | |
| 1165 | // Early exit if a defining object has already been generated. |
| 1166 | if (Entry && Entry->hasInitializer()) |
| 1167 | return Entry; |
| 1168 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1169 | // FIXME: I don't understand why gcc generates this, or where it is |
| 1170 | // resolved. Investigate. Its also wasteful to look this up over and |
| 1171 | // over. |
| 1172 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1173 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1174 | const char *ProtocolName = PD->getNameAsCString(); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1175 | |
| 1176 | // Construct method lists. |
| 1177 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1178 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1179 | for (ObjCProtocolDecl::instmeth_iterator |
| 1180 | i = PD->instmeth_begin(CGM.getContext()), |
| 1181 | e = PD->instmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1182 | ObjCMethodDecl *MD = *i; |
| 1183 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1184 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1185 | OptInstanceMethods.push_back(C); |
| 1186 | } else { |
| 1187 | InstanceMethods.push_back(C); |
| 1188 | } |
| 1189 | } |
| 1190 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1191 | for (ObjCProtocolDecl::classmeth_iterator |
| 1192 | i = PD->classmeth_begin(CGM.getContext()), |
| 1193 | e = PD->classmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1194 | ObjCMethodDecl *MD = *i; |
| 1195 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1196 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1197 | OptClassMethods.push_back(C); |
| 1198 | } else { |
| 1199 | ClassMethods.push_back(C); |
| 1200 | } |
| 1201 | } |
| 1202 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1203 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1204 | Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1205 | Values[1] = GetClassName(PD->getIdentifier()); |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1206 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1207 | EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1208 | PD->protocol_begin(), |
| 1209 | PD->protocol_end()); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1210 | Values[3] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1211 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" |
| 1212 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1213 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1214 | InstanceMethods); |
| 1215 | Values[4] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1216 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" |
| 1217 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1218 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1219 | ClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1220 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 1221 | Values); |
| 1222 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1223 | if (Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1224 | // Already created, fix the linkage and update the initializer. |
| 1225 | Entry->setLinkage(llvm::GlobalValue::InternalLinkage); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1226 | Entry->setInitializer(Init); |
| 1227 | } else { |
| 1228 | Entry = |
| 1229 | new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, |
| 1230 | llvm::GlobalValue::InternalLinkage, |
| 1231 | Init, |
| 1232 | std::string("\01L_OBJC_PROTOCOL_")+ProtocolName, |
| 1233 | &CGM.getModule()); |
| 1234 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1235 | Entry->setAlignment(4); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1236 | UsedGlobals.push_back(Entry); |
| 1237 | // FIXME: Is this necessary? Why only for protocol? |
| 1238 | Entry->setAlignment(4); |
| 1239 | } |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1240 | |
| 1241 | return Entry; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1244 | llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1245 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1246 | |
| 1247 | if (!Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1248 | // We use the initializer as a marker of whether this is a forward |
| 1249 | // reference or not. At module finalization we add the empty |
| 1250 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1251 | Entry = |
| 1252 | new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1253 | llvm::GlobalValue::ExternalLinkage, |
| 1254 | 0, |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1255 | "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1256 | &CGM.getModule()); |
| 1257 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1258 | Entry->setAlignment(4); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1259 | UsedGlobals.push_back(Entry); |
| 1260 | // FIXME: Is this necessary? Why only for protocol? |
| 1261 | Entry->setAlignment(4); |
| 1262 | } |
| 1263 | |
| 1264 | return Entry; |
| 1265 | } |
| 1266 | |
| 1267 | /* |
| 1268 | struct _objc_protocol_extension { |
| 1269 | uint32_t size; |
| 1270 | struct objc_method_description_list *optional_instance_methods; |
| 1271 | struct objc_method_description_list *optional_class_methods; |
| 1272 | struct objc_property_list *instance_properties; |
| 1273 | }; |
| 1274 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1275 | llvm::Constant * |
| 1276 | CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 1277 | const ConstantVector &OptInstanceMethods, |
| 1278 | const ConstantVector &OptClassMethods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1279 | uint64_t Size = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1280 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1281 | std::vector<llvm::Constant*> Values(4); |
| 1282 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1283 | Values[1] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1284 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_" |
| 1285 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1286 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1287 | OptInstanceMethods); |
| 1288 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1289 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" |
| 1290 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1291 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1292 | OptClassMethods); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1293 | Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + |
| 1294 | PD->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1295 | 0, PD, ObjCTypes); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1296 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1297 | // Return null if no extension bits are used. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1298 | if (Values[1]->isNullValue() && Values[2]->isNullValue() && |
| 1299 | Values[3]->isNullValue()) |
| 1300 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
| 1301 | |
| 1302 | llvm::Constant *Init = |
| 1303 | llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1304 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1305 | // No special section, but goes in llvm.used |
| 1306 | return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(), |
| 1307 | Init, |
| 1308 | 0, 0, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | /* |
| 1312 | struct objc_protocol_list { |
| 1313 | struct objc_protocol_list *next; |
| 1314 | long count; |
| 1315 | Protocol *list[]; |
| 1316 | }; |
| 1317 | */ |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1318 | llvm::Constant * |
| 1319 | CGObjCMac::EmitProtocolList(const std::string &Name, |
| 1320 | ObjCProtocolDecl::protocol_iterator begin, |
| 1321 | ObjCProtocolDecl::protocol_iterator end) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1322 | std::vector<llvm::Constant*> ProtocolRefs; |
| 1323 | |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1324 | for (; begin != end; ++begin) |
| 1325 | ProtocolRefs.push_back(GetProtocolRef(*begin)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1326 | |
| 1327 | // Just return null for empty protocol lists |
| 1328 | if (ProtocolRefs.empty()) |
| 1329 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1330 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1331 | // This list is null terminated. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1332 | ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy)); |
| 1333 | |
| 1334 | std::vector<llvm::Constant*> Values(3); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1335 | // This field is only used by the runtime. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1336 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1337 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
| 1338 | Values[2] = |
| 1339 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy, |
| 1340 | ProtocolRefs.size()), |
| 1341 | ProtocolRefs); |
| 1342 | |
| 1343 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1344 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1345 | CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1346 | 4, false); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1347 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy); |
| 1348 | } |
| 1349 | |
| 1350 | /* |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1351 | struct _objc_property { |
| 1352 | const char * const name; |
| 1353 | const char * const attributes; |
| 1354 | }; |
| 1355 | |
| 1356 | struct _objc_property_list { |
| 1357 | uint32_t entsize; // sizeof (struct _objc_property) |
| 1358 | uint32_t prop_count; |
| 1359 | struct _objc_property[prop_count]; |
| 1360 | }; |
| 1361 | */ |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1362 | llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name, |
| 1363 | const Decl *Container, |
| 1364 | const ObjCContainerDecl *OCD, |
| 1365 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1366 | std::vector<llvm::Constant*> Properties, Prop(2); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1367 | for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(CGM.getContext()), |
| 1368 | E = OCD->prop_end(CGM.getContext()); I != E; ++I) { |
Steve Naroff | 93983f8 | 2009-01-11 12:47:58 +0000 | [diff] [blame] | 1369 | const ObjCPropertyDecl *PD = *I; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1370 | Prop[0] = GetPropertyName(PD->getIdentifier()); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1371 | Prop[1] = GetPropertyTypeString(PD, Container); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1372 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, |
| 1373 | Prop)); |
| 1374 | } |
| 1375 | |
| 1376 | // Return null for empty list. |
| 1377 | if (Properties.empty()) |
| 1378 | return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 1379 | |
| 1380 | unsigned PropertySize = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1381 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1382 | std::vector<llvm::Constant*> Values(3); |
| 1383 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize); |
| 1384 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size()); |
| 1385 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy, |
| 1386 | Properties.size()); |
| 1387 | Values[2] = llvm::ConstantArray::get(AT, Properties); |
| 1388 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1389 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1390 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1391 | CreateMetadataVar(Name, Init, |
| 1392 | (ObjCABI == 2) ? "__DATA, __objc_const" : |
| 1393 | "__OBJC,__property,regular,no_dead_strip", |
| 1394 | (ObjCABI == 2) ? 8 : 4, |
| 1395 | true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1396 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | /* |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1400 | struct objc_method_description_list { |
| 1401 | int count; |
| 1402 | struct objc_method_description list[]; |
| 1403 | }; |
| 1404 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1405 | llvm::Constant * |
| 1406 | CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 1407 | std::vector<llvm::Constant*> Desc(2); |
| 1408 | Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 1409 | ObjCTypes.SelectorPtrTy); |
| 1410 | Desc[1] = GetMethodVarType(MD); |
| 1411 | return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy, |
| 1412 | Desc); |
| 1413 | } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1414 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1415 | llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name, |
| 1416 | const char *Section, |
| 1417 | const ConstantVector &Methods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1418 | // Return null for empty list. |
| 1419 | if (Methods.empty()) |
| 1420 | return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
| 1421 | |
| 1422 | std::vector<llvm::Constant*> Values(2); |
| 1423 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 1424 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy, |
| 1425 | Methods.size()); |
| 1426 | Values[1] = llvm::ConstantArray::get(AT, Methods); |
| 1427 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1428 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1429 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1430 | return llvm::ConstantExpr::getBitCast(GV, |
| 1431 | ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1432 | } |
| 1433 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1434 | /* |
| 1435 | struct _objc_category { |
| 1436 | char *category_name; |
| 1437 | char *class_name; |
| 1438 | struct _objc_method_list *instance_methods; |
| 1439 | struct _objc_method_list *class_methods; |
| 1440 | struct _objc_protocol_list *protocols; |
| 1441 | uint32_t size; // <rdar://4585769> |
| 1442 | struct _objc_property_list *instance_properties; |
| 1443 | }; |
| 1444 | */ |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1445 | void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1446 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1447 | |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1448 | // FIXME: This is poor design, the OCD should have a pointer to the |
| 1449 | // category decl. Additionally, note that Category can be null for |
| 1450 | // the @implementation w/o an @interface case. Sema should just |
| 1451 | // create one for us as it does for @implementation so everyone else |
| 1452 | // can live life under a clear blue sky. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1453 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1454 | const ObjCCategoryDecl *Category = |
| 1455 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1456 | std::string ExtName(Interface->getNameAsString() + "_" + |
| 1457 | OCD->getNameAsString()); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1458 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1459 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1460 | for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(), |
| 1461 | e = OCD->instmeth_end(); i != e; ++i) { |
| 1462 | // Instance methods should always be defined. |
| 1463 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1464 | } |
| 1465 | for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(), |
| 1466 | e = OCD->classmeth_end(); i != e; ++i) { |
| 1467 | // Class methods should always be defined. |
| 1468 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1469 | } |
| 1470 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1471 | std::vector<llvm::Constant*> Values(7); |
| 1472 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 1473 | Values[1] = GetClassName(Interface->getIdentifier()); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1474 | Values[2] = |
| 1475 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") + |
| 1476 | ExtName, |
| 1477 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1478 | InstanceMethods); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1479 | Values[3] = |
| 1480 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName, |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1481 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1482 | ClassMethods); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1483 | if (Category) { |
| 1484 | Values[4] = |
| 1485 | EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName, |
| 1486 | Category->protocol_begin(), |
| 1487 | Category->protocol_end()); |
| 1488 | } else { |
| 1489 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1490 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1491 | Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1492 | |
| 1493 | // If there is no category @interface then there can be no properties. |
| 1494 | if (Category) { |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1495 | Values[6] = EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName, |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1496 | OCD, Category, ObjCTypes); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1497 | } else { |
| 1498 | Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 1499 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1500 | |
| 1501 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy, |
| 1502 | Values); |
| 1503 | |
| 1504 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1505 | CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init, |
| 1506 | "__OBJC,__category,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1507 | 4, true); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1508 | DefinedCategories.push_back(GV); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1511 | // FIXME: Get from somewhere? |
| 1512 | enum ClassFlags { |
| 1513 | eClassFlags_Factory = 0x00001, |
| 1514 | eClassFlags_Meta = 0x00002, |
| 1515 | // <rdr://5142207> |
| 1516 | eClassFlags_HasCXXStructors = 0x02000, |
| 1517 | eClassFlags_Hidden = 0x20000, |
| 1518 | eClassFlags_ABI2_Hidden = 0x00010, |
| 1519 | eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634> |
| 1520 | }; |
| 1521 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1522 | /* |
| 1523 | struct _objc_class { |
| 1524 | Class isa; |
| 1525 | Class super_class; |
| 1526 | const char *name; |
| 1527 | long version; |
| 1528 | long info; |
| 1529 | long instance_size; |
| 1530 | struct _objc_ivar_list *ivars; |
| 1531 | struct _objc_method_list *methods; |
| 1532 | struct _objc_cache *cache; |
| 1533 | struct _objc_protocol_list *protocols; |
| 1534 | // Objective-C 1.0 extensions (<rdr://4585769>) |
| 1535 | const char *ivar_layout; |
| 1536 | struct _objc_class_ext *ext; |
| 1537 | }; |
| 1538 | |
| 1539 | See EmitClassExtension(); |
| 1540 | */ |
| 1541 | void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1542 | DefinedSymbols.insert(ID->getIdentifier()); |
| 1543 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1544 | std::string ClassName = ID->getNameAsString(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1545 | // FIXME: Gross |
| 1546 | ObjCInterfaceDecl *Interface = |
| 1547 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1548 | llvm::Constant *Protocols = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1549 | EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1550 | Interface->protocol_begin(), |
| 1551 | Interface->protocol_end()); |
Chris Lattner | b7b58b1 | 2009-04-19 06:02:28 +0000 | [diff] [blame] | 1552 | const llvm::Type *InterfaceTy; |
| 1553 | if (Interface->isForwardDecl()) |
| 1554 | InterfaceTy = llvm::StructType::get(NULL, NULL); |
| 1555 | else |
| 1556 | InterfaceTy = |
Chris Lattner | 03d9f34 | 2009-04-01 06:23:52 +0000 | [diff] [blame] | 1557 | CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1558 | unsigned Flags = eClassFlags_Factory; |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1559 | unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1560 | |
| 1561 | // FIXME: Set CXX-structors flag. |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 1562 | if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1563 | Flags |= eClassFlags_Hidden; |
| 1564 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1565 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1566 | for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(), |
| 1567 | e = ID->instmeth_end(); i != e; ++i) { |
| 1568 | // Instance methods should always be defined. |
| 1569 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1570 | } |
| 1571 | for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(), |
| 1572 | e = ID->classmeth_end(); i != e; ++i) { |
| 1573 | // Class methods should always be defined. |
| 1574 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1575 | } |
| 1576 | |
| 1577 | for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(), |
| 1578 | e = ID->propimpl_end(); i != e; ++i) { |
| 1579 | ObjCPropertyImplDecl *PID = *i; |
| 1580 | |
| 1581 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 1582 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 1583 | |
| 1584 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 1585 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 1586 | InstanceMethods.push_back(C); |
| 1587 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 1588 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 1589 | InstanceMethods.push_back(C); |
| 1590 | } |
| 1591 | } |
| 1592 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1593 | std::vector<llvm::Constant*> Values(12); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1594 | Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1595 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1596 | // Record a reference to the super class. |
| 1597 | LazySymbols.insert(Super->getIdentifier()); |
| 1598 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1599 | Values[ 1] = |
| 1600 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 1601 | ObjCTypes.ClassPtrTy); |
| 1602 | } else { |
| 1603 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
| 1604 | } |
| 1605 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 1606 | // Version is always 0. |
| 1607 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 1608 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 1609 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1610 | Values[ 6] = EmitIvarList(ID, false); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1611 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1612 | EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1613 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1614 | InstanceMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1615 | // cache is always NULL. |
| 1616 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
| 1617 | Values[ 9] = Protocols; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1618 | // FIXME: Set ivar_layout |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 1619 | // Values[10] = BuildIvarLayout(ID, true); |
| 1620 | Values[10] = GetIvarLayoutName(0, ObjCTypes); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1621 | Values[11] = EmitClassExtension(ID); |
| 1622 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 1623 | Values); |
| 1624 | |
| 1625 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1626 | CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init, |
| 1627 | "__OBJC,__class,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1628 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1629 | DefinedClasses.push_back(GV); |
| 1630 | } |
| 1631 | |
| 1632 | llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID, |
| 1633 | llvm::Constant *Protocols, |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1634 | const llvm::Type *InterfaceTy, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1635 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1636 | unsigned Flags = eClassFlags_Meta; |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1637 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1638 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 1639 | if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1640 | Flags |= eClassFlags_Hidden; |
| 1641 | |
| 1642 | std::vector<llvm::Constant*> Values(12); |
| 1643 | // The isa for the metaclass is the root of the hierarchy. |
| 1644 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 1645 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 1646 | Root = Super; |
| 1647 | Values[ 0] = |
| 1648 | llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()), |
| 1649 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1650 | // The super class for the metaclass is emitted as the name of the |
| 1651 | // super class. The runtime fixes this up to point to the |
| 1652 | // *metaclass* for the super class. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1653 | if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) { |
| 1654 | Values[ 1] = |
| 1655 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 1656 | ObjCTypes.ClassPtrTy); |
| 1657 | } else { |
| 1658 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
| 1659 | } |
| 1660 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 1661 | // Version is always 0. |
| 1662 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 1663 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 1664 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1665 | Values[ 6] = EmitIvarList(ID, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1666 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1667 | EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1668 | "__OBJC,__cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1669 | Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1670 | // cache is always NULL. |
| 1671 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
| 1672 | Values[ 9] = Protocols; |
| 1673 | // ivar_layout for metaclass is always NULL. |
| 1674 | Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 1675 | // The class extension is always unused for metaclasses. |
| 1676 | Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
| 1677 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 1678 | Values); |
| 1679 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1680 | std::string Name("\01L_OBJC_METACLASS_"); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1681 | Name += ID->getNameAsCString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1682 | |
| 1683 | // Check for a forward reference. |
| 1684 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 1685 | if (GV) { |
| 1686 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 1687 | "Forward metaclass reference has incorrect type."); |
| 1688 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 1689 | GV->setInitializer(Init); |
| 1690 | } else { |
| 1691 | GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 1692 | llvm::GlobalValue::InternalLinkage, |
| 1693 | Init, Name, |
| 1694 | &CGM.getModule()); |
| 1695 | } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1696 | GV->setSection("__OBJC,__meta_class,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1697 | GV->setAlignment(4); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1698 | UsedGlobals.push_back(GV); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1699 | |
| 1700 | return GV; |
| 1701 | } |
| 1702 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1703 | llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1704 | std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1705 | |
| 1706 | // FIXME: Should we look these up somewhere other than the |
| 1707 | // module. Its a bit silly since we only generate these while |
| 1708 | // processing an implementation, so exactly one pointer would work |
| 1709 | // if know when we entered/exitted an implementation block. |
| 1710 | |
| 1711 | // Check for an existing forward reference. |
Fariborz Jahanian | b0d2794 | 2009-01-07 20:11:22 +0000 | [diff] [blame] | 1712 | // Previously, metaclass with internal linkage may have been defined. |
| 1713 | // pass 'true' as 2nd argument so it is returned. |
| 1714 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) { |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1715 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 1716 | "Forward metaclass reference has incorrect type."); |
| 1717 | return GV; |
| 1718 | } else { |
| 1719 | // Generate as an external reference to keep a consistent |
| 1720 | // module. This will be patched up when we emit the metaclass. |
| 1721 | return new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 1722 | llvm::GlobalValue::ExternalLinkage, |
| 1723 | 0, |
| 1724 | Name, |
| 1725 | &CGM.getModule()); |
| 1726 | } |
| 1727 | } |
| 1728 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1729 | /* |
| 1730 | struct objc_class_ext { |
| 1731 | uint32_t size; |
| 1732 | const char *weak_ivar_layout; |
| 1733 | struct _objc_property_list *properties; |
| 1734 | }; |
| 1735 | */ |
| 1736 | llvm::Constant * |
| 1737 | CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) { |
| 1738 | uint64_t Size = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1739 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1740 | |
| 1741 | std::vector<llvm::Constant*> Values(3); |
| 1742 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1743 | // FIXME: Output weak_ivar_layout string. |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 1744 | // Values[1] = BuildIvarLayout(ID, false); |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 1745 | Values[1] = GetIvarLayoutName(0, ObjCTypes); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1746 | Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1747 | ID, ID->getClassInterface(), ObjCTypes); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1748 | |
| 1749 | // Return null if no extension bits are used. |
| 1750 | if (Values[1]->isNullValue() && Values[2]->isNullValue()) |
| 1751 | return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
| 1752 | |
| 1753 | llvm::Constant *Init = |
| 1754 | llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1755 | return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1756 | Init, "__OBJC,__class_ext,regular,no_dead_strip", |
| 1757 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1758 | } |
| 1759 | |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 1760 | /// getInterfaceDeclForIvar - Get the interface declaration node where |
| 1761 | /// this ivar is declared in. |
| 1762 | /// FIXME. Ideally, this info should be in the ivar node. But currently |
| 1763 | /// it is not and prevailing wisdom is that ASTs should not have more |
| 1764 | /// info than is absolutely needed, even though this info reflects the |
| 1765 | /// source language. |
| 1766 | /// |
| 1767 | static const ObjCInterfaceDecl *getInterfaceDeclForIvar( |
| 1768 | const ObjCInterfaceDecl *OI, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1769 | const ObjCIvarDecl *IVD, |
| 1770 | ASTContext &Context) { |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 1771 | if (!OI) |
| 1772 | return 0; |
| 1773 | assert(isa<ObjCInterfaceDecl>(OI) && "OI is not an interface"); |
| 1774 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 1775 | E = OI->ivar_end(); I != E; ++I) |
| 1776 | if ((*I)->getIdentifier() == IVD->getIdentifier()) |
| 1777 | return OI; |
Fariborz Jahanian | 5a4b453 | 2009-03-31 17:00:52 +0000 | [diff] [blame] | 1778 | // look into properties. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1779 | for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(Context), |
| 1780 | E = OI->prop_end(Context); I != E; ++I) { |
Fariborz Jahanian | 5a4b453 | 2009-03-31 17:00:52 +0000 | [diff] [blame] | 1781 | ObjCPropertyDecl *PDecl = (*I); |
| 1782 | if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl()) |
| 1783 | if (IV->getIdentifier() == IVD->getIdentifier()) |
| 1784 | return OI; |
| 1785 | } |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1786 | return getInterfaceDeclForIvar(OI->getSuperClass(), IVD, Context); |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 1787 | } |
| 1788 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1789 | /* |
| 1790 | struct objc_ivar { |
| 1791 | char *ivar_name; |
| 1792 | char *ivar_type; |
| 1793 | int ivar_offset; |
| 1794 | }; |
| 1795 | |
| 1796 | struct objc_ivar_list { |
| 1797 | int ivar_count; |
| 1798 | struct objc_ivar list[count]; |
| 1799 | }; |
| 1800 | */ |
| 1801 | llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1802 | bool ForClass) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1803 | std::vector<llvm::Constant*> Ivars, Ivar(3); |
| 1804 | |
| 1805 | // When emitting the root class GCC emits ivar entries for the |
| 1806 | // actual class structure. It is not clear if we need to follow this |
| 1807 | // behavior; for now lets try and get away with not doing it. If so, |
| 1808 | // the cleanest solution would be to make up an ObjCInterfaceDecl |
| 1809 | // for the class. |
| 1810 | if (ForClass) |
| 1811 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1812 | |
| 1813 | ObjCInterfaceDecl *OID = |
| 1814 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 1815 | const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OID); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1816 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 1817 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
| 1818 | GetNamedIvarList(OID, OIvars); |
| 1819 | |
| 1820 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 1821 | ObjCIvarDecl *IVD = OIvars[i]; |
| 1822 | const FieldDecl *Field = OID->lookupFieldDeclForIvar(CGM.getContext(), IVD); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 1823 | Ivar[0] = GetMethodVarName(Field->getIdentifier()); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 1824 | Ivar[1] = GetMethodVarType(Field); |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 1825 | Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, |
| 1826 | GetIvarBaseOffset(Layout, Field)); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1827 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
| 1830 | // Return null for empty list. |
| 1831 | if (Ivars.empty()) |
| 1832 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
| 1833 | |
| 1834 | std::vector<llvm::Constant*> Values(2); |
| 1835 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 1836 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy, |
| 1837 | Ivars.size()); |
| 1838 | Values[1] = llvm::ConstantArray::get(AT, Ivars); |
| 1839 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1840 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1841 | llvm::GlobalVariable *GV; |
| 1842 | if (ForClass) |
| 1843 | GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(), |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1844 | Init, "__OBJC,__class_vars,regular,no_dead_strip", |
| 1845 | 4, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1846 | else |
| 1847 | GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" |
| 1848 | + ID->getNameAsString(), |
| 1849 | Init, "__OBJC,__instance_vars,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1850 | 4, true); |
| 1851 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1852 | } |
| 1853 | |
| 1854 | /* |
| 1855 | struct objc_method { |
| 1856 | SEL method_name; |
| 1857 | char *method_types; |
| 1858 | void *method; |
| 1859 | }; |
| 1860 | |
| 1861 | struct objc_method_list { |
| 1862 | struct objc_method_list *obsolete; |
| 1863 | int count; |
| 1864 | struct objc_method methods_list[count]; |
| 1865 | }; |
| 1866 | */ |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1867 | |
| 1868 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 1869 | /// given method if it has been defined. The result is null if the |
| 1870 | /// method has not been defined. The return value has type MethodPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1871 | llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1872 | // FIXME: Use DenseMap::lookup |
| 1873 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 1874 | if (!Fn) |
| 1875 | return 0; |
| 1876 | |
| 1877 | std::vector<llvm::Constant*> Method(3); |
| 1878 | Method[0] = |
| 1879 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 1880 | ObjCTypes.SelectorPtrTy); |
| 1881 | Method[1] = GetMethodVarType(MD); |
| 1882 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 1883 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 1884 | } |
| 1885 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1886 | llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name, |
| 1887 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1888 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1889 | // Return null for empty list. |
| 1890 | if (Methods.empty()) |
| 1891 | return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy); |
| 1892 | |
| 1893 | std::vector<llvm::Constant*> Values(3); |
| 1894 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 1895 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 1896 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 1897 | Methods.size()); |
| 1898 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 1899 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1900 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1901 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1902 | return llvm::ConstantExpr::getBitCast(GV, |
| 1903 | ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1904 | } |
| 1905 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1906 | llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD, |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 1907 | const ObjCContainerDecl *CD) { |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1908 | std::string Name; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 1909 | GetNameForMethod(OMD, CD, Name); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1910 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1911 | CodeGenTypes &Types = CGM.getTypes(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1912 | const llvm::FunctionType *MethodTy = |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1913 | Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic()); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1914 | llvm::Function *Method = |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1915 | llvm::Function::Create(MethodTy, |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1916 | llvm::GlobalValue::InternalLinkage, |
| 1917 | Name, |
| 1918 | &CGM.getModule()); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1919 | MethodDefinitions.insert(std::make_pair(OMD, Method)); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1920 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1921 | return Method; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1922 | } |
| 1923 | |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 1924 | uint64_t CGObjCCommonMac::GetIvarBaseOffset(const llvm::StructLayout *Layout, |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 1925 | const FieldDecl *Field) { |
Fariborz Jahanian | a6681ae | 2009-03-09 20:44:22 +0000 | [diff] [blame] | 1926 | if (!Field->isBitField()) |
Daniel Dunbar | 48fa064 | 2009-04-19 02:03:42 +0000 | [diff] [blame] | 1927 | return Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field)); |
| 1928 | |
Fariborz Jahanian | a6681ae | 2009-03-09 20:44:22 +0000 | [diff] [blame] | 1929 | // FIXME. Must be a better way of getting a bitfield base offset. |
Daniel Dunbar | 48fa064 | 2009-04-19 02:03:42 +0000 | [diff] [blame] | 1930 | CodeGenTypes::BitFieldInfo BFI = CGM.getTypes().getBitFieldInfo(Field); |
| 1931 | // FIXME: The "field no" for bitfields is something completely |
| 1932 | // different; it is the offset in multiples of the base type size! |
| 1933 | uint64_t Offset = CGM.getTypes().getLLVMFieldNo(Field); |
| 1934 | const llvm::Type *Ty = |
| 1935 | CGM.getTypes().ConvertTypeForMemRecursive(Field->getType()); |
| 1936 | Offset *= CGM.getTypes().getTargetData().getTypePaddedSizeInBits(Ty); |
| 1937 | return (Offset + BFI.Begin) / 8; |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 1938 | } |
| 1939 | |
Daniel Dunbar | 48fa064 | 2009-04-19 02:03:42 +0000 | [diff] [blame] | 1940 | /// GetFieldBaseOffset - return the field's byte offset. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 1941 | uint64_t CGObjCCommonMac::GetFieldBaseOffset(const ObjCInterfaceDecl *OI, |
| 1942 | const llvm::StructLayout *Layout, |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 1943 | const FieldDecl *Field) { |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 1944 | // Is this a c struct? |
| 1945 | if (!OI) |
| 1946 | return Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field)); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 1947 | const ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field); |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 1948 | const FieldDecl *FD = OI->lookupFieldDeclForIvar(CGM.getContext(), Ivar); |
| 1949 | return GetIvarBaseOffset(Layout, FD); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 1952 | llvm::GlobalVariable * |
| 1953 | CGObjCCommonMac::CreateMetadataVar(const std::string &Name, |
| 1954 | llvm::Constant *Init, |
| 1955 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 1956 | unsigned Align, |
| 1957 | bool AddToUsed) { |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 1958 | const llvm::Type *Ty = Init->getType(); |
| 1959 | llvm::GlobalVariable *GV = |
| 1960 | new llvm::GlobalVariable(Ty, false, |
| 1961 | llvm::GlobalValue::InternalLinkage, |
| 1962 | Init, |
| 1963 | Name, |
| 1964 | &CGM.getModule()); |
| 1965 | if (Section) |
| 1966 | GV->setSection(Section); |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 1967 | if (Align) |
| 1968 | GV->setAlignment(Align); |
| 1969 | if (AddToUsed) |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 1970 | UsedGlobals.push_back(GV); |
| 1971 | return GV; |
| 1972 | } |
| 1973 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1974 | llvm::Function *CGObjCMac::ModuleInitFunction() { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1975 | // Abuse this interface function as a place to finalize. |
| 1976 | FinishModule(); |
| 1977 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1978 | return NULL; |
| 1979 | } |
| 1980 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1981 | llvm::Constant *CGObjCMac::GetPropertyGetFunction() { |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 1982 | return ObjCTypes.GetPropertyFn; |
| 1983 | } |
| 1984 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1985 | llvm::Constant *CGObjCMac::GetPropertySetFunction() { |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 1986 | return ObjCTypes.SetPropertyFn; |
| 1987 | } |
| 1988 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1989 | llvm::Constant *CGObjCMac::EnumerationMutationFunction() { |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 1990 | return ObjCTypes.EnumerationMutationFn; |
| 1991 | } |
| 1992 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1993 | /* |
| 1994 | |
| 1995 | Objective-C setjmp-longjmp (sjlj) Exception Handling |
| 1996 | -- |
| 1997 | |
| 1998 | The basic framework for a @try-catch-finally is as follows: |
| 1999 | { |
| 2000 | objc_exception_data d; |
| 2001 | id _rethrow = null; |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2002 | bool _call_try_exit = true; |
| 2003 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2004 | objc_exception_try_enter(&d); |
| 2005 | if (!setjmp(d.jmp_buf)) { |
| 2006 | ... try body ... |
| 2007 | } else { |
| 2008 | // exception path |
| 2009 | id _caught = objc_exception_extract(&d); |
| 2010 | |
| 2011 | // enter new try scope for handlers |
| 2012 | if (!setjmp(d.jmp_buf)) { |
| 2013 | ... match exception and execute catch blocks ... |
| 2014 | |
| 2015 | // fell off end, rethrow. |
| 2016 | _rethrow = _caught; |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2017 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2018 | } else { |
| 2019 | // exception in catch block |
| 2020 | _rethrow = objc_exception_extract(&d); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2021 | _call_try_exit = false; |
| 2022 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2023 | } |
| 2024 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2025 | ... jump-through-finally to finally_end ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2026 | |
| 2027 | finally: |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2028 | if (_call_try_exit) |
| 2029 | objc_exception_try_exit(&d); |
| 2030 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2031 | ... finally block .... |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2032 | ... dispatch to finally destination ... |
| 2033 | |
| 2034 | finally_rethrow: |
| 2035 | objc_exception_throw(_rethrow); |
| 2036 | |
| 2037 | finally_end: |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2038 | } |
| 2039 | |
| 2040 | This framework differs slightly from the one gcc uses, in that gcc |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2041 | uses _rethrow to determine if objc_exception_try_exit should be called |
| 2042 | and if the object should be rethrown. This breaks in the face of |
| 2043 | throwing nil and introduces unnecessary branches. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2044 | |
| 2045 | We specialize this framework for a few particular circumstances: |
| 2046 | |
| 2047 | - If there are no catch blocks, then we avoid emitting the second |
| 2048 | exception handling context. |
| 2049 | |
| 2050 | - If there is a catch-all catch block (i.e. @catch(...) or @catch(id |
| 2051 | e)) we avoid emitting the code to rethrow an uncaught exception. |
| 2052 | |
| 2053 | - FIXME: If there is no @finally block we can do a few more |
| 2054 | simplifications. |
| 2055 | |
| 2056 | Rethrows and Jumps-Through-Finally |
| 2057 | -- |
| 2058 | |
| 2059 | Support for implicit rethrows and jumping through the finally block is |
| 2060 | handled by storing the current exception-handling context in |
| 2061 | ObjCEHStack. |
| 2062 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2063 | In order to implement proper @finally semantics, we support one basic |
| 2064 | mechanism for jumping through the finally block to an arbitrary |
| 2065 | destination. Constructs which generate exits from a @try or @catch |
| 2066 | block use this mechanism to implement the proper semantics by chaining |
| 2067 | jumps, as necessary. |
| 2068 | |
| 2069 | This mechanism works like the one used for indirect goto: we |
| 2070 | arbitrarily assign an ID to each destination and store the ID for the |
| 2071 | destination in a variable prior to entering the finally block. At the |
| 2072 | end of the finally block we simply create a switch to the proper |
| 2073 | destination. |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2074 | |
| 2075 | Code gen for @synchronized(expr) stmt; |
| 2076 | Effectively generating code for: |
| 2077 | objc_sync_enter(expr); |
| 2078 | @try stmt @finally { objc_sync_exit(expr); } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2079 | */ |
| 2080 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2081 | void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 2082 | const Stmt &S) { |
| 2083 | bool isTry = isa<ObjCAtTryStmt>(S); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2084 | // Create various blocks we refer to for handling @finally. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2085 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2086 | llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit"); |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2087 | llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit"); |
| 2088 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
| 2089 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2090 | |
| 2091 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 2092 | // evaluation of the expression must occur before we enter the |
| 2093 | // @synchronized. We can safely avoid a temp here because jumps into |
| 2094 | // @synchronized are illegal & this will dominate uses. |
| 2095 | llvm::Value *SyncArg = 0; |
| 2096 | if (!isTry) { |
| 2097 | SyncArg = |
| 2098 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 2099 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 2100 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2101 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2102 | |
| 2103 | // Push an EH context entry, used for handling rethrows and jumps |
| 2104 | // through finally. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2105 | CGF.PushCleanupBlock(FinallyBlock); |
| 2106 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2107 | CGF.ObjCEHValueStack.push_back(0); |
| 2108 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2109 | // Allocate memory for the exception data and rethrow pointer. |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2110 | llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy, |
| 2111 | "exceptiondata.ptr"); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2112 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy, |
| 2113 | "_rethrow"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2114 | llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty, |
| 2115 | "_call_try_exit"); |
| 2116 | CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr); |
| 2117 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2118 | // Enter a new try block and call setjmp. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame^] | 2119 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2120 | llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0, |
| 2121 | "jmpbufarray"); |
| 2122 | JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp"); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame^] | 2123 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2124 | JmpBufPtr, "result"); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2125 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2126 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 2127 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2128 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2129 | TryHandler, TryBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2130 | |
| 2131 | // Emit the @try block. |
| 2132 | CGF.EmitBlock(TryBlock); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2133 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 2134 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2135 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2136 | |
| 2137 | // Emit the "exception in @try" block. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2138 | CGF.EmitBlock(TryHandler); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2139 | |
| 2140 | // Retrieve the exception object. We may emit multiple blocks but |
| 2141 | // nothing can cross this so the value is already in SSA form. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame^] | 2142 | llvm::Value *Caught = |
| 2143 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 2144 | ExceptionData, "caught"); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2145 | CGF.ObjCEHValueStack.back() = Caught; |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2146 | if (!isTry) |
| 2147 | { |
| 2148 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2149 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2150 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2151 | } |
| 2152 | else if (const ObjCAtCatchStmt* CatchStmt = |
| 2153 | cast<ObjCAtTryStmt>(S).getCatchStmts()) |
| 2154 | { |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2155 | // Enter a new exception try block (in case a @catch block throws |
| 2156 | // an exception). |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame^] | 2157 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2158 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame^] | 2159 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2160 | JmpBufPtr, "result"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2161 | llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2162 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2163 | llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch"); |
| 2164 | llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler"); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2165 | CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2166 | |
| 2167 | CGF.EmitBlock(CatchBlock); |
| 2168 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2169 | // Handle catch list. As a special case we check if everything is |
| 2170 | // matched and avoid generating code for falling off the end if |
| 2171 | // so. |
| 2172 | bool AllMatched = false; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2173 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2174 | llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2175 | |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2176 | const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl(); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2177 | const PointerType *PT = 0; |
| 2178 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2179 | // catch(...) always matches. |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2180 | if (!CatchParam) { |
| 2181 | AllMatched = true; |
| 2182 | } else { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2183 | PT = CatchParam->getType()->getAsPointerType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2184 | |
Daniel Dunbar | 97f61d1 | 2008-09-27 22:21:14 +0000 | [diff] [blame] | 2185 | // catch(id e) always matches. |
| 2186 | // FIXME: For the time being we also match id<X>; this should |
| 2187 | // be rejected by Sema instead. |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 2188 | if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) || |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2189 | CatchParam->getType()->isObjCQualifiedIdType()) |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2190 | AllMatched = true; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2191 | } |
| 2192 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2193 | if (AllMatched) { |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2194 | if (CatchParam) { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2195 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2196 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2197 | CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2198 | } |
Anders Carlsson | 1452f55 | 2008-09-11 08:21:54 +0000 | [diff] [blame] | 2199 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2200 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2201 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2202 | break; |
| 2203 | } |
| 2204 | |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2205 | assert(PT && "Unexpected non-pointer type in @catch"); |
| 2206 | QualType T = PT->getPointeeType(); |
Anders Carlsson | 4b7ff6e | 2008-09-11 06:35:14 +0000 | [diff] [blame] | 2207 | const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2208 | assert(ObjCType && "Catch parameter must have Objective-C type!"); |
| 2209 | |
| 2210 | // Check if the @catch block matches the exception object. |
| 2211 | llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl()); |
| 2212 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame^] | 2213 | llvm::Value *Match = |
| 2214 | CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(), |
| 2215 | Class, Caught, "match"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2216 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2217 | llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2218 | |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2219 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2220 | MatchedBlock, NextCatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2221 | |
| 2222 | // Emit the @catch block. |
| 2223 | CGF.EmitBlock(MatchedBlock); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2224 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2225 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2226 | |
| 2227 | llvm::Value *Tmp = |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2228 | CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()), |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2229 | "tmp"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2230 | CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2231 | |
| 2232 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2233 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2234 | |
| 2235 | CGF.EmitBlock(NextCatchBlock); |
| 2236 | } |
| 2237 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2238 | if (!AllMatched) { |
| 2239 | // None of the handlers caught the exception, so store it to be |
| 2240 | // rethrown at the end of the @finally block. |
| 2241 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2242 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2243 | } |
| 2244 | |
| 2245 | // Emit the exception handler for the @catch blocks. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2246 | CGF.EmitBlock(CatchHandler); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame^] | 2247 | CGF.Builder.CreateStore( |
| 2248 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 2249 | ExceptionData), |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2250 | RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2251 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2252 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2253 | } else { |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2254 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2255 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2256 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2257 | } |
| 2258 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2259 | // Pop the exception-handling stack entry. It is important to do |
| 2260 | // this now, because the code in the @finally block is not in this |
| 2261 | // context. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2262 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 2263 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2264 | CGF.ObjCEHValueStack.pop_back(); |
| 2265 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2266 | // Emit the @finally block. |
| 2267 | CGF.EmitBlock(FinallyBlock); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2268 | llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp"); |
| 2269 | |
| 2270 | CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit); |
| 2271 | |
| 2272 | CGF.EmitBlock(FinallyExit); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame^] | 2273 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2274 | |
| 2275 | CGF.EmitBlock(FinallyNoExit); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2276 | if (isTry) { |
| 2277 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 2278 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 2279 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2280 | } else { |
| 2281 | // Emit objc_sync_exit(expr); as finally's sole statement for |
| 2282 | // @synchronized. |
| 2283 | CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, SyncArg); |
Fariborz Jahanian | f2878e5 | 2008-11-21 19:21:53 +0000 | [diff] [blame] | 2284 | } |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2285 | |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2286 | // Emit the switch block |
| 2287 | if (Info.SwitchBlock) |
| 2288 | CGF.EmitBlock(Info.SwitchBlock); |
| 2289 | if (Info.EndBlock) |
| 2290 | CGF.EmitBlock(Info.EndBlock); |
| 2291 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2292 | CGF.EmitBlock(FinallyRethrow); |
| 2293 | CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, |
| 2294 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2295 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2296 | |
| 2297 | CGF.EmitBlock(FinallyEnd); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2298 | } |
| 2299 | |
| 2300 | void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2301 | const ObjCAtThrowStmt &S) { |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2302 | llvm::Value *ExceptionAsObject; |
| 2303 | |
| 2304 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
| 2305 | llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr); |
| 2306 | ExceptionAsObject = |
| 2307 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 2308 | } else { |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2309 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2310 | "Unexpected rethrow outside @catch block."); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2311 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2312 | } |
| 2313 | |
| 2314 | CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2315 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2316 | |
| 2317 | // Clear the insertion point to indicate we are in unreachable code. |
| 2318 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2319 | } |
| 2320 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2321 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2322 | /// object: objc_read_weak (id *src) |
| 2323 | /// |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2324 | llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2325 | llvm::Value *AddrWeakObj) |
| 2326 | { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2327 | const llvm::Type* DestTy = |
| 2328 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2329 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2330 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn, |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2331 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2332 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2333 | return read_weak; |
| 2334 | } |
| 2335 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2336 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 2337 | /// objc_assign_weak (id src, id *dst) |
| 2338 | /// |
| 2339 | void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 2340 | llvm::Value *src, llvm::Value *dst) |
| 2341 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2342 | const llvm::Type * SrcTy = src->getType(); |
| 2343 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2344 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2345 | assert(Size <= 8 && "does not support size > 8"); |
| 2346 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2347 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2348 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2349 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2350 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2351 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 2352 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2353 | src, dst, "weakassign"); |
| 2354 | return; |
| 2355 | } |
| 2356 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2357 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 2358 | /// objc_assign_global (id src, id *dst) |
| 2359 | /// |
| 2360 | void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 2361 | llvm::Value *src, llvm::Value *dst) |
| 2362 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2363 | const llvm::Type * SrcTy = src->getType(); |
| 2364 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2365 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2366 | assert(Size <= 8 && "does not support size > 8"); |
| 2367 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2368 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2369 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2370 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2371 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2372 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2373 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn, |
| 2374 | src, dst, "globalassign"); |
| 2375 | return; |
| 2376 | } |
| 2377 | |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2378 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 2379 | /// objc_assign_ivar (id src, id *dst) |
| 2380 | /// |
| 2381 | void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 2382 | llvm::Value *src, llvm::Value *dst) |
| 2383 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2384 | const llvm::Type * SrcTy = src->getType(); |
| 2385 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2386 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2387 | assert(Size <= 8 && "does not support size > 8"); |
| 2388 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2389 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2390 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2391 | } |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2392 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2393 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
| 2394 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn, |
| 2395 | src, dst, "assignivar"); |
| 2396 | return; |
| 2397 | } |
| 2398 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2399 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 2400 | /// objc_assign_strongCast (id src, id *dst) |
| 2401 | /// |
| 2402 | void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 2403 | llvm::Value *src, llvm::Value *dst) |
| 2404 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2405 | const llvm::Type * SrcTy = src->getType(); |
| 2406 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2407 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2408 | assert(Size <= 8 && "does not support size > 8"); |
| 2409 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2410 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2411 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2412 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2413 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2414 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2415 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn, |
| 2416 | src, dst, "weakassign"); |
| 2417 | return; |
| 2418 | } |
| 2419 | |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2420 | /// EmitObjCValueForIvar - Code Gen for ivar reference. |
| 2421 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2422 | LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 2423 | QualType ObjectTy, |
| 2424 | llvm::Value *BaseValue, |
| 2425 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2426 | unsigned CVRQualifiers) { |
Daniel Dunbar | 525c9b7 | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 2427 | const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl(); |
| 2428 | const FieldDecl *Field = ID->lookupFieldDeclForIvar(CGM.getContext(), Ivar); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2429 | if (Ivar->isBitField()) |
| 2430 | return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field), |
| 2431 | CVRQualifiers); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2432 | // TODO: Add a special case for isa (index 0) |
| 2433 | unsigned Index = CGM.getTypes().getLLVMFieldNo(Field); |
| 2434 | llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp"); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2435 | LValue LV = LValue::MakeAddr(V, |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 2436 | Ivar->getType().getCVRQualifiers()|CVRQualifiers, |
| 2437 | CGM.getContext().getObjCGCAttrKind(Ivar->getType())); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2438 | LValue::SetObjCIvar(LV, true); |
| 2439 | return LV; |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2440 | } |
| 2441 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2442 | llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
| 2443 | ObjCInterfaceDecl *Interface, |
| 2444 | const ObjCIvarDecl *Ivar) { |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 2445 | const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(Interface); |
Daniel Dunbar | 60952f9 | 2009-04-20 00:37:55 +0000 | [diff] [blame] | 2446 | const FieldDecl *Field = |
| 2447 | Interface->lookupFieldDeclForIvar(CGM.getContext(), Ivar); |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 2448 | uint64_t Offset = GetIvarBaseOffset(Layout, Field); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2449 | return llvm::ConstantInt::get( |
| 2450 | CGM.getTypes().ConvertType(CGM.getContext().LongTy), |
| 2451 | Offset); |
| 2452 | } |
| 2453 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2454 | /* *** Private Interface *** */ |
| 2455 | |
| 2456 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 2457 | /// level information. |
| 2458 | /// |
| 2459 | /// See: <rdr://4810609&4810587&4810587> |
| 2460 | /// struct IMAGE_INFO { |
| 2461 | /// unsigned version; |
| 2462 | /// unsigned flags; |
| 2463 | /// }; |
| 2464 | enum ImageInfoFlags { |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 2465 | eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what |
| 2466 | // this implies. |
| 2467 | eImageInfo_GarbageCollected = (1 << 1), |
| 2468 | eImageInfo_GCOnly = (1 << 2), |
| 2469 | eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set. |
| 2470 | |
| 2471 | // A flag indicating that the module has no instances of an |
| 2472 | // @synthesize of a superclass variable. <rdar://problem/6803242> |
| 2473 | eImageInfo_CorrectedSynthesize = (1 << 4) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2474 | }; |
| 2475 | |
| 2476 | void CGObjCMac::EmitImageInfo() { |
| 2477 | unsigned version = 0; // Version is unused? |
| 2478 | unsigned flags = 0; |
| 2479 | |
| 2480 | // FIXME: Fix and continue? |
| 2481 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 2482 | flags |= eImageInfo_GarbageCollected; |
| 2483 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 2484 | flags |= eImageInfo_GCOnly; |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 2485 | |
| 2486 | // We never allow @synthesize of a superclass property. |
| 2487 | flags |= eImageInfo_CorrectedSynthesize; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2488 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2489 | // Emitted as int[2]; |
| 2490 | llvm::Constant *values[2] = { |
| 2491 | llvm::ConstantInt::get(llvm::Type::Int32Ty, version), |
| 2492 | llvm::ConstantInt::get(llvm::Type::Int32Ty, flags) |
| 2493 | }; |
| 2494 | llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2495 | |
| 2496 | const char *Section; |
| 2497 | if (ObjCABI == 1) |
| 2498 | Section = "__OBJC, __image_info,regular"; |
| 2499 | else |
| 2500 | Section = "__DATA, __objc_imageinfo, regular, no_dead_strip"; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2501 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2502 | CreateMetadataVar("\01L_OBJC_IMAGE_INFO", |
| 2503 | llvm::ConstantArray::get(AT, values, 2), |
| 2504 | Section, |
| 2505 | 0, |
| 2506 | true); |
| 2507 | GV->setConstant(true); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2508 | } |
| 2509 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2510 | |
| 2511 | // struct objc_module { |
| 2512 | // unsigned long version; |
| 2513 | // unsigned long size; |
| 2514 | // const char *name; |
| 2515 | // Symtab symtab; |
| 2516 | // }; |
| 2517 | |
| 2518 | // FIXME: Get from somewhere |
| 2519 | static const int ModuleVersion = 7; |
| 2520 | |
| 2521 | void CGObjCMac::EmitModuleInfo() { |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 2522 | uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2523 | |
| 2524 | std::vector<llvm::Constant*> Values(4); |
| 2525 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion); |
| 2526 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2527 | // This used to be the filename, now it is unused. <rdr://4327263> |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2528 | Values[2] = GetClassName(&CGM.getContext().Idents.get("")); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2529 | Values[3] = EmitModuleSymbols(); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2530 | CreateMetadataVar("\01L_OBJC_MODULES", |
| 2531 | llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values), |
| 2532 | "__OBJC,__module_info,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2533 | 4, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2534 | } |
| 2535 | |
| 2536 | llvm::Constant *CGObjCMac::EmitModuleSymbols() { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2537 | unsigned NumClasses = DefinedClasses.size(); |
| 2538 | unsigned NumCategories = DefinedCategories.size(); |
| 2539 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2540 | // Return null if no symbols were defined. |
| 2541 | if (!NumClasses && !NumCategories) |
| 2542 | return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy); |
| 2543 | |
| 2544 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2545 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2546 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy); |
| 2547 | Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses); |
| 2548 | Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories); |
| 2549 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2550 | // The runtime expects exactly the list of defined classes followed |
| 2551 | // by the list of defined categories, in a single array. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2552 | std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2553 | for (unsigned i=0; i<NumClasses; i++) |
| 2554 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
| 2555 | ObjCTypes.Int8PtrTy); |
| 2556 | for (unsigned i=0; i<NumCategories; i++) |
| 2557 | Symbols[NumClasses + i] = |
| 2558 | llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
| 2559 | ObjCTypes.Int8PtrTy); |
| 2560 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2561 | Values[4] = |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2562 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2563 | NumClasses + NumCategories), |
| 2564 | Symbols); |
| 2565 | |
| 2566 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 2567 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2568 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2569 | CreateMetadataVar("\01L_OBJC_SYMBOLS", Init, |
| 2570 | "__OBJC,__symbols,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2571 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2572 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy); |
| 2573 | } |
| 2574 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2575 | llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2576 | const ObjCInterfaceDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2577 | LazySymbols.insert(ID->getIdentifier()); |
| 2578 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2579 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 2580 | |
| 2581 | if (!Entry) { |
| 2582 | llvm::Constant *Casted = |
| 2583 | llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()), |
| 2584 | ObjCTypes.ClassPtrTy); |
| 2585 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2586 | CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted, |
| 2587 | "__OBJC,__cls_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2588 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2589 | } |
| 2590 | |
| 2591 | return Builder.CreateLoad(Entry, false, "tmp"); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2592 | } |
| 2593 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2594 | llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2595 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 2596 | |
| 2597 | if (!Entry) { |
| 2598 | llvm::Constant *Casted = |
| 2599 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 2600 | ObjCTypes.SelectorPtrTy); |
| 2601 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2602 | CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted, |
| 2603 | "__OBJC,__message_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2604 | 4, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2605 | } |
| 2606 | |
| 2607 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 2608 | } |
| 2609 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 2610 | llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2611 | llvm::GlobalVariable *&Entry = ClassNames[Ident]; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2612 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2613 | if (!Entry) |
| 2614 | Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
| 2615 | llvm::ConstantArray::get(Ident->getName()), |
| 2616 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 2617 | 1, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2618 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2619 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2620 | } |
| 2621 | |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 2622 | /// GetInterfaceDeclStructLayout - Get layout for ivars of given |
| 2623 | /// interface declaration. |
| 2624 | const llvm::StructLayout *CGObjCCommonMac::GetInterfaceDeclStructLayout( |
| 2625 | const ObjCInterfaceDecl *OID) const { |
Daniel Dunbar | 24c8991 | 2009-04-21 21:41:56 +0000 | [diff] [blame] | 2626 | assert(!OID->isForwardDecl() && "Invalid interface decl!"); |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 2627 | QualType T = |
| 2628 | CGM.getContext().getObjCInterfaceType(const_cast<ObjCInterfaceDecl*>(OID)); |
| 2629 | const llvm::StructType *InterfaceTy = |
| 2630 | cast<llvm::StructType>(CGM.getTypes().ConvertType(T)); |
| 2631 | return CGM.getTargetData().getStructLayout(InterfaceTy); |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 2632 | } |
| 2633 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 2634 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 2635 | /// ivar layout bitmap. |
| 2636 | llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident, |
| 2637 | const ObjCCommonTypesHelper &ObjCTypes) { |
| 2638 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 2639 | } |
| 2640 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2641 | void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCInterfaceDecl *OI, |
| 2642 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2643 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2644 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2645 | unsigned int BytePos, bool ForStrongLayout, |
| 2646 | int &Index, int &SkIndex, bool &HasUnion) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2647 | bool IsUnion = (RD && RD->isUnion()); |
| 2648 | uint64_t MaxUnionIvarSize = 0; |
| 2649 | uint64_t MaxSkippedUnionIvarSize = 0; |
| 2650 | FieldDecl *MaxField = 0; |
| 2651 | FieldDecl *MaxSkippedField = 0; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2652 | FieldDecl *LastFieldBitfield = 0; |
| 2653 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2654 | unsigned base = 0; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2655 | if (RecFields.empty()) |
| 2656 | return; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2657 | if (IsUnion) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2658 | base = BytePos + GetFieldBaseOffset(OI, Layout, RecFields[0]); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2659 | unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0); |
| 2660 | unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth(); |
| 2661 | |
| 2662 | llvm::SmallVector<FieldDecl*, 16> TmpRecFields; |
| 2663 | |
| 2664 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2665 | FieldDecl *Field = RecFields[i]; |
| 2666 | // Skip over unnamed or bitfields |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2667 | if (!Field->getIdentifier() || Field->isBitField()) { |
| 2668 | LastFieldBitfield = Field; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2669 | continue; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2670 | } |
| 2671 | LastFieldBitfield = 0; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2672 | QualType FQT = Field->getType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2673 | if (FQT->isRecordType() || FQT->isUnionType()) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2674 | if (FQT->isUnionType()) |
| 2675 | HasUnion = true; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2676 | else |
| 2677 | assert(FQT->isRecordType() && |
| 2678 | "only union/record is supported for ivar layout bitmap"); |
| 2679 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2680 | const RecordType *RT = FQT->getAsRecordType(); |
| 2681 | const RecordDecl *RD = RT->getDecl(); |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 2682 | // FIXME - Find a more efficient way of passing records down. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2683 | TmpRecFields.append(RD->field_begin(CGM.getContext()), |
| 2684 | RD->field_end(CGM.getContext())); |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 2685 | const llvm::Type *Ty = CGM.getTypes().ConvertType(FQT); |
| 2686 | const llvm::StructLayout *RecLayout = |
| 2687 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty)); |
| 2688 | |
| 2689 | BuildAggrIvarLayout(0, RecLayout, RD, TmpRecFields, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2690 | BytePos + GetFieldBaseOffset(OI, Layout, Field), |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2691 | ForStrongLayout, Index, SkIndex, |
| 2692 | HasUnion); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2693 | TmpRecFields.clear(); |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2694 | continue; |
| 2695 | } |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2696 | |
| 2697 | if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2698 | const ConstantArrayType *CArray = |
| 2699 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2700 | uint64_t ElCount = CArray->getSize().getZExtValue(); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2701 | assert(CArray && "only array with know element size is supported"); |
| 2702 | FQT = CArray->getElementType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2703 | while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 2704 | const ConstantArrayType *CArray = |
| 2705 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2706 | ElCount *= CArray->getSize().getZExtValue(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2707 | FQT = CArray->getElementType(); |
| 2708 | } |
| 2709 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2710 | assert(!FQT->isUnionType() && |
| 2711 | "layout for array of unions not supported"); |
| 2712 | if (FQT->isRecordType()) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2713 | int OldIndex = Index; |
| 2714 | int OldSkIndex = SkIndex; |
| 2715 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2716 | // FIXME - Use a common routine with the above! |
| 2717 | const RecordType *RT = FQT->getAsRecordType(); |
| 2718 | const RecordDecl *RD = RT->getDecl(); |
| 2719 | // FIXME - Find a more efficiant way of passing records down. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2720 | TmpRecFields.append(RD->field_begin(CGM.getContext()), |
| 2721 | RD->field_end(CGM.getContext())); |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 2722 | const llvm::Type *Ty = CGM.getTypes().ConvertType(FQT); |
| 2723 | const llvm::StructLayout *RecLayout = |
| 2724 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty)); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2725 | |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 2726 | BuildAggrIvarLayout(0, RecLayout, RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2727 | TmpRecFields, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2728 | BytePos + GetFieldBaseOffset(OI, Layout, Field), |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2729 | ForStrongLayout, Index, SkIndex, |
| 2730 | HasUnion); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2731 | TmpRecFields.clear(); |
| 2732 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2733 | // Replicate layout information for each array element. Note that |
| 2734 | // one element is already done. |
| 2735 | uint64_t ElIx = 1; |
| 2736 | for (int FirstIndex = Index, FirstSkIndex = SkIndex; |
| 2737 | ElIx < ElCount; ElIx++) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2738 | uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2739 | for (int i = OldIndex+1; i <= FirstIndex; ++i) |
| 2740 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2741 | GC_IVAR gcivar; |
| 2742 | gcivar.ivar_bytepos = IvarsInfo[i].ivar_bytepos + Size*ElIx; |
| 2743 | gcivar.ivar_size = IvarsInfo[i].ivar_size; |
| 2744 | IvarsInfo.push_back(gcivar); ++Index; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2745 | } |
| 2746 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2747 | for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2748 | GC_IVAR skivar; |
| 2749 | skivar.ivar_bytepos = SkipIvars[i].ivar_bytepos + Size*ElIx; |
| 2750 | skivar.ivar_size = SkipIvars[i].ivar_size; |
| 2751 | SkipIvars.push_back(skivar); ++SkIndex; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2752 | } |
| 2753 | } |
| 2754 | continue; |
| 2755 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2756 | } |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2757 | // At this point, we are done with Record/Union and array there of. |
| 2758 | // For other arrays we are down to its element type. |
| 2759 | QualType::GCAttrTypes GCAttr = QualType::GCNone; |
| 2760 | do { |
| 2761 | if (FQT.isObjCGCStrong() || FQT.isObjCGCWeak()) { |
| 2762 | GCAttr = FQT.isObjCGCStrong() ? QualType::Strong : QualType::Weak; |
| 2763 | break; |
| 2764 | } |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2765 | else if (CGM.getContext().isObjCObjectPointerType(FQT)) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2766 | GCAttr = QualType::Strong; |
| 2767 | break; |
| 2768 | } |
| 2769 | else if (const PointerType *PT = FQT->getAsPointerType()) { |
| 2770 | FQT = PT->getPointeeType(); |
| 2771 | } |
| 2772 | else { |
| 2773 | break; |
| 2774 | } |
| 2775 | } while (true); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2776 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2777 | if ((ForStrongLayout && GCAttr == QualType::Strong) |
| 2778 | || (!ForStrongLayout && GCAttr == QualType::Weak)) { |
| 2779 | if (IsUnion) |
| 2780 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2781 | uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType()) |
| 2782 | / WordSizeInBits; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2783 | if (UnionIvarSize > MaxUnionIvarSize) |
| 2784 | { |
| 2785 | MaxUnionIvarSize = UnionIvarSize; |
| 2786 | MaxField = Field; |
| 2787 | } |
| 2788 | } |
| 2789 | else |
| 2790 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2791 | GC_IVAR gcivar; |
| 2792 | gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field); |
| 2793 | gcivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) / |
| 2794 | WordSizeInBits; |
| 2795 | IvarsInfo.push_back(gcivar); ++Index; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2796 | } |
| 2797 | } |
| 2798 | else if ((ForStrongLayout && |
| 2799 | (GCAttr == QualType::GCNone || GCAttr == QualType::Weak)) |
| 2800 | || (!ForStrongLayout && GCAttr != QualType::Weak)) { |
| 2801 | if (IsUnion) |
| 2802 | { |
| 2803 | uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType()); |
| 2804 | if (UnionIvarSize > MaxSkippedUnionIvarSize) |
| 2805 | { |
| 2806 | MaxSkippedUnionIvarSize = UnionIvarSize; |
| 2807 | MaxSkippedField = Field; |
| 2808 | } |
| 2809 | } |
| 2810 | else |
| 2811 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2812 | GC_IVAR skivar; |
| 2813 | skivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field); |
| 2814 | skivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) / |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2815 | ByteSizeInBits; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2816 | SkipIvars.push_back(skivar); ++SkIndex; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2817 | } |
| 2818 | } |
| 2819 | } |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2820 | if (LastFieldBitfield) { |
| 2821 | // Last field was a bitfield. Must update skip info. |
| 2822 | GC_IVAR skivar; |
| 2823 | skivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, |
| 2824 | LastFieldBitfield); |
| 2825 | Expr *BitWidth = LastFieldBitfield->getBitWidth(); |
| 2826 | uint64_t BitFieldSize = |
| 2827 | BitWidth->getIntegerConstantExprValue(CGM.getContext()).getZExtValue(); |
| 2828 | skivar.ivar_size = (BitFieldSize / ByteSizeInBits) |
| 2829 | + ((BitFieldSize % ByteSizeInBits) != 0); |
| 2830 | SkipIvars.push_back(skivar); ++SkIndex; |
| 2831 | } |
| 2832 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2833 | if (MaxField) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2834 | GC_IVAR gcivar; |
| 2835 | gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, MaxField); |
| 2836 | gcivar.ivar_size = MaxUnionIvarSize; |
| 2837 | IvarsInfo.push_back(gcivar); ++Index; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2838 | } |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2839 | |
| 2840 | if (MaxSkippedField) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2841 | GC_IVAR skivar; |
| 2842 | skivar.ivar_bytepos = BytePos + |
| 2843 | GetFieldBaseOffset(OI, Layout, MaxSkippedField); |
| 2844 | skivar.ivar_size = MaxSkippedUnionIvarSize; |
| 2845 | SkipIvars.push_back(skivar); ++SkIndex; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2846 | } |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2847 | } |
| 2848 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2849 | static int |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2850 | IvarBytePosCompare(const void *a, const void *b) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2851 | { |
| 2852 | unsigned int sa = ((CGObjCCommonMac::GC_IVAR *)a)->ivar_bytepos; |
| 2853 | unsigned int sb = ((CGObjCCommonMac::GC_IVAR *)b)->ivar_bytepos; |
| 2854 | |
| 2855 | if (sa < sb) |
| 2856 | return -1; |
| 2857 | if (sa > sb) |
| 2858 | return 1; |
| 2859 | return 0; |
| 2860 | } |
| 2861 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2862 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 2863 | /// implementation for the __strong or __weak case. |
| 2864 | /// The layout map displays which words in ivar list must be skipped |
| 2865 | /// and which must be scanned by GC (see below). String is built of bytes. |
| 2866 | /// Each byte is divided up in two nibbles (4-bit each). Left nibble is count |
| 2867 | /// of words to skip and right nibble is count of words to scan. So, each |
| 2868 | /// nibble represents up to 15 workds to skip or scan. Skipping the rest is |
| 2869 | /// represented by a 0x00 byte which also ends the string. |
| 2870 | /// 1. when ForStrongLayout is true, following ivars are scanned: |
| 2871 | /// - id, Class |
| 2872 | /// - object * |
| 2873 | /// - __strong anything |
| 2874 | /// |
| 2875 | /// 2. When ForStrongLayout is false, following ivars are scanned: |
| 2876 | /// - __weak anything |
| 2877 | /// |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2878 | llvm::Constant *CGObjCCommonMac::BuildIvarLayout( |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2879 | const ObjCImplementationDecl *OMD, |
| 2880 | bool ForStrongLayout) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2881 | int Index = -1; |
| 2882 | int SkIndex = -1; |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2883 | bool hasUnion = false; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2884 | int SkipScan; |
| 2885 | unsigned int WordsToScan, WordsToSkip; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2886 | const llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 2887 | if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC) |
| 2888 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2889 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2890 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2891 | const ObjCInterfaceDecl *OI = OMD->getClassInterface(); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2892 | CGM.getContext().CollectObjCIvars(OI, RecFields); |
| 2893 | if (RecFields.empty()) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2894 | return llvm::Constant::getNullValue(PtrTy); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2895 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2896 | SkipIvars.clear(); |
| 2897 | IvarsInfo.clear(); |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 2898 | |
| 2899 | const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OI); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2900 | BuildAggrIvarLayout(OI, Layout, 0, RecFields, 0, ForStrongLayout, |
| 2901 | Index, SkIndex, hasUnion); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2902 | if (Index == -1) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2903 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2904 | |
| 2905 | // Sort on byte position in case we encounterred a union nested in |
| 2906 | // the ivar list. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2907 | if (hasUnion && !IvarsInfo.empty()) |
| 2908 | qsort(&IvarsInfo[0], Index+1, sizeof(GC_IVAR), IvarBytePosCompare); |
| 2909 | if (hasUnion && !SkipIvars.empty()) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2910 | qsort(&SkipIvars[0], Index+1, sizeof(GC_IVAR), IvarBytePosCompare); |
| 2911 | |
| 2912 | // Build the string of skip/scan nibbles |
| 2913 | SkipScan = -1; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2914 | SkipScanIvars.clear(); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2915 | unsigned int WordSize = |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2916 | CGM.getTypes().getTargetData().getTypePaddedSize(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2917 | if (IvarsInfo[0].ivar_bytepos == 0) { |
| 2918 | WordsToSkip = 0; |
| 2919 | WordsToScan = IvarsInfo[0].ivar_size; |
| 2920 | } |
| 2921 | else { |
| 2922 | WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize; |
| 2923 | WordsToScan = IvarsInfo[0].ivar_size; |
| 2924 | } |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2925 | for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2926 | { |
| 2927 | unsigned int TailPrevGCObjC = |
| 2928 | IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize; |
| 2929 | if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) |
| 2930 | { |
| 2931 | // consecutive 'scanned' object pointers. |
| 2932 | WordsToScan += IvarsInfo[i].ivar_size; |
| 2933 | } |
| 2934 | else |
| 2935 | { |
| 2936 | // Skip over 'gc'able object pointer which lay over each other. |
| 2937 | if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos) |
| 2938 | continue; |
| 2939 | // Must skip over 1 or more words. We save current skip/scan values |
| 2940 | // and start a new pair. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2941 | SKIP_SCAN SkScan; |
| 2942 | SkScan.skip = WordsToSkip; |
| 2943 | SkScan.scan = WordsToScan; |
| 2944 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
| 2945 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2946 | // Skip the hole. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2947 | SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize; |
| 2948 | SkScan.scan = 0; |
| 2949 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2950 | WordsToSkip = 0; |
| 2951 | WordsToScan = IvarsInfo[i].ivar_size; |
| 2952 | } |
| 2953 | } |
| 2954 | if (WordsToScan > 0) |
| 2955 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2956 | SKIP_SCAN SkScan; |
| 2957 | SkScan.skip = WordsToSkip; |
| 2958 | SkScan.scan = WordsToScan; |
| 2959 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2960 | } |
| 2961 | |
| 2962 | bool BytesSkipped = false; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2963 | if (!SkipIvars.empty()) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2964 | { |
| 2965 | int LastByteSkipped = |
| 2966 | SkipIvars[SkIndex].ivar_bytepos + SkipIvars[SkIndex].ivar_size; |
| 2967 | int LastByteScanned = |
| 2968 | IvarsInfo[Index].ivar_bytepos + IvarsInfo[Index].ivar_size * WordSize; |
| 2969 | BytesSkipped = (LastByteSkipped > LastByteScanned); |
| 2970 | // Compute number of bytes to skip at the tail end of the last ivar scanned. |
| 2971 | if (BytesSkipped) |
| 2972 | { |
| 2973 | unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2974 | SKIP_SCAN SkScan; |
| 2975 | SkScan.skip = TotalWords - (LastByteScanned/WordSize); |
| 2976 | SkScan.scan = 0; |
| 2977 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2978 | } |
| 2979 | } |
| 2980 | // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced |
| 2981 | // as 0xMN. |
| 2982 | for (int i = 0; i <= SkipScan; i++) |
| 2983 | { |
| 2984 | if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0 |
| 2985 | && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) { |
| 2986 | // 0xM0 followed by 0x0N detected. |
| 2987 | SkipScanIvars[i].scan = SkipScanIvars[i+1].scan; |
| 2988 | for (int j = i+1; j < SkipScan; j++) |
| 2989 | SkipScanIvars[j] = SkipScanIvars[j+1]; |
| 2990 | --SkipScan; |
| 2991 | } |
| 2992 | } |
| 2993 | |
| 2994 | // Generate the string. |
| 2995 | std::string BitMap; |
| 2996 | for (int i = 0; i <= SkipScan; i++) |
| 2997 | { |
| 2998 | unsigned char byte; |
| 2999 | unsigned int skip_small = SkipScanIvars[i].skip % 0xf; |
| 3000 | unsigned int scan_small = SkipScanIvars[i].scan % 0xf; |
| 3001 | unsigned int skip_big = SkipScanIvars[i].skip / 0xf; |
| 3002 | unsigned int scan_big = SkipScanIvars[i].scan / 0xf; |
| 3003 | |
| 3004 | if (skip_small > 0 || skip_big > 0) |
| 3005 | BytesSkipped = true; |
| 3006 | // first skip big. |
| 3007 | for (unsigned int ix = 0; ix < skip_big; ix++) |
| 3008 | BitMap += (unsigned char)(0xf0); |
| 3009 | |
| 3010 | // next (skip small, scan) |
| 3011 | if (skip_small) |
| 3012 | { |
| 3013 | byte = skip_small << 4; |
| 3014 | if (scan_big > 0) |
| 3015 | { |
| 3016 | byte |= 0xf; |
| 3017 | --scan_big; |
| 3018 | } |
| 3019 | else if (scan_small) |
| 3020 | { |
| 3021 | byte |= scan_small; |
| 3022 | scan_small = 0; |
| 3023 | } |
| 3024 | BitMap += byte; |
| 3025 | } |
| 3026 | // next scan big |
| 3027 | for (unsigned int ix = 0; ix < scan_big; ix++) |
| 3028 | BitMap += (unsigned char)(0x0f); |
| 3029 | // last scan small |
| 3030 | if (scan_small) |
| 3031 | { |
| 3032 | byte = scan_small; |
| 3033 | BitMap += byte; |
| 3034 | } |
| 3035 | } |
| 3036 | // null terminate string. |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3037 | unsigned char zero = 0; |
| 3038 | BitMap += zero; |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 3039 | |
| 3040 | if (CGM.getLangOptions().ObjCGCBitmapPrint) { |
| 3041 | printf("\n%s ivar layout for class '%s': ", |
| 3042 | ForStrongLayout ? "strong" : "weak", |
| 3043 | OMD->getClassInterface()->getNameAsCString()); |
| 3044 | const unsigned char *s = (unsigned char*)BitMap.c_str(); |
| 3045 | for (unsigned i = 0; i < BitMap.size(); i++) |
| 3046 | if (!(s[i] & 0xf0)) |
| 3047 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 3048 | else |
| 3049 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 3050 | printf("\n"); |
| 3051 | } |
| 3052 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3053 | // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as |
| 3054 | // final layout. |
| 3055 | if (ForStrongLayout && !BytesSkipped) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3056 | return llvm::Constant::getNullValue(PtrTy); |
| 3057 | llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
| 3058 | llvm::ConstantArray::get(BitMap.c_str()), |
| 3059 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3060 | 1, true); |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 3061 | return getConstantGEP(Entry, 0, 0); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3062 | } |
| 3063 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3064 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3065 | llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; |
| 3066 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3067 | // FIXME: Avoid std::string copying. |
| 3068 | if (!Entry) |
| 3069 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_", |
| 3070 | llvm::ConstantArray::get(Sel.getAsString()), |
| 3071 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3072 | 1, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3073 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3074 | return getConstantGEP(Entry, 0, 0); |
| 3075 | } |
| 3076 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3077 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3078 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3079 | return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); |
| 3080 | } |
| 3081 | |
| 3082 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3083 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3084 | return GetMethodVarName(&CGM.getContext().Idents.get(Name)); |
| 3085 | } |
| 3086 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 3087 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) { |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3088 | std::string TypeStr; |
| 3089 | CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); |
| 3090 | |
| 3091 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3092 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3093 | if (!Entry) |
| 3094 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
| 3095 | llvm::ConstantArray::get(TypeStr), |
| 3096 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3097 | 1, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3098 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3099 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3100 | } |
| 3101 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3102 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3103 | std::string TypeStr; |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3104 | CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D), |
| 3105 | TypeStr); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3106 | |
| 3107 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
| 3108 | |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3109 | if (!Entry) |
| 3110 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
| 3111 | llvm::ConstantArray::get(TypeStr), |
| 3112 | "__TEXT,__cstring,cstring_literals", |
| 3113 | 1, true); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3114 | |
| 3115 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3116 | } |
| 3117 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3118 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3119 | llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3120 | llvm::GlobalVariable *&Entry = PropertyNames[Ident]; |
| 3121 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3122 | if (!Entry) |
| 3123 | Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_", |
| 3124 | llvm::ConstantArray::get(Ident->getName()), |
| 3125 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3126 | 1, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3127 | |
| 3128 | return getConstantGEP(Entry, 0, 0); |
| 3129 | } |
| 3130 | |
| 3131 | // FIXME: Merge into a single cstring creation function. |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3132 | // FIXME: This Decl should be more precise. |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3133 | llvm::Constant * |
| 3134 | CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 3135 | const Decl *Container) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3136 | std::string TypeStr; |
| 3137 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3138 | return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); |
| 3139 | } |
| 3140 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3141 | void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D, |
| 3142 | const ObjCContainerDecl *CD, |
| 3143 | std::string &NameOut) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 3144 | NameOut = '\01'; |
| 3145 | NameOut += (D->isInstanceMethod() ? '-' : '+'); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3146 | NameOut += '['; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 3147 | assert (CD && "Missing container decl in GetNameForMethod"); |
| 3148 | NameOut += CD->getNameAsString(); |
Fariborz Jahanian | 1e9aef3 | 2009-04-16 18:34:20 +0000 | [diff] [blame] | 3149 | if (const ObjCCategoryImplDecl *CID = |
| 3150 | dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) { |
| 3151 | NameOut += '('; |
| 3152 | NameOut += CID->getNameAsString(); |
| 3153 | NameOut+= ')'; |
| 3154 | } |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3155 | NameOut += ' '; |
| 3156 | NameOut += D->getSelector().getAsString(); |
| 3157 | NameOut += ']'; |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3158 | } |
| 3159 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3160 | void CGObjCMac::FinishModule() { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3161 | EmitModuleInfo(); |
| 3162 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3163 | // Emit the dummy bodies for any protocols which were referenced but |
| 3164 | // never defined. |
| 3165 | for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator |
| 3166 | i = Protocols.begin(), e = Protocols.end(); i != e; ++i) { |
| 3167 | if (i->second->hasInitializer()) |
| 3168 | continue; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3169 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3170 | std::vector<llvm::Constant*> Values(5); |
| 3171 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
| 3172 | Values[1] = GetClassName(i->first); |
| 3173 | Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 3174 | Values[3] = Values[4] = |
| 3175 | llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
| 3176 | i->second->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 3177 | i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 3178 | Values)); |
| 3179 | } |
| 3180 | |
| 3181 | std::vector<llvm::Constant*> Used; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3182 | for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3183 | e = UsedGlobals.end(); i != e; ++i) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3184 | Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3185 | } |
| 3186 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3187 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3188 | llvm::GlobalValue *GV = |
| 3189 | new llvm::GlobalVariable(AT, false, |
| 3190 | llvm::GlobalValue::AppendingLinkage, |
| 3191 | llvm::ConstantArray::get(AT, Used), |
| 3192 | "llvm.used", |
| 3193 | &CGM.getModule()); |
| 3194 | |
| 3195 | GV->setSection("llvm.metadata"); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3196 | |
| 3197 | // Add assembler directives to add lazy undefined symbol references |
| 3198 | // for classes which are referenced but not defined. This is |
| 3199 | // important for correct linker interaction. |
| 3200 | |
| 3201 | // FIXME: Uh, this isn't particularly portable. |
| 3202 | std::stringstream s; |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 3203 | |
| 3204 | if (!CGM.getModule().getModuleInlineAsm().empty()) |
| 3205 | s << "\n"; |
| 3206 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3207 | for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(), |
| 3208 | e = LazySymbols.end(); i != e; ++i) { |
| 3209 | s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n"; |
| 3210 | } |
| 3211 | for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(), |
| 3212 | e = DefinedSymbols.end(); i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3213 | s << "\t.objc_class_name_" << (*i)->getName() << "=0\n" |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3214 | << "\t.globl .objc_class_name_" << (*i)->getName() << "\n"; |
| 3215 | } |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 3216 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3217 | CGM.getModule().appendModuleInlineAsm(s.str()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3218 | } |
| 3219 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3220 | CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3221 | : CGObjCCommonMac(cgm), |
| 3222 | ObjCTypes(cgm) |
| 3223 | { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3224 | ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3225 | ObjCABI = 2; |
| 3226 | } |
| 3227 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3228 | /* *** */ |
| 3229 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3230 | ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm) |
| 3231 | : CGM(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3232 | { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3233 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3234 | ASTContext &Ctx = CGM.getContext(); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3235 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3236 | ShortTy = Types.ConvertType(Ctx.ShortTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3237 | IntTy = Types.ConvertType(Ctx.IntTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3238 | LongTy = Types.ConvertType(Ctx.LongTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3239 | LongLongTy = Types.ConvertType(Ctx.LongLongTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3240 | Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 3241 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3242 | ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType()); |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 3243 | PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3244 | SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3245 | |
| 3246 | // FIXME: It would be nice to unify this with the opaque type, so |
| 3247 | // that the IR comes out a bit cleaner. |
| 3248 | const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); |
| 3249 | ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3250 | |
| 3251 | // I'm not sure I like this. The implicit coordination is a bit |
| 3252 | // gross. We should solve this in a reasonable fashion because this |
| 3253 | // is a pretty common task (match some runtime data structure with |
| 3254 | // an LLVM data structure). |
| 3255 | |
| 3256 | // FIXME: This is leaked. |
| 3257 | // FIXME: Merge with rewriter code? |
| 3258 | |
| 3259 | // struct _objc_super { |
| 3260 | // id self; |
| 3261 | // Class cls; |
| 3262 | // } |
| 3263 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 3264 | SourceLocation(), |
| 3265 | &Ctx.Idents.get("_objc_super")); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3266 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3267 | Ctx.getObjCIdType(), 0, false)); |
| 3268 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3269 | Ctx.getObjCClassType(), 0, false)); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3270 | RD->completeDefinition(Ctx); |
| 3271 | |
| 3272 | SuperCTy = Ctx.getTagDeclType(RD); |
| 3273 | SuperPtrCTy = Ctx.getPointerType(SuperCTy); |
| 3274 | |
| 3275 | SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy)); |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3276 | SuperPtrTy = llvm::PointerType::getUnqual(SuperTy); |
| 3277 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3278 | // struct _prop_t { |
| 3279 | // char *name; |
| 3280 | // char *attributes; |
| 3281 | // } |
| 3282 | PropertyTy = llvm::StructType::get(Int8PtrTy, |
| 3283 | Int8PtrTy, |
| 3284 | NULL); |
| 3285 | CGM.getModule().addTypeName("struct._prop_t", |
| 3286 | PropertyTy); |
| 3287 | |
| 3288 | // struct _prop_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3289 | // uint32_t entsize; // sizeof(struct _prop_t) |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3290 | // uint32_t count_of_properties; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3291 | // struct _prop_t prop_list[count_of_properties]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3292 | // } |
| 3293 | PropertyListTy = llvm::StructType::get(IntTy, |
| 3294 | IntTy, |
| 3295 | llvm::ArrayType::get(PropertyTy, 0), |
| 3296 | NULL); |
| 3297 | CGM.getModule().addTypeName("struct._prop_list_t", |
| 3298 | PropertyListTy); |
| 3299 | // struct _prop_list_t * |
| 3300 | PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy); |
| 3301 | |
| 3302 | // struct _objc_method { |
| 3303 | // SEL _cmd; |
| 3304 | // char *method_type; |
| 3305 | // char *_imp; |
| 3306 | // } |
| 3307 | MethodTy = llvm::StructType::get(SelectorPtrTy, |
| 3308 | Int8PtrTy, |
| 3309 | Int8PtrTy, |
| 3310 | NULL); |
| 3311 | CGM.getModule().addTypeName("struct._objc_method", MethodTy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3312 | |
| 3313 | // struct _objc_cache * |
| 3314 | CacheTy = llvm::OpaqueType::get(); |
| 3315 | CGM.getModule().addTypeName("struct._objc_cache", CacheTy); |
| 3316 | CachePtrTy = llvm::PointerType::getUnqual(CacheTy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3317 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3318 | // Property manipulation functions. |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3319 | |
| 3320 | QualType IdType = Ctx.getObjCIdType(); |
| 3321 | QualType SelType = Ctx.getObjCSelType(); |
| 3322 | llvm::SmallVector<QualType,16> Params; |
| 3323 | const llvm::FunctionType *FTy; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3324 | |
| 3325 | // id objc_getProperty (id, SEL, ptrdiff_t, bool) |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3326 | Params.push_back(IdType); |
| 3327 | Params.push_back(SelType); |
| 3328 | Params.push_back(Ctx.LongTy); |
| 3329 | Params.push_back(Ctx.BoolTy); |
| 3330 | FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), |
| 3331 | false); |
| 3332 | GetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_getProperty"); |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3333 | |
| 3334 | // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool) |
| 3335 | Params.clear(); |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3336 | Params.push_back(IdType); |
| 3337 | Params.push_back(SelType); |
| 3338 | Params.push_back(Ctx.LongTy); |
| 3339 | Params.push_back(IdType); |
| 3340 | Params.push_back(Ctx.BoolTy); |
| 3341 | Params.push_back(Ctx.BoolTy); |
| 3342 | FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
| 3343 | SetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_setProperty"); |
| 3344 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3345 | // Enumeration mutation. |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3346 | |
| 3347 | // void objc_enumerationMutation (id) |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3348 | Params.clear(); |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3349 | Params.push_back(IdType); |
| 3350 | FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
| 3351 | EnumerationMutationFn = CGM.CreateRuntimeFunction(FTy, |
| 3352 | "objc_enumerationMutation"); |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3353 | |
| 3354 | // gc's API |
| 3355 | // id objc_read_weak (id *) |
| 3356 | Params.clear(); |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3357 | Params.push_back(Ctx.getPointerType(IdType)); |
| 3358 | FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false); |
| 3359 | GcReadWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_read_weak"); |
| 3360 | |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 3361 | // id objc_assign_global (id, id *) |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3362 | Params.clear(); |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3363 | Params.push_back(IdType); |
| 3364 | Params.push_back(Ctx.getPointerType(IdType)); |
| 3365 | |
| 3366 | FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false); |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3367 | GcAssignGlobalFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_global"); |
| 3368 | GcAssignIvarFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar"); |
| 3369 | GcAssignStrongCastFn = |
| 3370 | CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast"); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 3371 | |
| 3372 | // void objc_exception_throw(id) |
| 3373 | Params.clear(); |
| 3374 | Params.push_back(IdType); |
| 3375 | |
| 3376 | FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 3377 | ExceptionThrowFn = |
| 3378 | CGM.CreateRuntimeFunction(FTy, "objc_exception_throw"); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3379 | |
| 3380 | // synchronized APIs |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3381 | // void objc_sync_exit (id) |
| 3382 | Params.clear(); |
| 3383 | Params.push_back(IdType); |
| 3384 | |
| 3385 | FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3386 | SyncExitFn = CGM.CreateRuntimeFunction(FTy, "objc_sync_exit"); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3387 | } |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3388 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3389 | ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) |
| 3390 | : ObjCCommonTypesHelper(cgm) |
| 3391 | { |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3392 | // struct _objc_method_description { |
| 3393 | // SEL name; |
| 3394 | // char *types; |
| 3395 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3396 | MethodDescriptionTy = |
| 3397 | llvm::StructType::get(SelectorPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3398 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3399 | NULL); |
| 3400 | CGM.getModule().addTypeName("struct._objc_method_description", |
| 3401 | MethodDescriptionTy); |
| 3402 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3403 | // struct _objc_method_description_list { |
| 3404 | // int count; |
| 3405 | // struct _objc_method_description[1]; |
| 3406 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3407 | MethodDescriptionListTy = |
| 3408 | llvm::StructType::get(IntTy, |
| 3409 | llvm::ArrayType::get(MethodDescriptionTy, 0), |
| 3410 | NULL); |
| 3411 | CGM.getModule().addTypeName("struct._objc_method_description_list", |
| 3412 | MethodDescriptionListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3413 | |
| 3414 | // struct _objc_method_description_list * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3415 | MethodDescriptionListPtrTy = |
| 3416 | llvm::PointerType::getUnqual(MethodDescriptionListTy); |
| 3417 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3418 | // Protocol description structures |
| 3419 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3420 | // struct _objc_protocol_extension { |
| 3421 | // uint32_t size; // sizeof(struct _objc_protocol_extension) |
| 3422 | // struct _objc_method_description_list *optional_instance_methods; |
| 3423 | // struct _objc_method_description_list *optional_class_methods; |
| 3424 | // struct _objc_property_list *instance_properties; |
| 3425 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3426 | ProtocolExtensionTy = |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3427 | llvm::StructType::get(IntTy, |
| 3428 | MethodDescriptionListPtrTy, |
| 3429 | MethodDescriptionListPtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3430 | PropertyListPtrTy, |
| 3431 | NULL); |
| 3432 | CGM.getModule().addTypeName("struct._objc_protocol_extension", |
| 3433 | ProtocolExtensionTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3434 | |
| 3435 | // struct _objc_protocol_extension * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3436 | ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy); |
| 3437 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3438 | // Handle recursive construction of Protocol and ProtocolList types |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3439 | |
| 3440 | llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(); |
| 3441 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 3442 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3443 | const llvm::Type *T = |
| 3444 | llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 3445 | LongTy, |
| 3446 | llvm::ArrayType::get(ProtocolTyHolder, 0), |
| 3447 | NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3448 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T); |
| 3449 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3450 | // struct _objc_protocol { |
| 3451 | // struct _objc_protocol_extension *isa; |
| 3452 | // char *protocol_name; |
| 3453 | // struct _objc_protocol **_objc_protocol_list; |
| 3454 | // struct _objc_method_description_list *instance_methods; |
| 3455 | // struct _objc_method_description_list *class_methods; |
| 3456 | // } |
| 3457 | T = llvm::StructType::get(ProtocolExtensionPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3458 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3459 | llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 3460 | MethodDescriptionListPtrTy, |
| 3461 | MethodDescriptionListPtrTy, |
| 3462 | NULL); |
| 3463 | cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T); |
| 3464 | |
| 3465 | ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get()); |
| 3466 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 3467 | ProtocolListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3468 | // struct _objc_protocol_list * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3469 | ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy); |
| 3470 | |
| 3471 | ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get()); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3472 | CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3473 | ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3474 | |
| 3475 | // Class description structures |
| 3476 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3477 | // struct _objc_ivar { |
| 3478 | // char *ivar_name; |
| 3479 | // char *ivar_type; |
| 3480 | // int ivar_offset; |
| 3481 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3482 | IvarTy = llvm::StructType::get(Int8PtrTy, |
| 3483 | Int8PtrTy, |
| 3484 | IntTy, |
| 3485 | NULL); |
| 3486 | CGM.getModule().addTypeName("struct._objc_ivar", IvarTy); |
| 3487 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3488 | // struct _objc_ivar_list * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3489 | IvarListTy = llvm::OpaqueType::get(); |
| 3490 | CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy); |
| 3491 | IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy); |
| 3492 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3493 | // struct _objc_method_list * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3494 | MethodListTy = llvm::OpaqueType::get(); |
| 3495 | CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy); |
| 3496 | MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy); |
| 3497 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3498 | // struct _objc_class_extension * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3499 | ClassExtensionTy = |
| 3500 | llvm::StructType::get(IntTy, |
| 3501 | Int8PtrTy, |
| 3502 | PropertyListPtrTy, |
| 3503 | NULL); |
| 3504 | CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy); |
| 3505 | ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy); |
| 3506 | |
| 3507 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 3508 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3509 | // struct _objc_class { |
| 3510 | // Class isa; |
| 3511 | // Class super_class; |
| 3512 | // char *name; |
| 3513 | // long version; |
| 3514 | // long info; |
| 3515 | // long instance_size; |
| 3516 | // struct _objc_ivar_list *ivars; |
| 3517 | // struct _objc_method_list *methods; |
| 3518 | // struct _objc_cache *cache; |
| 3519 | // struct _objc_protocol_list *protocols; |
| 3520 | // char *ivar_layout; |
| 3521 | // struct _objc_class_ext *ext; |
| 3522 | // }; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3523 | T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 3524 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 3525 | Int8PtrTy, |
| 3526 | LongTy, |
| 3527 | LongTy, |
| 3528 | LongTy, |
| 3529 | IvarListPtrTy, |
| 3530 | MethodListPtrTy, |
| 3531 | CachePtrTy, |
| 3532 | ProtocolListPtrTy, |
| 3533 | Int8PtrTy, |
| 3534 | ClassExtensionPtrTy, |
| 3535 | NULL); |
| 3536 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T); |
| 3537 | |
| 3538 | ClassTy = cast<llvm::StructType>(ClassTyHolder.get()); |
| 3539 | CGM.getModule().addTypeName("struct._objc_class", ClassTy); |
| 3540 | ClassPtrTy = llvm::PointerType::getUnqual(ClassTy); |
| 3541 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3542 | // struct _objc_category { |
| 3543 | // char *category_name; |
| 3544 | // char *class_name; |
| 3545 | // struct _objc_method_list *instance_method; |
| 3546 | // struct _objc_method_list *class_method; |
| 3547 | // uint32_t size; // sizeof(struct _objc_category) |
| 3548 | // struct _objc_property_list *instance_properties;// category's @property |
| 3549 | // } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3550 | CategoryTy = llvm::StructType::get(Int8PtrTy, |
| 3551 | Int8PtrTy, |
| 3552 | MethodListPtrTy, |
| 3553 | MethodListPtrTy, |
| 3554 | ProtocolListPtrTy, |
| 3555 | IntTy, |
| 3556 | PropertyListPtrTy, |
| 3557 | NULL); |
| 3558 | CGM.getModule().addTypeName("struct._objc_category", CategoryTy); |
| 3559 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3560 | // Global metadata structures |
| 3561 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3562 | // struct _objc_symtab { |
| 3563 | // long sel_ref_cnt; |
| 3564 | // SEL *refs; |
| 3565 | // short cls_def_cnt; |
| 3566 | // short cat_def_cnt; |
| 3567 | // char *defs[cls_def_cnt + cat_def_cnt]; |
| 3568 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3569 | SymtabTy = llvm::StructType::get(LongTy, |
| 3570 | SelectorPtrTy, |
| 3571 | ShortTy, |
| 3572 | ShortTy, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3573 | llvm::ArrayType::get(Int8PtrTy, 0), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3574 | NULL); |
| 3575 | CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy); |
| 3576 | SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy); |
| 3577 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3578 | // struct _objc_module { |
| 3579 | // long version; |
| 3580 | // long size; // sizeof(struct _objc_module) |
| 3581 | // char *name; |
| 3582 | // struct _objc_symtab* symtab; |
| 3583 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3584 | ModuleTy = |
| 3585 | llvm::StructType::get(LongTy, |
| 3586 | LongTy, |
| 3587 | Int8PtrTy, |
| 3588 | SymtabPtrTy, |
| 3589 | NULL); |
| 3590 | CGM.getModule().addTypeName("struct._objc_module", ModuleTy); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3591 | |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 3592 | // Message send functions. |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3593 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3594 | // id objc_msgSend (id, SEL, ...) |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3595 | std::vector<const llvm::Type*> Params; |
| 3596 | Params.push_back(ObjectPtrTy); |
| 3597 | Params.push_back(SelectorPtrTy); |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3598 | MessageSendFn = |
| 3599 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3600 | Params, |
| 3601 | true), |
| 3602 | "objc_msgSend"); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3603 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3604 | // id objc_msgSend_stret (id, SEL, ...) |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3605 | Params.clear(); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3606 | Params.push_back(ObjectPtrTy); |
| 3607 | Params.push_back(SelectorPtrTy); |
| 3608 | MessageSendStretFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3609 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3610 | Params, |
| 3611 | true), |
| 3612 | "objc_msgSend_stret"); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 3613 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3614 | // |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 3615 | Params.clear(); |
| 3616 | Params.push_back(ObjectPtrTy); |
| 3617 | Params.push_back(SelectorPtrTy); |
| 3618 | // FIXME: This should be long double on x86_64? |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3619 | // [double | long double] objc_msgSend_fpret(id self, SEL op, ...) |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 3620 | MessageSendFpretFn = |
| 3621 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy, |
| 3622 | Params, |
| 3623 | true), |
| 3624 | "objc_msgSend_fpret"); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3625 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3626 | // id objc_msgSendSuper(struct objc_super *super, SEL op, ...) |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3627 | Params.clear(); |
| 3628 | Params.push_back(SuperPtrTy); |
| 3629 | Params.push_back(SelectorPtrTy); |
| 3630 | MessageSendSuperFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3631 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3632 | Params, |
| 3633 | true), |
| 3634 | "objc_msgSendSuper"); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3635 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3636 | // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super, |
| 3637 | // SEL op, ...) |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3638 | Params.clear(); |
| 3639 | Params.push_back(Int8PtrTy); |
| 3640 | Params.push_back(SuperPtrTy); |
| 3641 | Params.push_back(SelectorPtrTy); |
| 3642 | MessageSendSuperStretFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3643 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3644 | Params, |
| 3645 | true), |
| 3646 | "objc_msgSendSuper_stret"); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 3647 | |
| 3648 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 3649 | MessageSendSuperFpretFn = MessageSendSuperFn; |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 3650 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3651 | // FIXME: This is the size of the setjmp buffer and should be |
| 3652 | // target specific. 18 is what's used on 32-bit X86. |
| 3653 | uint64_t SetJmpBufferSize = 18; |
| 3654 | |
| 3655 | // Exceptions |
| 3656 | const llvm::Type *StackPtrTy = |
Daniel Dunbar | 1000491 | 2008-09-27 06:32:25 +0000 | [diff] [blame] | 3657 | llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3658 | |
| 3659 | ExceptionDataTy = |
| 3660 | llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty, |
| 3661 | SetJmpBufferSize), |
| 3662 | StackPtrTy, NULL); |
| 3663 | CGM.getModule().addTypeName("struct._objc_exception_data", |
| 3664 | ExceptionDataTy); |
| 3665 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3666 | } |
| 3667 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3668 | ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3669 | : ObjCCommonTypesHelper(cgm) |
| 3670 | { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3671 | // struct _method_list_t { |
| 3672 | // uint32_t entsize; // sizeof(struct _objc_method) |
| 3673 | // uint32_t method_count; |
| 3674 | // struct _objc_method method_list[method_count]; |
| 3675 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3676 | MethodListnfABITy = llvm::StructType::get(IntTy, |
| 3677 | IntTy, |
| 3678 | llvm::ArrayType::get(MethodTy, 0), |
| 3679 | NULL); |
| 3680 | CGM.getModule().addTypeName("struct.__method_list_t", |
| 3681 | MethodListnfABITy); |
| 3682 | // struct method_list_t * |
| 3683 | MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3684 | |
| 3685 | // struct _protocol_t { |
| 3686 | // id isa; // NULL |
| 3687 | // const char * const protocol_name; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3688 | // const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3689 | // const struct method_list_t * const instance_methods; |
| 3690 | // const struct method_list_t * const class_methods; |
| 3691 | // const struct method_list_t *optionalInstanceMethods; |
| 3692 | // const struct method_list_t *optionalClassMethods; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3693 | // const struct _prop_list_t * properties; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3694 | // const uint32_t size; // sizeof(struct _protocol_t) |
| 3695 | // const uint32_t flags; // = 0 |
| 3696 | // } |
| 3697 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3698 | // Holder for struct _protocol_list_t * |
| 3699 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 3700 | |
| 3701 | ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy, |
| 3702 | Int8PtrTy, |
| 3703 | llvm::PointerType::getUnqual( |
| 3704 | ProtocolListTyHolder), |
| 3705 | MethodListnfABIPtrTy, |
| 3706 | MethodListnfABIPtrTy, |
| 3707 | MethodListnfABIPtrTy, |
| 3708 | MethodListnfABIPtrTy, |
| 3709 | PropertyListPtrTy, |
| 3710 | IntTy, |
| 3711 | IntTy, |
| 3712 | NULL); |
| 3713 | CGM.getModule().addTypeName("struct._protocol_t", |
| 3714 | ProtocolnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3715 | |
| 3716 | // struct _protocol_t* |
| 3717 | ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3718 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 3719 | // struct _protocol_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3720 | // long protocol_count; // Note, this is 32/64 bit |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3721 | // struct _protocol_t *[protocol_count]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3722 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3723 | ProtocolListnfABITy = llvm::StructType::get(LongTy, |
| 3724 | llvm::ArrayType::get( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3725 | ProtocolnfABIPtrTy, 0), |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3726 | NULL); |
| 3727 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 3728 | ProtocolListnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3729 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo( |
| 3730 | ProtocolListnfABITy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3731 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3732 | // struct _objc_protocol_list* |
| 3733 | ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3734 | |
| 3735 | // struct _ivar_t { |
| 3736 | // unsigned long int *offset; // pointer to ivar offset location |
| 3737 | // char *name; |
| 3738 | // char *type; |
| 3739 | // uint32_t alignment; |
| 3740 | // uint32_t size; |
| 3741 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3742 | IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy), |
| 3743 | Int8PtrTy, |
| 3744 | Int8PtrTy, |
| 3745 | IntTy, |
| 3746 | IntTy, |
| 3747 | NULL); |
| 3748 | CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy); |
| 3749 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3750 | // struct _ivar_list_t { |
| 3751 | // uint32 entsize; // sizeof(struct _ivar_t) |
| 3752 | // uint32 count; |
| 3753 | // struct _iver_t list[count]; |
| 3754 | // } |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3755 | IvarListnfABITy = llvm::StructType::get(IntTy, |
| 3756 | IntTy, |
| 3757 | llvm::ArrayType::get( |
| 3758 | IvarnfABITy, 0), |
| 3759 | NULL); |
| 3760 | CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy); |
| 3761 | |
| 3762 | IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3763 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3764 | // struct _class_ro_t { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3765 | // uint32_t const flags; |
| 3766 | // uint32_t const instanceStart; |
| 3767 | // uint32_t const instanceSize; |
| 3768 | // uint32_t const reserved; // only when building for 64bit targets |
| 3769 | // const uint8_t * const ivarLayout; |
| 3770 | // const char *const name; |
| 3771 | // const struct _method_list_t * const baseMethods; |
| 3772 | // const struct _objc_protocol_list *const baseProtocols; |
| 3773 | // const struct _ivar_list_t *const ivars; |
| 3774 | // const uint8_t * const weakIvarLayout; |
| 3775 | // const struct _prop_list_t * const properties; |
| 3776 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3777 | |
| 3778 | // FIXME. Add 'reserved' field in 64bit abi mode! |
| 3779 | ClassRonfABITy = llvm::StructType::get(IntTy, |
| 3780 | IntTy, |
| 3781 | IntTy, |
| 3782 | Int8PtrTy, |
| 3783 | Int8PtrTy, |
| 3784 | MethodListnfABIPtrTy, |
| 3785 | ProtocolListnfABIPtrTy, |
| 3786 | IvarListnfABIPtrTy, |
| 3787 | Int8PtrTy, |
| 3788 | PropertyListPtrTy, |
| 3789 | NULL); |
| 3790 | CGM.getModule().addTypeName("struct._class_ro_t", |
| 3791 | ClassRonfABITy); |
| 3792 | |
| 3793 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 3794 | std::vector<const llvm::Type*> Params; |
| 3795 | Params.push_back(ObjectPtrTy); |
| 3796 | Params.push_back(SelectorPtrTy); |
| 3797 | ImpnfABITy = llvm::PointerType::getUnqual( |
| 3798 | llvm::FunctionType::get(ObjectPtrTy, Params, false)); |
| 3799 | |
| 3800 | // struct _class_t { |
| 3801 | // struct _class_t *isa; |
| 3802 | // struct _class_t * const superclass; |
| 3803 | // void *cache; |
| 3804 | // IMP *vtable; |
| 3805 | // struct class_ro_t *ro; |
| 3806 | // } |
| 3807 | |
| 3808 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 3809 | ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 3810 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 3811 | CachePtrTy, |
| 3812 | llvm::PointerType::getUnqual(ImpnfABITy), |
| 3813 | llvm::PointerType::getUnqual( |
| 3814 | ClassRonfABITy), |
| 3815 | NULL); |
| 3816 | CGM.getModule().addTypeName("struct._class_t", ClassnfABITy); |
| 3817 | |
| 3818 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo( |
| 3819 | ClassnfABITy); |
| 3820 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3821 | // LLVM for struct _class_t * |
| 3822 | ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy); |
| 3823 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3824 | // struct _category_t { |
| 3825 | // const char * const name; |
| 3826 | // struct _class_t *const cls; |
| 3827 | // const struct _method_list_t * const instance_methods; |
| 3828 | // const struct _method_list_t * const class_methods; |
| 3829 | // const struct _protocol_list_t * const protocols; |
| 3830 | // const struct _prop_list_t * const properties; |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3831 | // } |
| 3832 | CategorynfABITy = llvm::StructType::get(Int8PtrTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3833 | ClassnfABIPtrTy, |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3834 | MethodListnfABIPtrTy, |
| 3835 | MethodListnfABIPtrTy, |
| 3836 | ProtocolListnfABIPtrTy, |
| 3837 | PropertyListPtrTy, |
| 3838 | NULL); |
| 3839 | CGM.getModule().addTypeName("struct._category_t", CategorynfABITy); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3840 | |
| 3841 | // New types for nonfragile abi messaging. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3842 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3843 | ASTContext &Ctx = CGM.getContext(); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3844 | |
| 3845 | // MessageRefTy - LLVM for: |
| 3846 | // struct _message_ref_t { |
| 3847 | // IMP messenger; |
| 3848 | // SEL name; |
| 3849 | // }; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3850 | |
| 3851 | // First the clang type for struct _message_ref_t |
| 3852 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 3853 | SourceLocation(), |
| 3854 | &Ctx.Idents.get("_message_ref_t")); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3855 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3856 | Ctx.VoidPtrTy, 0, false)); |
| 3857 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3858 | Ctx.getObjCSelType(), 0, false)); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3859 | RD->completeDefinition(Ctx); |
| 3860 | |
| 3861 | MessageRefCTy = Ctx.getTagDeclType(RD); |
| 3862 | MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy); |
| 3863 | MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy)); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3864 | |
| 3865 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
| 3866 | MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy); |
| 3867 | |
| 3868 | // SuperMessageRefTy - LLVM for: |
| 3869 | // struct _super_message_ref_t { |
| 3870 | // SUPER_IMP messenger; |
| 3871 | // SEL name; |
| 3872 | // }; |
| 3873 | SuperMessageRefTy = llvm::StructType::get(ImpnfABITy, |
| 3874 | SelectorPtrTy, |
| 3875 | NULL); |
| 3876 | CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy); |
| 3877 | |
| 3878 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
| 3879 | SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy); |
| 3880 | |
| 3881 | // id objc_msgSend_fixup (id, struct message_ref_t*, ...) |
| 3882 | Params.clear(); |
| 3883 | Params.push_back(ObjectPtrTy); |
| 3884 | Params.push_back(MessageRefPtrTy); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 3885 | MessengerTy = llvm::FunctionType::get(ObjectPtrTy, |
| 3886 | Params, |
| 3887 | true); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3888 | MessageSendFixupFn = |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 3889 | CGM.CreateRuntimeFunction(MessengerTy, |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3890 | "objc_msgSend_fixup"); |
| 3891 | |
| 3892 | // id objc_msgSend_fpret_fixup (id, struct message_ref_t*, ...) |
| 3893 | MessageSendFpretFixupFn = |
| 3894 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3895 | Params, |
| 3896 | true), |
| 3897 | "objc_msgSend_fpret_fixup"); |
| 3898 | |
| 3899 | // id objc_msgSend_stret_fixup (id, struct message_ref_t*, ...) |
| 3900 | MessageSendStretFixupFn = |
| 3901 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3902 | Params, |
| 3903 | true), |
| 3904 | "objc_msgSend_stret_fixup"); |
| 3905 | |
| 3906 | // id objc_msgSendId_fixup (id, struct message_ref_t*, ...) |
| 3907 | MessageSendIdFixupFn = |
| 3908 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3909 | Params, |
| 3910 | true), |
| 3911 | "objc_msgSendId_fixup"); |
| 3912 | |
| 3913 | |
| 3914 | // id objc_msgSendId_stret_fixup (id, struct message_ref_t*, ...) |
| 3915 | MessageSendIdStretFixupFn = |
| 3916 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3917 | Params, |
| 3918 | true), |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3919 | "objc_msgSendId_stret_fixup"); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3920 | |
| 3921 | // id objc_msgSendSuper2_fixup (struct objc_super *, |
| 3922 | // struct _super_message_ref_t*, ...) |
| 3923 | Params.clear(); |
| 3924 | Params.push_back(SuperPtrTy); |
| 3925 | Params.push_back(SuperMessageRefPtrTy); |
| 3926 | MessageSendSuper2FixupFn = |
| 3927 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3928 | Params, |
| 3929 | true), |
| 3930 | "objc_msgSendSuper2_fixup"); |
| 3931 | |
| 3932 | |
| 3933 | // id objc_msgSendSuper2_stret_fixup (struct objc_super *, |
| 3934 | // struct _super_message_ref_t*, ...) |
| 3935 | MessageSendSuper2StretFixupFn = |
| 3936 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3937 | Params, |
| 3938 | true), |
| 3939 | "objc_msgSendSuper2_stret_fixup"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 3940 | |
| 3941 | Params.clear(); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 3942 | |
| 3943 | // struct objc_typeinfo { |
| 3944 | // const void** vtable; // objc_ehtype_vtable + 2 |
| 3945 | // const char* name; // c++ typeinfo string |
| 3946 | // Class cls; |
| 3947 | // }; |
| 3948 | EHTypeTy = llvm::StructType::get(llvm::PointerType::getUnqual(Int8PtrTy), |
| 3949 | Int8PtrTy, |
| 3950 | ClassnfABIPtrTy, |
| 3951 | NULL); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 3952 | CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 3953 | EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3954 | } |
| 3955 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3956 | llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() { |
| 3957 | FinishNonFragileABIModule(); |
| 3958 | |
| 3959 | return NULL; |
| 3960 | } |
| 3961 | |
| 3962 | void CGObjCNonFragileABIMac::FinishNonFragileABIModule() { |
| 3963 | // nonfragile abi has no module definition. |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 3964 | |
| 3965 | // Build list of all implemented classe addresses in array |
| 3966 | // L_OBJC_LABEL_CLASS_$. |
| 3967 | // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$ |
| 3968 | // list of 'nonlazy' implementations (defined as those with a +load{} |
| 3969 | // method!!). |
| 3970 | unsigned NumClasses = DefinedClasses.size(); |
| 3971 | if (NumClasses) { |
| 3972 | std::vector<llvm::Constant*> Symbols(NumClasses); |
| 3973 | for (unsigned i=0; i<NumClasses; i++) |
| 3974 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
| 3975 | ObjCTypes.Int8PtrTy); |
| 3976 | llvm::Constant* Init = |
| 3977 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 3978 | NumClasses), |
| 3979 | Symbols); |
| 3980 | |
| 3981 | llvm::GlobalVariable *GV = |
| 3982 | new llvm::GlobalVariable(Init->getType(), false, |
| 3983 | llvm::GlobalValue::InternalLinkage, |
| 3984 | Init, |
| 3985 | "\01L_OBJC_LABEL_CLASS_$", |
| 3986 | &CGM.getModule()); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 3987 | GV->setAlignment(8); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 3988 | GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip"); |
| 3989 | UsedGlobals.push_back(GV); |
| 3990 | } |
| 3991 | |
| 3992 | // Build list of all implemented category addresses in array |
| 3993 | // L_OBJC_LABEL_CATEGORY_$. |
| 3994 | // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$ |
| 3995 | // list of 'nonlazy' category implementations (defined as those with a +load{} |
| 3996 | // method!!). |
| 3997 | unsigned NumCategory = DefinedCategories.size(); |
| 3998 | if (NumCategory) { |
| 3999 | std::vector<llvm::Constant*> Symbols(NumCategory); |
| 4000 | for (unsigned i=0; i<NumCategory; i++) |
| 4001 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
| 4002 | ObjCTypes.Int8PtrTy); |
| 4003 | llvm::Constant* Init = |
| 4004 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 4005 | NumCategory), |
| 4006 | Symbols); |
| 4007 | |
| 4008 | llvm::GlobalVariable *GV = |
| 4009 | new llvm::GlobalVariable(Init->getType(), false, |
| 4010 | llvm::GlobalValue::InternalLinkage, |
| 4011 | Init, |
| 4012 | "\01L_OBJC_LABEL_CATEGORY_$", |
| 4013 | &CGM.getModule()); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 4014 | GV->setAlignment(8); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4015 | GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip"); |
| 4016 | UsedGlobals.push_back(GV); |
| 4017 | } |
| 4018 | |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4019 | // static int L_OBJC_IMAGE_INFO[2] = { 0, flags }; |
| 4020 | // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0 |
| 4021 | std::vector<llvm::Constant*> Values(2); |
| 4022 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0); |
Fariborz Jahanian | 067986e | 2009-02-24 21:08:09 +0000 | [diff] [blame] | 4023 | unsigned int flags = 0; |
Fariborz Jahanian | 66a5c2c | 2009-02-24 23:34:44 +0000 | [diff] [blame] | 4024 | // FIXME: Fix and continue? |
| 4025 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 4026 | flags |= eImageInfo_GarbageCollected; |
| 4027 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 4028 | flags |= eImageInfo_GCOnly; |
Fariborz Jahanian | 067986e | 2009-02-24 21:08:09 +0000 | [diff] [blame] | 4029 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4030 | llvm::Constant* Init = llvm::ConstantArray::get( |
| 4031 | llvm::ArrayType::get(ObjCTypes.IntTy, 2), |
| 4032 | Values); |
| 4033 | llvm::GlobalVariable *IMGV = |
| 4034 | new llvm::GlobalVariable(Init->getType(), false, |
| 4035 | llvm::GlobalValue::InternalLinkage, |
| 4036 | Init, |
| 4037 | "\01L_OBJC_IMAGE_INFO", |
| 4038 | &CGM.getModule()); |
| 4039 | IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip"); |
| 4040 | UsedGlobals.push_back(IMGV); |
| 4041 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4042 | std::vector<llvm::Constant*> Used; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4043 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4044 | for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), |
| 4045 | e = UsedGlobals.end(); i != e; ++i) { |
| 4046 | Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); |
| 4047 | } |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4048 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4049 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); |
| 4050 | llvm::GlobalValue *GV = |
| 4051 | new llvm::GlobalVariable(AT, false, |
| 4052 | llvm::GlobalValue::AppendingLinkage, |
| 4053 | llvm::ConstantArray::get(AT, Used), |
| 4054 | "llvm.used", |
| 4055 | &CGM.getModule()); |
| 4056 | |
| 4057 | GV->setSection("llvm.metadata"); |
| 4058 | |
| 4059 | } |
| 4060 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4061 | // Metadata flags |
| 4062 | enum MetaDataDlags { |
| 4063 | CLS = 0x0, |
| 4064 | CLS_META = 0x1, |
| 4065 | CLS_ROOT = 0x2, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4066 | OBJC2_CLS_HIDDEN = 0x10, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4067 | CLS_EXCEPTION = 0x20 |
| 4068 | }; |
| 4069 | /// BuildClassRoTInitializer - generate meta-data for: |
| 4070 | /// struct _class_ro_t { |
| 4071 | /// uint32_t const flags; |
| 4072 | /// uint32_t const instanceStart; |
| 4073 | /// uint32_t const instanceSize; |
| 4074 | /// uint32_t const reserved; // only when building for 64bit targets |
| 4075 | /// const uint8_t * const ivarLayout; |
| 4076 | /// const char *const name; |
| 4077 | /// const struct _method_list_t * const baseMethods; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4078 | /// const struct _protocol_list_t *const baseProtocols; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4079 | /// const struct _ivar_list_t *const ivars; |
| 4080 | /// const uint8_t * const weakIvarLayout; |
| 4081 | /// const struct _prop_list_t * const properties; |
| 4082 | /// } |
| 4083 | /// |
| 4084 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer( |
| 4085 | unsigned flags, |
| 4086 | unsigned InstanceStart, |
| 4087 | unsigned InstanceSize, |
| 4088 | const ObjCImplementationDecl *ID) { |
| 4089 | std::string ClassName = ID->getNameAsString(); |
| 4090 | std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets! |
| 4091 | Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
| 4092 | Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart); |
| 4093 | Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize); |
| 4094 | // FIXME. For 64bit targets add 0 here. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4095 | // FIXME. ivarLayout is currently null! |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 4096 | // Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4097 | // : BuildIvarLayout(ID, true); |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 4098 | Values[ 3] = GetIvarLayoutName(0, ObjCTypes); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4099 | Values[ 4] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4100 | // const struct _method_list_t * const baseMethods; |
| 4101 | std::vector<llvm::Constant*> Methods; |
| 4102 | std::string MethodListName("\01l_OBJC_$_"); |
| 4103 | if (flags & CLS_META) { |
| 4104 | MethodListName += "CLASS_METHODS_" + ID->getNameAsString(); |
| 4105 | for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(), |
| 4106 | e = ID->classmeth_end(); i != e; ++i) { |
| 4107 | // Class methods should always be defined. |
| 4108 | Methods.push_back(GetMethodConstant(*i)); |
| 4109 | } |
| 4110 | } else { |
| 4111 | MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString(); |
| 4112 | for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(), |
| 4113 | e = ID->instmeth_end(); i != e; ++i) { |
| 4114 | // Instance methods should always be defined. |
| 4115 | Methods.push_back(GetMethodConstant(*i)); |
| 4116 | } |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4117 | for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(), |
| 4118 | e = ID->propimpl_end(); i != e; ++i) { |
| 4119 | ObjCPropertyImplDecl *PID = *i; |
| 4120 | |
| 4121 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){ |
| 4122 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 4123 | |
| 4124 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 4125 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4126 | Methods.push_back(C); |
| 4127 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 4128 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4129 | Methods.push_back(C); |
| 4130 | } |
| 4131 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4132 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4133 | Values[ 5] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4134 | "__DATA, __objc_const", Methods); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4135 | |
| 4136 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4137 | assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer"); |
| 4138 | Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_" |
| 4139 | + OID->getNameAsString(), |
| 4140 | OID->protocol_begin(), |
| 4141 | OID->protocol_end()); |
| 4142 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4143 | if (flags & CLS_META) |
| 4144 | Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
| 4145 | else |
| 4146 | Values[ 7] = EmitIvarList(ID); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4147 | // FIXME. weakIvarLayout is currently null. |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 4148 | // Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4149 | // : BuildIvarLayout(ID, false); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 4150 | Values[ 8] = GetIvarLayoutName(0, ObjCTypes); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4151 | if (flags & CLS_META) |
| 4152 | Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 4153 | else |
| 4154 | Values[ 9] = |
| 4155 | EmitPropertyList( |
| 4156 | "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
| 4157 | ID, ID->getClassInterface(), ObjCTypes); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4158 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy, |
| 4159 | Values); |
| 4160 | llvm::GlobalVariable *CLASS_RO_GV = |
| 4161 | new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false, |
| 4162 | llvm::GlobalValue::InternalLinkage, |
| 4163 | Init, |
| 4164 | (flags & CLS_META) ? |
| 4165 | std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName : |
| 4166 | std::string("\01l_OBJC_CLASS_RO_$_")+ClassName, |
| 4167 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4168 | CLASS_RO_GV->setAlignment( |
| 4169 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4170 | CLASS_RO_GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4171 | return CLASS_RO_GV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4172 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4173 | } |
| 4174 | |
| 4175 | /// BuildClassMetaData - This routine defines that to-level meta-data |
| 4176 | /// for the given ClassName for: |
| 4177 | /// struct _class_t { |
| 4178 | /// struct _class_t *isa; |
| 4179 | /// struct _class_t * const superclass; |
| 4180 | /// void *cache; |
| 4181 | /// IMP *vtable; |
| 4182 | /// struct class_ro_t *ro; |
| 4183 | /// } |
| 4184 | /// |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4185 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData( |
| 4186 | std::string &ClassName, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4187 | llvm::Constant *IsAGV, |
| 4188 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4189 | llvm::Constant *ClassRoGV, |
| 4190 | bool HiddenVisibility) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4191 | std::vector<llvm::Constant*> Values(5); |
| 4192 | Values[0] = IsAGV; |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4193 | Values[1] = SuperClassGV |
| 4194 | ? SuperClassGV |
| 4195 | : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4196 | Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar |
| 4197 | Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar |
| 4198 | Values[4] = ClassRoGV; // &CLASS_RO_GV |
| 4199 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy, |
| 4200 | Values); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4201 | llvm::GlobalVariable *GV = GetClassGlobal(ClassName); |
| 4202 | GV->setInitializer(Init); |
Fariborz Jahanian | dd0db2a | 2009-01-31 01:07:39 +0000 | [diff] [blame] | 4203 | GV->setSection("__DATA, __objc_data"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4204 | GV->setAlignment( |
| 4205 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy)); |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4206 | if (HiddenVisibility) |
| 4207 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4208 | return GV; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4209 | } |
| 4210 | |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 4211 | /// countInheritedIvars - count number of ivars in class and its super class(s) |
| 4212 | /// |
| 4213 | static int countInheritedIvars(const ObjCInterfaceDecl *OI, |
| 4214 | ASTContext &Context) { |
| 4215 | int count = 0; |
| 4216 | if (!OI) |
| 4217 | return 0; |
| 4218 | const ObjCInterfaceDecl *SuperClass = OI->getSuperClass(); |
| 4219 | if (SuperClass) |
| 4220 | count += countInheritedIvars(SuperClass, Context); |
| 4221 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 4222 | E = OI->ivar_end(); I != E; ++I) |
| 4223 | ++count; |
| 4224 | // look into properties. |
| 4225 | for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(Context), |
| 4226 | E = OI->prop_end(Context); I != E; ++I) { |
| 4227 | if ((*I)->getPropertyIvarDecl()) |
| 4228 | ++count; |
| 4229 | } |
| 4230 | return count; |
| 4231 | } |
| 4232 | |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4233 | void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCInterfaceDecl *OID, |
| 4234 | uint32_t &InstanceStart, |
| 4235 | uint32_t &InstanceSize) { |
Daniel Dunbar | 24c8991 | 2009-04-21 21:41:56 +0000 | [diff] [blame] | 4236 | assert(!OID->isForwardDecl() && "Invalid interface decl!"); |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4237 | const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OID); |
| 4238 | |
Daniel Dunbar | 6ec0716 | 2009-04-20 07:18:49 +0000 | [diff] [blame] | 4239 | int countSuperClassIvars = countInheritedIvars(OID->getSuperClass(), |
| 4240 | CGM.getContext()); |
| 4241 | const RecordDecl *RD = CGM.getContext().addRecordToClass(OID); |
| 4242 | RecordDecl::field_iterator firstField = RD->field_begin(CGM.getContext()); |
| 4243 | RecordDecl::field_iterator lastField = RD->field_end(CGM.getContext()); |
| 4244 | while (countSuperClassIvars-- > 0) { |
| 4245 | lastField = firstField; |
| 4246 | ++firstField; |
| 4247 | } |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4248 | |
| 4249 | for (RecordDecl::field_iterator e = RD->field_end(CGM.getContext()), |
| 4250 | ifield = firstField; ifield != e; ++ifield) |
| 4251 | lastField = ifield; |
| 4252 | |
| 4253 | InstanceStart = InstanceSize = 0; |
| 4254 | if (lastField != RD->field_end(CGM.getContext())) { |
| 4255 | FieldDecl *Field = *lastField; |
| 4256 | const llvm::Type *FieldTy = |
| 4257 | CGM.getTypes().ConvertTypeForMem(Field->getType()); |
| 4258 | unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy); |
| 4259 | InstanceSize = GetIvarBaseOffset(Layout, Field) + Size; |
| 4260 | if (firstField == RD->field_end(CGM.getContext())) |
| 4261 | InstanceStart = InstanceSize; |
| 4262 | else { |
| 4263 | Field = *firstField; |
| 4264 | InstanceStart = GetIvarBaseOffset(Layout, Field); |
| 4265 | } |
| 4266 | } |
| 4267 | } |
| 4268 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4269 | void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) { |
| 4270 | std::string ClassName = ID->getNameAsString(); |
| 4271 | if (!ObjCEmptyCacheVar) { |
| 4272 | ObjCEmptyCacheVar = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4273 | ObjCTypes.CacheTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4274 | false, |
| 4275 | llvm::GlobalValue::ExternalLinkage, |
| 4276 | 0, |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4277 | "_objc_empty_cache", |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4278 | &CGM.getModule()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4279 | |
| 4280 | ObjCEmptyVtableVar = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4281 | ObjCTypes.ImpnfABITy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4282 | false, |
| 4283 | llvm::GlobalValue::ExternalLinkage, |
| 4284 | 0, |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4285 | "_objc_empty_vtable", |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4286 | &CGM.getModule()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4287 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4288 | assert(ID->getClassInterface() && |
| 4289 | "CGObjCNonFragileABIMac::GenerateClass - class is 0"); |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 4290 | // FIXME: Is this correct (that meta class size is never computed)? |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4291 | uint32_t InstanceStart = |
| 4292 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy); |
| 4293 | uint32_t InstanceSize = InstanceStart; |
| 4294 | uint32_t flags = CLS_META; |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4295 | std::string ObjCMetaClassName(getMetaclassSymbolPrefix()); |
| 4296 | std::string ObjCClassName(getClassSymbolPrefix()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4297 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4298 | llvm::GlobalVariable *SuperClassGV, *IsAGV; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4299 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4300 | bool classIsHidden = |
| 4301 | CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4302 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4303 | flags |= OBJC2_CLS_HIDDEN; |
| 4304 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4305 | // class is root |
| 4306 | flags |= CLS_ROOT; |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4307 | SuperClassGV = GetClassGlobal(ObjCClassName + ClassName); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4308 | IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4309 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4310 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4311 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 4312 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 4313 | Root = Super; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4314 | IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString()); |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4315 | // work on super class metadata symbol. |
| 4316 | std::string SuperClassName = |
| 4317 | ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4318 | SuperClassGV = GetClassGlobal(SuperClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4319 | } |
| 4320 | llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags, |
| 4321 | InstanceStart, |
| 4322 | InstanceSize,ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4323 | std::string TClassName = ObjCMetaClassName + ClassName; |
| 4324 | llvm::GlobalVariable *MetaTClass = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4325 | BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV, |
| 4326 | classIsHidden); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4327 | |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4328 | // Metadata for the class |
| 4329 | flags = CLS; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4330 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4331 | flags |= OBJC2_CLS_HIDDEN; |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4332 | |
| 4333 | if (hasObjCExceptionAttribute(ID->getClassInterface())) |
| 4334 | flags |= CLS_EXCEPTION; |
| 4335 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4336 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4337 | flags |= CLS_ROOT; |
| 4338 | SuperClassGV = 0; |
Chris Lattner | b7b58b1 | 2009-04-19 06:02:28 +0000 | [diff] [blame] | 4339 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4340 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4341 | std::string RootClassName = |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4342 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4343 | SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4344 | } |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4345 | GetClassSizeInfo(ID->getClassInterface(), InstanceStart, InstanceSize); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4346 | CLASS_RO_GV = BuildClassRoTInitializer(flags, |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4347 | InstanceStart, |
| 4348 | InstanceSize, |
| 4349 | ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4350 | |
| 4351 | TClassName = ObjCClassName + ClassName; |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4352 | llvm::GlobalVariable *ClassMD = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4353 | BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV, |
| 4354 | classIsHidden); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4355 | DefinedClasses.push_back(ClassMD); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4356 | |
| 4357 | // Force the definition of the EHType if necessary. |
| 4358 | if (flags & CLS_EXCEPTION) |
| 4359 | GetInterfaceEHType(ID->getClassInterface(), true); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4360 | } |
| 4361 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4362 | /// GenerateProtocolRef - This routine is called to generate code for |
| 4363 | /// a protocol reference expression; as in: |
| 4364 | /// @code |
| 4365 | /// @protocol(Proto1); |
| 4366 | /// @endcode |
| 4367 | /// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1 |
| 4368 | /// which will hold address of the protocol meta-data. |
| 4369 | /// |
| 4370 | llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder, |
| 4371 | const ObjCProtocolDecl *PD) { |
| 4372 | |
Fariborz Jahanian | 960cd06 | 2009-04-10 18:47:34 +0000 | [diff] [blame] | 4373 | // This routine is called for @protocol only. So, we must build definition |
| 4374 | // of protocol's meta-data (not a reference to it!) |
| 4375 | // |
| 4376 | llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD), |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4377 | ObjCTypes.ExternalProtocolPtrTy); |
| 4378 | |
| 4379 | std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_"); |
| 4380 | ProtocolName += PD->getNameAsCString(); |
| 4381 | |
| 4382 | llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName); |
| 4383 | if (PTGV) |
| 4384 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 4385 | PTGV = new llvm::GlobalVariable( |
| 4386 | Init->getType(), false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4387 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4388 | Init, |
| 4389 | ProtocolName, |
| 4390 | &CGM.getModule()); |
| 4391 | PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip"); |
| 4392 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4393 | UsedGlobals.push_back(PTGV); |
| 4394 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 4395 | } |
| 4396 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4397 | /// GenerateCategory - Build metadata for a category implementation. |
| 4398 | /// struct _category_t { |
| 4399 | /// const char * const name; |
| 4400 | /// struct _class_t *const cls; |
| 4401 | /// const struct _method_list_t * const instance_methods; |
| 4402 | /// const struct _method_list_t * const class_methods; |
| 4403 | /// const struct _protocol_list_t * const protocols; |
| 4404 | /// const struct _prop_list_t * const properties; |
| 4405 | /// } |
| 4406 | /// |
| 4407 | void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) |
| 4408 | { |
| 4409 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4410 | const char *Prefix = "\01l_OBJC_$_CATEGORY_"; |
| 4411 | std::string ExtCatName(Prefix + Interface->getNameAsString()+ |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4412 | "_$_" + OCD->getNameAsString()); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4413 | std::string ExtClassName(getClassSymbolPrefix() + |
| 4414 | Interface->getNameAsString()); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4415 | |
| 4416 | std::vector<llvm::Constant*> Values(6); |
| 4417 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 4418 | // meta-class entry symbol |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4419 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4420 | Values[1] = ClassGV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4421 | std::vector<llvm::Constant*> Methods; |
| 4422 | std::string MethodListName(Prefix); |
| 4423 | MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() + |
| 4424 | "_$_" + OCD->getNameAsString(); |
| 4425 | |
| 4426 | for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(), |
| 4427 | e = OCD->instmeth_end(); i != e; ++i) { |
| 4428 | // Instance methods should always be defined. |
| 4429 | Methods.push_back(GetMethodConstant(*i)); |
| 4430 | } |
| 4431 | |
| 4432 | Values[2] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4433 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4434 | Methods); |
| 4435 | |
| 4436 | MethodListName = Prefix; |
| 4437 | MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" + |
| 4438 | OCD->getNameAsString(); |
| 4439 | Methods.clear(); |
| 4440 | for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(), |
| 4441 | e = OCD->classmeth_end(); i != e; ++i) { |
| 4442 | // Class methods should always be defined. |
| 4443 | Methods.push_back(GetMethodConstant(*i)); |
| 4444 | } |
| 4445 | |
| 4446 | Values[3] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4447 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4448 | Methods); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4449 | const ObjCCategoryDecl *Category = |
| 4450 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 4451 | if (Category) { |
| 4452 | std::string ExtName(Interface->getNameAsString() + "_$_" + |
| 4453 | OCD->getNameAsString()); |
| 4454 | Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_" |
| 4455 | + Interface->getNameAsString() + "_$_" |
| 4456 | + Category->getNameAsString(), |
| 4457 | Category->protocol_begin(), |
| 4458 | Category->protocol_end()); |
| 4459 | Values[5] = |
| 4460 | EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName, |
| 4461 | OCD, Category, ObjCTypes); |
| 4462 | } |
| 4463 | else { |
| 4464 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 4465 | Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 4466 | } |
| 4467 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4468 | llvm::Constant *Init = |
| 4469 | llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy, |
| 4470 | Values); |
| 4471 | llvm::GlobalVariable *GCATV |
| 4472 | = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy, |
| 4473 | false, |
| 4474 | llvm::GlobalValue::InternalLinkage, |
| 4475 | Init, |
| 4476 | ExtCatName, |
| 4477 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4478 | GCATV->setAlignment( |
| 4479 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4480 | GCATV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4481 | UsedGlobals.push_back(GCATV); |
| 4482 | DefinedCategories.push_back(GCATV); |
| 4483 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4484 | |
| 4485 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 4486 | /// given method if it has been defined. The result is null if the |
| 4487 | /// method has not been defined. The return value has type MethodPtrTy. |
| 4488 | llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant( |
| 4489 | const ObjCMethodDecl *MD) { |
| 4490 | // FIXME: Use DenseMap::lookup |
| 4491 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 4492 | if (!Fn) |
| 4493 | return 0; |
| 4494 | |
| 4495 | std::vector<llvm::Constant*> Method(3); |
| 4496 | Method[0] = |
| 4497 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 4498 | ObjCTypes.SelectorPtrTy); |
| 4499 | Method[1] = GetMethodVarType(MD); |
| 4500 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 4501 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 4502 | } |
| 4503 | |
| 4504 | /// EmitMethodList - Build meta-data for method declarations |
| 4505 | /// struct _method_list_t { |
| 4506 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 4507 | /// uint32_t method_count; |
| 4508 | /// struct _objc_method method_list[method_count]; |
| 4509 | /// } |
| 4510 | /// |
| 4511 | llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList( |
| 4512 | const std::string &Name, |
| 4513 | const char *Section, |
| 4514 | const ConstantVector &Methods) { |
| 4515 | // Return null for empty list. |
| 4516 | if (Methods.empty()) |
| 4517 | return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy); |
| 4518 | |
| 4519 | std::vector<llvm::Constant*> Values(3); |
| 4520 | // sizeof(struct _objc_method) |
| 4521 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy); |
| 4522 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4523 | // method_count |
| 4524 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 4525 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 4526 | Methods.size()); |
| 4527 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 4528 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4529 | |
| 4530 | llvm::GlobalVariable *GV = |
| 4531 | new llvm::GlobalVariable(Init->getType(), false, |
| 4532 | llvm::GlobalValue::InternalLinkage, |
| 4533 | Init, |
| 4534 | Name, |
| 4535 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4536 | GV->setAlignment( |
| 4537 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4538 | GV->setSection(Section); |
| 4539 | UsedGlobals.push_back(GV); |
| 4540 | return llvm::ConstantExpr::getBitCast(GV, |
| 4541 | ObjCTypes.MethodListnfABIPtrTy); |
| 4542 | } |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4543 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4544 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 4545 | /// the given ivar. |
| 4546 | /// |
| 4547 | llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable( |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 4548 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4549 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 4550 | std::string Name = "OBJC_IVAR_$_" + |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4551 | getInterfaceDeclForIvar(ID, Ivar, CGM.getContext())->getNameAsString() + |
| 4552 | '.' + Ivar->getNameAsString(); |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4553 | llvm::GlobalVariable *IvarOffsetGV = |
| 4554 | CGM.getModule().getGlobalVariable(Name); |
| 4555 | if (!IvarOffsetGV) |
| 4556 | IvarOffsetGV = |
| 4557 | new llvm::GlobalVariable(ObjCTypes.LongTy, |
| 4558 | false, |
| 4559 | llvm::GlobalValue::ExternalLinkage, |
| 4560 | 0, |
| 4561 | Name, |
| 4562 | &CGM.getModule()); |
| 4563 | return IvarOffsetGV; |
| 4564 | } |
| 4565 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4566 | llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar( |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4567 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4568 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4569 | unsigned long int Offset) { |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4570 | llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar); |
| 4571 | IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy, |
| 4572 | Offset)); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4573 | IvarOffsetGV->setAlignment( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4574 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy)); |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4575 | |
| 4576 | // FIXME: This matches gcc, but shouldn't the visibility be set on |
| 4577 | // the use as well (i.e., in ObjCIvarOffsetVariable). |
| 4578 | if (Ivar->getAccessControl() == ObjCIvarDecl::Private || |
| 4579 | Ivar->getAccessControl() == ObjCIvarDecl::Package || |
| 4580 | CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden) |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4581 | IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4582 | else |
Fariborz Jahanian | 77c9fd2 | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 4583 | IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4584 | IvarOffsetGV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4585 | return IvarOffsetGV; |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4586 | } |
| 4587 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4588 | /// EmitIvarList - Emit the ivar list for the given |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 4589 | /// implementation. The return value has type |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4590 | /// IvarListnfABIPtrTy. |
| 4591 | /// struct _ivar_t { |
| 4592 | /// unsigned long int *offset; // pointer to ivar offset location |
| 4593 | /// char *name; |
| 4594 | /// char *type; |
| 4595 | /// uint32_t alignment; |
| 4596 | /// uint32_t size; |
| 4597 | /// } |
| 4598 | /// struct _ivar_list_t { |
| 4599 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 4600 | /// uint32 count; |
| 4601 | /// struct _iver_t list[count]; |
| 4602 | /// } |
| 4603 | /// |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4604 | |
| 4605 | void CGObjCCommonMac::GetNamedIvarList(const ObjCInterfaceDecl *OID, |
| 4606 | llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const { |
| 4607 | for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(), |
| 4608 | E = OID->ivar_end(); I != E; ++I) { |
| 4609 | // Ignore unnamed bit-fields. |
| 4610 | if (!(*I)->getDeclName()) |
| 4611 | continue; |
| 4612 | |
| 4613 | Res.push_back(*I); |
| 4614 | } |
| 4615 | |
| 4616 | for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(CGM.getContext()), |
| 4617 | E = OID->prop_end(CGM.getContext()); I != E; ++I) |
| 4618 | if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl()) |
| 4619 | Res.push_back(IV); |
| 4620 | } |
| 4621 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4622 | llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList( |
| 4623 | const ObjCImplementationDecl *ID) { |
| 4624 | |
| 4625 | std::vector<llvm::Constant*> Ivars, Ivar(5); |
| 4626 | |
| 4627 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4628 | assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface"); |
| 4629 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4630 | // FIXME. Consolidate this with similar code in GenerateClass. |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 4631 | const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OID); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 4632 | |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 4633 | // Collect declared and synthesized ivars in a small vector. |
Fariborz Jahanian | 1819188 | 2009-03-31 18:11:23 +0000 | [diff] [blame] | 4634 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4635 | GetNamedIvarList(OID, OIvars); |
Fariborz Jahanian | 99eee36 | 2009-04-01 19:37:34 +0000 | [diff] [blame] | 4636 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4637 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 4638 | ObjCIvarDecl *IVD = OIvars[i]; |
| 4639 | const FieldDecl *Field = OID->lookupFieldDeclForIvar(CGM.getContext(), IVD); |
Daniel Dunbar | 3eec8aa | 2009-04-20 05:53:40 +0000 | [diff] [blame] | 4640 | Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD, |
Daniel Dunbar | 48fa064 | 2009-04-19 02:03:42 +0000 | [diff] [blame] | 4641 | GetIvarBaseOffset(Layout, Field)); |
Daniel Dunbar | 3eec8aa | 2009-04-20 05:53:40 +0000 | [diff] [blame] | 4642 | Ivar[1] = GetMethodVarName(Field->getIdentifier()); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4643 | Ivar[2] = GetMethodVarType(Field); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4644 | const llvm::Type *FieldTy = |
| 4645 | CGM.getTypes().ConvertTypeForMem(Field->getType()); |
| 4646 | unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy); |
| 4647 | unsigned Align = CGM.getContext().getPreferredTypeAlign( |
| 4648 | Field->getType().getTypePtr()) >> 3; |
| 4649 | Align = llvm::Log2_32(Align); |
| 4650 | Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align); |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 4651 | // NOTE. Size of a bitfield does not match gcc's, because of the |
| 4652 | // way bitfields are treated special in each. But I am told that |
| 4653 | // 'size' for bitfield ivars is ignored by the runtime so it does |
| 4654 | // not matter. If it matters, there is enough info to get the |
| 4655 | // bitfield right! |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4656 | Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4657 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar)); |
| 4658 | } |
| 4659 | // Return null for empty list. |
| 4660 | if (Ivars.empty()) |
| 4661 | return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
| 4662 | std::vector<llvm::Constant*> Values(3); |
| 4663 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy); |
| 4664 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4665 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 4666 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy, |
| 4667 | Ivars.size()); |
| 4668 | Values[2] = llvm::ConstantArray::get(AT, Ivars); |
| 4669 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4670 | const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_"; |
| 4671 | llvm::GlobalVariable *GV = |
| 4672 | new llvm::GlobalVariable(Init->getType(), false, |
| 4673 | llvm::GlobalValue::InternalLinkage, |
| 4674 | Init, |
| 4675 | Prefix + OID->getNameAsString(), |
| 4676 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4677 | GV->setAlignment( |
| 4678 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4679 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4680 | |
| 4681 | UsedGlobals.push_back(GV); |
| 4682 | return llvm::ConstantExpr::getBitCast(GV, |
| 4683 | ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4684 | } |
| 4685 | |
| 4686 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef( |
| 4687 | const ObjCProtocolDecl *PD) { |
| 4688 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4689 | |
| 4690 | if (!Entry) { |
| 4691 | // We use the initializer as a marker of whether this is a forward |
| 4692 | // reference or not. At module finalization we add the empty |
| 4693 | // contents for protocols which were referenced but never defined. |
| 4694 | Entry = |
| 4695 | new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false, |
| 4696 | llvm::GlobalValue::ExternalLinkage, |
| 4697 | 0, |
| 4698 | "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(), |
| 4699 | &CGM.getModule()); |
| 4700 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 4701 | UsedGlobals.push_back(Entry); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4702 | } |
| 4703 | |
| 4704 | return Entry; |
| 4705 | } |
| 4706 | |
| 4707 | /// GetOrEmitProtocol - Generate the protocol meta-data: |
| 4708 | /// @code |
| 4709 | /// struct _protocol_t { |
| 4710 | /// id isa; // NULL |
| 4711 | /// const char * const protocol_name; |
| 4712 | /// const struct _protocol_list_t * protocol_list; // super protocols |
| 4713 | /// const struct method_list_t * const instance_methods; |
| 4714 | /// const struct method_list_t * const class_methods; |
| 4715 | /// const struct method_list_t *optionalInstanceMethods; |
| 4716 | /// const struct method_list_t *optionalClassMethods; |
| 4717 | /// const struct _prop_list_t * properties; |
| 4718 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 4719 | /// const uint32_t flags; // = 0 |
| 4720 | /// } |
| 4721 | /// @endcode |
| 4722 | /// |
| 4723 | |
| 4724 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol( |
| 4725 | const ObjCProtocolDecl *PD) { |
| 4726 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4727 | |
| 4728 | // Early exit if a defining object has already been generated. |
| 4729 | if (Entry && Entry->hasInitializer()) |
| 4730 | return Entry; |
| 4731 | |
| 4732 | const char *ProtocolName = PD->getNameAsCString(); |
| 4733 | |
| 4734 | // Construct method lists. |
| 4735 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 4736 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4737 | for (ObjCProtocolDecl::instmeth_iterator |
| 4738 | i = PD->instmeth_begin(CGM.getContext()), |
| 4739 | e = PD->instmeth_end(CGM.getContext()); |
| 4740 | i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4741 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4742 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4743 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4744 | OptInstanceMethods.push_back(C); |
| 4745 | } else { |
| 4746 | InstanceMethods.push_back(C); |
| 4747 | } |
| 4748 | } |
| 4749 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4750 | for (ObjCProtocolDecl::classmeth_iterator |
| 4751 | i = PD->classmeth_begin(CGM.getContext()), |
| 4752 | e = PD->classmeth_end(CGM.getContext()); |
| 4753 | i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4754 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4755 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4756 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4757 | OptClassMethods.push_back(C); |
| 4758 | } else { |
| 4759 | ClassMethods.push_back(C); |
| 4760 | } |
| 4761 | } |
| 4762 | |
| 4763 | std::vector<llvm::Constant*> Values(10); |
| 4764 | // isa is NULL |
| 4765 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy); |
| 4766 | Values[1] = GetClassName(PD->getIdentifier()); |
| 4767 | Values[2] = EmitProtocolList( |
| 4768 | "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(), |
| 4769 | PD->protocol_begin(), |
| 4770 | PD->protocol_end()); |
| 4771 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4772 | Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4773 | + PD->getNameAsString(), |
| 4774 | "__DATA, __objc_const", |
| 4775 | InstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4776 | Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4777 | + PD->getNameAsString(), |
| 4778 | "__DATA, __objc_const", |
| 4779 | ClassMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4780 | Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4781 | + PD->getNameAsString(), |
| 4782 | "__DATA, __objc_const", |
| 4783 | OptInstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4784 | Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4785 | + PD->getNameAsString(), |
| 4786 | "__DATA, __objc_const", |
| 4787 | OptClassMethods); |
| 4788 | Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(), |
| 4789 | 0, PD, ObjCTypes); |
| 4790 | uint32_t Size = |
| 4791 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy); |
| 4792 | Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4793 | Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy); |
| 4794 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy, |
| 4795 | Values); |
| 4796 | |
| 4797 | if (Entry) { |
| 4798 | // Already created, fix the linkage and update the initializer. |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4799 | Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4800 | Entry->setInitializer(Init); |
| 4801 | } else { |
| 4802 | Entry = |
| 4803 | new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4804 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4805 | Init, |
| 4806 | std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName, |
| 4807 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4808 | Entry->setAlignment( |
| 4809 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4810 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4811 | } |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4812 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4813 | |
| 4814 | // Use this protocol meta-data to build protocol list table in section |
| 4815 | // __DATA, __objc_protolist |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4816 | llvm::GlobalVariable *PTGV = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4817 | ObjCTypes.ProtocolnfABIPtrTy, false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4818 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4819 | Entry, |
| 4820 | std::string("\01l_OBJC_LABEL_PROTOCOL_$_") |
| 4821 | +ProtocolName, |
| 4822 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4823 | PTGV->setAlignment( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4824 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy)); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 4825 | PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip"); |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4826 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4827 | UsedGlobals.push_back(PTGV); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4828 | return Entry; |
| 4829 | } |
| 4830 | |
| 4831 | /// EmitProtocolList - Generate protocol list meta-data: |
| 4832 | /// @code |
| 4833 | /// struct _protocol_list_t { |
| 4834 | /// long protocol_count; // Note, this is 32/64 bit |
| 4835 | /// struct _protocol_t[protocol_count]; |
| 4836 | /// } |
| 4837 | /// @endcode |
| 4838 | /// |
| 4839 | llvm::Constant * |
| 4840 | CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name, |
| 4841 | ObjCProtocolDecl::protocol_iterator begin, |
| 4842 | ObjCProtocolDecl::protocol_iterator end) { |
| 4843 | std::vector<llvm::Constant*> ProtocolRefs; |
| 4844 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4845 | // Just return null for empty protocol lists |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4846 | if (begin == end) |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4847 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 4848 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4849 | // FIXME: We shouldn't need to do this lookup here, should we? |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4850 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true); |
| 4851 | if (GV) |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4852 | return llvm::ConstantExpr::getBitCast(GV, |
| 4853 | ObjCTypes.ProtocolListnfABIPtrTy); |
| 4854 | |
| 4855 | for (; begin != end; ++begin) |
| 4856 | ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented??? |
| 4857 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4858 | // This list is null terminated. |
| 4859 | ProtocolRefs.push_back(llvm::Constant::getNullValue( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4860 | ObjCTypes.ProtocolnfABIPtrTy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4861 | |
| 4862 | std::vector<llvm::Constant*> Values(2); |
| 4863 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
| 4864 | Values[1] = |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4865 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4866 | ProtocolRefs.size()), |
| 4867 | ProtocolRefs); |
| 4868 | |
| 4869 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4870 | GV = new llvm::GlobalVariable(Init->getType(), false, |
| 4871 | llvm::GlobalValue::InternalLinkage, |
| 4872 | Init, |
| 4873 | Name, |
| 4874 | &CGM.getModule()); |
| 4875 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4876 | GV->setAlignment( |
| 4877 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4878 | UsedGlobals.push_back(GV); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4879 | return llvm::ConstantExpr::getBitCast(GV, |
| 4880 | ObjCTypes.ProtocolListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4881 | } |
| 4882 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4883 | /// GetMethodDescriptionConstant - This routine build following meta-data: |
| 4884 | /// struct _objc_method { |
| 4885 | /// SEL _cmd; |
| 4886 | /// char *method_type; |
| 4887 | /// char *_imp; |
| 4888 | /// } |
| 4889 | |
| 4890 | llvm::Constant * |
| 4891 | CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 4892 | std::vector<llvm::Constant*> Desc(3); |
| 4893 | Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 4894 | ObjCTypes.SelectorPtrTy); |
| 4895 | Desc[1] = GetMethodVarType(MD); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4896 | // Protocol methods have no implementation. So, this entry is always NULL. |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4897 | Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 4898 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc); |
| 4899 | } |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4900 | |
| 4901 | /// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference. |
| 4902 | /// This code gen. amounts to generating code for: |
| 4903 | /// @code |
| 4904 | /// (type *)((char *)base + _OBJC_IVAR_$_.ivar; |
| 4905 | /// @encode |
| 4906 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4907 | LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4908 | CodeGen::CodeGenFunction &CGF, |
| 4909 | QualType ObjectTy, |
| 4910 | llvm::Value *BaseValue, |
| 4911 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4912 | unsigned CVRQualifiers) { |
Daniel Dunbar | 525c9b7 | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 4913 | const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl(); |
| 4914 | const FieldDecl *Field = ID->lookupFieldDeclForIvar(CGM.getContext(), Ivar); |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 4915 | llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4916 | |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4917 | // (char *) BaseValue |
Chris Lattner | 51123fe | 2009-04-17 17:46:19 +0000 | [diff] [blame] | 4918 | llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4919 | llvm::Value *Offset = CGF.Builder.CreateLoad(IvarOffsetGV); |
| 4920 | // (char*)BaseValue + Offset_symbol |
| 4921 | V = CGF.Builder.CreateGEP(V, Offset, "add.ptr"); |
| 4922 | // (type *)((char*)BaseValue + Offset_symbol) |
| 4923 | const llvm::Type *IvarTy = |
Chris Lattner | 51123fe | 2009-04-17 17:46:19 +0000 | [diff] [blame] | 4924 | CGM.getTypes().ConvertTypeForMem(Ivar->getType()); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4925 | llvm::Type *ptrIvarTy = llvm::PointerType::getUnqual(IvarTy); |
| 4926 | V = CGF.Builder.CreateBitCast(V, ptrIvarTy); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4927 | |
Fariborz Jahanian | a6681ae | 2009-03-09 20:44:22 +0000 | [diff] [blame] | 4928 | if (Ivar->isBitField()) { |
Chris Lattner | 51123fe | 2009-04-17 17:46:19 +0000 | [diff] [blame] | 4929 | QualType FieldTy = Field->getType(); |
Fariborz Jahanian | a6681ae | 2009-03-09 20:44:22 +0000 | [diff] [blame] | 4930 | CodeGenTypes::BitFieldInfo bitFieldInfo = |
| 4931 | CGM.getTypes().getBitFieldInfo(Field); |
| 4932 | return LValue::MakeBitfield(V, bitFieldInfo.Begin, bitFieldInfo.Size, |
Chris Lattner | 51123fe | 2009-04-17 17:46:19 +0000 | [diff] [blame] | 4933 | FieldTy->isSignedIntegerType(), |
| 4934 | FieldTy.getCVRQualifiers()|CVRQualifiers); |
Fariborz Jahanian | a6681ae | 2009-03-09 20:44:22 +0000 | [diff] [blame] | 4935 | } |
| 4936 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4937 | LValue LV = LValue::MakeAddr(V, |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 4938 | Ivar->getType().getCVRQualifiers()|CVRQualifiers, |
| 4939 | CGM.getContext().getObjCGCAttrKind(Ivar->getType())); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4940 | LValue::SetObjCIvar(LV, true); |
| 4941 | return LV; |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4942 | } |
| 4943 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4944 | llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset( |
| 4945 | CodeGen::CodeGenFunction &CGF, |
| 4946 | ObjCInterfaceDecl *Interface, |
| 4947 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 4948 | return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar), |
| 4949 | false, "ivar"); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4950 | } |
| 4951 | |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 4952 | CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend( |
| 4953 | CodeGen::CodeGenFunction &CGF, |
| 4954 | QualType ResultType, |
| 4955 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4956 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 4957 | QualType Arg0Ty, |
| 4958 | bool IsSuper, |
| 4959 | const CallArgList &CallArgs) { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4960 | // FIXME. Even though IsSuper is passes. This function doese not |
| 4961 | // handle calls to 'super' receivers. |
| 4962 | CodeGenTypes &Types = CGM.getTypes(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4963 | llvm::Value *Arg0 = Receiver; |
| 4964 | if (!IsSuper) |
| 4965 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4966 | |
| 4967 | // Find the message function name. |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 4968 | // FIXME. This is too much work to get the ABI-specific result type |
| 4969 | // needed to find the message name. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4970 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, |
| 4971 | llvm::SmallVector<QualType, 16>()); |
| 4972 | llvm::Constant *Fn; |
| 4973 | std::string Name("\01l_"); |
| 4974 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4975 | #if 0 |
| 4976 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4977 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
| 4978 | Fn = ObjCTypes.MessageSendIdStretFixupFn; |
| 4979 | // FIXME. Is there a better way of getting these names. |
| 4980 | // They are available in RuntimeFunctions vector pair. |
| 4981 | Name += "objc_msgSendId_stret_fixup"; |
| 4982 | } |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4983 | else |
| 4984 | #endif |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4985 | if (IsSuper) { |
| 4986 | Fn = ObjCTypes.MessageSendSuper2StretFixupFn; |
| 4987 | Name += "objc_msgSendSuper2_stret_fixup"; |
| 4988 | } |
| 4989 | else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4990 | { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4991 | Fn = ObjCTypes.MessageSendStretFixupFn; |
| 4992 | Name += "objc_msgSend_stret_fixup"; |
| 4993 | } |
| 4994 | } |
Fariborz Jahanian | 1a6b368 | 2009-02-05 19:35:43 +0000 | [diff] [blame] | 4995 | else if (ResultType->isFloatingType() && |
| 4996 | // Selection of frret API only happens in 32bit nonfragile ABI. |
| 4997 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.LongTy) == 4) { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4998 | Fn = ObjCTypes.MessageSendFpretFixupFn; |
| 4999 | Name += "objc_msgSend_fpret_fixup"; |
| 5000 | } |
| 5001 | else { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5002 | #if 0 |
| 5003 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5004 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
| 5005 | Fn = ObjCTypes.MessageSendIdFixupFn; |
| 5006 | Name += "objc_msgSendId_fixup"; |
| 5007 | } |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5008 | else |
| 5009 | #endif |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5010 | if (IsSuper) { |
| 5011 | Fn = ObjCTypes.MessageSendSuper2FixupFn; |
| 5012 | Name += "objc_msgSendSuper2_fixup"; |
| 5013 | } |
| 5014 | else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5015 | { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5016 | Fn = ObjCTypes.MessageSendFixupFn; |
| 5017 | Name += "objc_msgSend_fixup"; |
| 5018 | } |
| 5019 | } |
| 5020 | Name += '_'; |
| 5021 | std::string SelName(Sel.getAsString()); |
| 5022 | // Replace all ':' in selector name with '_' ouch! |
| 5023 | for(unsigned i = 0; i < SelName.size(); i++) |
| 5024 | if (SelName[i] == ':') |
| 5025 | SelName[i] = '_'; |
| 5026 | Name += SelName; |
| 5027 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5028 | if (!GV) { |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5029 | // Build message ref table entry. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5030 | std::vector<llvm::Constant*> Values(2); |
| 5031 | Values[0] = Fn; |
| 5032 | Values[1] = GetMethodVarName(Sel); |
| 5033 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 5034 | GV = new llvm::GlobalVariable(Init->getType(), false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 5035 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5036 | Init, |
| 5037 | Name, |
| 5038 | &CGM.getModule()); |
| 5039 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | f59c1a6 | 2009-04-15 19:04:46 +0000 | [diff] [blame] | 5040 | GV->setAlignment(16); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5041 | GV->setSection("__DATA, __objc_msgrefs, coalesced"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5042 | } |
| 5043 | llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5044 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5045 | CallArgList ActualArgs; |
| 5046 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
| 5047 | ActualArgs.push_back(std::make_pair(RValue::get(Arg1), |
| 5048 | ObjCTypes.MessageRefCPtrTy)); |
| 5049 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5050 | const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs); |
| 5051 | llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0); |
| 5052 | Callee = CGF.Builder.CreateLoad(Callee); |
Fariborz Jahanian | 3ab75bd | 2009-02-14 21:25:36 +0000 | [diff] [blame] | 5053 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5054 | Callee = CGF.Builder.CreateBitCast(Callee, |
| 5055 | llvm::PointerType::getUnqual(FTy)); |
| 5056 | return CGF.EmitCall(FnInfo1, Callee, ActualArgs); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5057 | } |
| 5058 | |
| 5059 | /// Generate code for a message send expression in the nonfragile abi. |
| 5060 | CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend( |
| 5061 | CodeGen::CodeGenFunction &CGF, |
| 5062 | QualType ResultType, |
| 5063 | Selector Sel, |
| 5064 | llvm::Value *Receiver, |
| 5065 | bool IsClassMessage, |
| 5066 | const CallArgList &CallArgs) { |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5067 | return EmitMessageSend(CGF, ResultType, Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5068 | Receiver, CGF.getContext().getObjCIdType(), |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5069 | false, CallArgs); |
| 5070 | } |
| 5071 | |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5072 | llvm::GlobalVariable * |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5073 | CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5074 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5075 | |
Daniel Dunbar | dfff230 | 2009-03-02 05:18:14 +0000 | [diff] [blame] | 5076 | if (!GV) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5077 | GV = new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, |
| 5078 | llvm::GlobalValue::ExternalLinkage, |
| 5079 | 0, Name, &CGM.getModule()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5080 | } |
| 5081 | |
| 5082 | return GV; |
| 5083 | } |
| 5084 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5085 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5086 | const ObjCInterfaceDecl *ID) { |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5087 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 5088 | |
| 5089 | if (!Entry) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5090 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5091 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5092 | Entry = |
| 5093 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5094 | llvm::GlobalValue::InternalLinkage, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5095 | ClassGV, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5096 | "\01L_OBJC_CLASSLIST_REFERENCES_$_", |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5097 | &CGM.getModule()); |
| 5098 | Entry->setAlignment( |
| 5099 | CGM.getTargetData().getPrefTypeAlignment( |
| 5100 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5101 | Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip"); |
| 5102 | UsedGlobals.push_back(Entry); |
| 5103 | } |
| 5104 | |
| 5105 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5106 | } |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5107 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5108 | llvm::Value * |
| 5109 | CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder, |
| 5110 | const ObjCInterfaceDecl *ID) { |
| 5111 | llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()]; |
| 5112 | |
| 5113 | if (!Entry) { |
| 5114 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
| 5115 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
| 5116 | Entry = |
| 5117 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5118 | llvm::GlobalValue::InternalLinkage, |
| 5119 | ClassGV, |
| 5120 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_", |
| 5121 | &CGM.getModule()); |
| 5122 | Entry->setAlignment( |
| 5123 | CGM.getTargetData().getPrefTypeAlignment( |
| 5124 | ObjCTypes.ClassnfABIPtrTy)); |
| 5125 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5126 | UsedGlobals.push_back(Entry); |
| 5127 | } |
| 5128 | |
| 5129 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5130 | } |
| 5131 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5132 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 5133 | /// meta-data |
| 5134 | /// |
| 5135 | llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder, |
| 5136 | const ObjCInterfaceDecl *ID) { |
| 5137 | llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()]; |
| 5138 | if (Entry) |
| 5139 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5140 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5141 | std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5142 | llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5143 | Entry = |
| 5144 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5145 | llvm::GlobalValue::InternalLinkage, |
| 5146 | MetaClassGV, |
| 5147 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_", |
| 5148 | &CGM.getModule()); |
| 5149 | Entry->setAlignment( |
| 5150 | CGM.getTargetData().getPrefTypeAlignment( |
| 5151 | ObjCTypes.ClassnfABIPtrTy)); |
| 5152 | |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5153 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5154 | UsedGlobals.push_back(Entry); |
| 5155 | |
| 5156 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5157 | } |
| 5158 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5159 | /// GetClass - Return a reference to the class for the given interface |
| 5160 | /// decl. |
| 5161 | llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder, |
| 5162 | const ObjCInterfaceDecl *ID) { |
| 5163 | return EmitClassRef(Builder, ID); |
| 5164 | } |
| 5165 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5166 | /// Generates a message send where the super is the receiver. This is |
| 5167 | /// a message send to self with special delivery semantics indicating |
| 5168 | /// which class's method should be called. |
| 5169 | CodeGen::RValue |
| 5170 | CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 5171 | QualType ResultType, |
| 5172 | Selector Sel, |
| 5173 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5174 | bool isCategoryImpl, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5175 | llvm::Value *Receiver, |
| 5176 | bool IsClassMessage, |
| 5177 | const CodeGen::CallArgList &CallArgs) { |
| 5178 | // ... |
| 5179 | // Create and init a super structure; this is a (receiver, class) |
| 5180 | // pair we will pass to objc_msgSendSuper. |
| 5181 | llvm::Value *ObjCSuper = |
| 5182 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 5183 | |
| 5184 | llvm::Value *ReceiverAsObject = |
| 5185 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 5186 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 5187 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
| 5188 | |
| 5189 | // 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] | 5190 | llvm::Value *Target; |
| 5191 | if (IsClassMessage) { |
| 5192 | if (isCategoryImpl) { |
| 5193 | // Message sent to "super' in a class method defined in |
| 5194 | // a category implementation. |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5195 | Target = EmitClassRef(CGF.Builder, Class); |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5196 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 5197 | Target = CGF.Builder.CreateLoad(Target); |
| 5198 | } |
| 5199 | else |
| 5200 | Target = EmitMetaClassRef(CGF.Builder, Class); |
| 5201 | } |
| 5202 | else |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5203 | Target = EmitSuperClassRef(CGF.Builder, Class); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5204 | |
| 5205 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext |
| 5206 | // and ObjCTypes types. |
| 5207 | const llvm::Type *ClassTy = |
| 5208 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
| 5209 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
| 5210 | CGF.Builder.CreateStore(Target, |
| 5211 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
| 5212 | |
| 5213 | return EmitMessageSend(CGF, ResultType, Sel, |
| 5214 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 5215 | true, CallArgs); |
| 5216 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5217 | |
| 5218 | llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder, |
| 5219 | Selector Sel) { |
| 5220 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 5221 | |
| 5222 | if (!Entry) { |
| 5223 | llvm::Constant *Casted = |
| 5224 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 5225 | ObjCTypes.SelectorPtrTy); |
| 5226 | Entry = |
| 5227 | new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false, |
| 5228 | llvm::GlobalValue::InternalLinkage, |
| 5229 | Casted, "\01L_OBJC_SELECTOR_REFERENCES_", |
| 5230 | &CGM.getModule()); |
| 5231 | Entry->setSection("__DATA,__objc_selrefs,literal_pointers,no_dead_strip"); |
| 5232 | UsedGlobals.push_back(Entry); |
| 5233 | } |
| 5234 | |
| 5235 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5236 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5237 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 5238 | /// objc_assign_ivar (id src, id *dst) |
| 5239 | /// |
| 5240 | void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 5241 | llvm::Value *src, llvm::Value *dst) |
| 5242 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5243 | const llvm::Type * SrcTy = src->getType(); |
| 5244 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5245 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5246 | assert(Size <= 8 && "does not support size > 8"); |
| 5247 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5248 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5249 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5250 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5251 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5252 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
| 5253 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn, |
| 5254 | src, dst, "assignivar"); |
| 5255 | return; |
| 5256 | } |
| 5257 | |
| 5258 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 5259 | /// objc_assign_strongCast (id src, id *dst) |
| 5260 | /// |
| 5261 | void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign( |
| 5262 | CodeGen::CodeGenFunction &CGF, |
| 5263 | llvm::Value *src, llvm::Value *dst) |
| 5264 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5265 | const llvm::Type * SrcTy = src->getType(); |
| 5266 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5267 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5268 | assert(Size <= 8 && "does not support size > 8"); |
| 5269 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5270 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5271 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5272 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5273 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5274 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
| 5275 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn, |
| 5276 | src, dst, "weakassign"); |
| 5277 | return; |
| 5278 | } |
| 5279 | |
| 5280 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
| 5281 | /// object: objc_read_weak (id *src) |
| 5282 | /// |
| 5283 | llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead( |
| 5284 | CodeGen::CodeGenFunction &CGF, |
| 5285 | llvm::Value *AddrWeakObj) |
| 5286 | { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5287 | const llvm::Type* DestTy = |
| 5288 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5289 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
| 5290 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn, |
| 5291 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5292 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5293 | return read_weak; |
| 5294 | } |
| 5295 | |
| 5296 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 5297 | /// objc_assign_weak (id src, id *dst) |
| 5298 | /// |
| 5299 | void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 5300 | llvm::Value *src, llvm::Value *dst) |
| 5301 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5302 | const llvm::Type * SrcTy = src->getType(); |
| 5303 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5304 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5305 | assert(Size <= 8 && "does not support size > 8"); |
| 5306 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5307 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5308 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5309 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5310 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5311 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 5312 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5313 | src, dst, "weakassign"); |
| 5314 | return; |
| 5315 | } |
| 5316 | |
| 5317 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 5318 | /// objc_assign_global (id src, id *dst) |
| 5319 | /// |
| 5320 | void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 5321 | llvm::Value *src, llvm::Value *dst) |
| 5322 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5323 | const llvm::Type * SrcTy = src->getType(); |
| 5324 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5325 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5326 | assert(Size <= 8 && "does not support size > 8"); |
| 5327 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5328 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5329 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5330 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5331 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5332 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
| 5333 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn, |
| 5334 | src, dst, "globalassign"); |
| 5335 | return; |
| 5336 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5337 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5338 | void |
| 5339 | CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 5340 | const Stmt &S) { |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5341 | bool isTry = isa<ObjCAtTryStmt>(S); |
| 5342 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 5343 | llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5344 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5345 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5346 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5347 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
| 5348 | |
| 5349 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 5350 | // evaluation of the expression must occur before we enter the |
| 5351 | // @synchronized. We can safely avoid a temp here because jumps into |
| 5352 | // @synchronized are illegal & this will dominate uses. |
| 5353 | llvm::Value *SyncArg = 0; |
| 5354 | if (!isTry) { |
| 5355 | SyncArg = |
| 5356 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 5357 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5358 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5359 | } |
| 5360 | |
| 5361 | // Push an EH context entry, used for handling rethrows and jumps |
| 5362 | // through finally. |
| 5363 | CGF.PushCleanupBlock(FinallyBlock); |
| 5364 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5365 | CGF.setInvokeDest(TryHandler); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5366 | |
| 5367 | CGF.EmitBlock(TryBlock); |
| 5368 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 5369 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
| 5370 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 5371 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5372 | // Emit the exception handler. |
| 5373 | |
| 5374 | CGF.EmitBlock(TryHandler); |
| 5375 | |
| 5376 | llvm::Value *llvm_eh_exception = |
| 5377 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception); |
| 5378 | llvm::Value *llvm_eh_selector_i64 = |
| 5379 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64); |
| 5380 | llvm::Value *llvm_eh_typeid_for_i64 = |
| 5381 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64); |
| 5382 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5383 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow"); |
| 5384 | |
| 5385 | llvm::SmallVector<llvm::Value*, 8> SelectorArgs; |
| 5386 | SelectorArgs.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5387 | SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5388 | |
| 5389 | // Construct the lists of (type, catch body) to handle. |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5390 | llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5391 | bool HasCatchAll = false; |
| 5392 | if (isTry) { |
| 5393 | if (const ObjCAtCatchStmt* CatchStmt = |
| 5394 | cast<ObjCAtTryStmt>(S).getCatchStmts()) { |
| 5395 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5396 | const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl(); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5397 | Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody())); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5398 | |
| 5399 | // catch(...) always matches. |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5400 | if (!CatchDecl) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5401 | // Use i8* null here to signal this is a catch all, not a cleanup. |
| 5402 | llvm::Value *Null = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 5403 | SelectorArgs.push_back(Null); |
| 5404 | HasCatchAll = true; |
| 5405 | break; |
| 5406 | } |
| 5407 | |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5408 | if (CGF.getContext().isObjCIdType(CatchDecl->getType()) || |
| 5409 | CatchDecl->getType()->isObjCQualifiedIdType()) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5410 | llvm::Value *IDEHType = |
| 5411 | CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id"); |
| 5412 | if (!IDEHType) |
| 5413 | IDEHType = |
| 5414 | new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5415 | llvm::GlobalValue::ExternalLinkage, |
| 5416 | 0, "OBJC_EHTYPE_id", &CGM.getModule()); |
| 5417 | SelectorArgs.push_back(IDEHType); |
| 5418 | HasCatchAll = true; |
| 5419 | break; |
| 5420 | } |
| 5421 | |
| 5422 | // All other types should be Objective-C interface pointer types. |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5423 | const PointerType *PT = CatchDecl->getType()->getAsPointerType(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5424 | assert(PT && "Invalid @catch type."); |
| 5425 | const ObjCInterfaceType *IT = |
| 5426 | PT->getPointeeType()->getAsObjCInterfaceType(); |
| 5427 | assert(IT && "Invalid @catch type."); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5428 | llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5429 | SelectorArgs.push_back(EHType); |
| 5430 | } |
| 5431 | } |
| 5432 | } |
| 5433 | |
| 5434 | // We use a cleanup unless there was already a catch all. |
| 5435 | if (!HasCatchAll) { |
| 5436 | SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0)); |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5437 | Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5438 | } |
| 5439 | |
| 5440 | llvm::Value *Selector = |
| 5441 | CGF.Builder.CreateCall(llvm_eh_selector_i64, |
| 5442 | SelectorArgs.begin(), SelectorArgs.end(), |
| 5443 | "selector"); |
| 5444 | for (unsigned i = 0, e = Handlers.size(); i != e; ++i) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5445 | const ParmVarDecl *CatchParam = Handlers[i].first; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5446 | const Stmt *CatchBody = Handlers[i].second; |
| 5447 | |
| 5448 | llvm::BasicBlock *Next = 0; |
| 5449 | |
| 5450 | // The last handler always matches. |
| 5451 | if (i + 1 != e) { |
| 5452 | assert(CatchParam && "Only last handler can be a catch all."); |
| 5453 | |
| 5454 | llvm::BasicBlock *Match = CGF.createBasicBlock("match"); |
| 5455 | Next = CGF.createBasicBlock("catch.next"); |
| 5456 | llvm::Value *Id = |
| 5457 | CGF.Builder.CreateCall(llvm_eh_typeid_for_i64, |
| 5458 | CGF.Builder.CreateBitCast(SelectorArgs[i+2], |
| 5459 | ObjCTypes.Int8PtrTy)); |
| 5460 | CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id), |
| 5461 | Match, Next); |
| 5462 | |
| 5463 | CGF.EmitBlock(Match); |
| 5464 | } |
| 5465 | |
| 5466 | if (CatchBody) { |
| 5467 | llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end"); |
| 5468 | llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler"); |
| 5469 | |
| 5470 | // Cleanups must call objc_end_catch. |
| 5471 | // |
| 5472 | // FIXME: It seems incorrect for objc_begin_catch to be inside |
| 5473 | // this context, but this matches gcc. |
| 5474 | CGF.PushCleanupBlock(MatchEnd); |
| 5475 | CGF.setInvokeDest(MatchHandler); |
| 5476 | |
| 5477 | llvm::Value *ExcObject = |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5478 | CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), Exc); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5479 | |
| 5480 | // Bind the catch parameter if it exists. |
| 5481 | if (CatchParam) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5482 | ExcObject = |
| 5483 | CGF.Builder.CreateBitCast(ExcObject, |
| 5484 | CGF.ConvertType(CatchParam->getType())); |
| 5485 | // CatchParam is a ParmVarDecl because of the grammar |
| 5486 | // construction used to handle this, but for codegen purposes |
| 5487 | // we treat this as a local decl. |
| 5488 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
| 5489 | CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5490 | } |
| 5491 | |
| 5492 | CGF.ObjCEHValueStack.push_back(ExcObject); |
| 5493 | CGF.EmitStmt(CatchBody); |
| 5494 | CGF.ObjCEHValueStack.pop_back(); |
| 5495 | |
| 5496 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 5497 | |
| 5498 | CGF.EmitBlock(MatchHandler); |
| 5499 | |
| 5500 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5501 | // We are required to emit this call to satisfy LLVM, even |
| 5502 | // though we don't use the result. |
| 5503 | llvm::SmallVector<llvm::Value*, 8> Args; |
| 5504 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5505 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5506 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 5507 | 0)); |
| 5508 | CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end()); |
| 5509 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5510 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5511 | |
| 5512 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 5513 | |
| 5514 | CGF.EmitBlock(MatchEnd); |
| 5515 | |
| 5516 | // Unfortunately, we also have to generate another EH frame here |
| 5517 | // in case this throws. |
| 5518 | llvm::BasicBlock *MatchEndHandler = |
| 5519 | CGF.createBasicBlock("match.end.handler"); |
| 5520 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5521 | CGF.Builder.CreateInvoke(ObjCTypes.getObjCEndCatchFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5522 | Cont, MatchEndHandler, |
| 5523 | Args.begin(), Args.begin()); |
| 5524 | |
| 5525 | CGF.EmitBlock(Cont); |
| 5526 | if (Info.SwitchBlock) |
| 5527 | CGF.EmitBlock(Info.SwitchBlock); |
| 5528 | if (Info.EndBlock) |
| 5529 | CGF.EmitBlock(Info.EndBlock); |
| 5530 | |
| 5531 | CGF.EmitBlock(MatchEndHandler); |
| 5532 | Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5533 | // We are required to emit this call to satisfy LLVM, even |
| 5534 | // though we don't use the result. |
| 5535 | Args.clear(); |
| 5536 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5537 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5538 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 5539 | 0)); |
| 5540 | CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end()); |
| 5541 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5542 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5543 | |
| 5544 | if (Next) |
| 5545 | CGF.EmitBlock(Next); |
| 5546 | } else { |
| 5547 | assert(!Next && "catchup should be last handler."); |
| 5548 | |
| 5549 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5550 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5551 | } |
| 5552 | } |
| 5553 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5554 | // Pop the cleanup entry, the @finally is outside this cleanup |
| 5555 | // scope. |
| 5556 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 5557 | CGF.setInvokeDest(PrevLandingPad); |
| 5558 | |
| 5559 | CGF.EmitBlock(FinallyBlock); |
| 5560 | |
| 5561 | if (isTry) { |
| 5562 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 5563 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 5564 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
| 5565 | } else { |
| 5566 | // Emit 'objc_sync_exit(expr)' as finally's sole statement for |
| 5567 | // @synchronized. |
| 5568 | CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, SyncArg); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5569 | } |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5570 | |
| 5571 | if (Info.SwitchBlock) |
| 5572 | CGF.EmitBlock(Info.SwitchBlock); |
| 5573 | if (Info.EndBlock) |
| 5574 | CGF.EmitBlock(Info.EndBlock); |
| 5575 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5576 | // Branch around the rethrow code. |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5577 | CGF.EmitBranch(FinallyEnd); |
| 5578 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5579 | CGF.EmitBlock(FinallyRethrow); |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5580 | CGF.Builder.CreateCall(ObjCTypes.getUnwindResumeOrRethrowFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5581 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5582 | CGF.Builder.CreateUnreachable(); |
| 5583 | |
| 5584 | CGF.EmitBlock(FinallyEnd); |
| 5585 | } |
| 5586 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5587 | /// EmitThrowStmt - Generate code for a throw statement. |
| 5588 | void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 5589 | const ObjCAtThrowStmt &S) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5590 | llvm::Value *Exception; |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5591 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5592 | Exception = CGF.EmitScalarExpr(ThrowExpr); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5593 | } else { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5594 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
| 5595 | "Unexpected rethrow outside @catch block."); |
| 5596 | Exception = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5597 | } |
| 5598 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5599 | llvm::Value *ExceptionAsObject = |
| 5600 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 5601 | llvm::BasicBlock *InvokeDest = CGF.getInvokeDest(); |
| 5602 | if (InvokeDest) { |
| 5603 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
| 5604 | CGF.Builder.CreateInvoke(ObjCTypes.ExceptionThrowFn, |
| 5605 | Cont, InvokeDest, |
| 5606 | &ExceptionAsObject, &ExceptionAsObject + 1); |
| 5607 | CGF.EmitBlock(Cont); |
| 5608 | } else |
| 5609 | CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject); |
| 5610 | CGF.Builder.CreateUnreachable(); |
| 5611 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5612 | // Clear the insertion point to indicate we are in unreachable code. |
| 5613 | CGF.Builder.ClearInsertionPoint(); |
| 5614 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5615 | |
| 5616 | llvm::Value * |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5617 | CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
| 5618 | bool ForDefinition) { |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5619 | llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()]; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5620 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5621 | // If we don't need a definition, return the entry if found or check |
| 5622 | // if we use an external reference. |
| 5623 | if (!ForDefinition) { |
| 5624 | if (Entry) |
| 5625 | return Entry; |
Daniel Dunbar | 7e075cb | 2009-04-07 06:43:45 +0000 | [diff] [blame] | 5626 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5627 | // If this type (or a super class) has the __objc_exception__ |
| 5628 | // attribute, emit an external reference. |
| 5629 | if (hasObjCExceptionAttribute(ID)) |
| 5630 | return Entry = |
| 5631 | new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5632 | llvm::GlobalValue::ExternalLinkage, |
| 5633 | 0, |
| 5634 | (std::string("OBJC_EHTYPE_$_") + |
| 5635 | ID->getIdentifier()->getName()), |
| 5636 | &CGM.getModule()); |
| 5637 | } |
| 5638 | |
| 5639 | // Otherwise we need to either make a new entry or fill in the |
| 5640 | // initializer. |
| 5641 | assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition"); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5642 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5643 | std::string VTableName = "objc_ehtype_vtable"; |
| 5644 | llvm::GlobalVariable *VTableGV = |
| 5645 | CGM.getModule().getGlobalVariable(VTableName); |
| 5646 | if (!VTableGV) |
| 5647 | VTableGV = new llvm::GlobalVariable(ObjCTypes.Int8PtrTy, false, |
| 5648 | llvm::GlobalValue::ExternalLinkage, |
| 5649 | 0, VTableName, &CGM.getModule()); |
| 5650 | |
| 5651 | llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2); |
| 5652 | |
| 5653 | std::vector<llvm::Constant*> Values(3); |
| 5654 | Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1); |
| 5655 | Values[1] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5656 | Values[2] = GetClassGlobal(ClassName); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5657 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values); |
| 5658 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5659 | if (Entry) { |
| 5660 | Entry->setInitializer(Init); |
| 5661 | } else { |
| 5662 | Entry = new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5663 | llvm::GlobalValue::WeakAnyLinkage, |
| 5664 | Init, |
| 5665 | (std::string("OBJC_EHTYPE_$_") + |
| 5666 | ID->getIdentifier()->getName()), |
| 5667 | &CGM.getModule()); |
| 5668 | } |
| 5669 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 5670 | if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden) |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5671 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5672 | Entry->setAlignment(8); |
| 5673 | |
| 5674 | if (ForDefinition) { |
| 5675 | Entry->setSection("__DATA,__objc_const"); |
| 5676 | Entry->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 5677 | } else { |
| 5678 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 5679 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5680 | |
| 5681 | return Entry; |
| 5682 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5683 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 5684 | /* *** */ |
| 5685 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5686 | CodeGen::CGObjCRuntime * |
| 5687 | CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 5688 | return new CGObjCMac(CGM); |
| 5689 | } |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5690 | |
| 5691 | CodeGen::CGObjCRuntime * |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5692 | CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5693 | return new CGObjCNonFragileABIMac(CGM); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5694 | } |