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