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