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