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