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