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