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