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: |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 44 | const llvm::Type *ShortTy, *IntTy, *LongTy; |
| 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 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 87 | llvm::Function *GetPropertyFn, *SetPropertyFn; |
| 88 | |
| 89 | llvm::Function *EnumerationMutationFn; |
| 90 | |
| 91 | /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function. |
| 92 | llvm::Function *GcReadWeakFn; |
| 93 | |
| 94 | /// GcAssignWeakFn -- LLVM objc_assign_weak function. |
| 95 | llvm::Function *GcAssignWeakFn; |
| 96 | |
| 97 | /// GcAssignGlobalFn -- LLVM objc_assign_global function. |
| 98 | llvm::Function *GcAssignGlobalFn; |
| 99 | |
| 100 | /// GcAssignIvarFn -- LLVM objc_assign_ivar function. |
| 101 | llvm::Function *GcAssignIvarFn; |
| 102 | |
| 103 | /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function. |
| 104 | llvm::Function *GcAssignStrongCastFn; |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 105 | |
| 106 | /// ExceptionThrowFn - LLVM objc_exception_throw function. |
| 107 | llvm::Function *ExceptionThrowFn; |
| 108 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 109 | /// SyncEnterFn - LLVM object_sync_enter function. |
| 110 | llvm::Function *SyncEnterFn; |
| 111 | |
| 112 | /// SyncExitFn - LLVM object_sync_exit function. |
| 113 | llvm::Function *SyncExitFn; |
| 114 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 115 | ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm); |
| 116 | ~ObjCCommonTypesHelper(){} |
| 117 | }; |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 118 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 119 | /// ObjCTypesHelper - Helper class that encapsulates lazy |
| 120 | /// construction of varies types used during ObjC generation. |
| 121 | class ObjCTypesHelper : public ObjCCommonTypesHelper { |
| 122 | private: |
| 123 | |
| 124 | llvm::Function *MessageSendFn, *MessageSendStretFn, *MessageSendFpretFn; |
| 125 | llvm::Function *MessageSendSuperFn, *MessageSendSuperStretFn, |
| 126 | *MessageSendSuperFpretFn; |
| 127 | |
| 128 | public: |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 129 | /// SymtabTy - LLVM type for struct objc_symtab. |
| 130 | const llvm::StructType *SymtabTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 131 | /// SymtabPtrTy - LLVM type for struct objc_symtab *. |
| 132 | const llvm::Type *SymtabPtrTy; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 133 | /// ModuleTy - LLVM type for struct objc_module. |
| 134 | const llvm::StructType *ModuleTy; |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 135 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 136 | /// ProtocolTy - LLVM type for struct objc_protocol. |
| 137 | const llvm::StructType *ProtocolTy; |
| 138 | /// ProtocolPtrTy - LLVM type for struct objc_protocol *. |
| 139 | const llvm::Type *ProtocolPtrTy; |
| 140 | /// ProtocolExtensionTy - LLVM type for struct |
| 141 | /// objc_protocol_extension. |
| 142 | const llvm::StructType *ProtocolExtensionTy; |
| 143 | /// ProtocolExtensionTy - LLVM type for struct |
| 144 | /// objc_protocol_extension *. |
| 145 | const llvm::Type *ProtocolExtensionPtrTy; |
| 146 | /// MethodDescriptionTy - LLVM type for struct |
| 147 | /// objc_method_description. |
| 148 | const llvm::StructType *MethodDescriptionTy; |
| 149 | /// MethodDescriptionListTy - LLVM type for struct |
| 150 | /// objc_method_description_list. |
| 151 | const llvm::StructType *MethodDescriptionListTy; |
| 152 | /// MethodDescriptionListPtrTy - LLVM type for struct |
| 153 | /// objc_method_description_list *. |
| 154 | const llvm::Type *MethodDescriptionListPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 155 | /// ProtocolListTy - LLVM type for struct objc_property_list. |
| 156 | const llvm::Type *ProtocolListTy; |
| 157 | /// ProtocolListPtrTy - LLVM type for struct objc_property_list*. |
| 158 | const llvm::Type *ProtocolListPtrTy; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 159 | /// CategoryTy - LLVM type for struct objc_category. |
| 160 | const llvm::StructType *CategoryTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 161 | /// ClassTy - LLVM type for struct objc_class. |
| 162 | const llvm::StructType *ClassTy; |
| 163 | /// ClassPtrTy - LLVM type for struct objc_class *. |
| 164 | const llvm::Type *ClassPtrTy; |
| 165 | /// ClassExtensionTy - LLVM type for struct objc_class_ext. |
| 166 | const llvm::StructType *ClassExtensionTy; |
| 167 | /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *. |
| 168 | const llvm::Type *ClassExtensionPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 169 | // IvarTy - LLVM type for struct objc_ivar. |
| 170 | const llvm::StructType *IvarTy; |
| 171 | /// IvarListTy - LLVM type for struct objc_ivar_list. |
| 172 | const llvm::Type *IvarListTy; |
| 173 | /// IvarListPtrTy - LLVM type for struct objc_ivar_list *. |
| 174 | const llvm::Type *IvarListPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 175 | /// MethodListTy - LLVM type for struct objc_method_list. |
| 176 | const llvm::Type *MethodListTy; |
| 177 | /// MethodListPtrTy - LLVM type for struct objc_method_list *. |
| 178 | const llvm::Type *MethodListPtrTy; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 179 | |
| 180 | /// ExceptionDataTy - LLVM type for struct _objc_exception_data. |
| 181 | const llvm::Type *ExceptionDataTy; |
| 182 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 183 | /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function. |
| 184 | llvm::Function *ExceptionTryEnterFn; |
| 185 | |
| 186 | /// ExceptionTryExitFn - LLVM objc_exception_try_exit function. |
| 187 | llvm::Function *ExceptionTryExitFn; |
| 188 | |
| 189 | /// ExceptionExtractFn - LLVM objc_exception_extract function. |
| 190 | llvm::Function *ExceptionExtractFn; |
| 191 | |
| 192 | /// ExceptionMatchFn - LLVM objc_exception_match function. |
| 193 | llvm::Function *ExceptionMatchFn; |
| 194 | |
| 195 | /// SetJmpFn - LLVM _setjmp function. |
| 196 | llvm::Function *SetJmpFn; |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 197 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 198 | public: |
| 199 | ObjCTypesHelper(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 200 | ~ObjCTypesHelper() {} |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 201 | |
| 202 | |
| 203 | llvm::Function *getSendFn(bool IsSuper) { |
| 204 | return IsSuper ? MessageSendSuperFn : MessageSendFn; |
| 205 | } |
| 206 | |
| 207 | llvm::Function *getSendStretFn(bool IsSuper) { |
| 208 | return IsSuper ? MessageSendSuperStretFn : MessageSendStretFn; |
| 209 | } |
| 210 | |
| 211 | llvm::Function *getSendFpretFn(bool IsSuper) { |
| 212 | return IsSuper ? MessageSendSuperFpretFn : MessageSendFpretFn; |
| 213 | } |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 214 | }; |
| 215 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 216 | /// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 217 | /// modern abi |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 218 | class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 219 | public: |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 220 | llvm::Function *MessageSendFixupFn, *MessageSendFpretFixupFn, |
| 221 | *MessageSendStretFixupFn, *MessageSendIdFixupFn, |
| 222 | *MessageSendIdStretFixupFn, *MessageSendSuper2FixupFn, |
| 223 | *MessageSendSuper2StretFixupFn; |
| 224 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 225 | // MethodListnfABITy - LLVM for struct _method_list_t |
| 226 | const llvm::StructType *MethodListnfABITy; |
| 227 | |
| 228 | // MethodListnfABIPtrTy - LLVM for struct _method_list_t* |
| 229 | const llvm::Type *MethodListnfABIPtrTy; |
| 230 | |
| 231 | // ProtocolnfABITy = LLVM for struct _protocol_t |
| 232 | const llvm::StructType *ProtocolnfABITy; |
| 233 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 234 | // ProtocolnfABIPtrTy = LLVM for struct _protocol_t* |
| 235 | const llvm::Type *ProtocolnfABIPtrTy; |
| 236 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 237 | // ProtocolListnfABITy - LLVM for struct _objc_protocol_list |
| 238 | const llvm::StructType *ProtocolListnfABITy; |
| 239 | |
| 240 | // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list* |
| 241 | const llvm::Type *ProtocolListnfABIPtrTy; |
| 242 | |
| 243 | // ClassnfABITy - LLVM for struct _class_t |
| 244 | const llvm::StructType *ClassnfABITy; |
| 245 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 246 | // ClassnfABIPtrTy - LLVM for struct _class_t* |
| 247 | const llvm::Type *ClassnfABIPtrTy; |
| 248 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 249 | // IvarnfABITy - LLVM for struct _ivar_t |
| 250 | const llvm::StructType *IvarnfABITy; |
| 251 | |
| 252 | // IvarListnfABITy - LLVM for struct _ivar_list_t |
| 253 | const llvm::StructType *IvarListnfABITy; |
| 254 | |
| 255 | // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t* |
| 256 | const llvm::Type *IvarListnfABIPtrTy; |
| 257 | |
| 258 | // ClassRonfABITy - LLVM for struct _class_ro_t |
| 259 | const llvm::StructType *ClassRonfABITy; |
| 260 | |
| 261 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 262 | const llvm::Type *ImpnfABITy; |
| 263 | |
| 264 | // CategorynfABITy - LLVM for struct _category_t |
| 265 | const llvm::StructType *CategorynfABITy; |
| 266 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 267 | // New types for nonfragile abi messaging. |
| 268 | |
| 269 | // MessageRefTy - LLVM for: |
| 270 | // struct _message_ref_t { |
| 271 | // IMP messenger; |
| 272 | // SEL name; |
| 273 | // }; |
| 274 | const llvm::StructType *MessageRefTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 275 | // MessageRefCTy - clang type for struct _message_ref_t |
| 276 | QualType MessageRefCTy; |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 277 | |
| 278 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
| 279 | const llvm::Type *MessageRefPtrTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 280 | // MessageRefCPtrTy - clang type for struct _message_ref_t* |
| 281 | QualType MessageRefCPtrTy; |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 282 | |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 283 | // MessengerTy - Type of the messenger (shown as IMP above) |
| 284 | const llvm::FunctionType *MessengerTy; |
| 285 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 286 | // SuperMessageRefTy - LLVM for: |
| 287 | // struct _super_message_ref_t { |
| 288 | // SUPER_IMP messenger; |
| 289 | // SEL name; |
| 290 | // }; |
| 291 | const llvm::StructType *SuperMessageRefTy; |
| 292 | |
| 293 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
| 294 | const llvm::Type *SuperMessageRefPtrTy; |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 295 | |
| 296 | /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C |
| 297 | /// exception personality function. |
| 298 | llvm::Value *EHPersonalityPtr; |
| 299 | |
| 300 | llvm::Function *UnwindResumeOrRethrowFn; |
| 301 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 302 | ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm); |
| 303 | ~ObjCNonFragileABITypesHelper(){} |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 304 | }; |
| 305 | |
| 306 | class CGObjCCommonMac : public CodeGen::CGObjCRuntime { |
| 307 | protected: |
| 308 | CodeGen::CodeGenModule &CGM; |
| 309 | // FIXME! May not be needing this after all. |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 310 | unsigned ObjCABI; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 311 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 312 | /// LazySymbols - Symbols to generate a lazy reference for. See |
| 313 | /// DefinedSymbols and FinishModule(). |
| 314 | std::set<IdentifierInfo*> LazySymbols; |
| 315 | |
| 316 | /// DefinedSymbols - External symbols which are defined by this |
| 317 | /// module. The symbols in this list and LazySymbols are used to add |
| 318 | /// special linker symbols which ensure that Objective-C modules are |
| 319 | /// linked properly. |
| 320 | std::set<IdentifierInfo*> DefinedSymbols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 321 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 322 | /// ClassNames - uniqued class names. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 323 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 324 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 325 | /// MethodVarNames - uniqued method variable names. |
| 326 | llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 327 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 328 | /// MethodVarTypes - uniqued method type signatures. We have to use |
| 329 | /// a StringMap here because have no other unique reference. |
| 330 | llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 331 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 332 | /// MethodDefinitions - map of methods which have been defined in |
| 333 | /// this translation unit. |
| 334 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 335 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 336 | /// PropertyNames - uniqued method variable names. |
| 337 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 338 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 339 | /// ClassReferences - uniqued class references. |
| 340 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 341 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 342 | /// SelectorReferences - uniqued selector references. |
| 343 | llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 344 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 345 | /// Protocols - Protocols for which an objc_protocol structure has |
| 346 | /// been emitted. Forward declarations are handled by creating an |
| 347 | /// empty structure whose initializer is filled in when/if defined. |
| 348 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 349 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 350 | /// DefinedProtocols - Protocols which have actually been |
| 351 | /// defined. We should not need this, see FIXME in GenerateProtocol. |
| 352 | llvm::DenseSet<IdentifierInfo*> DefinedProtocols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 353 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 354 | /// DefinedClasses - List of defined classes. |
| 355 | std::vector<llvm::GlobalValue*> DefinedClasses; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 356 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 357 | /// DefinedCategories - List of defined categories. |
| 358 | std::vector<llvm::GlobalValue*> DefinedCategories; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 359 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 360 | /// UsedGlobals - List of globals to pack into the llvm.used metadata |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 361 | /// to prevent them from being clobbered. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 362 | std::vector<llvm::GlobalVariable*> UsedGlobals; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 363 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 364 | /// GetNameForMethod - Return a name for the given method. |
| 365 | /// \param[out] NameOut - The return value. |
| 366 | void GetNameForMethod(const ObjCMethodDecl *OMD, |
| 367 | const ObjCContainerDecl *CD, |
| 368 | std::string &NameOut); |
| 369 | |
| 370 | /// GetMethodVarName - Return a unique constant for the given |
| 371 | /// selector's name. The return value has type char *. |
| 372 | llvm::Constant *GetMethodVarName(Selector Sel); |
| 373 | llvm::Constant *GetMethodVarName(IdentifierInfo *Ident); |
| 374 | llvm::Constant *GetMethodVarName(const std::string &Name); |
| 375 | |
| 376 | /// GetMethodVarType - Return a unique constant for the given |
| 377 | /// selector's name. The return value has type char *. |
| 378 | |
| 379 | // FIXME: This is a horrible name. |
| 380 | llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D); |
| 381 | llvm::Constant *GetMethodVarType(const std::string &Name); |
| 382 | |
| 383 | /// GetPropertyName - Return a unique constant for the given |
| 384 | /// name. The return value has type char *. |
| 385 | llvm::Constant *GetPropertyName(IdentifierInfo *Ident); |
| 386 | |
| 387 | // FIXME: This can be dropped once string functions are unified. |
| 388 | llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 389 | const Decl *Container); |
| 390 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 391 | /// GetClassName - Return a unique constant for the given selector's |
| 392 | /// name. The return value has type char *. |
| 393 | llvm::Constant *GetClassName(IdentifierInfo *Ident); |
| 394 | |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 395 | const RecordDecl *GetFirstIvarInRecord(const ObjCInterfaceDecl *OID, |
| 396 | RecordDecl::field_iterator &FIV, |
| 397 | RecordDecl::field_iterator &PIV); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 398 | /// EmitPropertyList - Emit the given property list. The return |
| 399 | /// value has type PropertyListPtrTy. |
| 400 | llvm::Constant *EmitPropertyList(const std::string &Name, |
| 401 | const Decl *Container, |
| 402 | const ObjCContainerDecl *OCD, |
| 403 | const ObjCCommonTypesHelper &ObjCTypes); |
| 404 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 405 | /// GetProtocolRef - Return a reference to the internal protocol |
| 406 | /// description, creating an empty one if it has not been |
| 407 | /// defined. The return value has type ProtocolPtrTy. |
| 408 | llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD); |
| 409 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 410 | public: |
| 411 | CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm) |
| 412 | { } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 413 | |
| 414 | virtual llvm::Constant *GenerateConstantString(const std::string &String); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 415 | |
| 416 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
| 417 | const ObjCContainerDecl *CD=0); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 418 | |
| 419 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); |
| 420 | |
| 421 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 422 | /// declaration, emitting it if necessary. The return value has type |
| 423 | /// ProtocolPtrTy. |
| 424 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0; |
| 425 | |
| 426 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 427 | /// object for the given declaration, emitting it if needed. These |
| 428 | /// forward references will be filled in with empty bodies if no |
| 429 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 430 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 431 | }; |
| 432 | |
| 433 | class CGObjCMac : public CGObjCCommonMac { |
| 434 | private: |
| 435 | ObjCTypesHelper ObjCTypes; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 436 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 437 | /// level information. |
| 438 | void EmitImageInfo(); |
| 439 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 440 | /// EmitModuleInfo - Another marker encoding module level |
| 441 | /// information. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 442 | void EmitModuleInfo(); |
| 443 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 444 | /// EmitModuleSymols - Emit module symbols, the list of defined |
| 445 | /// classes and categories. The result has type SymtabPtrTy. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 446 | llvm::Constant *EmitModuleSymbols(); |
| 447 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 448 | /// FinishModule - Write out global data structures at the end of |
| 449 | /// processing a translation unit. |
| 450 | void FinishModule(); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 451 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 452 | /// EmitClassExtension - Generate the class extension structure used |
| 453 | /// to store the weak ivar layout and properties. The return value |
| 454 | /// has type ClassExtensionPtrTy. |
| 455 | llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID); |
| 456 | |
| 457 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 458 | /// for the given class. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 459 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 460 | const ObjCInterfaceDecl *ID); |
| 461 | |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 462 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 463 | QualType ResultType, |
| 464 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 465 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 466 | QualType Arg0Ty, |
| 467 | bool IsSuper, |
| 468 | const CallArgList &CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 469 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 470 | /// EmitIvarList - Emit the ivar list for the given |
| 471 | /// implementation. If ForClass is true the list of class ivars |
| 472 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 473 | /// interface ivars will be emitted. The return value has type |
| 474 | /// IvarListPtrTy. |
| 475 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 476 | bool ForClass); |
| 477 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 478 | /// EmitMetaClass - Emit a forward reference to the class structure |
| 479 | /// for the metaclass of the given interface. The return value has |
| 480 | /// type ClassPtrTy. |
| 481 | llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID); |
| 482 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 483 | /// EmitMetaClass - Emit a class structure for the metaclass of the |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 484 | /// given implementation. The return value has type ClassPtrTy. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 485 | llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID, |
| 486 | llvm::Constant *Protocols, |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 487 | const llvm::Type *InterfaceTy, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 488 | const ConstantVector &Methods); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 489 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 490 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 491 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 492 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 493 | |
| 494 | /// EmitMethodList - Emit the method list for the given |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 495 | /// implementation. The return value has type MethodListPtrTy. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 496 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 497 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 498 | const ConstantVector &Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 499 | |
| 500 | /// EmitMethodDescList - Emit a method description list for a list of |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 501 | /// method declarations. |
| 502 | /// - TypeName: The name for the type containing the methods. |
| 503 | /// - IsProtocol: True iff these methods are for a protocol. |
| 504 | /// - ClassMethds: True iff these are class methods. |
| 505 | /// - Required: When true, only "required" methods are |
| 506 | /// listed. Similarly, when false only "optional" methods are |
| 507 | /// listed. For classes this should always be true. |
| 508 | /// - begin, end: The method list to output. |
| 509 | /// |
| 510 | /// The return value has type MethodDescriptionListPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 511 | llvm::Constant *EmitMethodDescList(const std::string &Name, |
| 512 | const char *Section, |
| 513 | const ConstantVector &Methods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 514 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 515 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 516 | /// declaration, emitting it if necessary. The return value has type |
| 517 | /// ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 518 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 519 | |
| 520 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 521 | /// object for the given declaration, emitting it if needed. These |
| 522 | /// forward references will be filled in with empty bodies if no |
| 523 | /// definition is seen. The return value has type ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 524 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 525 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 526 | /// EmitProtocolExtension - Generate the protocol extension |
| 527 | /// structure used to store optional instance and class methods, and |
| 528 | /// protocol properties. The return value has type |
| 529 | /// ProtocolExtensionPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 530 | llvm::Constant * |
| 531 | EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 532 | const ConstantVector &OptInstanceMethods, |
| 533 | const ConstantVector &OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 534 | |
| 535 | /// EmitProtocolList - Generate the list of referenced |
| 536 | /// protocols. The return value has type ProtocolListPtrTy. |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 537 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 538 | ObjCProtocolDecl::protocol_iterator begin, |
| 539 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 540 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 541 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 542 | /// for the given selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 543 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 544 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 545 | public: |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 546 | CGObjCMac(CodeGen::CodeGenModule &cgm); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 547 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 548 | virtual llvm::Function *ModuleInitFunction(); |
| 549 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 550 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 551 | QualType ResultType, |
| 552 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 553 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 554 | bool IsClassMessage, |
| 555 | const CallArgList &CallArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 556 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 557 | virtual CodeGen::RValue |
| 558 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 559 | QualType ResultType, |
| 560 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 561 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame^] | 562 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 563 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 564 | bool IsClassMessage, |
| 565 | const CallArgList &CallArgs); |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 566 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 567 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 568 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 569 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 570 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 571 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 572 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 573 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 574 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 575 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 576 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 577 | const ObjCProtocolDecl *PD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 578 | |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 579 | virtual llvm::Function *GetPropertyGetFunction(); |
| 580 | virtual llvm::Function *GetPropertySetFunction(); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 581 | virtual llvm::Function *EnumerationMutationFunction(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 582 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 583 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 584 | const Stmt &S); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 585 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 586 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 587 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 588 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 589 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 590 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 591 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 592 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 593 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 594 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 595 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 596 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 597 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 598 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 599 | QualType ObjectTy, |
| 600 | llvm::Value *BaseValue, |
| 601 | const ObjCIvarDecl *Ivar, |
| 602 | const FieldDecl *Field, |
| 603 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 604 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
| 605 | ObjCInterfaceDecl *Interface, |
| 606 | const ObjCIvarDecl *Ivar); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 607 | }; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 608 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 609 | class CGObjCNonFragileABIMac : public CGObjCCommonMac { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 610 | private: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 611 | ObjCNonFragileABITypesHelper ObjCTypes; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 612 | llvm::GlobalVariable* ObjCEmptyCacheVar; |
| 613 | llvm::GlobalVariable* ObjCEmptyVtableVar; |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 614 | /// MetaClassReferences - uniqued meta class references. |
| 615 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 616 | |
| 617 | /// FinishNonFragileABIModule - Write out global data structures at the end of |
| 618 | /// processing a translation unit. |
| 619 | void FinishNonFragileABIModule(); |
| 620 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 621 | llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags, |
| 622 | unsigned InstanceStart, |
| 623 | unsigned InstanceSize, |
| 624 | const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 625 | llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName, |
| 626 | llvm::Constant *IsAGV, |
| 627 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 628 | llvm::Constant *ClassRoGV, |
| 629 | bool HiddenVisibility); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 630 | |
| 631 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
| 632 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 633 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
| 634 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 635 | /// EmitMethodList - Emit the method list for the given |
| 636 | /// implementation. The return value has type MethodListnfABITy. |
| 637 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 638 | const char *Section, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 639 | const ConstantVector &Methods); |
| 640 | /// EmitIvarList - Emit the ivar list for the given |
| 641 | /// implementation. If ForClass is true the list of class ivars |
| 642 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 643 | /// interface ivars will be emitted. The return value has type |
| 644 | /// IvarListnfABIPtrTy. |
| 645 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 646 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 647 | llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 648 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 649 | unsigned long int offset); |
| 650 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 651 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 652 | /// declaration, emitting it if necessary. The return value has type |
| 653 | /// ProtocolPtrTy. |
| 654 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
| 655 | |
| 656 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 657 | /// object for the given declaration, emitting it if needed. These |
| 658 | /// forward references will be filled in with empty bodies if no |
| 659 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 660 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
| 661 | |
| 662 | /// EmitProtocolList - Generate the list of referenced |
| 663 | /// protocols. The return value has type ProtocolListPtrTy. |
| 664 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 665 | ObjCProtocolDecl::protocol_iterator begin, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 666 | ObjCProtocolDecl::protocol_iterator end); |
| 667 | |
| 668 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 669 | QualType ResultType, |
| 670 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 671 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 672 | QualType Arg0Ty, |
| 673 | bool IsSuper, |
| 674 | const CallArgList &CallArgs); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 675 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 676 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 677 | /// for the given class. |
| 678 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 679 | const ObjCInterfaceDecl *ID, |
| 680 | bool IsSuper = false); |
| 681 | |
| 682 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 683 | /// meta-data |
| 684 | llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder, |
| 685 | const ObjCInterfaceDecl *ID); |
| 686 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 687 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 688 | /// the given ivar. |
| 689 | /// |
| 690 | llvm::GlobalVariable * ObjCIvarOffsetVariable(std::string &Name, |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 691 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 692 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 693 | |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 694 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 695 | /// for the given selector. |
| 696 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
| 697 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 698 | public: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 699 | CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 700 | // FIXME. All stubs for now! |
| 701 | virtual llvm::Function *ModuleInitFunction(); |
| 702 | |
| 703 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
| 704 | QualType ResultType, |
| 705 | Selector Sel, |
| 706 | llvm::Value *Receiver, |
| 707 | bool IsClassMessage, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 708 | const CallArgList &CallArgs); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 709 | |
| 710 | virtual CodeGen::RValue |
| 711 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 712 | QualType ResultType, |
| 713 | Selector Sel, |
| 714 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame^] | 715 | bool isCategoryImpl, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 716 | llvm::Value *Receiver, |
| 717 | bool IsClassMessage, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 718 | const CallArgList &CallArgs); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 719 | |
| 720 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 721 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 722 | |
| 723 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel) |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 724 | { return EmitSelector(Builder, Sel); } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 725 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 726 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 727 | |
| 728 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 729 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 730 | const ObjCProtocolDecl *PD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 731 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 732 | virtual llvm::Function *GetPropertyGetFunction(){ |
| 733 | return ObjCTypes.GetPropertyFn; |
| 734 | } |
| 735 | virtual llvm::Function *GetPropertySetFunction(){ |
| 736 | return ObjCTypes.SetPropertyFn; |
| 737 | } |
Daniel Dunbar | 28ed084 | 2009-02-16 18:48:45 +0000 | [diff] [blame] | 738 | virtual llvm::Function *EnumerationMutationFunction() { |
| 739 | return ObjCTypes.EnumerationMutationFn; |
| 740 | } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 741 | |
| 742 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 743 | const Stmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 744 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 745 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 746 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 747 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 748 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 749 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 750 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 751 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 752 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 753 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 754 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 755 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 756 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 757 | QualType ObjectTy, |
| 758 | llvm::Value *BaseValue, |
| 759 | const ObjCIvarDecl *Ivar, |
| 760 | const FieldDecl *Field, |
| 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); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 765 | }; |
| 766 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 767 | } // end anonymous namespace |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 768 | |
| 769 | /* *** Helper Functions *** */ |
| 770 | |
| 771 | /// getConstantGEP() - Help routine to construct simple GEPs. |
| 772 | static llvm::Constant *getConstantGEP(llvm::Constant *C, |
| 773 | unsigned idx0, |
| 774 | unsigned idx1) { |
| 775 | llvm::Value *Idxs[] = { |
| 776 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0), |
| 777 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1) |
| 778 | }; |
| 779 | return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2); |
| 780 | } |
| 781 | |
| 782 | /* *** CGObjCMac Public Interface *** */ |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 783 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 784 | CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm), |
| 785 | ObjCTypes(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 786 | { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 787 | ObjCABI = 1; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 788 | EmitImageInfo(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 791 | /// GetClass - Return a reference to the class for the given interface |
| 792 | /// decl. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 793 | llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 794 | const ObjCInterfaceDecl *ID) { |
| 795 | return EmitClassRef(Builder, ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | /// GetSelector - Return the pointer to the unique'd string for this selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 799 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 800 | return EmitSelector(Builder, Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 803 | /// Generate a constant CFString object. |
| 804 | /* |
| 805 | struct __builtin_CFString { |
| 806 | const int *isa; // point to __CFConstantStringClassReference |
| 807 | int flags; |
| 808 | const char *str; |
| 809 | long length; |
| 810 | }; |
| 811 | */ |
| 812 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 813 | llvm::Constant *CGObjCCommonMac::GenerateConstantString( |
| 814 | const std::string &String) { |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 815 | return CGM.GetAddrOfConstantCFString(String); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | /// Generates a message send where the super is the receiver. This is |
| 819 | /// a message send to self with special delivery semantics indicating |
| 820 | /// which class's method should be called. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 821 | CodeGen::RValue |
| 822 | CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 823 | QualType ResultType, |
| 824 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 825 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame^] | 826 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 827 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 828 | bool IsClassMessage, |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 829 | const CodeGen::CallArgList &CallArgs) { |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 830 | // Create and init a super structure; this is a (receiver, class) |
| 831 | // pair we will pass to objc_msgSendSuper. |
| 832 | llvm::Value *ObjCSuper = |
| 833 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 834 | llvm::Value *ReceiverAsObject = |
| 835 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 836 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 837 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 838 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 839 | // If this is a class message the metaclass is passed as the target. |
| 840 | llvm::Value *Target; |
| 841 | if (IsClassMessage) { |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame^] | 842 | if (isCategoryImpl) { |
| 843 | // Message sent to 'super' in a class method defined in a category |
| 844 | // implementation requires an odd treatment. |
| 845 | // If we are in a class method, we must retrieve the |
| 846 | // _metaclass_ for the current class, pointed at by |
| 847 | // the class's "isa" pointer. The following assumes that |
| 848 | // isa" is the first ivar in a class (which it must be). |
| 849 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 850 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 851 | Target = CGF.Builder.CreateLoad(Target); |
| 852 | } |
| 853 | else { |
| 854 | llvm::Value *MetaClassPtr = EmitMetaClassRef(Class); |
| 855 | llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1); |
| 856 | llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr); |
| 857 | Target = Super; |
| 858 | } |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 859 | } else { |
| 860 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 861 | } |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 862 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext |
| 863 | // and ObjCTypes types. |
| 864 | const llvm::Type *ClassTy = |
| 865 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 866 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 867 | CGF.Builder.CreateStore(Target, |
| 868 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
| 869 | |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 870 | return EmitMessageSend(CGF, ResultType, Sel, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 871 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 872 | true, CallArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 873 | } |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 874 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 875 | /// Generate code for a message send expression. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 876 | CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 877 | QualType ResultType, |
| 878 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 879 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 880 | bool IsClassMessage, |
| 881 | const CallArgList &CallArgs) { |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 882 | llvm::Value *Arg0 = |
| 883 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp"); |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 884 | return EmitMessageSend(CGF, ResultType, Sel, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 885 | Arg0, CGF.getContext().getObjCIdType(), |
| 886 | false, CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 890 | QualType ResultType, |
| 891 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 892 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 893 | QualType Arg0Ty, |
| 894 | bool IsSuper, |
| 895 | const CallArgList &CallArgs) { |
| 896 | CallArgList ActualArgs; |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 897 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
| 898 | ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder, |
| 899 | Sel)), |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 900 | CGF.getContext().getObjCSelType())); |
| 901 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 902 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 903 | CodeGenTypes &Types = CGM.getTypes(); |
| 904 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs); |
| 905 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 906 | |
| 907 | llvm::Constant *Fn; |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 908 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 909 | Fn = ObjCTypes.getSendStretFn(IsSuper); |
| 910 | } else if (ResultType->isFloatingType()) { |
| 911 | // FIXME: Sadly, this is wrong. This actually depends on the |
| 912 | // architecture. This happens to be right for x86-32 though. |
| 913 | Fn = ObjCTypes.getSendFpretFn(IsSuper); |
| 914 | } else { |
| 915 | Fn = ObjCTypes.getSendFn(IsSuper); |
| 916 | } |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 917 | Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy)); |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 918 | return CGF.EmitCall(FnInfo, Fn, ActualArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 919 | } |
| 920 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 921 | llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 922 | const ObjCProtocolDecl *PD) { |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 923 | // FIXME: I don't understand why gcc generates this, or where it is |
| 924 | // resolved. Investigate. Its also wasteful to look this up over and |
| 925 | // over. |
| 926 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 927 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 928 | return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), |
| 929 | ObjCTypes.ExternalProtocolPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 930 | } |
| 931 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 932 | void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 933 | // FIXME: We shouldn't need this, the protocol decl should contain |
| 934 | // enough information to tell us whether this was a declaration or a |
| 935 | // definition. |
| 936 | DefinedProtocols.insert(PD->getIdentifier()); |
| 937 | |
| 938 | // If we have generated a forward reference to this protocol, emit |
| 939 | // it now. Otherwise do nothing, the protocol objects are lazily |
| 940 | // emitted. |
| 941 | if (Protocols.count(PD->getIdentifier())) |
| 942 | GetOrEmitProtocol(PD); |
| 943 | } |
| 944 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 945 | llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 946 | if (DefinedProtocols.count(PD->getIdentifier())) |
| 947 | return GetOrEmitProtocol(PD); |
| 948 | return GetOrEmitProtocolRef(PD); |
| 949 | } |
| 950 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 951 | /* |
| 952 | // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions |
| 953 | struct _objc_protocol { |
| 954 | struct _objc_protocol_extension *isa; |
| 955 | char *protocol_name; |
| 956 | struct _objc_protocol_list *protocol_list; |
| 957 | struct _objc__method_prototype_list *instance_methods; |
| 958 | struct _objc__method_prototype_list *class_methods |
| 959 | }; |
| 960 | |
| 961 | See EmitProtocolExtension(). |
| 962 | */ |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 963 | llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) { |
| 964 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 965 | |
| 966 | // Early exit if a defining object has already been generated. |
| 967 | if (Entry && Entry->hasInitializer()) |
| 968 | return Entry; |
| 969 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 970 | // FIXME: I don't understand why gcc generates this, or where it is |
| 971 | // resolved. Investigate. Its also wasteful to look this up over and |
| 972 | // over. |
| 973 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 974 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 975 | const char *ProtocolName = PD->getNameAsCString(); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 976 | |
| 977 | // Construct method lists. |
| 978 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 979 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
| 980 | for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(), |
| 981 | e = PD->instmeth_end(); i != e; ++i) { |
| 982 | ObjCMethodDecl *MD = *i; |
| 983 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 984 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 985 | OptInstanceMethods.push_back(C); |
| 986 | } else { |
| 987 | InstanceMethods.push_back(C); |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(), |
| 992 | e = PD->classmeth_end(); i != e; ++i) { |
| 993 | ObjCMethodDecl *MD = *i; |
| 994 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 995 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 996 | OptClassMethods.push_back(C); |
| 997 | } else { |
| 998 | ClassMethods.push_back(C); |
| 999 | } |
| 1000 | } |
| 1001 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1002 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1003 | Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1004 | Values[1] = GetClassName(PD->getIdentifier()); |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1005 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1006 | EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1007 | PD->protocol_begin(), |
| 1008 | PD->protocol_end()); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1009 | Values[3] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1010 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" |
| 1011 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1012 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1013 | InstanceMethods); |
| 1014 | Values[4] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1015 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" |
| 1016 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1017 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1018 | ClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1019 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 1020 | Values); |
| 1021 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1022 | if (Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1023 | // Already created, fix the linkage and update the initializer. |
| 1024 | Entry->setLinkage(llvm::GlobalValue::InternalLinkage); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1025 | Entry->setInitializer(Init); |
| 1026 | } else { |
| 1027 | Entry = |
| 1028 | new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, |
| 1029 | llvm::GlobalValue::InternalLinkage, |
| 1030 | Init, |
| 1031 | std::string("\01L_OBJC_PROTOCOL_")+ProtocolName, |
| 1032 | &CGM.getModule()); |
| 1033 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
| 1034 | UsedGlobals.push_back(Entry); |
| 1035 | // FIXME: Is this necessary? Why only for protocol? |
| 1036 | Entry->setAlignment(4); |
| 1037 | } |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1038 | |
| 1039 | return Entry; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1042 | llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1043 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1044 | |
| 1045 | if (!Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1046 | // We use the initializer as a marker of whether this is a forward |
| 1047 | // reference or not. At module finalization we add the empty |
| 1048 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1049 | Entry = |
| 1050 | new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1051 | llvm::GlobalValue::ExternalLinkage, |
| 1052 | 0, |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1053 | "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1054 | &CGM.getModule()); |
| 1055 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
| 1056 | UsedGlobals.push_back(Entry); |
| 1057 | // FIXME: Is this necessary? Why only for protocol? |
| 1058 | Entry->setAlignment(4); |
| 1059 | } |
| 1060 | |
| 1061 | return Entry; |
| 1062 | } |
| 1063 | |
| 1064 | /* |
| 1065 | struct _objc_protocol_extension { |
| 1066 | uint32_t size; |
| 1067 | struct objc_method_description_list *optional_instance_methods; |
| 1068 | struct objc_method_description_list *optional_class_methods; |
| 1069 | struct objc_property_list *instance_properties; |
| 1070 | }; |
| 1071 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1072 | llvm::Constant * |
| 1073 | CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 1074 | const ConstantVector &OptInstanceMethods, |
| 1075 | const ConstantVector &OptClassMethods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1076 | uint64_t Size = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1077 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1078 | std::vector<llvm::Constant*> Values(4); |
| 1079 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1080 | Values[1] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1081 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_" |
| 1082 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1083 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1084 | OptInstanceMethods); |
| 1085 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1086 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" |
| 1087 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1088 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1089 | OptClassMethods); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1090 | Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + |
| 1091 | PD->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1092 | 0, PD, ObjCTypes); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1093 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1094 | // Return null if no extension bits are used. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1095 | if (Values[1]->isNullValue() && Values[2]->isNullValue() && |
| 1096 | Values[3]->isNullValue()) |
| 1097 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
| 1098 | |
| 1099 | llvm::Constant *Init = |
| 1100 | llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values); |
| 1101 | llvm::GlobalVariable *GV = |
| 1102 | new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false, |
| 1103 | llvm::GlobalValue::InternalLinkage, |
| 1104 | Init, |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1105 | "\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1106 | &CGM.getModule()); |
| 1107 | // No special section, but goes in llvm.used |
| 1108 | UsedGlobals.push_back(GV); |
| 1109 | |
| 1110 | return GV; |
| 1111 | } |
| 1112 | |
| 1113 | /* |
| 1114 | struct objc_protocol_list { |
| 1115 | struct objc_protocol_list *next; |
| 1116 | long count; |
| 1117 | Protocol *list[]; |
| 1118 | }; |
| 1119 | */ |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1120 | llvm::Constant * |
| 1121 | CGObjCMac::EmitProtocolList(const std::string &Name, |
| 1122 | ObjCProtocolDecl::protocol_iterator begin, |
| 1123 | ObjCProtocolDecl::protocol_iterator end) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1124 | std::vector<llvm::Constant*> ProtocolRefs; |
| 1125 | |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1126 | for (; begin != end; ++begin) |
| 1127 | ProtocolRefs.push_back(GetProtocolRef(*begin)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1128 | |
| 1129 | // Just return null for empty protocol lists |
| 1130 | if (ProtocolRefs.empty()) |
| 1131 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1132 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1133 | // This list is null terminated. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1134 | ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy)); |
| 1135 | |
| 1136 | std::vector<llvm::Constant*> Values(3); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1137 | // This field is only used by the runtime. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1138 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1139 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
| 1140 | Values[2] = |
| 1141 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy, |
| 1142 | ProtocolRefs.size()), |
| 1143 | ProtocolRefs); |
| 1144 | |
| 1145 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1146 | llvm::GlobalVariable *GV = |
| 1147 | new llvm::GlobalVariable(Init->getType(), false, |
| 1148 | llvm::GlobalValue::InternalLinkage, |
| 1149 | Init, |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1150 | Name, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1151 | &CGM.getModule()); |
| 1152 | GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip"); |
| 1153 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy); |
| 1154 | } |
| 1155 | |
| 1156 | /* |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1157 | struct _objc_property { |
| 1158 | const char * const name; |
| 1159 | const char * const attributes; |
| 1160 | }; |
| 1161 | |
| 1162 | struct _objc_property_list { |
| 1163 | uint32_t entsize; // sizeof (struct _objc_property) |
| 1164 | uint32_t prop_count; |
| 1165 | struct _objc_property[prop_count]; |
| 1166 | }; |
| 1167 | */ |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1168 | llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name, |
| 1169 | const Decl *Container, |
| 1170 | const ObjCContainerDecl *OCD, |
| 1171 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1172 | std::vector<llvm::Constant*> Properties, Prop(2); |
Steve Naroff | 93983f8 | 2009-01-11 12:47:58 +0000 | [diff] [blame] | 1173 | for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(), |
| 1174 | E = OCD->prop_end(); I != E; ++I) { |
| 1175 | const ObjCPropertyDecl *PD = *I; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1176 | Prop[0] = GetPropertyName(PD->getIdentifier()); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1177 | Prop[1] = GetPropertyTypeString(PD, Container); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1178 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, |
| 1179 | Prop)); |
| 1180 | } |
| 1181 | |
| 1182 | // Return null for empty list. |
| 1183 | if (Properties.empty()) |
| 1184 | return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 1185 | |
| 1186 | unsigned PropertySize = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1187 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1188 | std::vector<llvm::Constant*> Values(3); |
| 1189 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize); |
| 1190 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size()); |
| 1191 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy, |
| 1192 | Properties.size()); |
| 1193 | Values[2] = llvm::ConstantArray::get(AT, Properties); |
| 1194 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1195 | |
| 1196 | llvm::GlobalVariable *GV = |
| 1197 | new llvm::GlobalVariable(Init->getType(), false, |
| 1198 | llvm::GlobalValue::InternalLinkage, |
| 1199 | Init, |
| 1200 | Name, |
| 1201 | &CGM.getModule()); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1202 | if (ObjCABI == 2) |
| 1203 | GV->setSection("__DATA, __objc_const"); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1204 | // No special section on property lists? |
| 1205 | UsedGlobals.push_back(GV); |
| 1206 | return llvm::ConstantExpr::getBitCast(GV, |
| 1207 | ObjCTypes.PropertyListPtrTy); |
| 1208 | |
| 1209 | } |
| 1210 | |
| 1211 | /* |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1212 | struct objc_method_description_list { |
| 1213 | int count; |
| 1214 | struct objc_method_description list[]; |
| 1215 | }; |
| 1216 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1217 | llvm::Constant * |
| 1218 | CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 1219 | std::vector<llvm::Constant*> Desc(2); |
| 1220 | Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 1221 | ObjCTypes.SelectorPtrTy); |
| 1222 | Desc[1] = GetMethodVarType(MD); |
| 1223 | return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy, |
| 1224 | Desc); |
| 1225 | } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1226 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1227 | llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name, |
| 1228 | const char *Section, |
| 1229 | const ConstantVector &Methods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1230 | // Return null for empty list. |
| 1231 | if (Methods.empty()) |
| 1232 | return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
| 1233 | |
| 1234 | std::vector<llvm::Constant*> Values(2); |
| 1235 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 1236 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy, |
| 1237 | Methods.size()); |
| 1238 | Values[1] = llvm::ConstantArray::get(AT, Methods); |
| 1239 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1240 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1241 | llvm::GlobalVariable *GV = |
| 1242 | new llvm::GlobalVariable(Init->getType(), false, |
| 1243 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1244 | Init, Name, &CGM.getModule()); |
| 1245 | GV->setSection(Section); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1246 | UsedGlobals.push_back(GV); |
| 1247 | return llvm::ConstantExpr::getBitCast(GV, |
| 1248 | ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1251 | /* |
| 1252 | struct _objc_category { |
| 1253 | char *category_name; |
| 1254 | char *class_name; |
| 1255 | struct _objc_method_list *instance_methods; |
| 1256 | struct _objc_method_list *class_methods; |
| 1257 | struct _objc_protocol_list *protocols; |
| 1258 | uint32_t size; // <rdar://4585769> |
| 1259 | struct _objc_property_list *instance_properties; |
| 1260 | }; |
| 1261 | */ |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1262 | void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1263 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1264 | |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1265 | // FIXME: This is poor design, the OCD should have a pointer to the |
| 1266 | // category decl. Additionally, note that Category can be null for |
| 1267 | // the @implementation w/o an @interface case. Sema should just |
| 1268 | // create one for us as it does for @implementation so everyone else |
| 1269 | // can live life under a clear blue sky. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1270 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1271 | const ObjCCategoryDecl *Category = |
| 1272 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1273 | std::string ExtName(Interface->getNameAsString() + "_" + |
| 1274 | OCD->getNameAsString()); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1275 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1276 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1277 | for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(), |
| 1278 | e = OCD->instmeth_end(); i != e; ++i) { |
| 1279 | // Instance methods should always be defined. |
| 1280 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1281 | } |
| 1282 | for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(), |
| 1283 | e = OCD->classmeth_end(); i != e; ++i) { |
| 1284 | // Class methods should always be defined. |
| 1285 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1286 | } |
| 1287 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1288 | std::vector<llvm::Constant*> Values(7); |
| 1289 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 1290 | Values[1] = GetClassName(Interface->getIdentifier()); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1291 | Values[2] = |
| 1292 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") + |
| 1293 | ExtName, |
| 1294 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1295 | InstanceMethods); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1296 | Values[3] = |
| 1297 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName, |
| 1298 | "__OBJC,__cat_class_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1299 | ClassMethods); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1300 | if (Category) { |
| 1301 | Values[4] = |
| 1302 | EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName, |
| 1303 | Category->protocol_begin(), |
| 1304 | Category->protocol_end()); |
| 1305 | } else { |
| 1306 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1307 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1308 | Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1309 | |
| 1310 | // If there is no category @interface then there can be no properties. |
| 1311 | if (Category) { |
| 1312 | Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName, |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1313 | OCD, Category, ObjCTypes); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1314 | } else { |
| 1315 | Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 1316 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1317 | |
| 1318 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy, |
| 1319 | Values); |
| 1320 | |
| 1321 | llvm::GlobalVariable *GV = |
| 1322 | new llvm::GlobalVariable(ObjCTypes.CategoryTy, false, |
| 1323 | llvm::GlobalValue::InternalLinkage, |
| 1324 | Init, |
| 1325 | std::string("\01L_OBJC_CATEGORY_")+ExtName, |
| 1326 | &CGM.getModule()); |
| 1327 | GV->setSection("__OBJC,__category,regular,no_dead_strip"); |
| 1328 | UsedGlobals.push_back(GV); |
| 1329 | DefinedCategories.push_back(GV); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1332 | // FIXME: Get from somewhere? |
| 1333 | enum ClassFlags { |
| 1334 | eClassFlags_Factory = 0x00001, |
| 1335 | eClassFlags_Meta = 0x00002, |
| 1336 | // <rdr://5142207> |
| 1337 | eClassFlags_HasCXXStructors = 0x02000, |
| 1338 | eClassFlags_Hidden = 0x20000, |
| 1339 | eClassFlags_ABI2_Hidden = 0x00010, |
| 1340 | eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634> |
| 1341 | }; |
| 1342 | |
| 1343 | // <rdr://5142207&4705298&4843145> |
| 1344 | static bool IsClassHidden(const ObjCInterfaceDecl *ID) { |
| 1345 | if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) { |
| 1346 | // FIXME: Support -fvisibility |
| 1347 | switch (attr->getVisibility()) { |
| 1348 | default: |
| 1349 | assert(0 && "Unknown visibility"); |
| 1350 | return false; |
| 1351 | case VisibilityAttr::DefaultVisibility: |
| 1352 | case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here? |
| 1353 | return false; |
| 1354 | case VisibilityAttr::HiddenVisibility: |
| 1355 | return true; |
| 1356 | } |
| 1357 | } else { |
| 1358 | return false; // FIXME: Support -fvisibility |
| 1359 | } |
| 1360 | } |
| 1361 | |
| 1362 | /* |
| 1363 | struct _objc_class { |
| 1364 | Class isa; |
| 1365 | Class super_class; |
| 1366 | const char *name; |
| 1367 | long version; |
| 1368 | long info; |
| 1369 | long instance_size; |
| 1370 | struct _objc_ivar_list *ivars; |
| 1371 | struct _objc_method_list *methods; |
| 1372 | struct _objc_cache *cache; |
| 1373 | struct _objc_protocol_list *protocols; |
| 1374 | // Objective-C 1.0 extensions (<rdr://4585769>) |
| 1375 | const char *ivar_layout; |
| 1376 | struct _objc_class_ext *ext; |
| 1377 | }; |
| 1378 | |
| 1379 | See EmitClassExtension(); |
| 1380 | */ |
| 1381 | void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1382 | DefinedSymbols.insert(ID->getIdentifier()); |
| 1383 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1384 | std::string ClassName = ID->getNameAsString(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1385 | // FIXME: Gross |
| 1386 | ObjCInterfaceDecl *Interface = |
| 1387 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1388 | llvm::Constant *Protocols = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1389 | EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1390 | Interface->protocol_begin(), |
| 1391 | Interface->protocol_end()); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1392 | const llvm::Type *InterfaceTy = |
Fariborz Jahanian | f3710ba | 2009-02-14 20:13:28 +0000 | [diff] [blame] | 1393 | CGM.getTypes().ConvertType(CGM.getContext().buildObjCInterfaceType(Interface)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1394 | unsigned Flags = eClassFlags_Factory; |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1395 | unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1396 | |
| 1397 | // FIXME: Set CXX-structors flag. |
| 1398 | if (IsClassHidden(ID->getClassInterface())) |
| 1399 | Flags |= eClassFlags_Hidden; |
| 1400 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1401 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1402 | for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(), |
| 1403 | e = ID->instmeth_end(); i != e; ++i) { |
| 1404 | // Instance methods should always be defined. |
| 1405 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1406 | } |
| 1407 | for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(), |
| 1408 | e = ID->classmeth_end(); i != e; ++i) { |
| 1409 | // Class methods should always be defined. |
| 1410 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1411 | } |
| 1412 | |
| 1413 | for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(), |
| 1414 | e = ID->propimpl_end(); i != e; ++i) { |
| 1415 | ObjCPropertyImplDecl *PID = *i; |
| 1416 | |
| 1417 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 1418 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 1419 | |
| 1420 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 1421 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 1422 | InstanceMethods.push_back(C); |
| 1423 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 1424 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 1425 | InstanceMethods.push_back(C); |
| 1426 | } |
| 1427 | } |
| 1428 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1429 | std::vector<llvm::Constant*> Values(12); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1430 | Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1431 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1432 | // Record a reference to the super class. |
| 1433 | LazySymbols.insert(Super->getIdentifier()); |
| 1434 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1435 | Values[ 1] = |
| 1436 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 1437 | ObjCTypes.ClassPtrTy); |
| 1438 | } else { |
| 1439 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
| 1440 | } |
| 1441 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 1442 | // Version is always 0. |
| 1443 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 1444 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 1445 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1446 | Values[ 6] = EmitIvarList(ID, false); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1447 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1448 | EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1449 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1450 | InstanceMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1451 | // cache is always NULL. |
| 1452 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
| 1453 | Values[ 9] = Protocols; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1454 | // FIXME: Set ivar_layout |
| 1455 | Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1456 | Values[11] = EmitClassExtension(ID); |
| 1457 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 1458 | Values); |
| 1459 | |
| 1460 | llvm::GlobalVariable *GV = |
| 1461 | new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 1462 | llvm::GlobalValue::InternalLinkage, |
| 1463 | Init, |
| 1464 | std::string("\01L_OBJC_CLASS_")+ClassName, |
| 1465 | &CGM.getModule()); |
| 1466 | GV->setSection("__OBJC,__class,regular,no_dead_strip"); |
| 1467 | UsedGlobals.push_back(GV); |
| 1468 | // FIXME: Why? |
| 1469 | GV->setAlignment(32); |
| 1470 | DefinedClasses.push_back(GV); |
| 1471 | } |
| 1472 | |
| 1473 | llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID, |
| 1474 | llvm::Constant *Protocols, |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1475 | const llvm::Type *InterfaceTy, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1476 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1477 | unsigned Flags = eClassFlags_Meta; |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1478 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1479 | |
| 1480 | if (IsClassHidden(ID->getClassInterface())) |
| 1481 | Flags |= eClassFlags_Hidden; |
| 1482 | |
| 1483 | std::vector<llvm::Constant*> Values(12); |
| 1484 | // The isa for the metaclass is the root of the hierarchy. |
| 1485 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 1486 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 1487 | Root = Super; |
| 1488 | Values[ 0] = |
| 1489 | llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()), |
| 1490 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1491 | // The super class for the metaclass is emitted as the name of the |
| 1492 | // super class. The runtime fixes this up to point to the |
| 1493 | // *metaclass* for the super class. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1494 | if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) { |
| 1495 | Values[ 1] = |
| 1496 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 1497 | ObjCTypes.ClassPtrTy); |
| 1498 | } else { |
| 1499 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
| 1500 | } |
| 1501 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 1502 | // Version is always 0. |
| 1503 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 1504 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 1505 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1506 | Values[ 6] = EmitIvarList(ID, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1507 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1508 | EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1509 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1510 | Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1511 | // cache is always NULL. |
| 1512 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
| 1513 | Values[ 9] = Protocols; |
| 1514 | // ivar_layout for metaclass is always NULL. |
| 1515 | Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 1516 | // The class extension is always unused for metaclasses. |
| 1517 | Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
| 1518 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 1519 | Values); |
| 1520 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1521 | std::string Name("\01L_OBJC_METACLASS_"); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1522 | Name += ID->getNameAsCString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1523 | |
| 1524 | // Check for a forward reference. |
| 1525 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 1526 | if (GV) { |
| 1527 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 1528 | "Forward metaclass reference has incorrect type."); |
| 1529 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 1530 | GV->setInitializer(Init); |
| 1531 | } else { |
| 1532 | GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 1533 | llvm::GlobalValue::InternalLinkage, |
| 1534 | Init, Name, |
| 1535 | &CGM.getModule()); |
| 1536 | } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1537 | GV->setSection("__OBJC,__meta_class,regular,no_dead_strip"); |
| 1538 | UsedGlobals.push_back(GV); |
| 1539 | // FIXME: Why? |
| 1540 | GV->setAlignment(32); |
| 1541 | |
| 1542 | return GV; |
| 1543 | } |
| 1544 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1545 | llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1546 | std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1547 | |
| 1548 | // FIXME: Should we look these up somewhere other than the |
| 1549 | // module. Its a bit silly since we only generate these while |
| 1550 | // processing an implementation, so exactly one pointer would work |
| 1551 | // if know when we entered/exitted an implementation block. |
| 1552 | |
| 1553 | // Check for an existing forward reference. |
Fariborz Jahanian | b0d2794 | 2009-01-07 20:11:22 +0000 | [diff] [blame] | 1554 | // Previously, metaclass with internal linkage may have been defined. |
| 1555 | // pass 'true' as 2nd argument so it is returned. |
| 1556 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) { |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1557 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 1558 | "Forward metaclass reference has incorrect type."); |
| 1559 | return GV; |
| 1560 | } else { |
| 1561 | // Generate as an external reference to keep a consistent |
| 1562 | // module. This will be patched up when we emit the metaclass. |
| 1563 | return new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 1564 | llvm::GlobalValue::ExternalLinkage, |
| 1565 | 0, |
| 1566 | Name, |
| 1567 | &CGM.getModule()); |
| 1568 | } |
| 1569 | } |
| 1570 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1571 | /* |
| 1572 | struct objc_class_ext { |
| 1573 | uint32_t size; |
| 1574 | const char *weak_ivar_layout; |
| 1575 | struct _objc_property_list *properties; |
| 1576 | }; |
| 1577 | */ |
| 1578 | llvm::Constant * |
| 1579 | CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) { |
| 1580 | uint64_t Size = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1581 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1582 | |
| 1583 | std::vector<llvm::Constant*> Values(3); |
| 1584 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1585 | // FIXME: Output weak_ivar_layout string. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1586 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1587 | Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1588 | ID, ID->getClassInterface(), ObjCTypes); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1589 | |
| 1590 | // Return null if no extension bits are used. |
| 1591 | if (Values[1]->isNullValue() && Values[2]->isNullValue()) |
| 1592 | return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
| 1593 | |
| 1594 | llvm::Constant *Init = |
| 1595 | llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values); |
| 1596 | llvm::GlobalVariable *GV = |
| 1597 | new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false, |
| 1598 | llvm::GlobalValue::InternalLinkage, |
| 1599 | Init, |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1600 | "\01L_OBJC_CLASSEXT_" + ID->getNameAsString(), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1601 | &CGM.getModule()); |
| 1602 | // No special section, but goes in llvm.used |
| 1603 | UsedGlobals.push_back(GV); |
| 1604 | |
| 1605 | return GV; |
| 1606 | } |
| 1607 | |
Fariborz Jahanian | f54b194 | 2009-01-17 19:36:33 +0000 | [diff] [blame] | 1608 | /// countInheritedIvars - count number of ivars in class and its super class(s) |
| 1609 | /// |
| 1610 | static int countInheritedIvars(const ObjCInterfaceDecl *OI) { |
| 1611 | int count = 0; |
| 1612 | if (!OI) |
| 1613 | return 0; |
| 1614 | const ObjCInterfaceDecl *SuperClass = OI->getSuperClass(); |
| 1615 | if (SuperClass) |
| 1616 | count += countInheritedIvars(SuperClass); |
| 1617 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 1618 | E = OI->ivar_end(); I != E; ++I) |
| 1619 | ++count; |
| 1620 | return count; |
| 1621 | } |
| 1622 | |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 1623 | /// getInterfaceDeclForIvar - Get the interface declaration node where |
| 1624 | /// this ivar is declared in. |
| 1625 | /// FIXME. Ideally, this info should be in the ivar node. But currently |
| 1626 | /// it is not and prevailing wisdom is that ASTs should not have more |
| 1627 | /// info than is absolutely needed, even though this info reflects the |
| 1628 | /// source language. |
| 1629 | /// |
| 1630 | static const ObjCInterfaceDecl *getInterfaceDeclForIvar( |
| 1631 | const ObjCInterfaceDecl *OI, |
| 1632 | const ObjCIvarDecl *IVD) { |
| 1633 | if (!OI) |
| 1634 | return 0; |
| 1635 | assert(isa<ObjCInterfaceDecl>(OI) && "OI is not an interface"); |
| 1636 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 1637 | E = OI->ivar_end(); I != E; ++I) |
| 1638 | if ((*I)->getIdentifier() == IVD->getIdentifier()) |
| 1639 | return OI; |
| 1640 | return getInterfaceDeclForIvar(OI->getSuperClass(), IVD); |
| 1641 | } |
| 1642 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1643 | /* |
| 1644 | struct objc_ivar { |
| 1645 | char *ivar_name; |
| 1646 | char *ivar_type; |
| 1647 | int ivar_offset; |
| 1648 | }; |
| 1649 | |
| 1650 | struct objc_ivar_list { |
| 1651 | int ivar_count; |
| 1652 | struct objc_ivar list[count]; |
| 1653 | }; |
| 1654 | */ |
| 1655 | llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1656 | bool ForClass) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1657 | std::vector<llvm::Constant*> Ivars, Ivar(3); |
| 1658 | |
| 1659 | // When emitting the root class GCC emits ivar entries for the |
| 1660 | // actual class structure. It is not clear if we need to follow this |
| 1661 | // behavior; for now lets try and get away with not doing it. If so, |
| 1662 | // the cleanest solution would be to make up an ObjCInterfaceDecl |
| 1663 | // for the class. |
| 1664 | if (ForClass) |
| 1665 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1666 | |
| 1667 | ObjCInterfaceDecl *OID = |
| 1668 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
| 1669 | const llvm::Type *InterfaceTy = |
| 1670 | CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1671 | const llvm::StructLayout *Layout = |
| 1672 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy)); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1673 | |
| 1674 | RecordDecl::field_iterator ifield, pfield; |
| 1675 | const RecordDecl *RD = GetFirstIvarInRecord(OID, ifield, pfield); |
Fariborz Jahanian | f54b194 | 2009-01-17 19:36:33 +0000 | [diff] [blame] | 1676 | for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) { |
| 1677 | FieldDecl *Field = *ifield; |
| 1678 | unsigned Offset = Layout->getElementOffset(CGM.getTypes(). |
| 1679 | getLLVMFieldNo(Field)); |
| 1680 | if (Field->getIdentifier()) |
| 1681 | Ivar[0] = GetMethodVarName(Field->getIdentifier()); |
| 1682 | else |
| 1683 | Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1684 | std::string TypeStr; |
Fariborz Jahanian | f54b194 | 2009-01-17 19:36:33 +0000 | [diff] [blame] | 1685 | CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1686 | Ivar[1] = GetMethodVarType(TypeStr); |
| 1687 | Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1688 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
| 1691 | // Return null for empty list. |
| 1692 | if (Ivars.empty()) |
| 1693 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
| 1694 | |
| 1695 | std::vector<llvm::Constant*> Values(2); |
| 1696 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 1697 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy, |
| 1698 | Ivars.size()); |
| 1699 | Values[1] = llvm::ConstantArray::get(AT, Ivars); |
| 1700 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1701 | |
| 1702 | const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" : |
| 1703 | "\01L_OBJC_INSTANCE_VARIABLES_"); |
| 1704 | llvm::GlobalVariable *GV = |
| 1705 | new llvm::GlobalVariable(Init->getType(), false, |
| 1706 | llvm::GlobalValue::InternalLinkage, |
| 1707 | Init, |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1708 | Prefix + ID->getNameAsString(), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1709 | &CGM.getModule()); |
| 1710 | if (ForClass) { |
| 1711 | GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip"); |
| 1712 | // FIXME: Why is this only here? |
| 1713 | GV->setAlignment(32); |
| 1714 | } else { |
| 1715 | GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip"); |
| 1716 | } |
| 1717 | UsedGlobals.push_back(GV); |
| 1718 | return llvm::ConstantExpr::getBitCast(GV, |
| 1719 | ObjCTypes.IvarListPtrTy); |
| 1720 | } |
| 1721 | |
| 1722 | /* |
| 1723 | struct objc_method { |
| 1724 | SEL method_name; |
| 1725 | char *method_types; |
| 1726 | void *method; |
| 1727 | }; |
| 1728 | |
| 1729 | struct objc_method_list { |
| 1730 | struct objc_method_list *obsolete; |
| 1731 | int count; |
| 1732 | struct objc_method methods_list[count]; |
| 1733 | }; |
| 1734 | */ |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1735 | |
| 1736 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 1737 | /// given method if it has been defined. The result is null if the |
| 1738 | /// method has not been defined. The return value has type MethodPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1739 | llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1740 | // FIXME: Use DenseMap::lookup |
| 1741 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 1742 | if (!Fn) |
| 1743 | return 0; |
| 1744 | |
| 1745 | std::vector<llvm::Constant*> Method(3); |
| 1746 | Method[0] = |
| 1747 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 1748 | ObjCTypes.SelectorPtrTy); |
| 1749 | Method[1] = GetMethodVarType(MD); |
| 1750 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 1751 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 1752 | } |
| 1753 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1754 | llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name, |
| 1755 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1756 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1757 | // Return null for empty list. |
| 1758 | if (Methods.empty()) |
| 1759 | return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy); |
| 1760 | |
| 1761 | std::vector<llvm::Constant*> Values(3); |
| 1762 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 1763 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 1764 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 1765 | Methods.size()); |
| 1766 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 1767 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1768 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1769 | llvm::GlobalVariable *GV = |
| 1770 | new llvm::GlobalVariable(Init->getType(), false, |
| 1771 | llvm::GlobalValue::InternalLinkage, |
| 1772 | Init, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1773 | Name, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1774 | &CGM.getModule()); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1775 | GV->setSection(Section); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1776 | UsedGlobals.push_back(GV); |
| 1777 | return llvm::ConstantExpr::getBitCast(GV, |
| 1778 | ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1781 | llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD, |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 1782 | const ObjCContainerDecl *CD) { |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1783 | std::string Name; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 1784 | GetNameForMethod(OMD, CD, Name); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1785 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1786 | CodeGenTypes &Types = CGM.getTypes(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1787 | const llvm::FunctionType *MethodTy = |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1788 | Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic()); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1789 | llvm::Function *Method = |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1790 | llvm::Function::Create(MethodTy, |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1791 | llvm::GlobalValue::InternalLinkage, |
| 1792 | Name, |
| 1793 | &CGM.getModule()); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1794 | MethodDefinitions.insert(std::make_pair(OMD, Method)); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1795 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1796 | return Method; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
| 1799 | llvm::Function *CGObjCMac::ModuleInitFunction() { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1800 | // Abuse this interface function as a place to finalize. |
| 1801 | FinishModule(); |
| 1802 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1803 | return NULL; |
| 1804 | } |
| 1805 | |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 1806 | llvm::Function *CGObjCMac::GetPropertyGetFunction() { |
| 1807 | return ObjCTypes.GetPropertyFn; |
| 1808 | } |
| 1809 | |
| 1810 | llvm::Function *CGObjCMac::GetPropertySetFunction() { |
| 1811 | return ObjCTypes.SetPropertyFn; |
| 1812 | } |
| 1813 | |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 1814 | llvm::Function *CGObjCMac::EnumerationMutationFunction() |
| 1815 | { |
| 1816 | return ObjCTypes.EnumerationMutationFn; |
| 1817 | } |
| 1818 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1819 | /* |
| 1820 | |
| 1821 | Objective-C setjmp-longjmp (sjlj) Exception Handling |
| 1822 | -- |
| 1823 | |
| 1824 | The basic framework for a @try-catch-finally is as follows: |
| 1825 | { |
| 1826 | objc_exception_data d; |
| 1827 | id _rethrow = null; |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 1828 | bool _call_try_exit = true; |
| 1829 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1830 | objc_exception_try_enter(&d); |
| 1831 | if (!setjmp(d.jmp_buf)) { |
| 1832 | ... try body ... |
| 1833 | } else { |
| 1834 | // exception path |
| 1835 | id _caught = objc_exception_extract(&d); |
| 1836 | |
| 1837 | // enter new try scope for handlers |
| 1838 | if (!setjmp(d.jmp_buf)) { |
| 1839 | ... match exception and execute catch blocks ... |
| 1840 | |
| 1841 | // fell off end, rethrow. |
| 1842 | _rethrow = _caught; |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1843 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1844 | } else { |
| 1845 | // exception in catch block |
| 1846 | _rethrow = objc_exception_extract(&d); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 1847 | _call_try_exit = false; |
| 1848 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1849 | } |
| 1850 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1851 | ... jump-through-finally to finally_end ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1852 | |
| 1853 | finally: |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 1854 | if (_call_try_exit) |
| 1855 | objc_exception_try_exit(&d); |
| 1856 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1857 | ... finally block .... |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1858 | ... dispatch to finally destination ... |
| 1859 | |
| 1860 | finally_rethrow: |
| 1861 | objc_exception_throw(_rethrow); |
| 1862 | |
| 1863 | finally_end: |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1864 | } |
| 1865 | |
| 1866 | This framework differs slightly from the one gcc uses, in that gcc |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1867 | uses _rethrow to determine if objc_exception_try_exit should be called |
| 1868 | and if the object should be rethrown. This breaks in the face of |
| 1869 | throwing nil and introduces unnecessary branches. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1870 | |
| 1871 | We specialize this framework for a few particular circumstances: |
| 1872 | |
| 1873 | - If there are no catch blocks, then we avoid emitting the second |
| 1874 | exception handling context. |
| 1875 | |
| 1876 | - If there is a catch-all catch block (i.e. @catch(...) or @catch(id |
| 1877 | e)) we avoid emitting the code to rethrow an uncaught exception. |
| 1878 | |
| 1879 | - FIXME: If there is no @finally block we can do a few more |
| 1880 | simplifications. |
| 1881 | |
| 1882 | Rethrows and Jumps-Through-Finally |
| 1883 | -- |
| 1884 | |
| 1885 | Support for implicit rethrows and jumping through the finally block is |
| 1886 | handled by storing the current exception-handling context in |
| 1887 | ObjCEHStack. |
| 1888 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1889 | In order to implement proper @finally semantics, we support one basic |
| 1890 | mechanism for jumping through the finally block to an arbitrary |
| 1891 | destination. Constructs which generate exits from a @try or @catch |
| 1892 | block use this mechanism to implement the proper semantics by chaining |
| 1893 | jumps, as necessary. |
| 1894 | |
| 1895 | This mechanism works like the one used for indirect goto: we |
| 1896 | arbitrarily assign an ID to each destination and store the ID for the |
| 1897 | destination in a variable prior to entering the finally block. At the |
| 1898 | end of the finally block we simply create a switch to the proper |
| 1899 | destination. |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 1900 | |
| 1901 | Code gen for @synchronized(expr) stmt; |
| 1902 | Effectively generating code for: |
| 1903 | objc_sync_enter(expr); |
| 1904 | @try stmt @finally { objc_sync_exit(expr); } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1905 | */ |
| 1906 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 1907 | void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1908 | const Stmt &S) { |
| 1909 | bool isTry = isa<ObjCAtTryStmt>(S); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1910 | // Create various blocks we refer to for handling @finally. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 1911 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 1912 | llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit"); |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 1913 | llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit"); |
| 1914 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
| 1915 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 1916 | |
| 1917 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 1918 | // evaluation of the expression must occur before we enter the |
| 1919 | // @synchronized. We can safely avoid a temp here because jumps into |
| 1920 | // @synchronized are illegal & this will dominate uses. |
| 1921 | llvm::Value *SyncArg = 0; |
| 1922 | if (!isTry) { |
| 1923 | SyncArg = |
| 1924 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 1925 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
| 1926 | CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, SyncArg); |
| 1927 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1928 | |
| 1929 | // Push an EH context entry, used for handling rethrows and jumps |
| 1930 | // through finally. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 1931 | CGF.PushCleanupBlock(FinallyBlock); |
| 1932 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 1933 | CGF.ObjCEHValueStack.push_back(0); |
| 1934 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1935 | // Allocate memory for the exception data and rethrow pointer. |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1936 | llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy, |
| 1937 | "exceptiondata.ptr"); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1938 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy, |
| 1939 | "_rethrow"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 1940 | llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty, |
| 1941 | "_call_try_exit"); |
| 1942 | CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr); |
| 1943 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1944 | // Enter a new try block and call setjmp. |
| 1945 | CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData); |
| 1946 | llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0, |
| 1947 | "jmpbufarray"); |
| 1948 | JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp"); |
| 1949 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn, |
| 1950 | JmpBufPtr, "result"); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1951 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 1952 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 1953 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 1954 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 1955 | TryHandler, TryBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1956 | |
| 1957 | // Emit the @try block. |
| 1958 | CGF.EmitBlock(TryBlock); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 1959 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 1960 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 1961 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1962 | |
| 1963 | // Emit the "exception in @try" block. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 1964 | CGF.EmitBlock(TryHandler); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 1965 | |
| 1966 | // Retrieve the exception object. We may emit multiple blocks but |
| 1967 | // nothing can cross this so the value is already in SSA form. |
| 1968 | llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn, |
| 1969 | ExceptionData, |
| 1970 | "caught"); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 1971 | CGF.ObjCEHValueStack.back() = Caught; |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 1972 | if (!isTry) |
| 1973 | { |
| 1974 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 1975 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 1976 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 1977 | } |
| 1978 | else if (const ObjCAtCatchStmt* CatchStmt = |
| 1979 | cast<ObjCAtTryStmt>(S).getCatchStmts()) |
| 1980 | { |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 1981 | // Enter a new exception try block (in case a @catch block throws |
| 1982 | // an exception). |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1983 | CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 1984 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1985 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn, |
| 1986 | JmpBufPtr, "result"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 1987 | llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1988 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 1989 | llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch"); |
| 1990 | llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler"); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 1991 | CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1992 | |
| 1993 | CGF.EmitBlock(CatchBlock); |
| 1994 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 1995 | // Handle catch list. As a special case we check if everything is |
| 1996 | // matched and avoid generating code for falling off the end if |
| 1997 | // so. |
| 1998 | bool AllMatched = false; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1999 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2000 | llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2001 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2002 | const DeclStmt *CatchParam = |
| 2003 | cast_or_null<DeclStmt>(CatchStmt->getCatchParamStmt()); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2004 | const VarDecl *VD = 0; |
| 2005 | const PointerType *PT = 0; |
| 2006 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2007 | // catch(...) always matches. |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2008 | if (!CatchParam) { |
| 2009 | AllMatched = true; |
| 2010 | } else { |
Ted Kremenek | de3b8fb | 2008-10-06 20:58:56 +0000 | [diff] [blame] | 2011 | VD = cast<VarDecl>(CatchParam->getSolitaryDecl()); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2012 | PT = VD->getType()->getAsPointerType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2013 | |
Daniel Dunbar | 97f61d1 | 2008-09-27 22:21:14 +0000 | [diff] [blame] | 2014 | // catch(id e) always matches. |
| 2015 | // FIXME: For the time being we also match id<X>; this should |
| 2016 | // be rejected by Sema instead. |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 2017 | if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) || |
Daniel Dunbar | 97f61d1 | 2008-09-27 22:21:14 +0000 | [diff] [blame] | 2018 | VD->getType()->isObjCQualifiedIdType()) |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2019 | AllMatched = true; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2020 | } |
| 2021 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2022 | if (AllMatched) { |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2023 | if (CatchParam) { |
| 2024 | CGF.EmitStmt(CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2025 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2026 | CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(VD)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2027 | } |
Anders Carlsson | 1452f55 | 2008-09-11 08:21:54 +0000 | [diff] [blame] | 2028 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2029 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2030 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2031 | break; |
| 2032 | } |
| 2033 | |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2034 | assert(PT && "Unexpected non-pointer type in @catch"); |
| 2035 | QualType T = PT->getPointeeType(); |
Anders Carlsson | 4b7ff6e | 2008-09-11 06:35:14 +0000 | [diff] [blame] | 2036 | const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2037 | assert(ObjCType && "Catch parameter must have Objective-C type!"); |
| 2038 | |
| 2039 | // Check if the @catch block matches the exception object. |
| 2040 | llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl()); |
| 2041 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2042 | llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn, |
| 2043 | Class, Caught, "match"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2044 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2045 | llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2046 | |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2047 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2048 | MatchedBlock, NextCatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2049 | |
| 2050 | // Emit the @catch block. |
| 2051 | CGF.EmitBlock(MatchedBlock); |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2052 | CGF.EmitStmt(CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2053 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2054 | |
| 2055 | llvm::Value *Tmp = |
| 2056 | CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(VD->getType()), |
| 2057 | "tmp"); |
| 2058 | CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(VD)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2059 | |
| 2060 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2061 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2062 | |
| 2063 | CGF.EmitBlock(NextCatchBlock); |
| 2064 | } |
| 2065 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2066 | if (!AllMatched) { |
| 2067 | // None of the handlers caught the exception, so store it to be |
| 2068 | // rethrown at the end of the @finally block. |
| 2069 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2070 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2071 | } |
| 2072 | |
| 2073 | // Emit the exception handler for the @catch blocks. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2074 | CGF.EmitBlock(CatchHandler); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2075 | CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn, |
| 2076 | ExceptionData), |
| 2077 | RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2078 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2079 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2080 | } else { |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2081 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2082 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2083 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2084 | } |
| 2085 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2086 | // Pop the exception-handling stack entry. It is important to do |
| 2087 | // this now, because the code in the @finally block is not in this |
| 2088 | // context. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2089 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 2090 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2091 | CGF.ObjCEHValueStack.pop_back(); |
| 2092 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2093 | // Emit the @finally block. |
| 2094 | CGF.EmitBlock(FinallyBlock); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2095 | llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp"); |
| 2096 | |
| 2097 | CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit); |
| 2098 | |
| 2099 | CGF.EmitBlock(FinallyExit); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2100 | CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2101 | |
| 2102 | CGF.EmitBlock(FinallyNoExit); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2103 | if (isTry) { |
| 2104 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 2105 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 2106 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2107 | } else { |
| 2108 | // Emit objc_sync_exit(expr); as finally's sole statement for |
| 2109 | // @synchronized. |
| 2110 | CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, SyncArg); |
Fariborz Jahanian | f2878e5 | 2008-11-21 19:21:53 +0000 | [diff] [blame] | 2111 | } |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2112 | |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2113 | // Emit the switch block |
| 2114 | if (Info.SwitchBlock) |
| 2115 | CGF.EmitBlock(Info.SwitchBlock); |
| 2116 | if (Info.EndBlock) |
| 2117 | CGF.EmitBlock(Info.EndBlock); |
| 2118 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2119 | CGF.EmitBlock(FinallyRethrow); |
| 2120 | CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, |
| 2121 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2122 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2123 | |
| 2124 | CGF.EmitBlock(FinallyEnd); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2125 | } |
| 2126 | |
| 2127 | void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2128 | const ObjCAtThrowStmt &S) { |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2129 | llvm::Value *ExceptionAsObject; |
| 2130 | |
| 2131 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
| 2132 | llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr); |
| 2133 | ExceptionAsObject = |
| 2134 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 2135 | } else { |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2136 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2137 | "Unexpected rethrow outside @catch block."); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2138 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2139 | } |
| 2140 | |
| 2141 | CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2142 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2143 | |
| 2144 | // Clear the insertion point to indicate we are in unreachable code. |
| 2145 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2146 | } |
| 2147 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2148 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2149 | /// object: objc_read_weak (id *src) |
| 2150 | /// |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2151 | llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2152 | llvm::Value *AddrWeakObj) |
| 2153 | { |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2154 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2155 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn, |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2156 | AddrWeakObj, "weakread"); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2157 | return read_weak; |
| 2158 | } |
| 2159 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2160 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 2161 | /// objc_assign_weak (id src, id *dst) |
| 2162 | /// |
| 2163 | void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 2164 | llvm::Value *src, llvm::Value *dst) |
| 2165 | { |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2166 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2167 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2168 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn, |
| 2169 | src, dst, "weakassign"); |
| 2170 | return; |
| 2171 | } |
| 2172 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2173 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 2174 | /// objc_assign_global (id src, id *dst) |
| 2175 | /// |
| 2176 | void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 2177 | llvm::Value *src, llvm::Value *dst) |
| 2178 | { |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2179 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2180 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2181 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn, |
| 2182 | src, dst, "globalassign"); |
| 2183 | return; |
| 2184 | } |
| 2185 | |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2186 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 2187 | /// objc_assign_ivar (id src, id *dst) |
| 2188 | /// |
| 2189 | void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 2190 | llvm::Value *src, llvm::Value *dst) |
| 2191 | { |
| 2192 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2193 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
| 2194 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn, |
| 2195 | src, dst, "assignivar"); |
| 2196 | return; |
| 2197 | } |
| 2198 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2199 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 2200 | /// objc_assign_strongCast (id src, id *dst) |
| 2201 | /// |
| 2202 | void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 2203 | llvm::Value *src, llvm::Value *dst) |
| 2204 | { |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2205 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2206 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2207 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn, |
| 2208 | src, dst, "weakassign"); |
| 2209 | return; |
| 2210 | } |
| 2211 | |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2212 | /// EmitObjCValueForIvar - Code Gen for ivar reference. |
| 2213 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2214 | LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 2215 | QualType ObjectTy, |
| 2216 | llvm::Value *BaseValue, |
| 2217 | const ObjCIvarDecl *Ivar, |
| 2218 | const FieldDecl *Field, |
| 2219 | unsigned CVRQualifiers) { |
| 2220 | if (Ivar->isBitField()) |
| 2221 | return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field), |
| 2222 | CVRQualifiers); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2223 | // TODO: Add a special case for isa (index 0) |
| 2224 | unsigned Index = CGM.getTypes().getLLVMFieldNo(Field); |
| 2225 | llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp"); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2226 | LValue LV = LValue::MakeAddr(V, |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 2227 | Ivar->getType().getCVRQualifiers()|CVRQualifiers, |
| 2228 | CGM.getContext().getObjCGCAttrKind(Ivar->getType())); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2229 | LValue::SetObjCIvar(LV, true); |
| 2230 | return LV; |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2231 | } |
| 2232 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2233 | llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
| 2234 | ObjCInterfaceDecl *Interface, |
| 2235 | const ObjCIvarDecl *Ivar) { |
| 2236 | const llvm::Type *InterfaceLTy = |
| 2237 | CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface)); |
| 2238 | const llvm::StructLayout *Layout = |
| 2239 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceLTy)); |
| 2240 | FieldDecl *Field = Interface->lookupFieldDeclForIvar(CGM.getContext(), Ivar); |
| 2241 | uint64_t Offset = |
| 2242 | Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field)); |
| 2243 | |
| 2244 | return llvm::ConstantInt::get( |
| 2245 | CGM.getTypes().ConvertType(CGM.getContext().LongTy), |
| 2246 | Offset); |
| 2247 | } |
| 2248 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2249 | /* *** Private Interface *** */ |
| 2250 | |
| 2251 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 2252 | /// level information. |
| 2253 | /// |
| 2254 | /// See: <rdr://4810609&4810587&4810587> |
| 2255 | /// struct IMAGE_INFO { |
| 2256 | /// unsigned version; |
| 2257 | /// unsigned flags; |
| 2258 | /// }; |
| 2259 | enum ImageInfoFlags { |
| 2260 | eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies |
| 2261 | eImageInfo_GarbageCollected = (1 << 1), |
| 2262 | eImageInfo_GCOnly = (1 << 2) |
| 2263 | }; |
| 2264 | |
| 2265 | void CGObjCMac::EmitImageInfo() { |
| 2266 | unsigned version = 0; // Version is unused? |
| 2267 | unsigned flags = 0; |
| 2268 | |
| 2269 | // FIXME: Fix and continue? |
| 2270 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 2271 | flags |= eImageInfo_GarbageCollected; |
| 2272 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 2273 | flags |= eImageInfo_GCOnly; |
| 2274 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2275 | // Emitted as int[2]; |
| 2276 | llvm::Constant *values[2] = { |
| 2277 | llvm::ConstantInt::get(llvm::Type::Int32Ty, version), |
| 2278 | llvm::ConstantInt::get(llvm::Type::Int32Ty, flags) |
| 2279 | }; |
| 2280 | llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2281 | llvm::GlobalVariable *GV = |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2282 | new llvm::GlobalVariable(AT, true, |
| 2283 | llvm::GlobalValue::InternalLinkage, |
| 2284 | llvm::ConstantArray::get(AT, values, 2), |
| 2285 | "\01L_OBJC_IMAGE_INFO", |
| 2286 | &CGM.getModule()); |
| 2287 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 2288 | if (ObjCABI == 1) { |
| 2289 | GV->setSection("__OBJC, __image_info,regular"); |
| 2290 | } else { |
| 2291 | GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip"); |
| 2292 | } |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2293 | |
| 2294 | UsedGlobals.push_back(GV); |
| 2295 | } |
| 2296 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2297 | |
| 2298 | // struct objc_module { |
| 2299 | // unsigned long version; |
| 2300 | // unsigned long size; |
| 2301 | // const char *name; |
| 2302 | // Symtab symtab; |
| 2303 | // }; |
| 2304 | |
| 2305 | // FIXME: Get from somewhere |
| 2306 | static const int ModuleVersion = 7; |
| 2307 | |
| 2308 | void CGObjCMac::EmitModuleInfo() { |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 2309 | uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2310 | |
| 2311 | std::vector<llvm::Constant*> Values(4); |
| 2312 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion); |
| 2313 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2314 | // This used to be the filename, now it is unused. <rdr://4327263> |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2315 | Values[2] = GetClassName(&CGM.getContext().Idents.get("")); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2316 | Values[3] = EmitModuleSymbols(); |
| 2317 | |
| 2318 | llvm::GlobalVariable *GV = |
| 2319 | new llvm::GlobalVariable(ObjCTypes.ModuleTy, false, |
| 2320 | llvm::GlobalValue::InternalLinkage, |
| 2321 | llvm::ConstantStruct::get(ObjCTypes.ModuleTy, |
| 2322 | Values), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2323 | "\01L_OBJC_MODULES", |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2324 | &CGM.getModule()); |
| 2325 | GV->setSection("__OBJC,__module_info,regular,no_dead_strip"); |
| 2326 | UsedGlobals.push_back(GV); |
| 2327 | } |
| 2328 | |
| 2329 | llvm::Constant *CGObjCMac::EmitModuleSymbols() { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2330 | unsigned NumClasses = DefinedClasses.size(); |
| 2331 | unsigned NumCategories = DefinedCategories.size(); |
| 2332 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2333 | // Return null if no symbols were defined. |
| 2334 | if (!NumClasses && !NumCategories) |
| 2335 | return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy); |
| 2336 | |
| 2337 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2338 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2339 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy); |
| 2340 | Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses); |
| 2341 | Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories); |
| 2342 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2343 | // The runtime expects exactly the list of defined classes followed |
| 2344 | // by the list of defined categories, in a single array. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2345 | std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2346 | for (unsigned i=0; i<NumClasses; i++) |
| 2347 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
| 2348 | ObjCTypes.Int8PtrTy); |
| 2349 | for (unsigned i=0; i<NumCategories; i++) |
| 2350 | Symbols[NumClasses + i] = |
| 2351 | llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
| 2352 | ObjCTypes.Int8PtrTy); |
| 2353 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2354 | Values[4] = |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2355 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2356 | NumClasses + NumCategories), |
| 2357 | Symbols); |
| 2358 | |
| 2359 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 2360 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2361 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2362 | new llvm::GlobalVariable(Init->getType(), false, |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2363 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2364 | Init, |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2365 | "\01L_OBJC_SYMBOLS", |
| 2366 | &CGM.getModule()); |
| 2367 | GV->setSection("__OBJC,__symbols,regular,no_dead_strip"); |
| 2368 | UsedGlobals.push_back(GV); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2369 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy); |
| 2370 | } |
| 2371 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2372 | llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2373 | const ObjCInterfaceDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2374 | LazySymbols.insert(ID->getIdentifier()); |
| 2375 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2376 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 2377 | |
| 2378 | if (!Entry) { |
| 2379 | llvm::Constant *Casted = |
| 2380 | llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()), |
| 2381 | ObjCTypes.ClassPtrTy); |
| 2382 | Entry = |
| 2383 | new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false, |
| 2384 | llvm::GlobalValue::InternalLinkage, |
| 2385 | Casted, "\01L_OBJC_CLASS_REFERENCES_", |
| 2386 | &CGM.getModule()); |
| 2387 | Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip"); |
| 2388 | UsedGlobals.push_back(Entry); |
| 2389 | } |
| 2390 | |
| 2391 | return Builder.CreateLoad(Entry, false, "tmp"); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2392 | } |
| 2393 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2394 | llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2395 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 2396 | |
| 2397 | if (!Entry) { |
| 2398 | llvm::Constant *Casted = |
| 2399 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 2400 | ObjCTypes.SelectorPtrTy); |
| 2401 | Entry = |
| 2402 | new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false, |
| 2403 | llvm::GlobalValue::InternalLinkage, |
| 2404 | Casted, "\01L_OBJC_SELECTOR_REFERENCES_", |
| 2405 | &CGM.getModule()); |
| 2406 | Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip"); |
| 2407 | UsedGlobals.push_back(Entry); |
| 2408 | } |
| 2409 | |
| 2410 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 2411 | } |
| 2412 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 2413 | llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2414 | llvm::GlobalVariable *&Entry = ClassNames[Ident]; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2415 | |
| 2416 | if (!Entry) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2417 | llvm::Constant *C = llvm::ConstantArray::get(Ident->getName()); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2418 | Entry = |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2419 | new llvm::GlobalVariable(C->getType(), false, |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2420 | llvm::GlobalValue::InternalLinkage, |
| 2421 | C, "\01L_OBJC_CLASS_NAME_", |
| 2422 | &CGM.getModule()); |
| 2423 | Entry->setSection("__TEXT,__cstring,cstring_literals"); |
| 2424 | UsedGlobals.push_back(Entry); |
| 2425 | } |
| 2426 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2427 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2428 | } |
| 2429 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 2430 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2431 | llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; |
| 2432 | |
| 2433 | if (!Entry) { |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2434 | // FIXME: Avoid std::string copying. |
| 2435 | llvm::Constant *C = llvm::ConstantArray::get(Sel.getAsString()); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2436 | Entry = |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2437 | new llvm::GlobalVariable(C->getType(), false, |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2438 | llvm::GlobalValue::InternalLinkage, |
| 2439 | C, "\01L_OBJC_METH_VAR_NAME_", |
| 2440 | &CGM.getModule()); |
| 2441 | Entry->setSection("__TEXT,__cstring,cstring_literals"); |
| 2442 | UsedGlobals.push_back(Entry); |
| 2443 | } |
| 2444 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2445 | return getConstantGEP(Entry, 0, 0); |
| 2446 | } |
| 2447 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2448 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 2449 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2450 | return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); |
| 2451 | } |
| 2452 | |
| 2453 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 2454 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2455 | return GetMethodVarName(&CGM.getContext().Idents.get(Name)); |
| 2456 | } |
| 2457 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 2458 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const std::string &Name) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2459 | llvm::GlobalVariable *&Entry = MethodVarTypes[Name]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2460 | |
| 2461 | if (!Entry) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2462 | llvm::Constant *C = llvm::ConstantArray::get(Name); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2463 | Entry = |
| 2464 | new llvm::GlobalVariable(C->getType(), false, |
| 2465 | llvm::GlobalValue::InternalLinkage, |
| 2466 | C, "\01L_OBJC_METH_VAR_TYPE_", |
| 2467 | &CGM.getModule()); |
| 2468 | Entry->setSection("__TEXT,__cstring,cstring_literals"); |
| 2469 | UsedGlobals.push_back(Entry); |
| 2470 | } |
| 2471 | |
| 2472 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2473 | } |
| 2474 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2475 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 2476 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2477 | std::string TypeStr; |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2478 | CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D), |
| 2479 | TypeStr); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2480 | return GetMethodVarType(TypeStr); |
| 2481 | } |
| 2482 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2483 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 2484 | llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2485 | llvm::GlobalVariable *&Entry = PropertyNames[Ident]; |
| 2486 | |
| 2487 | if (!Entry) { |
| 2488 | llvm::Constant *C = llvm::ConstantArray::get(Ident->getName()); |
| 2489 | Entry = |
| 2490 | new llvm::GlobalVariable(C->getType(), false, |
| 2491 | llvm::GlobalValue::InternalLinkage, |
| 2492 | C, "\01L_OBJC_PROP_NAME_ATTR_", |
| 2493 | &CGM.getModule()); |
| 2494 | Entry->setSection("__TEXT,__cstring,cstring_literals"); |
| 2495 | UsedGlobals.push_back(Entry); |
| 2496 | } |
| 2497 | |
| 2498 | return getConstantGEP(Entry, 0, 0); |
| 2499 | } |
| 2500 | |
| 2501 | // FIXME: Merge into a single cstring creation function. |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2502 | // FIXME: This Decl should be more precise. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 2503 | llvm::Constant *CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2504 | const Decl *Container) { |
| 2505 | std::string TypeStr; |
| 2506 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2507 | return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); |
| 2508 | } |
| 2509 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 2510 | void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D, |
| 2511 | const ObjCContainerDecl *CD, |
| 2512 | std::string &NameOut) { |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2513 | // FIXME: Find the mangling GCC uses. |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 2514 | NameOut = (D->isInstanceMethod() ? "-" : "+"); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2515 | NameOut += '['; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 2516 | assert (CD && "Missing container decl in GetNameForMethod"); |
| 2517 | NameOut += CD->getNameAsString(); |
Fariborz Jahanian | 5284733 | 2009-01-26 23:49:05 +0000 | [diff] [blame] | 2518 | // FIXME. For a method in a category, (CAT_NAME) is inserted here. |
| 2519 | // Right now! there is not enough info. to do this. |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2520 | NameOut += ' '; |
| 2521 | NameOut += D->getSelector().getAsString(); |
| 2522 | NameOut += ']'; |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2523 | } |
| 2524 | |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2525 | /// GetFirstIvarInRecord - This routine returns the record for the |
| 2526 | /// implementation of the fiven class OID. It also returns field |
| 2527 | /// corresponding to the first ivar in the class in FIV. It also |
| 2528 | /// returns the one before the first ivar. |
| 2529 | /// |
| 2530 | const RecordDecl *CGObjCCommonMac::GetFirstIvarInRecord( |
| 2531 | const ObjCInterfaceDecl *OID, |
| 2532 | RecordDecl::field_iterator &FIV, |
| 2533 | RecordDecl::field_iterator &PIV) { |
| 2534 | int countSuperClassIvars = countInheritedIvars(OID->getSuperClass()); |
| 2535 | const RecordDecl *RD = CGM.getContext().addRecordToClass(OID); |
| 2536 | RecordDecl::field_iterator ifield = RD->field_begin(); |
| 2537 | RecordDecl::field_iterator pfield = RD->field_end(); |
| 2538 | while (countSuperClassIvars-- > 0) { |
| 2539 | pfield = ifield; |
| 2540 | ++ifield; |
| 2541 | } |
| 2542 | FIV = ifield; |
| 2543 | PIV = pfield; |
| 2544 | return RD; |
| 2545 | } |
| 2546 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2547 | void CGObjCMac::FinishModule() { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2548 | EmitModuleInfo(); |
| 2549 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2550 | // Emit the dummy bodies for any protocols which were referenced but |
| 2551 | // never defined. |
| 2552 | for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator |
| 2553 | i = Protocols.begin(), e = Protocols.end(); i != e; ++i) { |
| 2554 | if (i->second->hasInitializer()) |
| 2555 | continue; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2556 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2557 | std::vector<llvm::Constant*> Values(5); |
| 2558 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
| 2559 | Values[1] = GetClassName(i->first); |
| 2560 | Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 2561 | Values[3] = Values[4] = |
| 2562 | llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
| 2563 | i->second->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 2564 | i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 2565 | Values)); |
| 2566 | } |
| 2567 | |
| 2568 | std::vector<llvm::Constant*> Used; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2569 | for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2570 | e = UsedGlobals.end(); i != e; ++i) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2571 | Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2572 | } |
| 2573 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2574 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2575 | llvm::GlobalValue *GV = |
| 2576 | new llvm::GlobalVariable(AT, false, |
| 2577 | llvm::GlobalValue::AppendingLinkage, |
| 2578 | llvm::ConstantArray::get(AT, Used), |
| 2579 | "llvm.used", |
| 2580 | &CGM.getModule()); |
| 2581 | |
| 2582 | GV->setSection("llvm.metadata"); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2583 | |
| 2584 | // Add assembler directives to add lazy undefined symbol references |
| 2585 | // for classes which are referenced but not defined. This is |
| 2586 | // important for correct linker interaction. |
| 2587 | |
| 2588 | // FIXME: Uh, this isn't particularly portable. |
| 2589 | std::stringstream s; |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 2590 | |
| 2591 | if (!CGM.getModule().getModuleInlineAsm().empty()) |
| 2592 | s << "\n"; |
| 2593 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2594 | for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(), |
| 2595 | e = LazySymbols.end(); i != e; ++i) { |
| 2596 | s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n"; |
| 2597 | } |
| 2598 | for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(), |
| 2599 | e = DefinedSymbols.end(); i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 2600 | s << "\t.objc_class_name_" << (*i)->getName() << "=0\n" |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2601 | << "\t.globl .objc_class_name_" << (*i)->getName() << "\n"; |
| 2602 | } |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 2603 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2604 | CGM.getModule().appendModuleInlineAsm(s.str()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2605 | } |
| 2606 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 2607 | CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 2608 | : CGObjCCommonMac(cgm), |
| 2609 | ObjCTypes(cgm) |
| 2610 | { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 2611 | ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 2612 | ObjCABI = 2; |
| 2613 | } |
| 2614 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2615 | /* *** */ |
| 2616 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 2617 | ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm) |
| 2618 | : CGM(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 2619 | { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2620 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 2621 | ASTContext &Ctx = CGM.getContext(); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 2622 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2623 | ShortTy = Types.ConvertType(Ctx.ShortTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2624 | IntTy = Types.ConvertType(Ctx.IntTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2625 | LongTy = Types.ConvertType(Ctx.LongTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2626 | Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 2627 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2628 | ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType()); |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 2629 | PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2630 | SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2631 | |
| 2632 | // FIXME: It would be nice to unify this with the opaque type, so |
| 2633 | // that the IR comes out a bit cleaner. |
| 2634 | const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); |
| 2635 | ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 2636 | |
| 2637 | // I'm not sure I like this. The implicit coordination is a bit |
| 2638 | // gross. We should solve this in a reasonable fashion because this |
| 2639 | // is a pretty common task (match some runtime data structure with |
| 2640 | // an LLVM data structure). |
| 2641 | |
| 2642 | // FIXME: This is leaked. |
| 2643 | // FIXME: Merge with rewriter code? |
| 2644 | |
| 2645 | // struct _objc_super { |
| 2646 | // id self; |
| 2647 | // Class cls; |
| 2648 | // } |
| 2649 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 2650 | SourceLocation(), |
| 2651 | &Ctx.Idents.get("_objc_super")); |
| 2652 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 2653 | Ctx.getObjCIdType(), 0, false)); |
| 2654 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 2655 | Ctx.getObjCClassType(), 0, false)); |
| 2656 | RD->completeDefinition(Ctx); |
| 2657 | |
| 2658 | SuperCTy = Ctx.getTagDeclType(RD); |
| 2659 | SuperPtrCTy = Ctx.getPointerType(SuperCTy); |
| 2660 | |
| 2661 | SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy)); |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 2662 | SuperPtrTy = llvm::PointerType::getUnqual(SuperTy); |
| 2663 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 2664 | // struct _prop_t { |
| 2665 | // char *name; |
| 2666 | // char *attributes; |
| 2667 | // } |
| 2668 | PropertyTy = llvm::StructType::get(Int8PtrTy, |
| 2669 | Int8PtrTy, |
| 2670 | NULL); |
| 2671 | CGM.getModule().addTypeName("struct._prop_t", |
| 2672 | PropertyTy); |
| 2673 | |
| 2674 | // struct _prop_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 2675 | // uint32_t entsize; // sizeof(struct _prop_t) |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 2676 | // uint32_t count_of_properties; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 2677 | // struct _prop_t prop_list[count_of_properties]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 2678 | // } |
| 2679 | PropertyListTy = llvm::StructType::get(IntTy, |
| 2680 | IntTy, |
| 2681 | llvm::ArrayType::get(PropertyTy, 0), |
| 2682 | NULL); |
| 2683 | CGM.getModule().addTypeName("struct._prop_list_t", |
| 2684 | PropertyListTy); |
| 2685 | // struct _prop_list_t * |
| 2686 | PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy); |
| 2687 | |
| 2688 | // struct _objc_method { |
| 2689 | // SEL _cmd; |
| 2690 | // char *method_type; |
| 2691 | // char *_imp; |
| 2692 | // } |
| 2693 | MethodTy = llvm::StructType::get(SelectorPtrTy, |
| 2694 | Int8PtrTy, |
| 2695 | Int8PtrTy, |
| 2696 | NULL); |
| 2697 | CGM.getModule().addTypeName("struct._objc_method", MethodTy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 2698 | |
| 2699 | // struct _objc_cache * |
| 2700 | CacheTy = llvm::OpaqueType::get(); |
| 2701 | CGM.getModule().addTypeName("struct._objc_cache", CacheTy); |
| 2702 | CachePtrTy = llvm::PointerType::getUnqual(CacheTy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 2703 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 2704 | // Property manipulation functions. |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 2705 | |
| 2706 | QualType IdType = Ctx.getObjCIdType(); |
| 2707 | QualType SelType = Ctx.getObjCSelType(); |
| 2708 | llvm::SmallVector<QualType,16> Params; |
| 2709 | const llvm::FunctionType *FTy; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 2710 | |
| 2711 | // id objc_getProperty (id, SEL, ptrdiff_t, bool) |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 2712 | Params.push_back(IdType); |
| 2713 | Params.push_back(SelType); |
| 2714 | Params.push_back(Ctx.LongTy); |
| 2715 | Params.push_back(Ctx.BoolTy); |
| 2716 | FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), |
| 2717 | false); |
| 2718 | GetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_getProperty"); |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 2719 | |
| 2720 | // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool) |
| 2721 | Params.clear(); |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 2722 | Params.push_back(IdType); |
| 2723 | Params.push_back(SelType); |
| 2724 | Params.push_back(Ctx.LongTy); |
| 2725 | Params.push_back(IdType); |
| 2726 | Params.push_back(Ctx.BoolTy); |
| 2727 | Params.push_back(Ctx.BoolTy); |
| 2728 | FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
| 2729 | SetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_setProperty"); |
| 2730 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 2731 | // Enumeration mutation. |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 2732 | |
| 2733 | // void objc_enumerationMutation (id) |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 2734 | Params.clear(); |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 2735 | Params.push_back(IdType); |
| 2736 | FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
| 2737 | EnumerationMutationFn = CGM.CreateRuntimeFunction(FTy, |
| 2738 | "objc_enumerationMutation"); |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 2739 | |
| 2740 | // gc's API |
| 2741 | // id objc_read_weak (id *) |
| 2742 | Params.clear(); |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 2743 | Params.push_back(Ctx.getPointerType(IdType)); |
| 2744 | FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false); |
| 2745 | GcReadWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_read_weak"); |
| 2746 | |
| 2747 | // id objc_assign_weak (id, id *) |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 2748 | Params.clear(); |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 2749 | Params.push_back(IdType); |
| 2750 | Params.push_back(Ctx.getPointerType(IdType)); |
| 2751 | |
| 2752 | FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false); |
| 2753 | GcAssignWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_weak"); |
| 2754 | GcAssignGlobalFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_global"); |
| 2755 | GcAssignIvarFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar"); |
| 2756 | GcAssignStrongCastFn = |
| 2757 | CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast"); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 2758 | |
| 2759 | // void objc_exception_throw(id) |
| 2760 | Params.clear(); |
| 2761 | Params.push_back(IdType); |
| 2762 | |
| 2763 | FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 2764 | ExceptionThrowFn = |
| 2765 | CGM.CreateRuntimeFunction(FTy, "objc_exception_throw"); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2766 | |
| 2767 | // synchronized APIs |
| 2768 | // void objc_sync_enter (id) |
| 2769 | // void objc_sync_exit (id) |
| 2770 | Params.clear(); |
| 2771 | Params.push_back(IdType); |
| 2772 | |
| 2773 | FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
| 2774 | SyncEnterFn = CGM.CreateRuntimeFunction(FTy, "objc_sync_enter"); |
| 2775 | SyncExitFn = CGM.CreateRuntimeFunction(FTy, "objc_sync_exit"); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 2776 | } |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2777 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 2778 | ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) |
| 2779 | : ObjCCommonTypesHelper(cgm) |
| 2780 | { |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 2781 | // struct _objc_method_description { |
| 2782 | // SEL name; |
| 2783 | // char *types; |
| 2784 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2785 | MethodDescriptionTy = |
| 2786 | llvm::StructType::get(SelectorPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2787 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2788 | NULL); |
| 2789 | CGM.getModule().addTypeName("struct._objc_method_description", |
| 2790 | MethodDescriptionTy); |
| 2791 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 2792 | // struct _objc_method_description_list { |
| 2793 | // int count; |
| 2794 | // struct _objc_method_description[1]; |
| 2795 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2796 | MethodDescriptionListTy = |
| 2797 | llvm::StructType::get(IntTy, |
| 2798 | llvm::ArrayType::get(MethodDescriptionTy, 0), |
| 2799 | NULL); |
| 2800 | CGM.getModule().addTypeName("struct._objc_method_description_list", |
| 2801 | MethodDescriptionListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 2802 | |
| 2803 | // struct _objc_method_description_list * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2804 | MethodDescriptionListPtrTy = |
| 2805 | llvm::PointerType::getUnqual(MethodDescriptionListTy); |
| 2806 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2807 | // Protocol description structures |
| 2808 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 2809 | // struct _objc_protocol_extension { |
| 2810 | // uint32_t size; // sizeof(struct _objc_protocol_extension) |
| 2811 | // struct _objc_method_description_list *optional_instance_methods; |
| 2812 | // struct _objc_method_description_list *optional_class_methods; |
| 2813 | // struct _objc_property_list *instance_properties; |
| 2814 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2815 | ProtocolExtensionTy = |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 2816 | llvm::StructType::get(IntTy, |
| 2817 | MethodDescriptionListPtrTy, |
| 2818 | MethodDescriptionListPtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2819 | PropertyListPtrTy, |
| 2820 | NULL); |
| 2821 | CGM.getModule().addTypeName("struct._objc_protocol_extension", |
| 2822 | ProtocolExtensionTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 2823 | |
| 2824 | // struct _objc_protocol_extension * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2825 | ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy); |
| 2826 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2827 | // Handle recursive construction of Protocol and ProtocolList types |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2828 | |
| 2829 | llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(); |
| 2830 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 2831 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 2832 | const llvm::Type *T = |
| 2833 | llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 2834 | LongTy, |
| 2835 | llvm::ArrayType::get(ProtocolTyHolder, 0), |
| 2836 | NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2837 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T); |
| 2838 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 2839 | // struct _objc_protocol { |
| 2840 | // struct _objc_protocol_extension *isa; |
| 2841 | // char *protocol_name; |
| 2842 | // struct _objc_protocol **_objc_protocol_list; |
| 2843 | // struct _objc_method_description_list *instance_methods; |
| 2844 | // struct _objc_method_description_list *class_methods; |
| 2845 | // } |
| 2846 | T = llvm::StructType::get(ProtocolExtensionPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2847 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2848 | llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 2849 | MethodDescriptionListPtrTy, |
| 2850 | MethodDescriptionListPtrTy, |
| 2851 | NULL); |
| 2852 | cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T); |
| 2853 | |
| 2854 | ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get()); |
| 2855 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 2856 | ProtocolListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 2857 | // struct _objc_protocol_list * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2858 | ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy); |
| 2859 | |
| 2860 | ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get()); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 2861 | CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2862 | ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2863 | |
| 2864 | // Class description structures |
| 2865 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 2866 | // struct _objc_ivar { |
| 2867 | // char *ivar_name; |
| 2868 | // char *ivar_type; |
| 2869 | // int ivar_offset; |
| 2870 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2871 | IvarTy = llvm::StructType::get(Int8PtrTy, |
| 2872 | Int8PtrTy, |
| 2873 | IntTy, |
| 2874 | NULL); |
| 2875 | CGM.getModule().addTypeName("struct._objc_ivar", IvarTy); |
| 2876 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 2877 | // struct _objc_ivar_list * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2878 | IvarListTy = llvm::OpaqueType::get(); |
| 2879 | CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy); |
| 2880 | IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy); |
| 2881 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 2882 | // struct _objc_method_list * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2883 | MethodListTy = llvm::OpaqueType::get(); |
| 2884 | CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy); |
| 2885 | MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy); |
| 2886 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 2887 | // struct _objc_class_extension * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2888 | ClassExtensionTy = |
| 2889 | llvm::StructType::get(IntTy, |
| 2890 | Int8PtrTy, |
| 2891 | PropertyListPtrTy, |
| 2892 | NULL); |
| 2893 | CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy); |
| 2894 | ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy); |
| 2895 | |
| 2896 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 2897 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 2898 | // struct _objc_class { |
| 2899 | // Class isa; |
| 2900 | // Class super_class; |
| 2901 | // char *name; |
| 2902 | // long version; |
| 2903 | // long info; |
| 2904 | // long instance_size; |
| 2905 | // struct _objc_ivar_list *ivars; |
| 2906 | // struct _objc_method_list *methods; |
| 2907 | // struct _objc_cache *cache; |
| 2908 | // struct _objc_protocol_list *protocols; |
| 2909 | // char *ivar_layout; |
| 2910 | // struct _objc_class_ext *ext; |
| 2911 | // }; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2912 | T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 2913 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 2914 | Int8PtrTy, |
| 2915 | LongTy, |
| 2916 | LongTy, |
| 2917 | LongTy, |
| 2918 | IvarListPtrTy, |
| 2919 | MethodListPtrTy, |
| 2920 | CachePtrTy, |
| 2921 | ProtocolListPtrTy, |
| 2922 | Int8PtrTy, |
| 2923 | ClassExtensionPtrTy, |
| 2924 | NULL); |
| 2925 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T); |
| 2926 | |
| 2927 | ClassTy = cast<llvm::StructType>(ClassTyHolder.get()); |
| 2928 | CGM.getModule().addTypeName("struct._objc_class", ClassTy); |
| 2929 | ClassPtrTy = llvm::PointerType::getUnqual(ClassTy); |
| 2930 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 2931 | // struct _objc_category { |
| 2932 | // char *category_name; |
| 2933 | // char *class_name; |
| 2934 | // struct _objc_method_list *instance_method; |
| 2935 | // struct _objc_method_list *class_method; |
| 2936 | // uint32_t size; // sizeof(struct _objc_category) |
| 2937 | // struct _objc_property_list *instance_properties;// category's @property |
| 2938 | // } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2939 | CategoryTy = llvm::StructType::get(Int8PtrTy, |
| 2940 | Int8PtrTy, |
| 2941 | MethodListPtrTy, |
| 2942 | MethodListPtrTy, |
| 2943 | ProtocolListPtrTy, |
| 2944 | IntTy, |
| 2945 | PropertyListPtrTy, |
| 2946 | NULL); |
| 2947 | CGM.getModule().addTypeName("struct._objc_category", CategoryTy); |
| 2948 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2949 | // Global metadata structures |
| 2950 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 2951 | // struct _objc_symtab { |
| 2952 | // long sel_ref_cnt; |
| 2953 | // SEL *refs; |
| 2954 | // short cls_def_cnt; |
| 2955 | // short cat_def_cnt; |
| 2956 | // char *defs[cls_def_cnt + cat_def_cnt]; |
| 2957 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2958 | SymtabTy = llvm::StructType::get(LongTy, |
| 2959 | SelectorPtrTy, |
| 2960 | ShortTy, |
| 2961 | ShortTy, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2962 | llvm::ArrayType::get(Int8PtrTy, 0), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2963 | NULL); |
| 2964 | CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy); |
| 2965 | SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy); |
| 2966 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 2967 | // struct _objc_module { |
| 2968 | // long version; |
| 2969 | // long size; // sizeof(struct _objc_module) |
| 2970 | // char *name; |
| 2971 | // struct _objc_symtab* symtab; |
| 2972 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2973 | ModuleTy = |
| 2974 | llvm::StructType::get(LongTy, |
| 2975 | LongTy, |
| 2976 | Int8PtrTy, |
| 2977 | SymtabPtrTy, |
| 2978 | NULL); |
| 2979 | CGM.getModule().addTypeName("struct._objc_module", ModuleTy); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 2980 | |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2981 | // Message send functions. |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 2982 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 2983 | // id objc_msgSend (id, SEL, ...) |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 2984 | std::vector<const llvm::Type*> Params; |
| 2985 | Params.push_back(ObjectPtrTy); |
| 2986 | Params.push_back(SelectorPtrTy); |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 2987 | MessageSendFn = |
| 2988 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 2989 | Params, |
| 2990 | true), |
| 2991 | "objc_msgSend"); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 2992 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 2993 | // id objc_msgSend_stret (id, SEL, ...) |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 2994 | Params.clear(); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 2995 | Params.push_back(ObjectPtrTy); |
| 2996 | Params.push_back(SelectorPtrTy); |
| 2997 | MessageSendStretFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 2998 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 2999 | Params, |
| 3000 | true), |
| 3001 | "objc_msgSend_stret"); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 3002 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3003 | // |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 3004 | Params.clear(); |
| 3005 | Params.push_back(ObjectPtrTy); |
| 3006 | Params.push_back(SelectorPtrTy); |
| 3007 | // FIXME: This should be long double on x86_64? |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3008 | // [double | long double] objc_msgSend_fpret(id self, SEL op, ...) |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 3009 | MessageSendFpretFn = |
| 3010 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy, |
| 3011 | Params, |
| 3012 | true), |
| 3013 | "objc_msgSend_fpret"); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3014 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3015 | // id objc_msgSendSuper(struct objc_super *super, SEL op, ...) |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3016 | Params.clear(); |
| 3017 | Params.push_back(SuperPtrTy); |
| 3018 | Params.push_back(SelectorPtrTy); |
| 3019 | MessageSendSuperFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3020 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3021 | Params, |
| 3022 | true), |
| 3023 | "objc_msgSendSuper"); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3024 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3025 | // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super, |
| 3026 | // SEL op, ...) |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3027 | Params.clear(); |
| 3028 | Params.push_back(Int8PtrTy); |
| 3029 | Params.push_back(SuperPtrTy); |
| 3030 | Params.push_back(SelectorPtrTy); |
| 3031 | MessageSendSuperStretFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3032 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3033 | Params, |
| 3034 | true), |
| 3035 | "objc_msgSendSuper_stret"); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 3036 | |
| 3037 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 3038 | MessageSendSuperFpretFn = MessageSendSuperFn; |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 3039 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3040 | // FIXME: This is the size of the setjmp buffer and should be |
| 3041 | // target specific. 18 is what's used on 32-bit X86. |
| 3042 | uint64_t SetJmpBufferSize = 18; |
| 3043 | |
| 3044 | // Exceptions |
| 3045 | const llvm::Type *StackPtrTy = |
Daniel Dunbar | 1000491 | 2008-09-27 06:32:25 +0000 | [diff] [blame] | 3046 | llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3047 | |
| 3048 | ExceptionDataTy = |
| 3049 | llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty, |
| 3050 | SetJmpBufferSize), |
| 3051 | StackPtrTy, NULL); |
| 3052 | CGM.getModule().addTypeName("struct._objc_exception_data", |
| 3053 | ExceptionDataTy); |
| 3054 | |
| 3055 | Params.clear(); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3056 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
| 3057 | ExceptionTryEnterFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3058 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3059 | Params, |
| 3060 | false), |
| 3061 | "objc_exception_try_enter"); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3062 | ExceptionTryExitFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3063 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3064 | Params, |
| 3065 | false), |
| 3066 | "objc_exception_try_exit"); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3067 | ExceptionExtractFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3068 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3069 | Params, |
| 3070 | false), |
| 3071 | "objc_exception_extract"); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3072 | |
| 3073 | Params.clear(); |
| 3074 | Params.push_back(ClassPtrTy); |
| 3075 | Params.push_back(ObjectPtrTy); |
| 3076 | ExceptionMatchFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3077 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 3078 | Params, |
| 3079 | false), |
| 3080 | "objc_exception_match"); |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 3081 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3082 | Params.clear(); |
| 3083 | Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty)); |
| 3084 | SetJmpFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3085 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 3086 | Params, |
| 3087 | false), |
| 3088 | "_setjmp"); |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 3089 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3090 | } |
| 3091 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3092 | ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3093 | : ObjCCommonTypesHelper(cgm) |
| 3094 | { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3095 | // struct _method_list_t { |
| 3096 | // uint32_t entsize; // sizeof(struct _objc_method) |
| 3097 | // uint32_t method_count; |
| 3098 | // struct _objc_method method_list[method_count]; |
| 3099 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3100 | MethodListnfABITy = llvm::StructType::get(IntTy, |
| 3101 | IntTy, |
| 3102 | llvm::ArrayType::get(MethodTy, 0), |
| 3103 | NULL); |
| 3104 | CGM.getModule().addTypeName("struct.__method_list_t", |
| 3105 | MethodListnfABITy); |
| 3106 | // struct method_list_t * |
| 3107 | MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3108 | |
| 3109 | // struct _protocol_t { |
| 3110 | // id isa; // NULL |
| 3111 | // const char * const protocol_name; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3112 | // const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3113 | // const struct method_list_t * const instance_methods; |
| 3114 | // const struct method_list_t * const class_methods; |
| 3115 | // const struct method_list_t *optionalInstanceMethods; |
| 3116 | // const struct method_list_t *optionalClassMethods; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3117 | // const struct _prop_list_t * properties; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3118 | // const uint32_t size; // sizeof(struct _protocol_t) |
| 3119 | // const uint32_t flags; // = 0 |
| 3120 | // } |
| 3121 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3122 | // Holder for struct _protocol_list_t * |
| 3123 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 3124 | |
| 3125 | ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy, |
| 3126 | Int8PtrTy, |
| 3127 | llvm::PointerType::getUnqual( |
| 3128 | ProtocolListTyHolder), |
| 3129 | MethodListnfABIPtrTy, |
| 3130 | MethodListnfABIPtrTy, |
| 3131 | MethodListnfABIPtrTy, |
| 3132 | MethodListnfABIPtrTy, |
| 3133 | PropertyListPtrTy, |
| 3134 | IntTy, |
| 3135 | IntTy, |
| 3136 | NULL); |
| 3137 | CGM.getModule().addTypeName("struct._protocol_t", |
| 3138 | ProtocolnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3139 | |
| 3140 | // struct _protocol_t* |
| 3141 | ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3142 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 3143 | // struct _protocol_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3144 | // long protocol_count; // Note, this is 32/64 bit |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3145 | // struct _protocol_t *[protocol_count]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3146 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3147 | ProtocolListnfABITy = llvm::StructType::get(LongTy, |
| 3148 | llvm::ArrayType::get( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3149 | ProtocolnfABIPtrTy, 0), |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3150 | NULL); |
| 3151 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 3152 | ProtocolListnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3153 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo( |
| 3154 | ProtocolListnfABITy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3155 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3156 | // struct _objc_protocol_list* |
| 3157 | ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3158 | |
| 3159 | // struct _ivar_t { |
| 3160 | // unsigned long int *offset; // pointer to ivar offset location |
| 3161 | // char *name; |
| 3162 | // char *type; |
| 3163 | // uint32_t alignment; |
| 3164 | // uint32_t size; |
| 3165 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3166 | IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy), |
| 3167 | Int8PtrTy, |
| 3168 | Int8PtrTy, |
| 3169 | IntTy, |
| 3170 | IntTy, |
| 3171 | NULL); |
| 3172 | CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy); |
| 3173 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3174 | // struct _ivar_list_t { |
| 3175 | // uint32 entsize; // sizeof(struct _ivar_t) |
| 3176 | // uint32 count; |
| 3177 | // struct _iver_t list[count]; |
| 3178 | // } |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3179 | IvarListnfABITy = llvm::StructType::get(IntTy, |
| 3180 | IntTy, |
| 3181 | llvm::ArrayType::get( |
| 3182 | IvarnfABITy, 0), |
| 3183 | NULL); |
| 3184 | CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy); |
| 3185 | |
| 3186 | IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3187 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3188 | // struct _class_ro_t { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3189 | // uint32_t const flags; |
| 3190 | // uint32_t const instanceStart; |
| 3191 | // uint32_t const instanceSize; |
| 3192 | // uint32_t const reserved; // only when building for 64bit targets |
| 3193 | // const uint8_t * const ivarLayout; |
| 3194 | // const char *const name; |
| 3195 | // const struct _method_list_t * const baseMethods; |
| 3196 | // const struct _objc_protocol_list *const baseProtocols; |
| 3197 | // const struct _ivar_list_t *const ivars; |
| 3198 | // const uint8_t * const weakIvarLayout; |
| 3199 | // const struct _prop_list_t * const properties; |
| 3200 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3201 | |
| 3202 | // FIXME. Add 'reserved' field in 64bit abi mode! |
| 3203 | ClassRonfABITy = llvm::StructType::get(IntTy, |
| 3204 | IntTy, |
| 3205 | IntTy, |
| 3206 | Int8PtrTy, |
| 3207 | Int8PtrTy, |
| 3208 | MethodListnfABIPtrTy, |
| 3209 | ProtocolListnfABIPtrTy, |
| 3210 | IvarListnfABIPtrTy, |
| 3211 | Int8PtrTy, |
| 3212 | PropertyListPtrTy, |
| 3213 | NULL); |
| 3214 | CGM.getModule().addTypeName("struct._class_ro_t", |
| 3215 | ClassRonfABITy); |
| 3216 | |
| 3217 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 3218 | std::vector<const llvm::Type*> Params; |
| 3219 | Params.push_back(ObjectPtrTy); |
| 3220 | Params.push_back(SelectorPtrTy); |
| 3221 | ImpnfABITy = llvm::PointerType::getUnqual( |
| 3222 | llvm::FunctionType::get(ObjectPtrTy, Params, false)); |
| 3223 | |
| 3224 | // struct _class_t { |
| 3225 | // struct _class_t *isa; |
| 3226 | // struct _class_t * const superclass; |
| 3227 | // void *cache; |
| 3228 | // IMP *vtable; |
| 3229 | // struct class_ro_t *ro; |
| 3230 | // } |
| 3231 | |
| 3232 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 3233 | ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 3234 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 3235 | CachePtrTy, |
| 3236 | llvm::PointerType::getUnqual(ImpnfABITy), |
| 3237 | llvm::PointerType::getUnqual( |
| 3238 | ClassRonfABITy), |
| 3239 | NULL); |
| 3240 | CGM.getModule().addTypeName("struct._class_t", ClassnfABITy); |
| 3241 | |
| 3242 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo( |
| 3243 | ClassnfABITy); |
| 3244 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3245 | // LLVM for struct _class_t * |
| 3246 | ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy); |
| 3247 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3248 | // struct _category_t { |
| 3249 | // const char * const name; |
| 3250 | // struct _class_t *const cls; |
| 3251 | // const struct _method_list_t * const instance_methods; |
| 3252 | // const struct _method_list_t * const class_methods; |
| 3253 | // const struct _protocol_list_t * const protocols; |
| 3254 | // const struct _prop_list_t * const properties; |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3255 | // } |
| 3256 | CategorynfABITy = llvm::StructType::get(Int8PtrTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3257 | ClassnfABIPtrTy, |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3258 | MethodListnfABIPtrTy, |
| 3259 | MethodListnfABIPtrTy, |
| 3260 | ProtocolListnfABIPtrTy, |
| 3261 | PropertyListPtrTy, |
| 3262 | NULL); |
| 3263 | CGM.getModule().addTypeName("struct._category_t", CategorynfABITy); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3264 | |
| 3265 | // New types for nonfragile abi messaging. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3266 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3267 | ASTContext &Ctx = CGM.getContext(); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3268 | |
| 3269 | // MessageRefTy - LLVM for: |
| 3270 | // struct _message_ref_t { |
| 3271 | // IMP messenger; |
| 3272 | // SEL name; |
| 3273 | // }; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3274 | |
| 3275 | // First the clang type for struct _message_ref_t |
| 3276 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 3277 | SourceLocation(), |
| 3278 | &Ctx.Idents.get("_message_ref_t")); |
| 3279 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3280 | Ctx.VoidPtrTy, 0, false)); |
| 3281 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3282 | Ctx.getObjCSelType(), 0, false)); |
| 3283 | RD->completeDefinition(Ctx); |
| 3284 | |
| 3285 | MessageRefCTy = Ctx.getTagDeclType(RD); |
| 3286 | MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy); |
| 3287 | MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy)); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3288 | |
| 3289 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
| 3290 | MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy); |
| 3291 | |
| 3292 | // SuperMessageRefTy - LLVM for: |
| 3293 | // struct _super_message_ref_t { |
| 3294 | // SUPER_IMP messenger; |
| 3295 | // SEL name; |
| 3296 | // }; |
| 3297 | SuperMessageRefTy = llvm::StructType::get(ImpnfABITy, |
| 3298 | SelectorPtrTy, |
| 3299 | NULL); |
| 3300 | CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy); |
| 3301 | |
| 3302 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
| 3303 | SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy); |
| 3304 | |
| 3305 | // id objc_msgSend_fixup (id, struct message_ref_t*, ...) |
| 3306 | Params.clear(); |
| 3307 | Params.push_back(ObjectPtrTy); |
| 3308 | Params.push_back(MessageRefPtrTy); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 3309 | MessengerTy = llvm::FunctionType::get(ObjectPtrTy, |
| 3310 | Params, |
| 3311 | true); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3312 | MessageSendFixupFn = |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 3313 | CGM.CreateRuntimeFunction(MessengerTy, |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3314 | "objc_msgSend_fixup"); |
| 3315 | |
| 3316 | // id objc_msgSend_fpret_fixup (id, struct message_ref_t*, ...) |
| 3317 | MessageSendFpretFixupFn = |
| 3318 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3319 | Params, |
| 3320 | true), |
| 3321 | "objc_msgSend_fpret_fixup"); |
| 3322 | |
| 3323 | // id objc_msgSend_stret_fixup (id, struct message_ref_t*, ...) |
| 3324 | MessageSendStretFixupFn = |
| 3325 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3326 | Params, |
| 3327 | true), |
| 3328 | "objc_msgSend_stret_fixup"); |
| 3329 | |
| 3330 | // id objc_msgSendId_fixup (id, struct message_ref_t*, ...) |
| 3331 | MessageSendIdFixupFn = |
| 3332 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3333 | Params, |
| 3334 | true), |
| 3335 | "objc_msgSendId_fixup"); |
| 3336 | |
| 3337 | |
| 3338 | // id objc_msgSendId_stret_fixup (id, struct message_ref_t*, ...) |
| 3339 | MessageSendIdStretFixupFn = |
| 3340 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3341 | Params, |
| 3342 | true), |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3343 | "objc_msgSendId_stret_fixup"); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3344 | |
| 3345 | // id objc_msgSendSuper2_fixup (struct objc_super *, |
| 3346 | // struct _super_message_ref_t*, ...) |
| 3347 | Params.clear(); |
| 3348 | Params.push_back(SuperPtrTy); |
| 3349 | Params.push_back(SuperMessageRefPtrTy); |
| 3350 | MessageSendSuper2FixupFn = |
| 3351 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3352 | Params, |
| 3353 | true), |
| 3354 | "objc_msgSendSuper2_fixup"); |
| 3355 | |
| 3356 | |
| 3357 | // id objc_msgSendSuper2_stret_fixup (struct objc_super *, |
| 3358 | // struct _super_message_ref_t*, ...) |
| 3359 | MessageSendSuper2StretFixupFn = |
| 3360 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3361 | Params, |
| 3362 | true), |
| 3363 | "objc_msgSendSuper2_stret_fixup"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 3364 | |
| 3365 | Params.clear(); |
| 3366 | llvm::Constant *Personality = |
| 3367 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 3368 | Params, |
| 3369 | true), |
| 3370 | "__objc_personality_v0"); |
| 3371 | EHPersonalityPtr = llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy); |
| 3372 | |
| 3373 | Params.clear(); |
| 3374 | Params.push_back(Int8PtrTy); |
| 3375 | UnwindResumeOrRethrowFn = |
| 3376 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3377 | Params, |
| 3378 | false), |
| 3379 | "_Unwind_Resume_or_Rethrow"); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3380 | } |
| 3381 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3382 | llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() { |
| 3383 | FinishNonFragileABIModule(); |
| 3384 | |
| 3385 | return NULL; |
| 3386 | } |
| 3387 | |
| 3388 | void CGObjCNonFragileABIMac::FinishNonFragileABIModule() { |
| 3389 | // nonfragile abi has no module definition. |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 3390 | |
| 3391 | // Build list of all implemented classe addresses in array |
| 3392 | // L_OBJC_LABEL_CLASS_$. |
| 3393 | // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$ |
| 3394 | // list of 'nonlazy' implementations (defined as those with a +load{} |
| 3395 | // method!!). |
| 3396 | unsigned NumClasses = DefinedClasses.size(); |
| 3397 | if (NumClasses) { |
| 3398 | std::vector<llvm::Constant*> Symbols(NumClasses); |
| 3399 | for (unsigned i=0; i<NumClasses; i++) |
| 3400 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
| 3401 | ObjCTypes.Int8PtrTy); |
| 3402 | llvm::Constant* Init = |
| 3403 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 3404 | NumClasses), |
| 3405 | Symbols); |
| 3406 | |
| 3407 | llvm::GlobalVariable *GV = |
| 3408 | new llvm::GlobalVariable(Init->getType(), false, |
| 3409 | llvm::GlobalValue::InternalLinkage, |
| 3410 | Init, |
| 3411 | "\01L_OBJC_LABEL_CLASS_$", |
| 3412 | &CGM.getModule()); |
Fariborz Jahanian | 5a1edf6 | 2009-02-28 00:54:00 +0000 | [diff] [blame] | 3413 | GV->setAlignment( |
| 3414 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.Int8PtrTy)); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 3415 | GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip"); |
| 3416 | UsedGlobals.push_back(GV); |
| 3417 | } |
| 3418 | |
| 3419 | // Build list of all implemented category addresses in array |
| 3420 | // L_OBJC_LABEL_CATEGORY_$. |
| 3421 | // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$ |
| 3422 | // list of 'nonlazy' category implementations (defined as those with a +load{} |
| 3423 | // method!!). |
| 3424 | unsigned NumCategory = DefinedCategories.size(); |
| 3425 | if (NumCategory) { |
| 3426 | std::vector<llvm::Constant*> Symbols(NumCategory); |
| 3427 | for (unsigned i=0; i<NumCategory; i++) |
| 3428 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
| 3429 | ObjCTypes.Int8PtrTy); |
| 3430 | llvm::Constant* Init = |
| 3431 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 3432 | NumCategory), |
| 3433 | Symbols); |
| 3434 | |
| 3435 | llvm::GlobalVariable *GV = |
| 3436 | new llvm::GlobalVariable(Init->getType(), false, |
| 3437 | llvm::GlobalValue::InternalLinkage, |
| 3438 | Init, |
| 3439 | "\01L_OBJC_LABEL_CATEGORY_$", |
| 3440 | &CGM.getModule()); |
| 3441 | GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip"); |
| 3442 | UsedGlobals.push_back(GV); |
| 3443 | } |
| 3444 | |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 3445 | // static int L_OBJC_IMAGE_INFO[2] = { 0, flags }; |
| 3446 | // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0 |
| 3447 | std::vector<llvm::Constant*> Values(2); |
| 3448 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0); |
Fariborz Jahanian | 067986e | 2009-02-24 21:08:09 +0000 | [diff] [blame] | 3449 | unsigned int flags = 0; |
Fariborz Jahanian | 66a5c2c | 2009-02-24 23:34:44 +0000 | [diff] [blame] | 3450 | // FIXME: Fix and continue? |
| 3451 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 3452 | flags |= eImageInfo_GarbageCollected; |
| 3453 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 3454 | flags |= eImageInfo_GCOnly; |
Fariborz Jahanian | 067986e | 2009-02-24 21:08:09 +0000 | [diff] [blame] | 3455 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 3456 | llvm::Constant* Init = llvm::ConstantArray::get( |
| 3457 | llvm::ArrayType::get(ObjCTypes.IntTy, 2), |
| 3458 | Values); |
| 3459 | llvm::GlobalVariable *IMGV = |
| 3460 | new llvm::GlobalVariable(Init->getType(), false, |
| 3461 | llvm::GlobalValue::InternalLinkage, |
| 3462 | Init, |
| 3463 | "\01L_OBJC_IMAGE_INFO", |
| 3464 | &CGM.getModule()); |
| 3465 | IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip"); |
| 3466 | UsedGlobals.push_back(IMGV); |
| 3467 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3468 | std::vector<llvm::Constant*> Used; |
| 3469 | for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), |
| 3470 | e = UsedGlobals.end(); i != e; ++i) { |
| 3471 | Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); |
| 3472 | } |
| 3473 | |
| 3474 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); |
| 3475 | llvm::GlobalValue *GV = |
| 3476 | new llvm::GlobalVariable(AT, false, |
| 3477 | llvm::GlobalValue::AppendingLinkage, |
| 3478 | llvm::ConstantArray::get(AT, Used), |
| 3479 | "llvm.used", |
| 3480 | &CGM.getModule()); |
| 3481 | |
| 3482 | GV->setSection("llvm.metadata"); |
| 3483 | |
| 3484 | } |
| 3485 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3486 | // Metadata flags |
| 3487 | enum MetaDataDlags { |
| 3488 | CLS = 0x0, |
| 3489 | CLS_META = 0x1, |
| 3490 | CLS_ROOT = 0x2, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 3491 | OBJC2_CLS_HIDDEN = 0x10, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3492 | CLS_EXCEPTION = 0x20 |
| 3493 | }; |
| 3494 | /// BuildClassRoTInitializer - generate meta-data for: |
| 3495 | /// struct _class_ro_t { |
| 3496 | /// uint32_t const flags; |
| 3497 | /// uint32_t const instanceStart; |
| 3498 | /// uint32_t const instanceSize; |
| 3499 | /// uint32_t const reserved; // only when building for 64bit targets |
| 3500 | /// const uint8_t * const ivarLayout; |
| 3501 | /// const char *const name; |
| 3502 | /// const struct _method_list_t * const baseMethods; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 3503 | /// const struct _protocol_list_t *const baseProtocols; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3504 | /// const struct _ivar_list_t *const ivars; |
| 3505 | /// const uint8_t * const weakIvarLayout; |
| 3506 | /// const struct _prop_list_t * const properties; |
| 3507 | /// } |
| 3508 | /// |
| 3509 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer( |
| 3510 | unsigned flags, |
| 3511 | unsigned InstanceStart, |
| 3512 | unsigned InstanceSize, |
| 3513 | const ObjCImplementationDecl *ID) { |
| 3514 | std::string ClassName = ID->getNameAsString(); |
| 3515 | std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets! |
| 3516 | Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
| 3517 | Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart); |
| 3518 | Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize); |
| 3519 | // FIXME. For 64bit targets add 0 here. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 3520 | // FIXME. ivarLayout is currently null! |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3521 | Values[ 3] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 3522 | Values[ 4] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 3523 | // const struct _method_list_t * const baseMethods; |
| 3524 | std::vector<llvm::Constant*> Methods; |
| 3525 | std::string MethodListName("\01l_OBJC_$_"); |
| 3526 | if (flags & CLS_META) { |
| 3527 | MethodListName += "CLASS_METHODS_" + ID->getNameAsString(); |
| 3528 | for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(), |
| 3529 | e = ID->classmeth_end(); i != e; ++i) { |
| 3530 | // Class methods should always be defined. |
| 3531 | Methods.push_back(GetMethodConstant(*i)); |
| 3532 | } |
| 3533 | } else { |
| 3534 | MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString(); |
| 3535 | for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(), |
| 3536 | e = ID->instmeth_end(); i != e; ++i) { |
| 3537 | // Instance methods should always be defined. |
| 3538 | Methods.push_back(GetMethodConstant(*i)); |
| 3539 | } |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 3540 | for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(), |
| 3541 | e = ID->propimpl_end(); i != e; ++i) { |
| 3542 | ObjCPropertyImplDecl *PID = *i; |
| 3543 | |
| 3544 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){ |
| 3545 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 3546 | |
| 3547 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 3548 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 3549 | Methods.push_back(C); |
| 3550 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 3551 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 3552 | Methods.push_back(C); |
| 3553 | } |
| 3554 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 3555 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 3556 | Values[ 5] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 3557 | "__DATA, __objc_const", Methods); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 3558 | |
| 3559 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 3560 | assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer"); |
| 3561 | Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_" |
| 3562 | + OID->getNameAsString(), |
| 3563 | OID->protocol_begin(), |
| 3564 | OID->protocol_end()); |
| 3565 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 3566 | if (flags & CLS_META) |
| 3567 | Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
| 3568 | else |
| 3569 | Values[ 7] = EmitIvarList(ID); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 3570 | // FIXME. weakIvarLayout is currently null. |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3571 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 3572 | if (flags & CLS_META) |
| 3573 | Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 3574 | else |
| 3575 | Values[ 9] = |
| 3576 | EmitPropertyList( |
| 3577 | "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
| 3578 | ID, ID->getClassInterface(), ObjCTypes); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3579 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy, |
| 3580 | Values); |
| 3581 | llvm::GlobalVariable *CLASS_RO_GV = |
| 3582 | new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false, |
| 3583 | llvm::GlobalValue::InternalLinkage, |
| 3584 | Init, |
| 3585 | (flags & CLS_META) ? |
| 3586 | std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName : |
| 3587 | std::string("\01l_OBJC_CLASS_RO_$_")+ClassName, |
| 3588 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 3589 | CLASS_RO_GV->setAlignment( |
| 3590 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 3591 | CLASS_RO_GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3592 | UsedGlobals.push_back(CLASS_RO_GV); |
| 3593 | return CLASS_RO_GV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 3594 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3595 | } |
| 3596 | |
| 3597 | /// BuildClassMetaData - This routine defines that to-level meta-data |
| 3598 | /// for the given ClassName for: |
| 3599 | /// struct _class_t { |
| 3600 | /// struct _class_t *isa; |
| 3601 | /// struct _class_t * const superclass; |
| 3602 | /// void *cache; |
| 3603 | /// IMP *vtable; |
| 3604 | /// struct class_ro_t *ro; |
| 3605 | /// } |
| 3606 | /// |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 3607 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData( |
| 3608 | std::string &ClassName, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3609 | llvm::Constant *IsAGV, |
| 3610 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 3611 | llvm::Constant *ClassRoGV, |
| 3612 | bool HiddenVisibility) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3613 | std::vector<llvm::Constant*> Values(5); |
| 3614 | Values[0] = IsAGV; |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 3615 | Values[1] = SuperClassGV |
| 3616 | ? SuperClassGV |
| 3617 | : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3618 | Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar |
| 3619 | Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar |
| 3620 | Values[4] = ClassRoGV; // &CLASS_RO_GV |
| 3621 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy, |
| 3622 | Values); |
| 3623 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(ClassName); |
| 3624 | if (GV) |
| 3625 | GV->setInitializer(Init); |
| 3626 | else |
| 3627 | GV = |
| 3628 | new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, |
| 3629 | llvm::GlobalValue::ExternalLinkage, |
| 3630 | Init, |
| 3631 | ClassName, |
| 3632 | &CGM.getModule()); |
Fariborz Jahanian | dd0db2a | 2009-01-31 01:07:39 +0000 | [diff] [blame] | 3633 | GV->setSection("__DATA, __objc_data"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 3634 | GV->setAlignment( |
| 3635 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy)); |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 3636 | if (HiddenVisibility) |
| 3637 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3638 | UsedGlobals.push_back(GV); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 3639 | return GV; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3640 | } |
| 3641 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3642 | void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) { |
| 3643 | std::string ClassName = ID->getNameAsString(); |
| 3644 | if (!ObjCEmptyCacheVar) { |
| 3645 | ObjCEmptyCacheVar = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3646 | ObjCTypes.CacheTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3647 | false, |
| 3648 | llvm::GlobalValue::ExternalLinkage, |
| 3649 | 0, |
Fariborz Jahanian | 07da367 | 2009-02-03 17:34:34 +0000 | [diff] [blame] | 3650 | "\01__objc_empty_cache", |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3651 | &CGM.getModule()); |
| 3652 | UsedGlobals.push_back(ObjCEmptyCacheVar); |
| 3653 | |
| 3654 | ObjCEmptyVtableVar = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3655 | ObjCTypes.ImpnfABITy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3656 | false, |
| 3657 | llvm::GlobalValue::ExternalLinkage, |
| 3658 | 0, |
Fariborz Jahanian | 07da367 | 2009-02-03 17:34:34 +0000 | [diff] [blame] | 3659 | "\01__objc_empty_vtable", |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3660 | &CGM.getModule()); |
| 3661 | UsedGlobals.push_back(ObjCEmptyVtableVar); |
| 3662 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 3663 | assert(ID->getClassInterface() && |
| 3664 | "CGObjCNonFragileABIMac::GenerateClass - class is 0"); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3665 | uint32_t InstanceStart = |
| 3666 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy); |
| 3667 | uint32_t InstanceSize = InstanceStart; |
| 3668 | uint32_t flags = CLS_META; |
| 3669 | std::string ObjCMetaClassName("\01_OBJC_METACLASS_$_"); |
| 3670 | std::string ObjCClassName("\01_OBJC_CLASS_$_"); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3671 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3672 | llvm::GlobalVariable *SuperClassGV, *IsAGV; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3673 | |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 3674 | bool classIsHidden = IsClassHidden(ID->getClassInterface()); |
| 3675 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 3676 | flags |= OBJC2_CLS_HIDDEN; |
| 3677 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3678 | // class is root |
| 3679 | flags |= CLS_ROOT; |
| 3680 | std::string SuperClassName = ObjCClassName + ClassName; |
| 3681 | SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 3682 | if (!SuperClassGV) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3683 | SuperClassGV = |
| 3684 | new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, |
| 3685 | llvm::GlobalValue::ExternalLinkage, |
| 3686 | 0, |
| 3687 | SuperClassName, |
| 3688 | &CGM.getModule()); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 3689 | UsedGlobals.push_back(SuperClassGV); |
| 3690 | } |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3691 | std::string IsAClassName = ObjCMetaClassName + ClassName; |
| 3692 | IsAGV = CGM.getModule().getGlobalVariable(IsAClassName); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 3693 | if (!IsAGV) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3694 | IsAGV = |
| 3695 | new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, |
| 3696 | llvm::GlobalValue::ExternalLinkage, |
| 3697 | 0, |
| 3698 | IsAClassName, |
| 3699 | &CGM.getModule()); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 3700 | UsedGlobals.push_back(IsAGV); |
| 3701 | } |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3702 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 3703 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 3704 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 3705 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 3706 | Root = Super; |
| 3707 | std::string RootClassName = ObjCMetaClassName + Root->getNameAsString(); |
| 3708 | IsAGV = CGM.getModule().getGlobalVariable(RootClassName); |
| 3709 | if (!IsAGV) { |
| 3710 | IsAGV = |
| 3711 | new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, |
| 3712 | llvm::GlobalValue::ExternalLinkage, |
| 3713 | 0, |
| 3714 | RootClassName, |
| 3715 | &CGM.getModule()); |
| 3716 | UsedGlobals.push_back(IsAGV); |
| 3717 | } |
| 3718 | // work on super class metadata symbol. |
| 3719 | std::string SuperClassName = |
| 3720 | ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3721 | SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 3722 | if (!SuperClassGV) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3723 | SuperClassGV = |
| 3724 | new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, |
| 3725 | llvm::GlobalValue::ExternalLinkage, |
| 3726 | 0, |
| 3727 | SuperClassName, |
| 3728 | &CGM.getModule()); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 3729 | UsedGlobals.push_back(SuperClassGV); |
| 3730 | } |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3731 | } |
| 3732 | llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags, |
| 3733 | InstanceStart, |
| 3734 | InstanceSize,ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 3735 | std::string TClassName = ObjCMetaClassName + ClassName; |
| 3736 | llvm::GlobalVariable *MetaTClass = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 3737 | BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV, |
| 3738 | classIsHidden); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 3739 | |
| 3740 | // Metadata for the class |
| 3741 | flags = CLS; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 3742 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 3743 | flags |= OBJC2_CLS_HIDDEN; |
| 3744 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 3745 | flags |= CLS_ROOT; |
| 3746 | SuperClassGV = 0; |
| 3747 | } |
| 3748 | else { |
| 3749 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 3750 | std::string RootClassName = |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 3751 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
| 3752 | std::string SuperClassName = ObjCClassName + RootClassName; |
| 3753 | SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 3754 | if (!SuperClassGV) { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 3755 | SuperClassGV = |
| 3756 | new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, |
| 3757 | llvm::GlobalValue::ExternalLinkage, |
| 3758 | 0, |
| 3759 | SuperClassName, |
| 3760 | &CGM.getModule()); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 3761 | UsedGlobals.push_back(SuperClassGV); |
| 3762 | } |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 3763 | } |
| 3764 | |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 3765 | InstanceStart = InstanceSize = 0; |
| 3766 | if (ObjCInterfaceDecl *OID = |
| 3767 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface())) { |
| 3768 | // FIXME. Share this with the one in EmitIvarList. |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 3769 | const llvm::Type *InterfaceTy = |
Fariborz Jahanian | f3710ba | 2009-02-14 20:13:28 +0000 | [diff] [blame] | 3770 | CGM.getTypes().ConvertType(CGM.getContext().buildObjCInterfaceType(OID)); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 3771 | const llvm::StructLayout *Layout = |
| 3772 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy)); |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 3773 | |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 3774 | RecordDecl::field_iterator firstField, lastField; |
| 3775 | const RecordDecl *RD = GetFirstIvarInRecord(OID, firstField, lastField); |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 3776 | |
| 3777 | for (RecordDecl::field_iterator e = RD->field_end(), |
| 3778 | ifield = firstField; ifield != e; ++ifield) |
| 3779 | lastField = ifield; |
| 3780 | |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 3781 | if (lastField != RD->field_end()) { |
| 3782 | FieldDecl *Field = *lastField; |
| 3783 | const llvm::Type *FieldTy = |
| 3784 | CGM.getTypes().ConvertTypeForMem(Field->getType()); |
| 3785 | unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy); |
| 3786 | InstanceSize = Layout->getElementOffset( |
| 3787 | CGM.getTypes().getLLVMFieldNo(Field)) + |
| 3788 | Size; |
| 3789 | if (firstField == RD->field_end()) |
| 3790 | InstanceStart = InstanceSize; |
| 3791 | else |
| 3792 | InstanceStart = Layout->getElementOffset(CGM.getTypes(). |
| 3793 | getLLVMFieldNo(*firstField)); |
| 3794 | } |
| 3795 | } |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 3796 | CLASS_RO_GV = BuildClassRoTInitializer(flags, |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 3797 | InstanceStart, |
| 3798 | InstanceSize, |
| 3799 | ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 3800 | |
| 3801 | TClassName = ObjCClassName + ClassName; |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 3802 | llvm::GlobalVariable *ClassMD = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 3803 | BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV, |
| 3804 | classIsHidden); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 3805 | DefinedClasses.push_back(ClassMD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3806 | } |
| 3807 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 3808 | /// GenerateProtocolRef - This routine is called to generate code for |
| 3809 | /// a protocol reference expression; as in: |
| 3810 | /// @code |
| 3811 | /// @protocol(Proto1); |
| 3812 | /// @endcode |
| 3813 | /// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1 |
| 3814 | /// which will hold address of the protocol meta-data. |
| 3815 | /// |
| 3816 | llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder, |
| 3817 | const ObjCProtocolDecl *PD) { |
| 3818 | |
| 3819 | llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), |
| 3820 | ObjCTypes.ExternalProtocolPtrTy); |
| 3821 | |
| 3822 | std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_"); |
| 3823 | ProtocolName += PD->getNameAsCString(); |
| 3824 | |
| 3825 | llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName); |
| 3826 | if (PTGV) |
| 3827 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 3828 | PTGV = new llvm::GlobalVariable( |
| 3829 | Init->getType(), false, |
| 3830 | llvm::GlobalValue::WeakLinkage, |
| 3831 | Init, |
| 3832 | ProtocolName, |
| 3833 | &CGM.getModule()); |
| 3834 | PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip"); |
| 3835 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 3836 | UsedGlobals.push_back(PTGV); |
| 3837 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 3838 | } |
| 3839 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 3840 | /// GenerateCategory - Build metadata for a category implementation. |
| 3841 | /// struct _category_t { |
| 3842 | /// const char * const name; |
| 3843 | /// struct _class_t *const cls; |
| 3844 | /// const struct _method_list_t * const instance_methods; |
| 3845 | /// const struct _method_list_t * const class_methods; |
| 3846 | /// const struct _protocol_list_t * const protocols; |
| 3847 | /// const struct _prop_list_t * const properties; |
| 3848 | /// } |
| 3849 | /// |
| 3850 | void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) |
| 3851 | { |
| 3852 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 3853 | const char *Prefix = "\01l_OBJC_$_CATEGORY_"; |
| 3854 | std::string ExtCatName(Prefix + Interface->getNameAsString()+ |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 3855 | "_$_" + OCD->getNameAsString()); |
| 3856 | std::string ExtClassName("\01_OBJC_CLASS_$_" + Interface->getNameAsString()); |
| 3857 | |
| 3858 | std::vector<llvm::Constant*> Values(6); |
| 3859 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 3860 | // meta-class entry symbol |
| 3861 | llvm::GlobalVariable *ClassGV = |
| 3862 | CGM.getModule().getGlobalVariable(ExtClassName); |
| 3863 | if (!ClassGV) |
| 3864 | ClassGV = |
| 3865 | new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, |
| 3866 | llvm::GlobalValue::ExternalLinkage, |
| 3867 | 0, |
| 3868 | ExtClassName, |
| 3869 | &CGM.getModule()); |
| 3870 | UsedGlobals.push_back(ClassGV); |
| 3871 | Values[1] = ClassGV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 3872 | std::vector<llvm::Constant*> Methods; |
| 3873 | std::string MethodListName(Prefix); |
| 3874 | MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() + |
| 3875 | "_$_" + OCD->getNameAsString(); |
| 3876 | |
| 3877 | for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(), |
| 3878 | e = OCD->instmeth_end(); i != e; ++i) { |
| 3879 | // Instance methods should always be defined. |
| 3880 | Methods.push_back(GetMethodConstant(*i)); |
| 3881 | } |
| 3882 | |
| 3883 | Values[2] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 3884 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 3885 | Methods); |
| 3886 | |
| 3887 | MethodListName = Prefix; |
| 3888 | MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" + |
| 3889 | OCD->getNameAsString(); |
| 3890 | Methods.clear(); |
| 3891 | for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(), |
| 3892 | e = OCD->classmeth_end(); i != e; ++i) { |
| 3893 | // Class methods should always be defined. |
| 3894 | Methods.push_back(GetMethodConstant(*i)); |
| 3895 | } |
| 3896 | |
| 3897 | Values[3] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 3898 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 3899 | Methods); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 3900 | const ObjCCategoryDecl *Category = |
| 3901 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 3902 | if (Category) { |
| 3903 | std::string ExtName(Interface->getNameAsString() + "_$_" + |
| 3904 | OCD->getNameAsString()); |
| 3905 | Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_" |
| 3906 | + Interface->getNameAsString() + "_$_" |
| 3907 | + Category->getNameAsString(), |
| 3908 | Category->protocol_begin(), |
| 3909 | Category->protocol_end()); |
| 3910 | Values[5] = |
| 3911 | EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName, |
| 3912 | OCD, Category, ObjCTypes); |
| 3913 | } |
| 3914 | else { |
| 3915 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 3916 | Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 3917 | } |
| 3918 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 3919 | llvm::Constant *Init = |
| 3920 | llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy, |
| 3921 | Values); |
| 3922 | llvm::GlobalVariable *GCATV |
| 3923 | = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy, |
| 3924 | false, |
| 3925 | llvm::GlobalValue::InternalLinkage, |
| 3926 | Init, |
| 3927 | ExtCatName, |
| 3928 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 3929 | GCATV->setAlignment( |
| 3930 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 3931 | GCATV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 3932 | UsedGlobals.push_back(GCATV); |
| 3933 | DefinedCategories.push_back(GCATV); |
| 3934 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 3935 | |
| 3936 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 3937 | /// given method if it has been defined. The result is null if the |
| 3938 | /// method has not been defined. The return value has type MethodPtrTy. |
| 3939 | llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant( |
| 3940 | const ObjCMethodDecl *MD) { |
| 3941 | // FIXME: Use DenseMap::lookup |
| 3942 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 3943 | if (!Fn) |
| 3944 | return 0; |
| 3945 | |
| 3946 | std::vector<llvm::Constant*> Method(3); |
| 3947 | Method[0] = |
| 3948 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 3949 | ObjCTypes.SelectorPtrTy); |
| 3950 | Method[1] = GetMethodVarType(MD); |
| 3951 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 3952 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 3953 | } |
| 3954 | |
| 3955 | /// EmitMethodList - Build meta-data for method declarations |
| 3956 | /// struct _method_list_t { |
| 3957 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 3958 | /// uint32_t method_count; |
| 3959 | /// struct _objc_method method_list[method_count]; |
| 3960 | /// } |
| 3961 | /// |
| 3962 | llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList( |
| 3963 | const std::string &Name, |
| 3964 | const char *Section, |
| 3965 | const ConstantVector &Methods) { |
| 3966 | // Return null for empty list. |
| 3967 | if (Methods.empty()) |
| 3968 | return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy); |
| 3969 | |
| 3970 | std::vector<llvm::Constant*> Values(3); |
| 3971 | // sizeof(struct _objc_method) |
| 3972 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy); |
| 3973 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 3974 | // method_count |
| 3975 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 3976 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 3977 | Methods.size()); |
| 3978 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 3979 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 3980 | |
| 3981 | llvm::GlobalVariable *GV = |
| 3982 | new llvm::GlobalVariable(Init->getType(), false, |
| 3983 | llvm::GlobalValue::InternalLinkage, |
| 3984 | Init, |
| 3985 | Name, |
| 3986 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 3987 | GV->setAlignment( |
| 3988 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 3989 | GV->setSection(Section); |
| 3990 | UsedGlobals.push_back(GV); |
| 3991 | return llvm::ConstantExpr::getBitCast(GV, |
| 3992 | ObjCTypes.MethodListnfABIPtrTy); |
| 3993 | } |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 3994 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 3995 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 3996 | /// the given ivar. |
| 3997 | /// |
| 3998 | llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable( |
| 3999 | std::string &Name, |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 4000 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4001 | const ObjCIvarDecl *Ivar) { |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 4002 | Name += "\01_OBJC_IVAR_$_" + |
| 4003 | getInterfaceDeclForIvar(ID, Ivar)->getNameAsString() + '.' |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4004 | + Ivar->getNameAsString(); |
| 4005 | llvm::GlobalVariable *IvarOffsetGV = |
| 4006 | CGM.getModule().getGlobalVariable(Name); |
| 4007 | if (!IvarOffsetGV) |
| 4008 | IvarOffsetGV = |
| 4009 | new llvm::GlobalVariable(ObjCTypes.LongTy, |
| 4010 | false, |
| 4011 | llvm::GlobalValue::ExternalLinkage, |
| 4012 | 0, |
| 4013 | Name, |
| 4014 | &CGM.getModule()); |
| 4015 | return IvarOffsetGV; |
| 4016 | } |
| 4017 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4018 | llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar( |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4019 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4020 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4021 | unsigned long int Offset) { |
| 4022 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4023 | assert(ID && "EmitIvarOffsetVar - null interface decl."); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4024 | std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.' |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4025 | + Ivar->getNameAsString()); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4026 | llvm::Constant *Init = llvm::ConstantInt::get(ObjCTypes.LongTy, Offset); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4027 | |
| 4028 | llvm::GlobalVariable *IvarOffsetGV = |
| 4029 | CGM.getModule().getGlobalVariable(ExternalName); |
| 4030 | if (IvarOffsetGV) { |
| 4031 | // ivar offset symbol already built due to user code referencing it. |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4032 | IvarOffsetGV->setAlignment( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4033 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4034 | IvarOffsetGV->setInitializer(Init); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4035 | IvarOffsetGV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4036 | UsedGlobals.push_back(IvarOffsetGV); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4037 | return IvarOffsetGV; |
| 4038 | } |
| 4039 | |
| 4040 | IvarOffsetGV = |
| 4041 | new llvm::GlobalVariable(Init->getType(), |
| 4042 | false, |
| 4043 | llvm::GlobalValue::ExternalLinkage, |
| 4044 | Init, |
| 4045 | ExternalName, |
| 4046 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4047 | IvarOffsetGV->setAlignment( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4048 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy)); |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4049 | // @private and @package have hidden visibility. |
| 4050 | bool globalVisibility = (Ivar->getAccessControl() == ObjCIvarDecl::Public || |
| 4051 | Ivar->getAccessControl() == ObjCIvarDecl::Protected); |
| 4052 | if (!globalVisibility) |
| 4053 | IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4054 | else |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4055 | if (IsClassHidden(ID)) |
| 4056 | IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4057 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4058 | IvarOffsetGV->setSection("__DATA, __objc_const"); |
| 4059 | UsedGlobals.push_back(IvarOffsetGV); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4060 | return IvarOffsetGV; |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4061 | } |
| 4062 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4063 | /// EmitIvarList - Emit the ivar list for the given |
| 4064 | /// implementation. If ForClass is true the list of class ivars |
| 4065 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 4066 | /// interface ivars will be emitted. The return value has type |
| 4067 | /// IvarListnfABIPtrTy. |
| 4068 | /// struct _ivar_t { |
| 4069 | /// unsigned long int *offset; // pointer to ivar offset location |
| 4070 | /// char *name; |
| 4071 | /// char *type; |
| 4072 | /// uint32_t alignment; |
| 4073 | /// uint32_t size; |
| 4074 | /// } |
| 4075 | /// struct _ivar_list_t { |
| 4076 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 4077 | /// uint32 count; |
| 4078 | /// struct _iver_t list[count]; |
| 4079 | /// } |
| 4080 | /// |
| 4081 | llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList( |
| 4082 | const ObjCImplementationDecl *ID) { |
| 4083 | |
| 4084 | std::vector<llvm::Constant*> Ivars, Ivar(5); |
| 4085 | |
| 4086 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4087 | assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface"); |
| 4088 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4089 | // FIXME. Consolidate this with similar code in GenerateClass. |
| 4090 | const llvm::Type *InterfaceTy = |
| 4091 | CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType( |
| 4092 | const_cast<ObjCInterfaceDecl*>(OID))); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4093 | const llvm::StructLayout *Layout = |
| 4094 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy)); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 4095 | |
| 4096 | RecordDecl::field_iterator i,p; |
| 4097 | const RecordDecl *RD = GetFirstIvarInRecord(OID, i,p); |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4098 | ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(); |
| 4099 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4100 | for (RecordDecl::field_iterator e = RD->field_end(); i != e; ++i) { |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4101 | FieldDecl *Field = *i; |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4102 | unsigned long offset = Layout->getElementOffset(CGM.getTypes(). |
| 4103 | getLLVMFieldNo(Field)); |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4104 | const ObjCIvarDecl *ivarDecl = *I++; |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4105 | Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), ivarDecl, offset); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4106 | if (Field->getIdentifier()) |
| 4107 | Ivar[1] = GetMethodVarName(Field->getIdentifier()); |
| 4108 | else |
| 4109 | Ivar[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 4110 | std::string TypeStr; |
| 4111 | CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); |
| 4112 | Ivar[2] = GetMethodVarType(TypeStr); |
| 4113 | const llvm::Type *FieldTy = |
| 4114 | CGM.getTypes().ConvertTypeForMem(Field->getType()); |
| 4115 | unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy); |
| 4116 | unsigned Align = CGM.getContext().getPreferredTypeAlign( |
| 4117 | Field->getType().getTypePtr()) >> 3; |
| 4118 | Align = llvm::Log2_32(Align); |
| 4119 | Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align); |
Fariborz Jahanian | 07236ba | 2009-01-27 22:27:56 +0000 | [diff] [blame] | 4120 | // NOTE. Size of a bitfield does not match gcc's, because of the way |
| 4121 | // bitfields are treated special in each. But I am told that 'size' |
| 4122 | // for bitfield ivars is ignored by the runtime so it does not matter. |
| 4123 | // (even if it matters, some day, there is enough info. to get the bitfield |
| 4124 | // right! |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4125 | Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4126 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar)); |
| 4127 | } |
| 4128 | // Return null for empty list. |
| 4129 | if (Ivars.empty()) |
| 4130 | return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
| 4131 | std::vector<llvm::Constant*> Values(3); |
| 4132 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy); |
| 4133 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4134 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 4135 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy, |
| 4136 | Ivars.size()); |
| 4137 | Values[2] = llvm::ConstantArray::get(AT, Ivars); |
| 4138 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4139 | const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_"; |
| 4140 | llvm::GlobalVariable *GV = |
| 4141 | new llvm::GlobalVariable(Init->getType(), false, |
| 4142 | llvm::GlobalValue::InternalLinkage, |
| 4143 | Init, |
| 4144 | Prefix + OID->getNameAsString(), |
| 4145 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4146 | GV->setAlignment( |
| 4147 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4148 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4149 | |
| 4150 | UsedGlobals.push_back(GV); |
| 4151 | return llvm::ConstantExpr::getBitCast(GV, |
| 4152 | ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4153 | } |
| 4154 | |
| 4155 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef( |
| 4156 | const ObjCProtocolDecl *PD) { |
| 4157 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4158 | |
| 4159 | if (!Entry) { |
| 4160 | // We use the initializer as a marker of whether this is a forward |
| 4161 | // reference or not. At module finalization we add the empty |
| 4162 | // contents for protocols which were referenced but never defined. |
| 4163 | Entry = |
| 4164 | new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false, |
| 4165 | llvm::GlobalValue::ExternalLinkage, |
| 4166 | 0, |
| 4167 | "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(), |
| 4168 | &CGM.getModule()); |
| 4169 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 4170 | UsedGlobals.push_back(Entry); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4171 | } |
| 4172 | |
| 4173 | return Entry; |
| 4174 | } |
| 4175 | |
| 4176 | /// GetOrEmitProtocol - Generate the protocol meta-data: |
| 4177 | /// @code |
| 4178 | /// struct _protocol_t { |
| 4179 | /// id isa; // NULL |
| 4180 | /// const char * const protocol_name; |
| 4181 | /// const struct _protocol_list_t * protocol_list; // super protocols |
| 4182 | /// const struct method_list_t * const instance_methods; |
| 4183 | /// const struct method_list_t * const class_methods; |
| 4184 | /// const struct method_list_t *optionalInstanceMethods; |
| 4185 | /// const struct method_list_t *optionalClassMethods; |
| 4186 | /// const struct _prop_list_t * properties; |
| 4187 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 4188 | /// const uint32_t flags; // = 0 |
| 4189 | /// } |
| 4190 | /// @endcode |
| 4191 | /// |
| 4192 | |
| 4193 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol( |
| 4194 | const ObjCProtocolDecl *PD) { |
| 4195 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4196 | |
| 4197 | // Early exit if a defining object has already been generated. |
| 4198 | if (Entry && Entry->hasInitializer()) |
| 4199 | return Entry; |
| 4200 | |
| 4201 | const char *ProtocolName = PD->getNameAsCString(); |
| 4202 | |
| 4203 | // Construct method lists. |
| 4204 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 4205 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
| 4206 | for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(), |
| 4207 | e = PD->instmeth_end(); i != e; ++i) { |
| 4208 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4209 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4210 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4211 | OptInstanceMethods.push_back(C); |
| 4212 | } else { |
| 4213 | InstanceMethods.push_back(C); |
| 4214 | } |
| 4215 | } |
| 4216 | |
| 4217 | for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(), |
| 4218 | e = PD->classmeth_end(); i != e; ++i) { |
| 4219 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4220 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4221 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4222 | OptClassMethods.push_back(C); |
| 4223 | } else { |
| 4224 | ClassMethods.push_back(C); |
| 4225 | } |
| 4226 | } |
| 4227 | |
| 4228 | std::vector<llvm::Constant*> Values(10); |
| 4229 | // isa is NULL |
| 4230 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy); |
| 4231 | Values[1] = GetClassName(PD->getIdentifier()); |
| 4232 | Values[2] = EmitProtocolList( |
| 4233 | "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(), |
| 4234 | PD->protocol_begin(), |
| 4235 | PD->protocol_end()); |
| 4236 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4237 | Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4238 | + PD->getNameAsString(), |
| 4239 | "__DATA, __objc_const", |
| 4240 | InstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4241 | Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4242 | + PD->getNameAsString(), |
| 4243 | "__DATA, __objc_const", |
| 4244 | ClassMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4245 | Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4246 | + PD->getNameAsString(), |
| 4247 | "__DATA, __objc_const", |
| 4248 | OptInstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4249 | Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4250 | + PD->getNameAsString(), |
| 4251 | "__DATA, __objc_const", |
| 4252 | OptClassMethods); |
| 4253 | Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(), |
| 4254 | 0, PD, ObjCTypes); |
| 4255 | uint32_t Size = |
| 4256 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy); |
| 4257 | Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4258 | Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy); |
| 4259 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy, |
| 4260 | Values); |
| 4261 | |
| 4262 | if (Entry) { |
| 4263 | // Already created, fix the linkage and update the initializer. |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4264 | Entry->setLinkage(llvm::GlobalValue::WeakLinkage); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4265 | Entry->setInitializer(Init); |
| 4266 | } else { |
| 4267 | Entry = |
| 4268 | new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false, |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4269 | llvm::GlobalValue::WeakLinkage, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4270 | Init, |
| 4271 | std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName, |
| 4272 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4273 | Entry->setAlignment( |
| 4274 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4275 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4276 | } |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4277 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4278 | |
| 4279 | // Use this protocol meta-data to build protocol list table in section |
| 4280 | // __DATA, __objc_protolist |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4281 | llvm::GlobalVariable *PTGV = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4282 | ObjCTypes.ProtocolnfABIPtrTy, false, |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4283 | llvm::GlobalValue::WeakLinkage, |
| 4284 | Entry, |
| 4285 | std::string("\01l_OBJC_LABEL_PROTOCOL_$_") |
| 4286 | +ProtocolName, |
| 4287 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4288 | PTGV->setAlignment( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4289 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy)); |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4290 | PTGV->setSection("__DATA, __objc_protolist"); |
| 4291 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4292 | UsedGlobals.push_back(PTGV); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4293 | return Entry; |
| 4294 | } |
| 4295 | |
| 4296 | /// EmitProtocolList - Generate protocol list meta-data: |
| 4297 | /// @code |
| 4298 | /// struct _protocol_list_t { |
| 4299 | /// long protocol_count; // Note, this is 32/64 bit |
| 4300 | /// struct _protocol_t[protocol_count]; |
| 4301 | /// } |
| 4302 | /// @endcode |
| 4303 | /// |
| 4304 | llvm::Constant * |
| 4305 | CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name, |
| 4306 | ObjCProtocolDecl::protocol_iterator begin, |
| 4307 | ObjCProtocolDecl::protocol_iterator end) { |
| 4308 | std::vector<llvm::Constant*> ProtocolRefs; |
| 4309 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4310 | // Just return null for empty protocol lists |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4311 | if (begin == end) |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4312 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 4313 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4314 | // FIXME: We shouldn't need to do this lookup here, should we? |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4315 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true); |
| 4316 | if (GV) |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4317 | return llvm::ConstantExpr::getBitCast(GV, |
| 4318 | ObjCTypes.ProtocolListnfABIPtrTy); |
| 4319 | |
| 4320 | for (; begin != end; ++begin) |
| 4321 | ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented??? |
| 4322 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4323 | // This list is null terminated. |
| 4324 | ProtocolRefs.push_back(llvm::Constant::getNullValue( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4325 | ObjCTypes.ProtocolnfABIPtrTy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4326 | |
| 4327 | std::vector<llvm::Constant*> Values(2); |
| 4328 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
| 4329 | Values[1] = |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4330 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4331 | ProtocolRefs.size()), |
| 4332 | ProtocolRefs); |
| 4333 | |
| 4334 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4335 | GV = new llvm::GlobalVariable(Init->getType(), false, |
| 4336 | llvm::GlobalValue::InternalLinkage, |
| 4337 | Init, |
| 4338 | Name, |
| 4339 | &CGM.getModule()); |
| 4340 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4341 | GV->setAlignment( |
| 4342 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4343 | UsedGlobals.push_back(GV); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4344 | return llvm::ConstantExpr::getBitCast(GV, |
| 4345 | ObjCTypes.ProtocolListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4346 | } |
| 4347 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4348 | /// GetMethodDescriptionConstant - This routine build following meta-data: |
| 4349 | /// struct _objc_method { |
| 4350 | /// SEL _cmd; |
| 4351 | /// char *method_type; |
| 4352 | /// char *_imp; |
| 4353 | /// } |
| 4354 | |
| 4355 | llvm::Constant * |
| 4356 | CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 4357 | std::vector<llvm::Constant*> Desc(3); |
| 4358 | Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 4359 | ObjCTypes.SelectorPtrTy); |
| 4360 | Desc[1] = GetMethodVarType(MD); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4361 | // Protocol methods have no implementation. So, this entry is always NULL. |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4362 | Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 4363 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc); |
| 4364 | } |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4365 | |
| 4366 | /// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference. |
| 4367 | /// This code gen. amounts to generating code for: |
| 4368 | /// @code |
| 4369 | /// (type *)((char *)base + _OBJC_IVAR_$_.ivar; |
| 4370 | /// @encode |
| 4371 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4372 | LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4373 | CodeGen::CodeGenFunction &CGF, |
| 4374 | QualType ObjectTy, |
| 4375 | llvm::Value *BaseValue, |
| 4376 | const ObjCIvarDecl *Ivar, |
| 4377 | const FieldDecl *Field, |
| 4378 | unsigned CVRQualifiers) { |
| 4379 | assert(ObjectTy->isObjCInterfaceType() && |
| 4380 | "CGObjCNonFragileABIMac::EmitObjCValueForIvar"); |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 4381 | ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl(); |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4382 | std::string ExternalName; |
| 4383 | llvm::GlobalVariable *IvarOffsetGV = |
| 4384 | ObjCIvarOffsetVariable(ExternalName, ID, Ivar); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4385 | |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4386 | // (char *) BaseValue |
| 4387 | llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, |
| 4388 | ObjCTypes.Int8PtrTy); |
| 4389 | llvm::Value *Offset = CGF.Builder.CreateLoad(IvarOffsetGV); |
| 4390 | // (char*)BaseValue + Offset_symbol |
| 4391 | V = CGF.Builder.CreateGEP(V, Offset, "add.ptr"); |
| 4392 | // (type *)((char*)BaseValue + Offset_symbol) |
| 4393 | const llvm::Type *IvarTy = |
| 4394 | CGM.getTypes().ConvertType(Ivar->getType()); |
| 4395 | llvm::Type *ptrIvarTy = llvm::PointerType::getUnqual(IvarTy); |
| 4396 | V = CGF.Builder.CreateBitCast(V, ptrIvarTy); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4397 | |
| 4398 | if (Ivar->isBitField()) |
| 4399 | return CGF.EmitLValueForBitfield(V, const_cast<FieldDecl *>(Field), |
| 4400 | CVRQualifiers); |
| 4401 | |
| 4402 | LValue LV = LValue::MakeAddr(V, |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 4403 | Ivar->getType().getCVRQualifiers()|CVRQualifiers, |
| 4404 | CGM.getContext().getObjCGCAttrKind(Ivar->getType())); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4405 | LValue::SetObjCIvar(LV, true); |
| 4406 | return LV; |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4407 | } |
| 4408 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4409 | llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset( |
| 4410 | CodeGen::CodeGenFunction &CGF, |
| 4411 | ObjCInterfaceDecl *Interface, |
| 4412 | const ObjCIvarDecl *Ivar) { |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4413 | std::string ExternalName; |
| 4414 | llvm::GlobalVariable *IvarOffsetGV = |
| 4415 | ObjCIvarOffsetVariable(ExternalName, Interface, Ivar); |
| 4416 | |
| 4417 | return CGF.Builder.CreateLoad(IvarOffsetGV, false, "ivar"); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4418 | } |
| 4419 | |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 4420 | CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend( |
| 4421 | CodeGen::CodeGenFunction &CGF, |
| 4422 | QualType ResultType, |
| 4423 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4424 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 4425 | QualType Arg0Ty, |
| 4426 | bool IsSuper, |
| 4427 | const CallArgList &CallArgs) { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4428 | // FIXME. Even though IsSuper is passes. This function doese not |
| 4429 | // handle calls to 'super' receivers. |
| 4430 | CodeGenTypes &Types = CGM.getTypes(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4431 | llvm::Value *Arg0 = Receiver; |
| 4432 | if (!IsSuper) |
| 4433 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4434 | |
| 4435 | // Find the message function name. |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 4436 | // FIXME. This is too much work to get the ABI-specific result type |
| 4437 | // needed to find the message name. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4438 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, |
| 4439 | llvm::SmallVector<QualType, 16>()); |
| 4440 | llvm::Constant *Fn; |
| 4441 | std::string Name("\01l_"); |
| 4442 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4443 | #if 0 |
| 4444 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4445 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
| 4446 | Fn = ObjCTypes.MessageSendIdStretFixupFn; |
| 4447 | // FIXME. Is there a better way of getting these names. |
| 4448 | // They are available in RuntimeFunctions vector pair. |
| 4449 | Name += "objc_msgSendId_stret_fixup"; |
| 4450 | } |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4451 | else |
| 4452 | #endif |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4453 | if (IsSuper) { |
| 4454 | Fn = ObjCTypes.MessageSendSuper2StretFixupFn; |
| 4455 | Name += "objc_msgSendSuper2_stret_fixup"; |
| 4456 | } |
| 4457 | else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4458 | { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4459 | Fn = ObjCTypes.MessageSendStretFixupFn; |
| 4460 | Name += "objc_msgSend_stret_fixup"; |
| 4461 | } |
| 4462 | } |
Fariborz Jahanian | 1a6b368 | 2009-02-05 19:35:43 +0000 | [diff] [blame] | 4463 | else if (ResultType->isFloatingType() && |
| 4464 | // Selection of frret API only happens in 32bit nonfragile ABI. |
| 4465 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.LongTy) == 4) { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4466 | Fn = ObjCTypes.MessageSendFpretFixupFn; |
| 4467 | Name += "objc_msgSend_fpret_fixup"; |
| 4468 | } |
| 4469 | else { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4470 | #if 0 |
| 4471 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4472 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
| 4473 | Fn = ObjCTypes.MessageSendIdFixupFn; |
| 4474 | Name += "objc_msgSendId_fixup"; |
| 4475 | } |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4476 | else |
| 4477 | #endif |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4478 | if (IsSuper) { |
| 4479 | Fn = ObjCTypes.MessageSendSuper2FixupFn; |
| 4480 | Name += "objc_msgSendSuper2_fixup"; |
| 4481 | } |
| 4482 | else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4483 | { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4484 | Fn = ObjCTypes.MessageSendFixupFn; |
| 4485 | Name += "objc_msgSend_fixup"; |
| 4486 | } |
| 4487 | } |
| 4488 | Name += '_'; |
| 4489 | std::string SelName(Sel.getAsString()); |
| 4490 | // Replace all ':' in selector name with '_' ouch! |
| 4491 | for(unsigned i = 0; i < SelName.size(); i++) |
| 4492 | if (SelName[i] == ':') |
| 4493 | SelName[i] = '_'; |
| 4494 | Name += SelName; |
| 4495 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 4496 | if (!GV) { |
| 4497 | // Build messafe ref table entry. |
| 4498 | std::vector<llvm::Constant*> Values(2); |
| 4499 | Values[0] = Fn; |
| 4500 | Values[1] = GetMethodVarName(Sel); |
| 4501 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4502 | GV = new llvm::GlobalVariable(Init->getType(), false, |
| 4503 | llvm::GlobalValue::WeakLinkage, |
| 4504 | Init, |
| 4505 | Name, |
| 4506 | &CGM.getModule()); |
| 4507 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4508 | GV->setAlignment( |
| 4509 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.MessageRefTy)); |
| 4510 | GV->setSection("__DATA, __objc_msgrefs, coalesced"); |
| 4511 | UsedGlobals.push_back(GV); |
| 4512 | } |
| 4513 | llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 4514 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4515 | CallArgList ActualArgs; |
| 4516 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
| 4517 | ActualArgs.push_back(std::make_pair(RValue::get(Arg1), |
| 4518 | ObjCTypes.MessageRefCPtrTy)); |
| 4519 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 4520 | const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs); |
| 4521 | llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0); |
| 4522 | Callee = CGF.Builder.CreateLoad(Callee); |
Fariborz Jahanian | 3ab75bd | 2009-02-14 21:25:36 +0000 | [diff] [blame] | 4523 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 4524 | Callee = CGF.Builder.CreateBitCast(Callee, |
| 4525 | llvm::PointerType::getUnqual(FTy)); |
| 4526 | return CGF.EmitCall(FnInfo1, Callee, ActualArgs); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 4527 | } |
| 4528 | |
| 4529 | /// Generate code for a message send expression in the nonfragile abi. |
| 4530 | CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend( |
| 4531 | CodeGen::CodeGenFunction &CGF, |
| 4532 | QualType ResultType, |
| 4533 | Selector Sel, |
| 4534 | llvm::Value *Receiver, |
| 4535 | bool IsClassMessage, |
| 4536 | const CallArgList &CallArgs) { |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 4537 | return EmitMessageSend(CGF, ResultType, Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4538 | Receiver, CGF.getContext().getObjCIdType(), |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 4539 | false, CallArgs); |
| 4540 | } |
| 4541 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 4542 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4543 | const ObjCInterfaceDecl *ID, |
| 4544 | bool IsSuper) { |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 4545 | |
| 4546 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 4547 | |
| 4548 | if (!Entry) { |
| 4549 | std::string ClassName("\01_OBJC_CLASS_$_" + ID->getNameAsString()); |
| 4550 | llvm::GlobalVariable *ClassGV = |
| 4551 | CGM.getModule().getGlobalVariable(ClassName); |
| 4552 | if (!ClassGV) { |
| 4553 | ClassGV = |
| 4554 | new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, |
| 4555 | llvm::GlobalValue::ExternalLinkage, |
| 4556 | 0, |
| 4557 | ClassName, |
| 4558 | &CGM.getModule()); |
| 4559 | UsedGlobals.push_back(ClassGV); |
| 4560 | } |
| 4561 | Entry = |
| 4562 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 4563 | llvm::GlobalValue::InternalLinkage, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4564 | ClassGV, |
| 4565 | IsSuper ? "\01L_OBJC_CLASSLIST_SUP_REFS_$_" |
| 4566 | : "\01L_OBJC_CLASSLIST_REFERENCES_$_", |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 4567 | &CGM.getModule()); |
| 4568 | Entry->setAlignment( |
| 4569 | CGM.getTargetData().getPrefTypeAlignment( |
| 4570 | ObjCTypes.ClassnfABIPtrTy)); |
| 4571 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4572 | if (IsSuper) |
Fariborz Jahanian | 21228b7 | 2009-02-26 22:30:39 +0000 | [diff] [blame] | 4573 | Entry->setSection("__DATA,__objc_superrefs,regular,no_dead_strip"); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4574 | else |
Fariborz Jahanian | 21228b7 | 2009-02-26 22:30:39 +0000 | [diff] [blame] | 4575 | Entry->setSection("__DATA,__objc_classrefs,regular,no_dead_strip"); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 4576 | UsedGlobals.push_back(Entry); |
| 4577 | } |
| 4578 | |
| 4579 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 4580 | } |
| 4581 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4582 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 4583 | /// meta-data |
| 4584 | /// |
| 4585 | llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder, |
| 4586 | const ObjCInterfaceDecl *ID) { |
| 4587 | llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()]; |
| 4588 | if (Entry) |
| 4589 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 4590 | |
| 4591 | std::string MetaClassName("\01_OBJC_METACLASS_$_" + ID->getNameAsString()); |
| 4592 | llvm::GlobalVariable *MetaClassGV = |
| 4593 | CGM.getModule().getGlobalVariable(MetaClassName); |
| 4594 | if (!MetaClassGV) { |
| 4595 | MetaClassGV = |
| 4596 | new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, |
| 4597 | llvm::GlobalValue::ExternalLinkage, |
| 4598 | 0, |
| 4599 | MetaClassName, |
| 4600 | &CGM.getModule()); |
| 4601 | UsedGlobals.push_back(MetaClassGV); |
| 4602 | } |
| 4603 | |
| 4604 | Entry = |
| 4605 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 4606 | llvm::GlobalValue::InternalLinkage, |
| 4607 | MetaClassGV, |
| 4608 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_", |
| 4609 | &CGM.getModule()); |
| 4610 | Entry->setAlignment( |
| 4611 | CGM.getTargetData().getPrefTypeAlignment( |
| 4612 | ObjCTypes.ClassnfABIPtrTy)); |
| 4613 | |
| 4614 | Entry->setSection("__OBJC,__objc_superrefs,regular,no_dead_strip"); |
| 4615 | UsedGlobals.push_back(Entry); |
| 4616 | |
| 4617 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 4618 | } |
| 4619 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 4620 | /// GetClass - Return a reference to the class for the given interface |
| 4621 | /// decl. |
| 4622 | llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder, |
| 4623 | const ObjCInterfaceDecl *ID) { |
| 4624 | return EmitClassRef(Builder, ID); |
| 4625 | } |
| 4626 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4627 | /// Generates a message send where the super is the receiver. This is |
| 4628 | /// a message send to self with special delivery semantics indicating |
| 4629 | /// which class's method should be called. |
| 4630 | CodeGen::RValue |
| 4631 | CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 4632 | QualType ResultType, |
| 4633 | Selector Sel, |
| 4634 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame^] | 4635 | bool isCategoryImpl, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4636 | llvm::Value *Receiver, |
| 4637 | bool IsClassMessage, |
| 4638 | const CodeGen::CallArgList &CallArgs) { |
| 4639 | // ... |
| 4640 | // Create and init a super structure; this is a (receiver, class) |
| 4641 | // pair we will pass to objc_msgSendSuper. |
| 4642 | llvm::Value *ObjCSuper = |
| 4643 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 4644 | |
| 4645 | llvm::Value *ReceiverAsObject = |
| 4646 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 4647 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 4648 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
| 4649 | |
| 4650 | // 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^] | 4651 | llvm::Value *Target; |
| 4652 | if (IsClassMessage) { |
| 4653 | if (isCategoryImpl) { |
| 4654 | // Message sent to "super' in a class method defined in |
| 4655 | // a category implementation. |
| 4656 | Target = EmitClassRef(CGF.Builder, Class, false); |
| 4657 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 4658 | Target = CGF.Builder.CreateLoad(Target); |
| 4659 | } |
| 4660 | else |
| 4661 | Target = EmitMetaClassRef(CGF.Builder, Class); |
| 4662 | } |
| 4663 | else |
| 4664 | Target = EmitClassRef(CGF.Builder, Class, true); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4665 | |
| 4666 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext |
| 4667 | // and ObjCTypes types. |
| 4668 | const llvm::Type *ClassTy = |
| 4669 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
| 4670 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
| 4671 | CGF.Builder.CreateStore(Target, |
| 4672 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
| 4673 | |
| 4674 | return EmitMessageSend(CGF, ResultType, Sel, |
| 4675 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 4676 | true, CallArgs); |
| 4677 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 4678 | |
| 4679 | llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder, |
| 4680 | Selector Sel) { |
| 4681 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 4682 | |
| 4683 | if (!Entry) { |
| 4684 | llvm::Constant *Casted = |
| 4685 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 4686 | ObjCTypes.SelectorPtrTy); |
| 4687 | Entry = |
| 4688 | new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false, |
| 4689 | llvm::GlobalValue::InternalLinkage, |
| 4690 | Casted, "\01L_OBJC_SELECTOR_REFERENCES_", |
| 4691 | &CGM.getModule()); |
| 4692 | Entry->setSection("__DATA,__objc_selrefs,literal_pointers,no_dead_strip"); |
| 4693 | UsedGlobals.push_back(Entry); |
| 4694 | } |
| 4695 | |
| 4696 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 4697 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 4698 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 4699 | /// objc_assign_ivar (id src, id *dst) |
| 4700 | /// |
| 4701 | void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 4702 | llvm::Value *src, llvm::Value *dst) |
| 4703 | { |
| 4704 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 4705 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
| 4706 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn, |
| 4707 | src, dst, "assignivar"); |
| 4708 | return; |
| 4709 | } |
| 4710 | |
| 4711 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 4712 | /// objc_assign_strongCast (id src, id *dst) |
| 4713 | /// |
| 4714 | void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign( |
| 4715 | CodeGen::CodeGenFunction &CGF, |
| 4716 | llvm::Value *src, llvm::Value *dst) |
| 4717 | { |
| 4718 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 4719 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
| 4720 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn, |
| 4721 | src, dst, "weakassign"); |
| 4722 | return; |
| 4723 | } |
| 4724 | |
| 4725 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
| 4726 | /// object: objc_read_weak (id *src) |
| 4727 | /// |
| 4728 | llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead( |
| 4729 | CodeGen::CodeGenFunction &CGF, |
| 4730 | llvm::Value *AddrWeakObj) |
| 4731 | { |
| 4732 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
| 4733 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn, |
| 4734 | AddrWeakObj, "weakread"); |
| 4735 | return read_weak; |
| 4736 | } |
| 4737 | |
| 4738 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 4739 | /// objc_assign_weak (id src, id *dst) |
| 4740 | /// |
| 4741 | void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 4742 | llvm::Value *src, llvm::Value *dst) |
| 4743 | { |
| 4744 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 4745 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
| 4746 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn, |
| 4747 | src, dst, "weakassign"); |
| 4748 | return; |
| 4749 | } |
| 4750 | |
| 4751 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 4752 | /// objc_assign_global (id src, id *dst) |
| 4753 | /// |
| 4754 | void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 4755 | llvm::Value *src, llvm::Value *dst) |
| 4756 | { |
| 4757 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 4758 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
| 4759 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn, |
| 4760 | src, dst, "globalassign"); |
| 4761 | return; |
| 4762 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 4763 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 4764 | void |
| 4765 | CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 4766 | const Stmt &S) { |
| 4767 | // We don't handle anything interesting yet. |
| 4768 | if (const ObjCAtTryStmt *TS = dyn_cast<ObjCAtTryStmt>(&S)) |
| 4769 | if (TS->getCatchStmts()) |
| 4770 | return CGF.ErrorUnsupported(&S, "try (with catch) statement"); |
| 4771 | |
| 4772 | bool isTry = isa<ObjCAtTryStmt>(S); |
| 4773 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 4774 | llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest(); |
| 4775 | llvm::BasicBlock *LandingPad = CGF.createBasicBlock("try.pad"); |
| 4776 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
| 4777 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
| 4778 | |
| 4779 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 4780 | // evaluation of the expression must occur before we enter the |
| 4781 | // @synchronized. We can safely avoid a temp here because jumps into |
| 4782 | // @synchronized are illegal & this will dominate uses. |
| 4783 | llvm::Value *SyncArg = 0; |
| 4784 | if (!isTry) { |
| 4785 | SyncArg = |
| 4786 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 4787 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
| 4788 | CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, SyncArg); |
| 4789 | } |
| 4790 | |
| 4791 | // Push an EH context entry, used for handling rethrows and jumps |
| 4792 | // through finally. |
| 4793 | CGF.PushCleanupBlock(FinallyBlock); |
| 4794 | |
| 4795 | CGF.setInvokeDest(LandingPad); |
| 4796 | |
| 4797 | CGF.EmitBlock(TryBlock); |
| 4798 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 4799 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
| 4800 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 4801 | |
| 4802 | // Pop the cleanup entry, the @finally is outside this cleanup |
| 4803 | // scope. |
| 4804 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 4805 | CGF.setInvokeDest(PrevLandingPad); |
| 4806 | |
| 4807 | CGF.EmitBlock(FinallyBlock); |
| 4808 | |
| 4809 | if (isTry) { |
| 4810 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 4811 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 4812 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
| 4813 | } else { |
| 4814 | // Emit 'objc_sync_exit(expr)' as finally's sole statement for |
| 4815 | // @synchronized. |
| 4816 | CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, SyncArg); |
| 4817 | } |
| 4818 | |
| 4819 | if (Info.SwitchBlock) |
| 4820 | CGF.EmitBlock(Info.SwitchBlock); |
| 4821 | if (Info.EndBlock) |
| 4822 | CGF.EmitBlock(Info.EndBlock); |
| 4823 | |
| 4824 | // Branch around the landing pad if necessary. |
| 4825 | CGF.EmitBranch(FinallyEnd); |
| 4826 | |
| 4827 | // Emit the landing pad. |
| 4828 | |
| 4829 | // Clear insertion point to avoid chaining. |
| 4830 | CGF.Builder.ClearInsertionPoint(); |
| 4831 | CGF.EmitBlock(LandingPad); |
| 4832 | |
| 4833 | llvm::Value *llvm_eh_exception = |
| 4834 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception); |
| 4835 | llvm::Value *llvm_eh_selector_i64 = |
| 4836 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64); |
| 4837 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 4838 | |
| 4839 | llvm::SmallVector<llvm::Value*, 8> Args; |
| 4840 | Args.push_back(Exc); |
| 4841 | Args.push_back(ObjCTypes.EHPersonalityPtr); |
| 4842 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0)); |
| 4843 | |
| 4844 | llvm::Value *Selector = |
| 4845 | CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end()); |
| 4846 | |
| 4847 | // The only valid result for the limited case we are considering is |
| 4848 | // the cleanup. |
| 4849 | (void) Selector; |
| 4850 | |
| 4851 | // Re-emit cleanup code for exceptional case. |
| 4852 | if (isTry) { |
| 4853 | // FIXME: This is horrible, in many ways: (a) it is broken because |
| 4854 | // we are messing with some global data structures (like where |
| 4855 | // labels point at), (b) it is exponential in the size of code |
| 4856 | // generated, (c) seriously, its just gross. |
| 4857 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 4858 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 4859 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
| 4860 | } else { |
| 4861 | // Emit 'objc_sync_exit(expr)' as finally's sole statement for |
| 4862 | // @synchronized. |
| 4863 | CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, SyncArg); |
| 4864 | } |
| 4865 | |
| 4866 | CGF.EnsureInsertPoint(); |
| 4867 | CGF.Builder.CreateCall(ObjCTypes.UnwindResumeOrRethrowFn, Exc); |
| 4868 | CGF.Builder.CreateUnreachable(); |
| 4869 | |
| 4870 | CGF.EmitBlock(FinallyEnd); |
| 4871 | } |
| 4872 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 4873 | /// EmitThrowStmt - Generate code for a throw statement. |
| 4874 | void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 4875 | const ObjCAtThrowStmt &S) { |
| 4876 | llvm::Value *ExceptionAsObject; |
| 4877 | |
| 4878 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
| 4879 | llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr); |
| 4880 | ExceptionAsObject = |
| 4881 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 4882 | |
| 4883 | CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject); |
| 4884 | CGF.Builder.CreateUnreachable(); |
| 4885 | } else { |
| 4886 | CGF.ErrorUnsupported(&S, "rethrow statement"); |
| 4887 | } |
| 4888 | |
| 4889 | // Clear the insertion point to indicate we are in unreachable code. |
| 4890 | CGF.Builder.ClearInsertionPoint(); |
| 4891 | } |
| 4892 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 4893 | /* *** */ |
| 4894 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 4895 | CodeGen::CGObjCRuntime * |
| 4896 | CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 4897 | return new CGObjCMac(CGM); |
| 4898 | } |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4899 | |
| 4900 | CodeGen::CGObjCRuntime * |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 4901 | CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4902 | return new CGObjCNonFragileABIMac(CGM); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 4903 | } |