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