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