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