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 | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 23 | #include "llvm/Intrinsics.h" |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 24 | #include "llvm/Module.h" |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/DenseSet.h" |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetData.h" |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 27 | #include <sstream> |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace clang; |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 30 | using namespace CodeGen; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 31 | |
| 32 | namespace { |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 33 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 34 | typedef std::vector<llvm::Constant*> ConstantVector; |
| 35 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 36 | // FIXME: We should find a nicer way to make the labels for |
| 37 | // metadata, string concatenation is lame. |
| 38 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 39 | class ObjCCommonTypesHelper { |
| 40 | protected: |
| 41 | CodeGen::CodeGenModule &CGM; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 42 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 43 | public: |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 44 | const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 45 | const llvm::Type *Int8PtrTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 46 | |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 47 | /// ObjectPtrTy - LLVM type for object handles (typeof(id)) |
| 48 | const llvm::Type *ObjectPtrTy; |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 49 | |
| 50 | /// PtrObjectPtrTy - LLVM type for id * |
| 51 | const llvm::Type *PtrObjectPtrTy; |
| 52 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 53 | /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL)) |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 54 | const llvm::Type *SelectorPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 55 | /// ProtocolPtrTy - LLVM type for external protocol handles |
| 56 | /// (typeof(Protocol)) |
| 57 | const llvm::Type *ExternalProtocolPtrTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 58 | |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 59 | // SuperCTy - clang type for struct objc_super. |
| 60 | QualType SuperCTy; |
| 61 | // SuperPtrCTy - clang type for struct objc_super *. |
| 62 | QualType SuperPtrCTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 63 | |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 64 | /// SuperTy - LLVM type for struct objc_super. |
| 65 | const llvm::StructType *SuperTy; |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 66 | /// SuperPtrTy - LLVM type for struct objc_super *. |
| 67 | const llvm::Type *SuperPtrTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 68 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 69 | /// PropertyTy - LLVM type for struct objc_property (struct _prop_t |
| 70 | /// in GCC parlance). |
| 71 | const llvm::StructType *PropertyTy; |
| 72 | |
| 73 | /// PropertyListTy - LLVM type for struct objc_property_list |
| 74 | /// (_prop_list_t in GCC parlance). |
| 75 | const llvm::StructType *PropertyListTy; |
| 76 | /// PropertyListPtrTy - LLVM type for struct objc_property_list*. |
| 77 | const llvm::Type *PropertyListPtrTy; |
| 78 | |
| 79 | // MethodTy - LLVM type for struct objc_method. |
| 80 | const llvm::StructType *MethodTy; |
| 81 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 82 | /// CacheTy - LLVM type for struct objc_cache. |
| 83 | const llvm::Type *CacheTy; |
| 84 | /// CachePtrTy - LLVM type for struct objc_cache *. |
| 85 | const llvm::Type *CachePtrTy; |
| 86 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 87 | llvm::Constant *GetPropertyFn, *SetPropertyFn; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 88 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 89 | llvm::Constant *EnumerationMutationFn; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 90 | |
| 91 | /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 92 | llvm::Constant *GcReadWeakFn; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 93 | |
| 94 | /// GcAssignWeakFn -- LLVM objc_assign_weak function. |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 95 | llvm::Constant *getGcAssignWeakFn() { |
| 96 | // id objc_assign_weak (id, id *) |
| 97 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 98 | Args.push_back(ObjectPtrTy->getPointerTo()); |
| 99 | llvm::FunctionType *FTy = |
| 100 | llvm::FunctionType::get(ObjectPtrTy, Args, false); |
| 101 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak"); |
| 102 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 103 | |
| 104 | /// GcAssignGlobalFn -- LLVM objc_assign_global function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 105 | llvm::Constant *GcAssignGlobalFn; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 106 | |
| 107 | /// GcAssignIvarFn -- LLVM objc_assign_ivar function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 108 | llvm::Constant *GcAssignIvarFn; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 109 | |
| 110 | /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 111 | llvm::Constant *GcAssignStrongCastFn; |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 112 | |
| 113 | /// ExceptionThrowFn - LLVM objc_exception_throw function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 114 | llvm::Constant *ExceptionThrowFn; |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 115 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 116 | /// SyncEnterFn - LLVM object_sync_enter function. |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 117 | llvm::Constant *getSyncEnterFn() { |
| 118 | // void objc_sync_enter (id) |
| 119 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 120 | llvm::FunctionType *FTy = |
| 121 | llvm::FunctionType::get(llvm::Type::VoidTy, Args, false); |
| 122 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter"); |
| 123 | } |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 124 | |
| 125 | /// SyncExitFn - LLVM object_sync_exit function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 126 | llvm::Constant *SyncExitFn; |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 127 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 128 | ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm); |
| 129 | ~ObjCCommonTypesHelper(){} |
| 130 | }; |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 131 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 132 | /// ObjCTypesHelper - Helper class that encapsulates lazy |
| 133 | /// construction of varies types used during ObjC generation. |
| 134 | class ObjCTypesHelper : public ObjCCommonTypesHelper { |
| 135 | private: |
| 136 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 137 | llvm::Constant *MessageSendFn, *MessageSendStretFn, *MessageSendFpretFn; |
| 138 | llvm::Constant *MessageSendSuperFn, *MessageSendSuperStretFn, |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 139 | *MessageSendSuperFpretFn; |
| 140 | |
| 141 | public: |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 142 | /// SymtabTy - LLVM type for struct objc_symtab. |
| 143 | const llvm::StructType *SymtabTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 144 | /// SymtabPtrTy - LLVM type for struct objc_symtab *. |
| 145 | const llvm::Type *SymtabPtrTy; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 146 | /// ModuleTy - LLVM type for struct objc_module. |
| 147 | const llvm::StructType *ModuleTy; |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 148 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 149 | /// ProtocolTy - LLVM type for struct objc_protocol. |
| 150 | const llvm::StructType *ProtocolTy; |
| 151 | /// ProtocolPtrTy - LLVM type for struct objc_protocol *. |
| 152 | const llvm::Type *ProtocolPtrTy; |
| 153 | /// ProtocolExtensionTy - LLVM type for struct |
| 154 | /// objc_protocol_extension. |
| 155 | const llvm::StructType *ProtocolExtensionTy; |
| 156 | /// ProtocolExtensionTy - LLVM type for struct |
| 157 | /// objc_protocol_extension *. |
| 158 | const llvm::Type *ProtocolExtensionPtrTy; |
| 159 | /// MethodDescriptionTy - LLVM type for struct |
| 160 | /// objc_method_description. |
| 161 | const llvm::StructType *MethodDescriptionTy; |
| 162 | /// MethodDescriptionListTy - LLVM type for struct |
| 163 | /// objc_method_description_list. |
| 164 | const llvm::StructType *MethodDescriptionListTy; |
| 165 | /// MethodDescriptionListPtrTy - LLVM type for struct |
| 166 | /// objc_method_description_list *. |
| 167 | const llvm::Type *MethodDescriptionListPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 168 | /// ProtocolListTy - LLVM type for struct objc_property_list. |
| 169 | const llvm::Type *ProtocolListTy; |
| 170 | /// ProtocolListPtrTy - LLVM type for struct objc_property_list*. |
| 171 | const llvm::Type *ProtocolListPtrTy; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 172 | /// CategoryTy - LLVM type for struct objc_category. |
| 173 | const llvm::StructType *CategoryTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 174 | /// ClassTy - LLVM type for struct objc_class. |
| 175 | const llvm::StructType *ClassTy; |
| 176 | /// ClassPtrTy - LLVM type for struct objc_class *. |
| 177 | const llvm::Type *ClassPtrTy; |
| 178 | /// ClassExtensionTy - LLVM type for struct objc_class_ext. |
| 179 | const llvm::StructType *ClassExtensionTy; |
| 180 | /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *. |
| 181 | const llvm::Type *ClassExtensionPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 182 | // IvarTy - LLVM type for struct objc_ivar. |
| 183 | const llvm::StructType *IvarTy; |
| 184 | /// IvarListTy - LLVM type for struct objc_ivar_list. |
| 185 | const llvm::Type *IvarListTy; |
| 186 | /// IvarListPtrTy - LLVM type for struct objc_ivar_list *. |
| 187 | const llvm::Type *IvarListPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 188 | /// MethodListTy - LLVM type for struct objc_method_list. |
| 189 | const llvm::Type *MethodListTy; |
| 190 | /// MethodListPtrTy - LLVM type for struct objc_method_list *. |
| 191 | const llvm::Type *MethodListPtrTy; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 192 | |
| 193 | /// ExceptionDataTy - LLVM type for struct _objc_exception_data. |
| 194 | const llvm::Type *ExceptionDataTy; |
| 195 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 196 | /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 197 | llvm::Constant *ExceptionTryEnterFn; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 198 | |
| 199 | /// ExceptionTryExitFn - LLVM objc_exception_try_exit function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 200 | llvm::Constant *ExceptionTryExitFn; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 201 | |
| 202 | /// ExceptionExtractFn - LLVM objc_exception_extract function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 203 | llvm::Constant *ExceptionExtractFn; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 204 | |
| 205 | /// ExceptionMatchFn - LLVM objc_exception_match function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 206 | llvm::Constant *ExceptionMatchFn; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 207 | |
| 208 | /// SetJmpFn - LLVM _setjmp function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 209 | llvm::Constant *SetJmpFn; |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 210 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 211 | public: |
| 212 | ObjCTypesHelper(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 213 | ~ObjCTypesHelper() {} |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 214 | |
| 215 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 216 | llvm::Constant *getSendFn(bool IsSuper) { |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 217 | return IsSuper ? MessageSendSuperFn : MessageSendFn; |
| 218 | } |
| 219 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 220 | llvm::Constant *getSendStretFn(bool IsSuper) { |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 221 | return IsSuper ? MessageSendSuperStretFn : MessageSendStretFn; |
| 222 | } |
| 223 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 224 | llvm::Constant *getSendFpretFn(bool IsSuper) { |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 225 | return IsSuper ? MessageSendSuperFpretFn : MessageSendFpretFn; |
| 226 | } |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 227 | }; |
| 228 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 229 | /// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 230 | /// modern abi |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 231 | class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 232 | public: |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 233 | llvm::Constant *MessageSendFixupFn, *MessageSendFpretFixupFn, |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 234 | *MessageSendStretFixupFn, *MessageSendIdFixupFn, |
| 235 | *MessageSendIdStretFixupFn, *MessageSendSuper2FixupFn, |
| 236 | *MessageSendSuper2StretFixupFn; |
| 237 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 238 | // MethodListnfABITy - LLVM for struct _method_list_t |
| 239 | const llvm::StructType *MethodListnfABITy; |
| 240 | |
| 241 | // MethodListnfABIPtrTy - LLVM for struct _method_list_t* |
| 242 | const llvm::Type *MethodListnfABIPtrTy; |
| 243 | |
| 244 | // ProtocolnfABITy = LLVM for struct _protocol_t |
| 245 | const llvm::StructType *ProtocolnfABITy; |
| 246 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 247 | // ProtocolnfABIPtrTy = LLVM for struct _protocol_t* |
| 248 | const llvm::Type *ProtocolnfABIPtrTy; |
| 249 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 250 | // ProtocolListnfABITy - LLVM for struct _objc_protocol_list |
| 251 | const llvm::StructType *ProtocolListnfABITy; |
| 252 | |
| 253 | // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list* |
| 254 | const llvm::Type *ProtocolListnfABIPtrTy; |
| 255 | |
| 256 | // ClassnfABITy - LLVM for struct _class_t |
| 257 | const llvm::StructType *ClassnfABITy; |
| 258 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 259 | // ClassnfABIPtrTy - LLVM for struct _class_t* |
| 260 | const llvm::Type *ClassnfABIPtrTy; |
| 261 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 262 | // IvarnfABITy - LLVM for struct _ivar_t |
| 263 | const llvm::StructType *IvarnfABITy; |
| 264 | |
| 265 | // IvarListnfABITy - LLVM for struct _ivar_list_t |
| 266 | const llvm::StructType *IvarListnfABITy; |
| 267 | |
| 268 | // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t* |
| 269 | const llvm::Type *IvarListnfABIPtrTy; |
| 270 | |
| 271 | // ClassRonfABITy - LLVM for struct _class_ro_t |
| 272 | const llvm::StructType *ClassRonfABITy; |
| 273 | |
| 274 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 275 | const llvm::Type *ImpnfABITy; |
| 276 | |
| 277 | // CategorynfABITy - LLVM for struct _category_t |
| 278 | const llvm::StructType *CategorynfABITy; |
| 279 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 280 | // New types for nonfragile abi messaging. |
| 281 | |
| 282 | // MessageRefTy - LLVM for: |
| 283 | // struct _message_ref_t { |
| 284 | // IMP messenger; |
| 285 | // SEL name; |
| 286 | // }; |
| 287 | const llvm::StructType *MessageRefTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 288 | // MessageRefCTy - clang type for struct _message_ref_t |
| 289 | QualType MessageRefCTy; |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 290 | |
| 291 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
| 292 | const llvm::Type *MessageRefPtrTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 293 | // MessageRefCPtrTy - clang type for struct _message_ref_t* |
| 294 | QualType MessageRefCPtrTy; |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 295 | |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 296 | // MessengerTy - Type of the messenger (shown as IMP above) |
| 297 | const llvm::FunctionType *MessengerTy; |
| 298 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 299 | // SuperMessageRefTy - LLVM for: |
| 300 | // struct _super_message_ref_t { |
| 301 | // SUPER_IMP messenger; |
| 302 | // SEL name; |
| 303 | // }; |
| 304 | const llvm::StructType *SuperMessageRefTy; |
| 305 | |
| 306 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
| 307 | const llvm::Type *SuperMessageRefPtrTy; |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 308 | |
| 309 | /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C |
| 310 | /// exception personality function. |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 311 | llvm::Value *getEHPersonalityPtr() { |
| 312 | llvm::Constant *Personality = |
| 313 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 314 | std::vector<const llvm::Type*>(), |
| 315 | true), |
| 316 | "__objc_personality_v0"); |
| 317 | return llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy); |
| 318 | } |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 319 | |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame^] | 320 | llvm::Constant *getUnwindResumeOrRethrowFn() { |
| 321 | std::vector<const llvm::Type*> Params; |
| 322 | Params.push_back(Int8PtrTy); |
| 323 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 324 | Params, false), |
| 325 | "_Unwind_Resume_or_Rethrow"); |
| 326 | } |
| 327 | |
| 328 | llvm::Constant *getObjCEndCatchFn() { |
| 329 | std::vector<const llvm::Type*> Params; |
| 330 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 331 | Params, false), |
| 332 | "objc_end_catch"); |
| 333 | |
| 334 | } |
| 335 | |
| 336 | llvm::Constant *getObjCBeginCatchFn() { |
| 337 | std::vector<const llvm::Type*> Params; |
| 338 | Params.push_back(Int8PtrTy); |
| 339 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy, |
| 340 | Params, false), |
| 341 | "objc_begin_catch"); |
| 342 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 343 | |
| 344 | const llvm::StructType *EHTypeTy; |
| 345 | const llvm::Type *EHTypePtrTy; |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 346 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 347 | ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm); |
| 348 | ~ObjCNonFragileABITypesHelper(){} |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 349 | }; |
| 350 | |
| 351 | class CGObjCCommonMac : public CodeGen::CGObjCRuntime { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 352 | public: |
| 353 | // FIXME - accessibility |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 354 | class GC_IVAR { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 355 | public: |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 356 | unsigned int ivar_bytepos; |
| 357 | unsigned int ivar_size; |
| 358 | GC_IVAR() : ivar_bytepos(0), ivar_size(0) {} |
| 359 | }; |
| 360 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 361 | class SKIP_SCAN { |
| 362 | public: |
| 363 | unsigned int skip; |
| 364 | unsigned int scan; |
| 365 | SKIP_SCAN() : skip(0), scan(0) {} |
| 366 | }; |
| 367 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 368 | protected: |
| 369 | CodeGen::CodeGenModule &CGM; |
| 370 | // FIXME! May not be needing this after all. |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 371 | unsigned ObjCABI; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 372 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 373 | // gc ivar layout bitmap calculation helper caches. |
| 374 | llvm::SmallVector<GC_IVAR, 16> SkipIvars; |
| 375 | llvm::SmallVector<GC_IVAR, 16> IvarsInfo; |
| 376 | llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 377 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 378 | /// LazySymbols - Symbols to generate a lazy reference for. See |
| 379 | /// DefinedSymbols and FinishModule(). |
| 380 | std::set<IdentifierInfo*> LazySymbols; |
| 381 | |
| 382 | /// DefinedSymbols - External symbols which are defined by this |
| 383 | /// module. The symbols in this list and LazySymbols are used to add |
| 384 | /// special linker symbols which ensure that Objective-C modules are |
| 385 | /// linked properly. |
| 386 | std::set<IdentifierInfo*> DefinedSymbols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 387 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 388 | /// ClassNames - uniqued class names. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 389 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 390 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 391 | /// MethodVarNames - uniqued method variable names. |
| 392 | llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 393 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 394 | /// MethodVarTypes - uniqued method type signatures. We have to use |
| 395 | /// a StringMap here because have no other unique reference. |
| 396 | llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 397 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 398 | /// MethodDefinitions - map of methods which have been defined in |
| 399 | /// this translation unit. |
| 400 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 401 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 402 | /// PropertyNames - uniqued method variable names. |
| 403 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 404 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 405 | /// ClassReferences - uniqued class references. |
| 406 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 407 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 408 | /// SelectorReferences - uniqued selector references. |
| 409 | llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 410 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 411 | /// Protocols - Protocols for which an objc_protocol structure has |
| 412 | /// been emitted. Forward declarations are handled by creating an |
| 413 | /// empty structure whose initializer is filled in when/if defined. |
| 414 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 415 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 416 | /// DefinedProtocols - Protocols which have actually been |
| 417 | /// defined. We should not need this, see FIXME in GenerateProtocol. |
| 418 | llvm::DenseSet<IdentifierInfo*> DefinedProtocols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 419 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 420 | /// DefinedClasses - List of defined classes. |
| 421 | std::vector<llvm::GlobalValue*> DefinedClasses; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 422 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 423 | /// DefinedCategories - List of defined categories. |
| 424 | std::vector<llvm::GlobalValue*> DefinedCategories; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 425 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 426 | /// UsedGlobals - List of globals to pack into the llvm.used metadata |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 427 | /// to prevent them from being clobbered. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 428 | std::vector<llvm::GlobalVariable*> UsedGlobals; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 429 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 430 | /// GetNameForMethod - Return a name for the given method. |
| 431 | /// \param[out] NameOut - The return value. |
| 432 | void GetNameForMethod(const ObjCMethodDecl *OMD, |
| 433 | const ObjCContainerDecl *CD, |
| 434 | std::string &NameOut); |
| 435 | |
| 436 | /// GetMethodVarName - Return a unique constant for the given |
| 437 | /// selector's name. The return value has type char *. |
| 438 | llvm::Constant *GetMethodVarName(Selector Sel); |
| 439 | llvm::Constant *GetMethodVarName(IdentifierInfo *Ident); |
| 440 | llvm::Constant *GetMethodVarName(const std::string &Name); |
| 441 | |
| 442 | /// GetMethodVarType - Return a unique constant for the given |
| 443 | /// selector's name. The return value has type char *. |
| 444 | |
| 445 | // FIXME: This is a horrible name. |
| 446 | llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 447 | llvm::Constant *GetMethodVarType(const FieldDecl *D); |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 448 | |
| 449 | /// GetPropertyName - Return a unique constant for the given |
| 450 | /// name. The return value has type char *. |
| 451 | llvm::Constant *GetPropertyName(IdentifierInfo *Ident); |
| 452 | |
| 453 | // FIXME: This can be dropped once string functions are unified. |
| 454 | llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 455 | const Decl *Container); |
| 456 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 457 | /// GetClassName - Return a unique constant for the given selector's |
| 458 | /// name. The return value has type char *. |
| 459 | llvm::Constant *GetClassName(IdentifierInfo *Ident); |
| 460 | |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 461 | /// GetInterfaceDeclStructLayout - Get layout for ivars of given |
| 462 | /// interface declaration. |
| 463 | const llvm::StructLayout *GetInterfaceDeclStructLayout( |
| 464 | const ObjCInterfaceDecl *ID) const; |
| 465 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 466 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 467 | /// implementation for the __strong or __weak case. |
| 468 | /// |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 469 | llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI, |
| 470 | bool ForStrongLayout); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 471 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 472 | void BuildAggrIvarLayout(const ObjCInterfaceDecl *OI, |
| 473 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 474 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 475 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 476 | unsigned int BytePos, bool ForStrongLayout, |
| 477 | int &Index, int &SkIndex, bool &HasUnion); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 478 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 479 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 480 | /// ivar layout bitmap. |
| 481 | llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident, |
| 482 | const ObjCCommonTypesHelper &ObjCTypes); |
| 483 | |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 484 | /// EmitPropertyList - Emit the given property list. The return |
| 485 | /// value has type PropertyListPtrTy. |
| 486 | llvm::Constant *EmitPropertyList(const std::string &Name, |
| 487 | const Decl *Container, |
| 488 | const ObjCContainerDecl *OCD, |
| 489 | const ObjCCommonTypesHelper &ObjCTypes); |
| 490 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 491 | /// GetProtocolRef - Return a reference to the internal protocol |
| 492 | /// description, creating an empty one if it has not been |
| 493 | /// defined. The return value has type ProtocolPtrTy. |
| 494 | llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD); |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 495 | |
| 496 | /// GetIvarBaseOffset - returns ivars byte offset. |
| 497 | uint64_t GetIvarBaseOffset(const llvm::StructLayout *Layout, |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 498 | const FieldDecl *Field); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 499 | |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 500 | /// GetFieldBaseOffset - return's field byte offset. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 501 | uint64_t GetFieldBaseOffset(const ObjCInterfaceDecl *OI, |
| 502 | const llvm::StructLayout *Layout, |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 503 | const FieldDecl *Field); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 504 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 505 | /// CreateMetadataVar - Create a global variable with internal |
| 506 | /// linkage for use by the Objective-C runtime. |
| 507 | /// |
| 508 | /// This is a convenience wrapper which not only creates the |
| 509 | /// variable, but also sets the section and alignment and adds the |
| 510 | /// global to the UsedGlobals list. |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 511 | /// |
| 512 | /// \param Name - The variable name. |
| 513 | /// \param Init - The variable initializer; this is also used to |
| 514 | /// define the type of the variable. |
| 515 | /// \param Section - The section the variable should go into, or 0. |
| 516 | /// \param Align - The alignment for the variable, or 0. |
| 517 | /// \param AddToUsed - Whether the variable should be added to |
Daniel Dunbar | c158306 | 2009-04-14 17:42:51 +0000 | [diff] [blame] | 518 | /// "llvm.used". |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 519 | llvm::GlobalVariable *CreateMetadataVar(const std::string &Name, |
| 520 | llvm::Constant *Init, |
| 521 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 522 | unsigned Align, |
| 523 | bool AddToUsed); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 524 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 525 | /// GetNamedIvarList - Return the list of ivars in the interface |
| 526 | /// itself (not including super classes and not including unnamed |
| 527 | /// bitfields). |
| 528 | /// |
| 529 | /// For the non-fragile ABI, this also includes synthesized property |
| 530 | /// ivars. |
| 531 | void GetNamedIvarList(const ObjCInterfaceDecl *OID, |
| 532 | llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const; |
| 533 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 534 | public: |
| 535 | CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm) |
| 536 | { } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 537 | |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 538 | virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *SL); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 539 | |
| 540 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
| 541 | const ObjCContainerDecl *CD=0); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 542 | |
| 543 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); |
| 544 | |
| 545 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 546 | /// declaration, emitting it if necessary. The return value has type |
| 547 | /// ProtocolPtrTy. |
| 548 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0; |
| 549 | |
| 550 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 551 | /// object for the given declaration, emitting it if needed. These |
| 552 | /// forward references will be filled in with empty bodies if no |
| 553 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 554 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 555 | }; |
| 556 | |
| 557 | class CGObjCMac : public CGObjCCommonMac { |
| 558 | private: |
| 559 | ObjCTypesHelper ObjCTypes; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 560 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 561 | /// level information. |
| 562 | void EmitImageInfo(); |
| 563 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 564 | /// EmitModuleInfo - Another marker encoding module level |
| 565 | /// information. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 566 | void EmitModuleInfo(); |
| 567 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 568 | /// EmitModuleSymols - Emit module symbols, the list of defined |
| 569 | /// classes and categories. The result has type SymtabPtrTy. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 570 | llvm::Constant *EmitModuleSymbols(); |
| 571 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 572 | /// FinishModule - Write out global data structures at the end of |
| 573 | /// processing a translation unit. |
| 574 | void FinishModule(); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 575 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 576 | /// EmitClassExtension - Generate the class extension structure used |
| 577 | /// to store the weak ivar layout and properties. The return value |
| 578 | /// has type ClassExtensionPtrTy. |
| 579 | llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID); |
| 580 | |
| 581 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 582 | /// for the given class. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 583 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 584 | const ObjCInterfaceDecl *ID); |
| 585 | |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 586 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 587 | QualType ResultType, |
| 588 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 589 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 590 | QualType Arg0Ty, |
| 591 | bool IsSuper, |
| 592 | const CallArgList &CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 593 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 594 | /// EmitIvarList - Emit the ivar list for the given |
| 595 | /// implementation. If ForClass is true the list of class ivars |
| 596 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 597 | /// interface ivars will be emitted. The return value has type |
| 598 | /// IvarListPtrTy. |
| 599 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 600 | bool ForClass); |
| 601 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 602 | /// EmitMetaClass - Emit a forward reference to the class structure |
| 603 | /// for the metaclass of the given interface. The return value has |
| 604 | /// type ClassPtrTy. |
| 605 | llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID); |
| 606 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 607 | /// EmitMetaClass - Emit a class structure for the metaclass of the |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 608 | /// given implementation. The return value has type ClassPtrTy. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 609 | llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID, |
| 610 | llvm::Constant *Protocols, |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 611 | const llvm::Type *InterfaceTy, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 612 | const ConstantVector &Methods); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 613 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 614 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 615 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 616 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 617 | |
| 618 | /// EmitMethodList - Emit the method list for the given |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 619 | /// implementation. The return value has type MethodListPtrTy. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 620 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 621 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 622 | const ConstantVector &Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 623 | |
| 624 | /// EmitMethodDescList - Emit a method description list for a list of |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 625 | /// method declarations. |
| 626 | /// - TypeName: The name for the type containing the methods. |
| 627 | /// - IsProtocol: True iff these methods are for a protocol. |
| 628 | /// - ClassMethds: True iff these are class methods. |
| 629 | /// - Required: When true, only "required" methods are |
| 630 | /// listed. Similarly, when false only "optional" methods are |
| 631 | /// listed. For classes this should always be true. |
| 632 | /// - begin, end: The method list to output. |
| 633 | /// |
| 634 | /// The return value has type MethodDescriptionListPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 635 | llvm::Constant *EmitMethodDescList(const std::string &Name, |
| 636 | const char *Section, |
| 637 | const ConstantVector &Methods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 638 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 639 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 640 | /// declaration, emitting it if necessary. The return value has type |
| 641 | /// ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 642 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 643 | |
| 644 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 645 | /// object for the given declaration, emitting it if needed. These |
| 646 | /// forward references will be filled in with empty bodies if no |
| 647 | /// definition is seen. The return value has type ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 648 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 649 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 650 | /// EmitProtocolExtension - Generate the protocol extension |
| 651 | /// structure used to store optional instance and class methods, and |
| 652 | /// protocol properties. The return value has type |
| 653 | /// ProtocolExtensionPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 654 | llvm::Constant * |
| 655 | EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 656 | const ConstantVector &OptInstanceMethods, |
| 657 | const ConstantVector &OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 658 | |
| 659 | /// EmitProtocolList - Generate the list of referenced |
| 660 | /// protocols. The return value has type ProtocolListPtrTy. |
Daniel Dunbar | dbc93370 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 661 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 662 | ObjCProtocolDecl::protocol_iterator begin, |
| 663 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 664 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 665 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 666 | /// for the given selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 667 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 668 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 669 | public: |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 670 | CGObjCMac(CodeGen::CodeGenModule &cgm); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 671 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 672 | virtual llvm::Function *ModuleInitFunction(); |
| 673 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 674 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 675 | QualType ResultType, |
| 676 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 677 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 678 | bool IsClassMessage, |
| 679 | const CallArgList &CallArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 680 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 681 | virtual CodeGen::RValue |
| 682 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 683 | QualType ResultType, |
| 684 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 685 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 686 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 687 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 688 | bool IsClassMessage, |
| 689 | const CallArgList &CallArgs); |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 690 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 691 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 692 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 693 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 694 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 695 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 696 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 697 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 698 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 699 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 700 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 701 | const ObjCProtocolDecl *PD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 702 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 703 | virtual llvm::Constant *GetPropertyGetFunction(); |
| 704 | virtual llvm::Constant *GetPropertySetFunction(); |
| 705 | virtual llvm::Constant *EnumerationMutationFunction(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 706 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 707 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 708 | const Stmt &S); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 709 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 710 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 711 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 712 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 713 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 714 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 715 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 716 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 717 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 718 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 719 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 720 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 721 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 722 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 723 | QualType ObjectTy, |
| 724 | llvm::Value *BaseValue, |
| 725 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 726 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 727 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
| 728 | ObjCInterfaceDecl *Interface, |
| 729 | const ObjCIvarDecl *Ivar); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 730 | }; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 731 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 732 | class CGObjCNonFragileABIMac : public CGObjCCommonMac { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 733 | private: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 734 | ObjCNonFragileABITypesHelper ObjCTypes; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 735 | llvm::GlobalVariable* ObjCEmptyCacheVar; |
| 736 | llvm::GlobalVariable* ObjCEmptyVtableVar; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 737 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 738 | /// SuperClassReferences - uniqued super class references. |
| 739 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences; |
| 740 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 741 | /// MetaClassReferences - uniqued meta class references. |
| 742 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 743 | |
| 744 | /// EHTypeReferences - uniqued class ehtype references. |
| 745 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 746 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 747 | /// FinishNonFragileABIModule - Write out global data structures at the end of |
| 748 | /// processing a translation unit. |
| 749 | void FinishNonFragileABIModule(); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 750 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 751 | llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags, |
| 752 | unsigned InstanceStart, |
| 753 | unsigned InstanceSize, |
| 754 | const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 755 | llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName, |
| 756 | llvm::Constant *IsAGV, |
| 757 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 758 | llvm::Constant *ClassRoGV, |
| 759 | bool HiddenVisibility); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 760 | |
| 761 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
| 762 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 763 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
| 764 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 765 | /// EmitMethodList - Emit the method list for the given |
| 766 | /// implementation. The return value has type MethodListnfABITy. |
| 767 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 768 | const char *Section, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 769 | const ConstantVector &Methods); |
| 770 | /// EmitIvarList - Emit the ivar list for the given |
| 771 | /// implementation. If ForClass is true the list of class ivars |
| 772 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 773 | /// interface ivars will be emitted. The return value has type |
| 774 | /// IvarListnfABIPtrTy. |
| 775 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 776 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 777 | llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 778 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 779 | unsigned long int offset); |
| 780 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 781 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 782 | /// declaration, emitting it if necessary. The return value has type |
| 783 | /// ProtocolPtrTy. |
| 784 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
| 785 | |
| 786 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 787 | /// object for the given declaration, emitting it if needed. These |
| 788 | /// forward references will be filled in with empty bodies if no |
| 789 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 790 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
| 791 | |
| 792 | /// EmitProtocolList - Generate the list of referenced |
| 793 | /// protocols. The return value has type ProtocolListPtrTy. |
| 794 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 795 | ObjCProtocolDecl::protocol_iterator begin, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 796 | ObjCProtocolDecl::protocol_iterator end); |
| 797 | |
| 798 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 799 | QualType ResultType, |
| 800 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 801 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 802 | QualType Arg0Ty, |
| 803 | bool IsSuper, |
| 804 | const CallArgList &CallArgs); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 805 | |
| 806 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 807 | /// class of the given name. |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 808 | llvm::GlobalVariable *GetClassGlobal(const std::string &Name); |
| 809 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 810 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 811 | /// for the given class reference. |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 812 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 813 | const ObjCInterfaceDecl *ID); |
| 814 | |
| 815 | /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 816 | /// for the given super class reference. |
| 817 | llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder, |
| 818 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 819 | |
| 820 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 821 | /// meta-data |
| 822 | llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder, |
| 823 | const ObjCInterfaceDecl *ID); |
| 824 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 825 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 826 | /// the given ivar. |
| 827 | /// |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 828 | llvm::GlobalVariable * ObjCIvarOffsetVariable( |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 829 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 830 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 831 | |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 832 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 833 | /// for the given selector. |
| 834 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 835 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 836 | /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 837 | /// interface. The return value has type EHTypePtrTy. |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 838 | llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
| 839 | bool ForDefinition); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 840 | |
| 841 | const char *getMetaclassSymbolPrefix() const { |
| 842 | return "OBJC_METACLASS_$_"; |
| 843 | } |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 844 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 845 | const char *getClassSymbolPrefix() const { |
| 846 | return "OBJC_CLASS_$_"; |
| 847 | } |
| 848 | |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 849 | void GetClassSizeInfo(const ObjCInterfaceDecl *OID, |
| 850 | uint32_t &InstanceStart, |
| 851 | uint32_t &InstanceSize); |
| 852 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 853 | public: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 854 | CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 855 | // FIXME. All stubs for now! |
| 856 | virtual llvm::Function *ModuleInitFunction(); |
| 857 | |
| 858 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
| 859 | QualType ResultType, |
| 860 | Selector Sel, |
| 861 | llvm::Value *Receiver, |
| 862 | bool IsClassMessage, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 863 | const CallArgList &CallArgs); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 864 | |
| 865 | virtual CodeGen::RValue |
| 866 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 867 | QualType ResultType, |
| 868 | Selector Sel, |
| 869 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 870 | bool isCategoryImpl, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 871 | llvm::Value *Receiver, |
| 872 | bool IsClassMessage, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 873 | const CallArgList &CallArgs); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 874 | |
| 875 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 876 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 877 | |
| 878 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel) |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 879 | { return EmitSelector(Builder, Sel); } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 880 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 881 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 882 | |
| 883 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 884 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 885 | const ObjCProtocolDecl *PD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 886 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 887 | virtual llvm::Constant *GetPropertyGetFunction() { |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 888 | return ObjCTypes.GetPropertyFn; |
| 889 | } |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 890 | virtual llvm::Constant *GetPropertySetFunction() { |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 891 | return ObjCTypes.SetPropertyFn; |
| 892 | } |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 893 | virtual llvm::Constant *EnumerationMutationFunction() { |
Daniel Dunbar | 28ed084 | 2009-02-16 18:48:45 +0000 | [diff] [blame] | 894 | return ObjCTypes.EnumerationMutationFn; |
| 895 | } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 896 | |
| 897 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 898 | const Stmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 899 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 900 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 901 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 902 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 903 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 904 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 905 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 906 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 907 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 908 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 909 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 910 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 911 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 912 | QualType ObjectTy, |
| 913 | llvm::Value *BaseValue, |
| 914 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 915 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 916 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
| 917 | ObjCInterfaceDecl *Interface, |
| 918 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 919 | }; |
| 920 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 921 | } // end anonymous namespace |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 922 | |
| 923 | /* *** Helper Functions *** */ |
| 924 | |
| 925 | /// getConstantGEP() - Help routine to construct simple GEPs. |
| 926 | static llvm::Constant *getConstantGEP(llvm::Constant *C, |
| 927 | unsigned idx0, |
| 928 | unsigned idx1) { |
| 929 | llvm::Value *Idxs[] = { |
| 930 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0), |
| 931 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1) |
| 932 | }; |
| 933 | return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2); |
| 934 | } |
| 935 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 936 | /// hasObjCExceptionAttribute - Return true if this class or any super |
| 937 | /// class has the __objc_exception__ attribute. |
| 938 | static bool hasObjCExceptionAttribute(const ObjCInterfaceDecl *OID) { |
Daniel Dunbar | b11fa0d | 2009-04-13 21:08:27 +0000 | [diff] [blame] | 939 | if (OID->hasAttr<ObjCExceptionAttr>()) |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 940 | return true; |
| 941 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
| 942 | return hasObjCExceptionAttribute(Super); |
| 943 | return false; |
| 944 | } |
| 945 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 946 | /* *** CGObjCMac Public Interface *** */ |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 947 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 948 | CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm), |
| 949 | ObjCTypes(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 950 | { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 951 | ObjCABI = 1; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 952 | EmitImageInfo(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 953 | } |
| 954 | |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 955 | /// GetClass - Return a reference to the class for the given interface |
| 956 | /// decl. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 957 | llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 958 | const ObjCInterfaceDecl *ID) { |
| 959 | return EmitClassRef(Builder, ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | /// GetSelector - Return the pointer to the unique'd string for this selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 963 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 964 | return EmitSelector(Builder, Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 965 | } |
| 966 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 967 | /// Generate a constant CFString object. |
| 968 | /* |
| 969 | struct __builtin_CFString { |
| 970 | const int *isa; // point to __CFConstantStringClassReference |
| 971 | int flags; |
| 972 | const char *str; |
| 973 | long length; |
| 974 | }; |
| 975 | */ |
| 976 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 977 | llvm::Constant *CGObjCCommonMac::GenerateConstantString( |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 978 | const ObjCStringLiteral *SL) { |
Steve Naroff | 8d4141f | 2009-04-01 13:55:36 +0000 | [diff] [blame] | 979 | return CGM.GetAddrOfConstantCFString(SL->getString()); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | /// Generates a message send where the super is the receiver. This is |
| 983 | /// a message send to self with special delivery semantics indicating |
| 984 | /// which class's method should be called. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 985 | CodeGen::RValue |
| 986 | CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 987 | QualType ResultType, |
| 988 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 989 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 990 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 991 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 992 | bool IsClassMessage, |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 993 | const CodeGen::CallArgList &CallArgs) { |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 994 | // Create and init a super structure; this is a (receiver, class) |
| 995 | // pair we will pass to objc_msgSendSuper. |
| 996 | llvm::Value *ObjCSuper = |
| 997 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 998 | llvm::Value *ReceiverAsObject = |
| 999 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 1000 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 1001 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1002 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1003 | // If this is a class message the metaclass is passed as the target. |
| 1004 | llvm::Value *Target; |
| 1005 | if (IsClassMessage) { |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1006 | if (isCategoryImpl) { |
| 1007 | // Message sent to 'super' in a class method defined in a category |
| 1008 | // implementation requires an odd treatment. |
| 1009 | // If we are in a class method, we must retrieve the |
| 1010 | // _metaclass_ for the current class, pointed at by |
| 1011 | // the class's "isa" pointer. The following assumes that |
| 1012 | // isa" is the first ivar in a class (which it must be). |
| 1013 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1014 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 1015 | Target = CGF.Builder.CreateLoad(Target); |
| 1016 | } |
| 1017 | else { |
| 1018 | llvm::Value *MetaClassPtr = EmitMetaClassRef(Class); |
| 1019 | llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1); |
| 1020 | llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr); |
| 1021 | Target = Super; |
| 1022 | } |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1023 | } else { |
| 1024 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1025 | } |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1026 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext |
| 1027 | // and ObjCTypes types. |
| 1028 | const llvm::Type *ClassTy = |
| 1029 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1030 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1031 | CGF.Builder.CreateStore(Target, |
| 1032 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
| 1033 | |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1034 | return EmitMessageSend(CGF, ResultType, Sel, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1035 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 1036 | true, CallArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1037 | } |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1038 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1039 | /// Generate code for a message send expression. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1040 | CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1041 | QualType ResultType, |
| 1042 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1043 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1044 | bool IsClassMessage, |
| 1045 | const CallArgList &CallArgs) { |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1046 | llvm::Value *Arg0 = |
| 1047 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp"); |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1048 | return EmitMessageSend(CGF, ResultType, Sel, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1049 | Arg0, CGF.getContext().getObjCIdType(), |
| 1050 | false, CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1054 | QualType ResultType, |
| 1055 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1056 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1057 | QualType Arg0Ty, |
| 1058 | bool IsSuper, |
| 1059 | const CallArgList &CallArgs) { |
| 1060 | CallArgList ActualArgs; |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 1061 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
| 1062 | ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder, |
| 1063 | Sel)), |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1064 | CGF.getContext().getObjCSelType())); |
| 1065 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1066 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1067 | CodeGenTypes &Types = CGM.getTypes(); |
| 1068 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs); |
| 1069 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1070 | |
| 1071 | llvm::Constant *Fn; |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1072 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1073 | Fn = ObjCTypes.getSendStretFn(IsSuper); |
| 1074 | } else if (ResultType->isFloatingType()) { |
| 1075 | // FIXME: Sadly, this is wrong. This actually depends on the |
| 1076 | // architecture. This happens to be right for x86-32 though. |
| 1077 | Fn = ObjCTypes.getSendFpretFn(IsSuper); |
| 1078 | } else { |
| 1079 | Fn = ObjCTypes.getSendFn(IsSuper); |
| 1080 | } |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 1081 | Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy)); |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1082 | return CGF.EmitCall(FnInfo, Fn, ActualArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1085 | llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1086 | const ObjCProtocolDecl *PD) { |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1087 | // FIXME: I don't understand why gcc generates this, or where it is |
| 1088 | // resolved. Investigate. Its also wasteful to look this up over and |
| 1089 | // over. |
| 1090 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1091 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1092 | return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), |
| 1093 | ObjCTypes.ExternalProtocolPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1096 | void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1097 | // FIXME: We shouldn't need this, the protocol decl should contain |
| 1098 | // enough information to tell us whether this was a declaration or a |
| 1099 | // definition. |
| 1100 | DefinedProtocols.insert(PD->getIdentifier()); |
| 1101 | |
| 1102 | // If we have generated a forward reference to this protocol, emit |
| 1103 | // it now. Otherwise do nothing, the protocol objects are lazily |
| 1104 | // emitted. |
| 1105 | if (Protocols.count(PD->getIdentifier())) |
| 1106 | GetOrEmitProtocol(PD); |
| 1107 | } |
| 1108 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1109 | llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1110 | if (DefinedProtocols.count(PD->getIdentifier())) |
| 1111 | return GetOrEmitProtocol(PD); |
| 1112 | return GetOrEmitProtocolRef(PD); |
| 1113 | } |
| 1114 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1115 | /* |
| 1116 | // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions |
| 1117 | struct _objc_protocol { |
| 1118 | struct _objc_protocol_extension *isa; |
| 1119 | char *protocol_name; |
| 1120 | struct _objc_protocol_list *protocol_list; |
| 1121 | struct _objc__method_prototype_list *instance_methods; |
| 1122 | struct _objc__method_prototype_list *class_methods |
| 1123 | }; |
| 1124 | |
| 1125 | See EmitProtocolExtension(). |
| 1126 | */ |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1127 | llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) { |
| 1128 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1129 | |
| 1130 | // Early exit if a defining object has already been generated. |
| 1131 | if (Entry && Entry->hasInitializer()) |
| 1132 | return Entry; |
| 1133 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1134 | // FIXME: I don't understand why gcc generates this, or where it is |
| 1135 | // resolved. Investigate. Its also wasteful to look this up over and |
| 1136 | // over. |
| 1137 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1138 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1139 | const char *ProtocolName = PD->getNameAsCString(); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1140 | |
| 1141 | // Construct method lists. |
| 1142 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1143 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1144 | for (ObjCProtocolDecl::instmeth_iterator |
| 1145 | i = PD->instmeth_begin(CGM.getContext()), |
| 1146 | e = PD->instmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1147 | ObjCMethodDecl *MD = *i; |
| 1148 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1149 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1150 | OptInstanceMethods.push_back(C); |
| 1151 | } else { |
| 1152 | InstanceMethods.push_back(C); |
| 1153 | } |
| 1154 | } |
| 1155 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1156 | for (ObjCProtocolDecl::classmeth_iterator |
| 1157 | i = PD->classmeth_begin(CGM.getContext()), |
| 1158 | e = PD->classmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1159 | ObjCMethodDecl *MD = *i; |
| 1160 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1161 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1162 | OptClassMethods.push_back(C); |
| 1163 | } else { |
| 1164 | ClassMethods.push_back(C); |
| 1165 | } |
| 1166 | } |
| 1167 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1168 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1169 | Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1170 | Values[1] = GetClassName(PD->getIdentifier()); |
Daniel Dunbar | dbc93370 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1171 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1172 | EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(), |
Daniel Dunbar | dbc93370 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1173 | PD->protocol_begin(), |
| 1174 | PD->protocol_end()); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1175 | Values[3] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1176 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" |
| 1177 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1178 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1179 | InstanceMethods); |
| 1180 | Values[4] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1181 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" |
| 1182 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1183 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1184 | ClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1185 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 1186 | Values); |
| 1187 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1188 | if (Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1189 | // Already created, fix the linkage and update the initializer. |
| 1190 | Entry->setLinkage(llvm::GlobalValue::InternalLinkage); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1191 | Entry->setInitializer(Init); |
| 1192 | } else { |
| 1193 | Entry = |
| 1194 | new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, |
| 1195 | llvm::GlobalValue::InternalLinkage, |
| 1196 | Init, |
| 1197 | std::string("\01L_OBJC_PROTOCOL_")+ProtocolName, |
| 1198 | &CGM.getModule()); |
| 1199 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1200 | Entry->setAlignment(4); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1201 | UsedGlobals.push_back(Entry); |
| 1202 | // FIXME: Is this necessary? Why only for protocol? |
| 1203 | Entry->setAlignment(4); |
| 1204 | } |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1205 | |
| 1206 | return Entry; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1209 | llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1210 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1211 | |
| 1212 | if (!Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1213 | // We use the initializer as a marker of whether this is a forward |
| 1214 | // reference or not. At module finalization we add the empty |
| 1215 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1216 | Entry = |
| 1217 | new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1218 | llvm::GlobalValue::ExternalLinkage, |
| 1219 | 0, |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1220 | "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1221 | &CGM.getModule()); |
| 1222 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1223 | Entry->setAlignment(4); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1224 | UsedGlobals.push_back(Entry); |
| 1225 | // FIXME: Is this necessary? Why only for protocol? |
| 1226 | Entry->setAlignment(4); |
| 1227 | } |
| 1228 | |
| 1229 | return Entry; |
| 1230 | } |
| 1231 | |
| 1232 | /* |
| 1233 | struct _objc_protocol_extension { |
| 1234 | uint32_t size; |
| 1235 | struct objc_method_description_list *optional_instance_methods; |
| 1236 | struct objc_method_description_list *optional_class_methods; |
| 1237 | struct objc_property_list *instance_properties; |
| 1238 | }; |
| 1239 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1240 | llvm::Constant * |
| 1241 | CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 1242 | const ConstantVector &OptInstanceMethods, |
| 1243 | const ConstantVector &OptClassMethods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1244 | uint64_t Size = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1245 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1246 | std::vector<llvm::Constant*> Values(4); |
| 1247 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1248 | Values[1] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1249 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_" |
| 1250 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1251 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1252 | OptInstanceMethods); |
| 1253 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1254 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" |
| 1255 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1256 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1257 | OptClassMethods); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1258 | Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + |
| 1259 | PD->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1260 | 0, PD, ObjCTypes); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1261 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1262 | // Return null if no extension bits are used. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1263 | if (Values[1]->isNullValue() && Values[2]->isNullValue() && |
| 1264 | Values[3]->isNullValue()) |
| 1265 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
| 1266 | |
| 1267 | llvm::Constant *Init = |
| 1268 | llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1269 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1270 | // No special section, but goes in llvm.used |
| 1271 | return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(), |
| 1272 | Init, |
| 1273 | 0, 0, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | /* |
| 1277 | struct objc_protocol_list { |
| 1278 | struct objc_protocol_list *next; |
| 1279 | long count; |
| 1280 | Protocol *list[]; |
| 1281 | }; |
| 1282 | */ |
Daniel Dunbar | dbc93370 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1283 | llvm::Constant * |
| 1284 | CGObjCMac::EmitProtocolList(const std::string &Name, |
| 1285 | ObjCProtocolDecl::protocol_iterator begin, |
| 1286 | ObjCProtocolDecl::protocol_iterator end) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1287 | std::vector<llvm::Constant*> ProtocolRefs; |
| 1288 | |
Daniel Dunbar | dbc93370 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1289 | for (; begin != end; ++begin) |
| 1290 | ProtocolRefs.push_back(GetProtocolRef(*begin)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1291 | |
| 1292 | // Just return null for empty protocol lists |
| 1293 | if (ProtocolRefs.empty()) |
| 1294 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1295 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1296 | // This list is null terminated. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1297 | ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy)); |
| 1298 | |
| 1299 | std::vector<llvm::Constant*> Values(3); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1300 | // This field is only used by the runtime. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1301 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1302 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
| 1303 | Values[2] = |
| 1304 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy, |
| 1305 | ProtocolRefs.size()), |
| 1306 | ProtocolRefs); |
| 1307 | |
| 1308 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1309 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1310 | CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1311 | 4, false); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1312 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy); |
| 1313 | } |
| 1314 | |
| 1315 | /* |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1316 | struct _objc_property { |
| 1317 | const char * const name; |
| 1318 | const char * const attributes; |
| 1319 | }; |
| 1320 | |
| 1321 | struct _objc_property_list { |
| 1322 | uint32_t entsize; // sizeof (struct _objc_property) |
| 1323 | uint32_t prop_count; |
| 1324 | struct _objc_property[prop_count]; |
| 1325 | }; |
| 1326 | */ |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1327 | llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name, |
| 1328 | const Decl *Container, |
| 1329 | const ObjCContainerDecl *OCD, |
| 1330 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1331 | std::vector<llvm::Constant*> Properties, Prop(2); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1332 | for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(CGM.getContext()), |
| 1333 | E = OCD->prop_end(CGM.getContext()); I != E; ++I) { |
Steve Naroff | 93983f8 | 2009-01-11 12:47:58 +0000 | [diff] [blame] | 1334 | const ObjCPropertyDecl *PD = *I; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1335 | Prop[0] = GetPropertyName(PD->getIdentifier()); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1336 | Prop[1] = GetPropertyTypeString(PD, Container); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1337 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, |
| 1338 | Prop)); |
| 1339 | } |
| 1340 | |
| 1341 | // Return null for empty list. |
| 1342 | if (Properties.empty()) |
| 1343 | return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 1344 | |
| 1345 | unsigned PropertySize = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1346 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1347 | std::vector<llvm::Constant*> Values(3); |
| 1348 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize); |
| 1349 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size()); |
| 1350 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy, |
| 1351 | Properties.size()); |
| 1352 | Values[2] = llvm::ConstantArray::get(AT, Properties); |
| 1353 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1354 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1355 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1356 | CreateMetadataVar(Name, Init, |
| 1357 | (ObjCABI == 2) ? "__DATA, __objc_const" : |
| 1358 | "__OBJC,__property,regular,no_dead_strip", |
| 1359 | (ObjCABI == 2) ? 8 : 4, |
| 1360 | true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1361 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
| 1364 | /* |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1365 | struct objc_method_description_list { |
| 1366 | int count; |
| 1367 | struct objc_method_description list[]; |
| 1368 | }; |
| 1369 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1370 | llvm::Constant * |
| 1371 | CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 1372 | std::vector<llvm::Constant*> Desc(2); |
| 1373 | Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 1374 | ObjCTypes.SelectorPtrTy); |
| 1375 | Desc[1] = GetMethodVarType(MD); |
| 1376 | return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy, |
| 1377 | Desc); |
| 1378 | } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1379 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1380 | llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name, |
| 1381 | const char *Section, |
| 1382 | const ConstantVector &Methods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1383 | // Return null for empty list. |
| 1384 | if (Methods.empty()) |
| 1385 | return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
| 1386 | |
| 1387 | std::vector<llvm::Constant*> Values(2); |
| 1388 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 1389 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy, |
| 1390 | Methods.size()); |
| 1391 | Values[1] = llvm::ConstantArray::get(AT, Methods); |
| 1392 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1393 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1394 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1395 | return llvm::ConstantExpr::getBitCast(GV, |
| 1396 | ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1399 | /* |
| 1400 | struct _objc_category { |
| 1401 | char *category_name; |
| 1402 | char *class_name; |
| 1403 | struct _objc_method_list *instance_methods; |
| 1404 | struct _objc_method_list *class_methods; |
| 1405 | struct _objc_protocol_list *protocols; |
| 1406 | uint32_t size; // <rdar://4585769> |
| 1407 | struct _objc_property_list *instance_properties; |
| 1408 | }; |
| 1409 | */ |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1410 | void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1411 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1412 | |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1413 | // FIXME: This is poor design, the OCD should have a pointer to the |
| 1414 | // category decl. Additionally, note that Category can be null for |
| 1415 | // the @implementation w/o an @interface case. Sema should just |
| 1416 | // create one for us as it does for @implementation so everyone else |
| 1417 | // can live life under a clear blue sky. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1418 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1419 | const ObjCCategoryDecl *Category = |
| 1420 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1421 | std::string ExtName(Interface->getNameAsString() + "_" + |
| 1422 | OCD->getNameAsString()); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1423 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1424 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1425 | for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(), |
| 1426 | e = OCD->instmeth_end(); i != e; ++i) { |
| 1427 | // Instance methods should always be defined. |
| 1428 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1429 | } |
| 1430 | for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(), |
| 1431 | e = OCD->classmeth_end(); i != e; ++i) { |
| 1432 | // Class methods should always be defined. |
| 1433 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1434 | } |
| 1435 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1436 | std::vector<llvm::Constant*> Values(7); |
| 1437 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 1438 | Values[1] = GetClassName(Interface->getIdentifier()); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1439 | Values[2] = |
| 1440 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") + |
| 1441 | ExtName, |
| 1442 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1443 | InstanceMethods); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1444 | Values[3] = |
| 1445 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName, |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1446 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1447 | ClassMethods); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1448 | if (Category) { |
| 1449 | Values[4] = |
| 1450 | EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName, |
| 1451 | Category->protocol_begin(), |
| 1452 | Category->protocol_end()); |
| 1453 | } else { |
| 1454 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1455 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1456 | Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1457 | |
| 1458 | // If there is no category @interface then there can be no properties. |
| 1459 | if (Category) { |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1460 | Values[6] = EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName, |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1461 | OCD, Category, ObjCTypes); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1462 | } else { |
| 1463 | Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 1464 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1465 | |
| 1466 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy, |
| 1467 | Values); |
| 1468 | |
| 1469 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1470 | CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init, |
| 1471 | "__OBJC,__category,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1472 | 4, true); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1473 | DefinedCategories.push_back(GV); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1476 | // FIXME: Get from somewhere? |
| 1477 | enum ClassFlags { |
| 1478 | eClassFlags_Factory = 0x00001, |
| 1479 | eClassFlags_Meta = 0x00002, |
| 1480 | // <rdr://5142207> |
| 1481 | eClassFlags_HasCXXStructors = 0x02000, |
| 1482 | eClassFlags_Hidden = 0x20000, |
| 1483 | eClassFlags_ABI2_Hidden = 0x00010, |
| 1484 | eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634> |
| 1485 | }; |
| 1486 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1487 | /* |
| 1488 | struct _objc_class { |
| 1489 | Class isa; |
| 1490 | Class super_class; |
| 1491 | const char *name; |
| 1492 | long version; |
| 1493 | long info; |
| 1494 | long instance_size; |
| 1495 | struct _objc_ivar_list *ivars; |
| 1496 | struct _objc_method_list *methods; |
| 1497 | struct _objc_cache *cache; |
| 1498 | struct _objc_protocol_list *protocols; |
| 1499 | // Objective-C 1.0 extensions (<rdr://4585769>) |
| 1500 | const char *ivar_layout; |
| 1501 | struct _objc_class_ext *ext; |
| 1502 | }; |
| 1503 | |
| 1504 | See EmitClassExtension(); |
| 1505 | */ |
| 1506 | void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1507 | DefinedSymbols.insert(ID->getIdentifier()); |
| 1508 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1509 | std::string ClassName = ID->getNameAsString(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1510 | // FIXME: Gross |
| 1511 | ObjCInterfaceDecl *Interface = |
| 1512 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | dbc93370 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1513 | llvm::Constant *Protocols = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1514 | EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(), |
Daniel Dunbar | dbc93370 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1515 | Interface->protocol_begin(), |
| 1516 | Interface->protocol_end()); |
Chris Lattner | b7b58b1 | 2009-04-19 06:02:28 +0000 | [diff] [blame] | 1517 | const llvm::Type *InterfaceTy; |
| 1518 | if (Interface->isForwardDecl()) |
| 1519 | InterfaceTy = llvm::StructType::get(NULL, NULL); |
| 1520 | else |
| 1521 | InterfaceTy = |
Chris Lattner | 03d9f34 | 2009-04-01 06:23:52 +0000 | [diff] [blame] | 1522 | CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1523 | unsigned Flags = eClassFlags_Factory; |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1524 | unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1525 | |
| 1526 | // FIXME: Set CXX-structors flag. |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 1527 | if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1528 | Flags |= eClassFlags_Hidden; |
| 1529 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1530 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1531 | for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(), |
| 1532 | e = ID->instmeth_end(); i != e; ++i) { |
| 1533 | // Instance methods should always be defined. |
| 1534 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1535 | } |
| 1536 | for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(), |
| 1537 | e = ID->classmeth_end(); i != e; ++i) { |
| 1538 | // Class methods should always be defined. |
| 1539 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1540 | } |
| 1541 | |
| 1542 | for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(), |
| 1543 | e = ID->propimpl_end(); i != e; ++i) { |
| 1544 | ObjCPropertyImplDecl *PID = *i; |
| 1545 | |
| 1546 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 1547 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 1548 | |
| 1549 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 1550 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 1551 | InstanceMethods.push_back(C); |
| 1552 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 1553 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 1554 | InstanceMethods.push_back(C); |
| 1555 | } |
| 1556 | } |
| 1557 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1558 | std::vector<llvm::Constant*> Values(12); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1559 | Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1560 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1561 | // Record a reference to the super class. |
| 1562 | LazySymbols.insert(Super->getIdentifier()); |
| 1563 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1564 | Values[ 1] = |
| 1565 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 1566 | ObjCTypes.ClassPtrTy); |
| 1567 | } else { |
| 1568 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
| 1569 | } |
| 1570 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 1571 | // Version is always 0. |
| 1572 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 1573 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 1574 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1575 | Values[ 6] = EmitIvarList(ID, false); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1576 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1577 | EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1578 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1579 | InstanceMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1580 | // cache is always NULL. |
| 1581 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
| 1582 | Values[ 9] = Protocols; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1583 | // FIXME: Set ivar_layout |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 1584 | // Values[10] = BuildIvarLayout(ID, true); |
| 1585 | Values[10] = GetIvarLayoutName(0, ObjCTypes); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1586 | Values[11] = EmitClassExtension(ID); |
| 1587 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 1588 | Values); |
| 1589 | |
| 1590 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1591 | CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init, |
| 1592 | "__OBJC,__class,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1593 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1594 | DefinedClasses.push_back(GV); |
| 1595 | } |
| 1596 | |
| 1597 | llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID, |
| 1598 | llvm::Constant *Protocols, |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1599 | const llvm::Type *InterfaceTy, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1600 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1601 | unsigned Flags = eClassFlags_Meta; |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1602 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1603 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 1604 | if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1605 | Flags |= eClassFlags_Hidden; |
| 1606 | |
| 1607 | std::vector<llvm::Constant*> Values(12); |
| 1608 | // The isa for the metaclass is the root of the hierarchy. |
| 1609 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 1610 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 1611 | Root = Super; |
| 1612 | Values[ 0] = |
| 1613 | llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()), |
| 1614 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1615 | // The super class for the metaclass is emitted as the name of the |
| 1616 | // super class. The runtime fixes this up to point to the |
| 1617 | // *metaclass* for the super class. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1618 | if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) { |
| 1619 | Values[ 1] = |
| 1620 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 1621 | ObjCTypes.ClassPtrTy); |
| 1622 | } else { |
| 1623 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
| 1624 | } |
| 1625 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 1626 | // Version is always 0. |
| 1627 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 1628 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 1629 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1630 | Values[ 6] = EmitIvarList(ID, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1631 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1632 | EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1633 | "__OBJC,__cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1634 | Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1635 | // cache is always NULL. |
| 1636 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
| 1637 | Values[ 9] = Protocols; |
| 1638 | // ivar_layout for metaclass is always NULL. |
| 1639 | Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 1640 | // The class extension is always unused for metaclasses. |
| 1641 | Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
| 1642 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 1643 | Values); |
| 1644 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1645 | std::string Name("\01L_OBJC_METACLASS_"); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1646 | Name += ID->getNameAsCString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1647 | |
| 1648 | // Check for a forward reference. |
| 1649 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 1650 | if (GV) { |
| 1651 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 1652 | "Forward metaclass reference has incorrect type."); |
| 1653 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 1654 | GV->setInitializer(Init); |
| 1655 | } else { |
| 1656 | GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 1657 | llvm::GlobalValue::InternalLinkage, |
| 1658 | Init, Name, |
| 1659 | &CGM.getModule()); |
| 1660 | } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1661 | GV->setSection("__OBJC,__meta_class,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1662 | GV->setAlignment(4); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1663 | UsedGlobals.push_back(GV); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1664 | |
| 1665 | return GV; |
| 1666 | } |
| 1667 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1668 | llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1669 | std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1670 | |
| 1671 | // FIXME: Should we look these up somewhere other than the |
| 1672 | // module. Its a bit silly since we only generate these while |
| 1673 | // processing an implementation, so exactly one pointer would work |
| 1674 | // if know when we entered/exitted an implementation block. |
| 1675 | |
| 1676 | // Check for an existing forward reference. |
Fariborz Jahanian | b0d2794 | 2009-01-07 20:11:22 +0000 | [diff] [blame] | 1677 | // Previously, metaclass with internal linkage may have been defined. |
| 1678 | // pass 'true' as 2nd argument so it is returned. |
| 1679 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) { |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1680 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 1681 | "Forward metaclass reference has incorrect type."); |
| 1682 | return GV; |
| 1683 | } else { |
| 1684 | // Generate as an external reference to keep a consistent |
| 1685 | // module. This will be patched up when we emit the metaclass. |
| 1686 | return new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 1687 | llvm::GlobalValue::ExternalLinkage, |
| 1688 | 0, |
| 1689 | Name, |
| 1690 | &CGM.getModule()); |
| 1691 | } |
| 1692 | } |
| 1693 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1694 | /* |
| 1695 | struct objc_class_ext { |
| 1696 | uint32_t size; |
| 1697 | const char *weak_ivar_layout; |
| 1698 | struct _objc_property_list *properties; |
| 1699 | }; |
| 1700 | */ |
| 1701 | llvm::Constant * |
| 1702 | CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) { |
| 1703 | uint64_t Size = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1704 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1705 | |
| 1706 | std::vector<llvm::Constant*> Values(3); |
| 1707 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1708 | // FIXME: Output weak_ivar_layout string. |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 1709 | // Values[1] = BuildIvarLayout(ID, false); |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 1710 | Values[1] = GetIvarLayoutName(0, ObjCTypes); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1711 | Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1712 | ID, ID->getClassInterface(), ObjCTypes); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1713 | |
| 1714 | // Return null if no extension bits are used. |
| 1715 | if (Values[1]->isNullValue() && Values[2]->isNullValue()) |
| 1716 | return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
| 1717 | |
| 1718 | llvm::Constant *Init = |
| 1719 | llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1720 | return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1721 | Init, "__OBJC,__class_ext,regular,no_dead_strip", |
| 1722 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1723 | } |
| 1724 | |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 1725 | /// getInterfaceDeclForIvar - Get the interface declaration node where |
| 1726 | /// this ivar is declared in. |
| 1727 | /// FIXME. Ideally, this info should be in the ivar node. But currently |
| 1728 | /// it is not and prevailing wisdom is that ASTs should not have more |
| 1729 | /// info than is absolutely needed, even though this info reflects the |
| 1730 | /// source language. |
| 1731 | /// |
| 1732 | static const ObjCInterfaceDecl *getInterfaceDeclForIvar( |
| 1733 | const ObjCInterfaceDecl *OI, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1734 | const ObjCIvarDecl *IVD, |
| 1735 | ASTContext &Context) { |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 1736 | if (!OI) |
| 1737 | return 0; |
| 1738 | assert(isa<ObjCInterfaceDecl>(OI) && "OI is not an interface"); |
| 1739 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 1740 | E = OI->ivar_end(); I != E; ++I) |
| 1741 | if ((*I)->getIdentifier() == IVD->getIdentifier()) |
| 1742 | return OI; |
Fariborz Jahanian | 5a4b453 | 2009-03-31 17:00:52 +0000 | [diff] [blame] | 1743 | // look into properties. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1744 | for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(Context), |
| 1745 | E = OI->prop_end(Context); I != E; ++I) { |
Fariborz Jahanian | 5a4b453 | 2009-03-31 17:00:52 +0000 | [diff] [blame] | 1746 | ObjCPropertyDecl *PDecl = (*I); |
| 1747 | if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl()) |
| 1748 | if (IV->getIdentifier() == IVD->getIdentifier()) |
| 1749 | return OI; |
| 1750 | } |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1751 | return getInterfaceDeclForIvar(OI->getSuperClass(), IVD, Context); |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 1752 | } |
| 1753 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1754 | /* |
| 1755 | struct objc_ivar { |
| 1756 | char *ivar_name; |
| 1757 | char *ivar_type; |
| 1758 | int ivar_offset; |
| 1759 | }; |
| 1760 | |
| 1761 | struct objc_ivar_list { |
| 1762 | int ivar_count; |
| 1763 | struct objc_ivar list[count]; |
| 1764 | }; |
| 1765 | */ |
| 1766 | llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1767 | bool ForClass) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1768 | std::vector<llvm::Constant*> Ivars, Ivar(3); |
| 1769 | |
| 1770 | // When emitting the root class GCC emits ivar entries for the |
| 1771 | // actual class structure. It is not clear if we need to follow this |
| 1772 | // behavior; for now lets try and get away with not doing it. If so, |
| 1773 | // the cleanest solution would be to make up an ObjCInterfaceDecl |
| 1774 | // for the class. |
| 1775 | if (ForClass) |
| 1776 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1777 | |
| 1778 | ObjCInterfaceDecl *OID = |
| 1779 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 1780 | const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OID); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1781 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 1782 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
| 1783 | GetNamedIvarList(OID, OIvars); |
| 1784 | |
| 1785 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 1786 | ObjCIvarDecl *IVD = OIvars[i]; |
| 1787 | const FieldDecl *Field = OID->lookupFieldDeclForIvar(CGM.getContext(), IVD); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 1788 | Ivar[0] = GetMethodVarName(Field->getIdentifier()); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 1789 | Ivar[1] = GetMethodVarType(Field); |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 1790 | Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, |
| 1791 | GetIvarBaseOffset(Layout, Field)); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1792 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | // Return null for empty list. |
| 1796 | if (Ivars.empty()) |
| 1797 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
| 1798 | |
| 1799 | std::vector<llvm::Constant*> Values(2); |
| 1800 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 1801 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy, |
| 1802 | Ivars.size()); |
| 1803 | Values[1] = llvm::ConstantArray::get(AT, Ivars); |
| 1804 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1805 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1806 | llvm::GlobalVariable *GV; |
| 1807 | if (ForClass) |
| 1808 | GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(), |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1809 | Init, "__OBJC,__class_vars,regular,no_dead_strip", |
| 1810 | 4, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1811 | else |
| 1812 | GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" |
| 1813 | + ID->getNameAsString(), |
| 1814 | Init, "__OBJC,__instance_vars,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1815 | 4, true); |
| 1816 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | /* |
| 1820 | struct objc_method { |
| 1821 | SEL method_name; |
| 1822 | char *method_types; |
| 1823 | void *method; |
| 1824 | }; |
| 1825 | |
| 1826 | struct objc_method_list { |
| 1827 | struct objc_method_list *obsolete; |
| 1828 | int count; |
| 1829 | struct objc_method methods_list[count]; |
| 1830 | }; |
| 1831 | */ |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1832 | |
| 1833 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 1834 | /// given method if it has been defined. The result is null if the |
| 1835 | /// method has not been defined. The return value has type MethodPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1836 | llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1837 | // FIXME: Use DenseMap::lookup |
| 1838 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 1839 | if (!Fn) |
| 1840 | return 0; |
| 1841 | |
| 1842 | std::vector<llvm::Constant*> Method(3); |
| 1843 | Method[0] = |
| 1844 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 1845 | ObjCTypes.SelectorPtrTy); |
| 1846 | Method[1] = GetMethodVarType(MD); |
| 1847 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 1848 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 1849 | } |
| 1850 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1851 | llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name, |
| 1852 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1853 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1854 | // Return null for empty list. |
| 1855 | if (Methods.empty()) |
| 1856 | return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy); |
| 1857 | |
| 1858 | std::vector<llvm::Constant*> Values(3); |
| 1859 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 1860 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 1861 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 1862 | Methods.size()); |
| 1863 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 1864 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1865 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1866 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1867 | return llvm::ConstantExpr::getBitCast(GV, |
| 1868 | ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1869 | } |
| 1870 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1871 | llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD, |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 1872 | const ObjCContainerDecl *CD) { |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1873 | std::string Name; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 1874 | GetNameForMethod(OMD, CD, Name); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1875 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1876 | CodeGenTypes &Types = CGM.getTypes(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1877 | const llvm::FunctionType *MethodTy = |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1878 | Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic()); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1879 | llvm::Function *Method = |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1880 | llvm::Function::Create(MethodTy, |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1881 | llvm::GlobalValue::InternalLinkage, |
| 1882 | Name, |
| 1883 | &CGM.getModule()); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1884 | MethodDefinitions.insert(std::make_pair(OMD, Method)); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1885 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1886 | return Method; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 1889 | uint64_t CGObjCCommonMac::GetIvarBaseOffset(const llvm::StructLayout *Layout, |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 1890 | const FieldDecl *Field) { |
Fariborz Jahanian | a6681ae | 2009-03-09 20:44:22 +0000 | [diff] [blame] | 1891 | if (!Field->isBitField()) |
Daniel Dunbar | 48fa064 | 2009-04-19 02:03:42 +0000 | [diff] [blame] | 1892 | return Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field)); |
| 1893 | |
Fariborz Jahanian | a6681ae | 2009-03-09 20:44:22 +0000 | [diff] [blame] | 1894 | // FIXME. Must be a better way of getting a bitfield base offset. |
Daniel Dunbar | 48fa064 | 2009-04-19 02:03:42 +0000 | [diff] [blame] | 1895 | CodeGenTypes::BitFieldInfo BFI = CGM.getTypes().getBitFieldInfo(Field); |
| 1896 | // FIXME: The "field no" for bitfields is something completely |
| 1897 | // different; it is the offset in multiples of the base type size! |
| 1898 | uint64_t Offset = CGM.getTypes().getLLVMFieldNo(Field); |
| 1899 | const llvm::Type *Ty = |
| 1900 | CGM.getTypes().ConvertTypeForMemRecursive(Field->getType()); |
| 1901 | Offset *= CGM.getTypes().getTargetData().getTypePaddedSizeInBits(Ty); |
| 1902 | return (Offset + BFI.Begin) / 8; |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 1903 | } |
| 1904 | |
Daniel Dunbar | 48fa064 | 2009-04-19 02:03:42 +0000 | [diff] [blame] | 1905 | /// GetFieldBaseOffset - return the field's byte offset. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 1906 | uint64_t CGObjCCommonMac::GetFieldBaseOffset(const ObjCInterfaceDecl *OI, |
| 1907 | const llvm::StructLayout *Layout, |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 1908 | const FieldDecl *Field) { |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 1909 | // Is this a c struct? |
| 1910 | if (!OI) |
| 1911 | return Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field)); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 1912 | const ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field); |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 1913 | const FieldDecl *FD = OI->lookupFieldDeclForIvar(CGM.getContext(), Ivar); |
| 1914 | return GetIvarBaseOffset(Layout, FD); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 1915 | } |
| 1916 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 1917 | llvm::GlobalVariable * |
| 1918 | CGObjCCommonMac::CreateMetadataVar(const std::string &Name, |
| 1919 | llvm::Constant *Init, |
| 1920 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 1921 | unsigned Align, |
| 1922 | bool AddToUsed) { |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 1923 | const llvm::Type *Ty = Init->getType(); |
| 1924 | llvm::GlobalVariable *GV = |
| 1925 | new llvm::GlobalVariable(Ty, false, |
| 1926 | llvm::GlobalValue::InternalLinkage, |
| 1927 | Init, |
| 1928 | Name, |
| 1929 | &CGM.getModule()); |
| 1930 | if (Section) |
| 1931 | GV->setSection(Section); |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 1932 | if (Align) |
| 1933 | GV->setAlignment(Align); |
| 1934 | if (AddToUsed) |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 1935 | UsedGlobals.push_back(GV); |
| 1936 | return GV; |
| 1937 | } |
| 1938 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1939 | llvm::Function *CGObjCMac::ModuleInitFunction() { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1940 | // Abuse this interface function as a place to finalize. |
| 1941 | FinishModule(); |
| 1942 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1943 | return NULL; |
| 1944 | } |
| 1945 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1946 | llvm::Constant *CGObjCMac::GetPropertyGetFunction() { |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 1947 | return ObjCTypes.GetPropertyFn; |
| 1948 | } |
| 1949 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1950 | llvm::Constant *CGObjCMac::GetPropertySetFunction() { |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 1951 | return ObjCTypes.SetPropertyFn; |
| 1952 | } |
| 1953 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1954 | llvm::Constant *CGObjCMac::EnumerationMutationFunction() { |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 1955 | return ObjCTypes.EnumerationMutationFn; |
| 1956 | } |
| 1957 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1958 | /* |
| 1959 | |
| 1960 | Objective-C setjmp-longjmp (sjlj) Exception Handling |
| 1961 | -- |
| 1962 | |
| 1963 | The basic framework for a @try-catch-finally is as follows: |
| 1964 | { |
| 1965 | objc_exception_data d; |
| 1966 | id _rethrow = null; |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 1967 | bool _call_try_exit = true; |
| 1968 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1969 | objc_exception_try_enter(&d); |
| 1970 | if (!setjmp(d.jmp_buf)) { |
| 1971 | ... try body ... |
| 1972 | } else { |
| 1973 | // exception path |
| 1974 | id _caught = objc_exception_extract(&d); |
| 1975 | |
| 1976 | // enter new try scope for handlers |
| 1977 | if (!setjmp(d.jmp_buf)) { |
| 1978 | ... match exception and execute catch blocks ... |
| 1979 | |
| 1980 | // fell off end, rethrow. |
| 1981 | _rethrow = _caught; |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1982 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1983 | } else { |
| 1984 | // exception in catch block |
| 1985 | _rethrow = objc_exception_extract(&d); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 1986 | _call_try_exit = false; |
| 1987 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1988 | } |
| 1989 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1990 | ... jump-through-finally to finally_end ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1991 | |
| 1992 | finally: |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 1993 | if (_call_try_exit) |
| 1994 | objc_exception_try_exit(&d); |
| 1995 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1996 | ... finally block .... |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1997 | ... dispatch to finally destination ... |
| 1998 | |
| 1999 | finally_rethrow: |
| 2000 | objc_exception_throw(_rethrow); |
| 2001 | |
| 2002 | finally_end: |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2003 | } |
| 2004 | |
| 2005 | This framework differs slightly from the one gcc uses, in that gcc |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2006 | uses _rethrow to determine if objc_exception_try_exit should be called |
| 2007 | and if the object should be rethrown. This breaks in the face of |
| 2008 | throwing nil and introduces unnecessary branches. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2009 | |
| 2010 | We specialize this framework for a few particular circumstances: |
| 2011 | |
| 2012 | - If there are no catch blocks, then we avoid emitting the second |
| 2013 | exception handling context. |
| 2014 | |
| 2015 | - If there is a catch-all catch block (i.e. @catch(...) or @catch(id |
| 2016 | e)) we avoid emitting the code to rethrow an uncaught exception. |
| 2017 | |
| 2018 | - FIXME: If there is no @finally block we can do a few more |
| 2019 | simplifications. |
| 2020 | |
| 2021 | Rethrows and Jumps-Through-Finally |
| 2022 | -- |
| 2023 | |
| 2024 | Support for implicit rethrows and jumping through the finally block is |
| 2025 | handled by storing the current exception-handling context in |
| 2026 | ObjCEHStack. |
| 2027 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2028 | In order to implement proper @finally semantics, we support one basic |
| 2029 | mechanism for jumping through the finally block to an arbitrary |
| 2030 | destination. Constructs which generate exits from a @try or @catch |
| 2031 | block use this mechanism to implement the proper semantics by chaining |
| 2032 | jumps, as necessary. |
| 2033 | |
| 2034 | This mechanism works like the one used for indirect goto: we |
| 2035 | arbitrarily assign an ID to each destination and store the ID for the |
| 2036 | destination in a variable prior to entering the finally block. At the |
| 2037 | end of the finally block we simply create a switch to the proper |
| 2038 | destination. |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2039 | |
| 2040 | Code gen for @synchronized(expr) stmt; |
| 2041 | Effectively generating code for: |
| 2042 | objc_sync_enter(expr); |
| 2043 | @try stmt @finally { objc_sync_exit(expr); } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2044 | */ |
| 2045 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2046 | void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 2047 | const Stmt &S) { |
| 2048 | bool isTry = isa<ObjCAtTryStmt>(S); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2049 | // Create various blocks we refer to for handling @finally. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2050 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2051 | llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit"); |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2052 | llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit"); |
| 2053 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
| 2054 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2055 | |
| 2056 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 2057 | // evaluation of the expression must occur before we enter the |
| 2058 | // @synchronized. We can safely avoid a temp here because jumps into |
| 2059 | // @synchronized are illegal & this will dominate uses. |
| 2060 | llvm::Value *SyncArg = 0; |
| 2061 | if (!isTry) { |
| 2062 | SyncArg = |
| 2063 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 2064 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 2065 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2066 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2067 | |
| 2068 | // Push an EH context entry, used for handling rethrows and jumps |
| 2069 | // through finally. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2070 | CGF.PushCleanupBlock(FinallyBlock); |
| 2071 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2072 | CGF.ObjCEHValueStack.push_back(0); |
| 2073 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2074 | // Allocate memory for the exception data and rethrow pointer. |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2075 | llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy, |
| 2076 | "exceptiondata.ptr"); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2077 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy, |
| 2078 | "_rethrow"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2079 | llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty, |
| 2080 | "_call_try_exit"); |
| 2081 | CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr); |
| 2082 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2083 | // Enter a new try block and call setjmp. |
| 2084 | CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData); |
| 2085 | llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0, |
| 2086 | "jmpbufarray"); |
| 2087 | JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp"); |
| 2088 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn, |
| 2089 | JmpBufPtr, "result"); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2090 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2091 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 2092 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2093 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2094 | TryHandler, TryBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2095 | |
| 2096 | // Emit the @try block. |
| 2097 | CGF.EmitBlock(TryBlock); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2098 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 2099 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2100 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2101 | |
| 2102 | // Emit the "exception in @try" block. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2103 | CGF.EmitBlock(TryHandler); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2104 | |
| 2105 | // Retrieve the exception object. We may emit multiple blocks but |
| 2106 | // nothing can cross this so the value is already in SSA form. |
| 2107 | llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn, |
| 2108 | ExceptionData, |
| 2109 | "caught"); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2110 | CGF.ObjCEHValueStack.back() = Caught; |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2111 | if (!isTry) |
| 2112 | { |
| 2113 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2114 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2115 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2116 | } |
| 2117 | else if (const ObjCAtCatchStmt* CatchStmt = |
| 2118 | cast<ObjCAtTryStmt>(S).getCatchStmts()) |
| 2119 | { |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2120 | // Enter a new exception try block (in case a @catch block throws |
| 2121 | // an exception). |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2122 | CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2123 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2124 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn, |
| 2125 | JmpBufPtr, "result"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2126 | llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2127 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2128 | llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch"); |
| 2129 | llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler"); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2130 | CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2131 | |
| 2132 | CGF.EmitBlock(CatchBlock); |
| 2133 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2134 | // Handle catch list. As a special case we check if everything is |
| 2135 | // matched and avoid generating code for falling off the end if |
| 2136 | // so. |
| 2137 | bool AllMatched = false; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2138 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2139 | llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2140 | |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2141 | const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl(); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2142 | const PointerType *PT = 0; |
| 2143 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2144 | // catch(...) always matches. |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2145 | if (!CatchParam) { |
| 2146 | AllMatched = true; |
| 2147 | } else { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2148 | PT = CatchParam->getType()->getAsPointerType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2149 | |
Daniel Dunbar | 97f61d1 | 2008-09-27 22:21:14 +0000 | [diff] [blame] | 2150 | // catch(id e) always matches. |
| 2151 | // FIXME: For the time being we also match id<X>; this should |
| 2152 | // be rejected by Sema instead. |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 2153 | if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) || |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2154 | CatchParam->getType()->isObjCQualifiedIdType()) |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2155 | AllMatched = true; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2156 | } |
| 2157 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2158 | if (AllMatched) { |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2159 | if (CatchParam) { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2160 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2161 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2162 | CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2163 | } |
Anders Carlsson | 1452f55 | 2008-09-11 08:21:54 +0000 | [diff] [blame] | 2164 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2165 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2166 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2167 | break; |
| 2168 | } |
| 2169 | |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2170 | assert(PT && "Unexpected non-pointer type in @catch"); |
| 2171 | QualType T = PT->getPointeeType(); |
Anders Carlsson | 4b7ff6e | 2008-09-11 06:35:14 +0000 | [diff] [blame] | 2172 | const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2173 | assert(ObjCType && "Catch parameter must have Objective-C type!"); |
| 2174 | |
| 2175 | // Check if the @catch block matches the exception object. |
| 2176 | llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl()); |
| 2177 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2178 | llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn, |
| 2179 | Class, Caught, "match"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2180 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2181 | llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2182 | |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2183 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2184 | MatchedBlock, NextCatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2185 | |
| 2186 | // Emit the @catch block. |
| 2187 | CGF.EmitBlock(MatchedBlock); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2188 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2189 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2190 | |
| 2191 | llvm::Value *Tmp = |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2192 | CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()), |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2193 | "tmp"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2194 | CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2195 | |
| 2196 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2197 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2198 | |
| 2199 | CGF.EmitBlock(NextCatchBlock); |
| 2200 | } |
| 2201 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2202 | if (!AllMatched) { |
| 2203 | // None of the handlers caught the exception, so store it to be |
| 2204 | // rethrown at the end of the @finally block. |
| 2205 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2206 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2207 | } |
| 2208 | |
| 2209 | // Emit the exception handler for the @catch blocks. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2210 | CGF.EmitBlock(CatchHandler); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2211 | CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn, |
| 2212 | ExceptionData), |
| 2213 | RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2214 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2215 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2216 | } else { |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2217 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2218 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2219 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2220 | } |
| 2221 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2222 | // Pop the exception-handling stack entry. It is important to do |
| 2223 | // this now, because the code in the @finally block is not in this |
| 2224 | // context. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2225 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 2226 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2227 | CGF.ObjCEHValueStack.pop_back(); |
| 2228 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2229 | // Emit the @finally block. |
| 2230 | CGF.EmitBlock(FinallyBlock); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2231 | llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp"); |
| 2232 | |
| 2233 | CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit); |
| 2234 | |
| 2235 | CGF.EmitBlock(FinallyExit); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2236 | CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2237 | |
| 2238 | CGF.EmitBlock(FinallyNoExit); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2239 | if (isTry) { |
| 2240 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 2241 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 2242 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2243 | } else { |
| 2244 | // Emit objc_sync_exit(expr); as finally's sole statement for |
| 2245 | // @synchronized. |
| 2246 | CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, SyncArg); |
Fariborz Jahanian | f2878e5 | 2008-11-21 19:21:53 +0000 | [diff] [blame] | 2247 | } |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2248 | |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2249 | // Emit the switch block |
| 2250 | if (Info.SwitchBlock) |
| 2251 | CGF.EmitBlock(Info.SwitchBlock); |
| 2252 | if (Info.EndBlock) |
| 2253 | CGF.EmitBlock(Info.EndBlock); |
| 2254 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2255 | CGF.EmitBlock(FinallyRethrow); |
| 2256 | CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, |
| 2257 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2258 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2259 | |
| 2260 | CGF.EmitBlock(FinallyEnd); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2261 | } |
| 2262 | |
| 2263 | void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2264 | const ObjCAtThrowStmt &S) { |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2265 | llvm::Value *ExceptionAsObject; |
| 2266 | |
| 2267 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
| 2268 | llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr); |
| 2269 | ExceptionAsObject = |
| 2270 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 2271 | } else { |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2272 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2273 | "Unexpected rethrow outside @catch block."); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2274 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2275 | } |
| 2276 | |
| 2277 | CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2278 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2279 | |
| 2280 | // Clear the insertion point to indicate we are in unreachable code. |
| 2281 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2282 | } |
| 2283 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2284 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2285 | /// object: objc_read_weak (id *src) |
| 2286 | /// |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2287 | llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2288 | llvm::Value *AddrWeakObj) |
| 2289 | { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2290 | const llvm::Type* DestTy = |
| 2291 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2292 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2293 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn, |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2294 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2295 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2296 | return read_weak; |
| 2297 | } |
| 2298 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2299 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 2300 | /// objc_assign_weak (id src, id *dst) |
| 2301 | /// |
| 2302 | void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 2303 | llvm::Value *src, llvm::Value *dst) |
| 2304 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2305 | const llvm::Type * SrcTy = src->getType(); |
| 2306 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2307 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2308 | assert(Size <= 8 && "does not support size > 8"); |
| 2309 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2310 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2311 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2312 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2313 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2314 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 2315 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2316 | src, dst, "weakassign"); |
| 2317 | return; |
| 2318 | } |
| 2319 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2320 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 2321 | /// objc_assign_global (id src, id *dst) |
| 2322 | /// |
| 2323 | void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 2324 | llvm::Value *src, llvm::Value *dst) |
| 2325 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2326 | const llvm::Type * SrcTy = src->getType(); |
| 2327 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2328 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2329 | assert(Size <= 8 && "does not support size > 8"); |
| 2330 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2331 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2332 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2333 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2334 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2335 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2336 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn, |
| 2337 | src, dst, "globalassign"); |
| 2338 | return; |
| 2339 | } |
| 2340 | |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2341 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 2342 | /// objc_assign_ivar (id src, id *dst) |
| 2343 | /// |
| 2344 | void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 2345 | llvm::Value *src, llvm::Value *dst) |
| 2346 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2347 | const llvm::Type * SrcTy = src->getType(); |
| 2348 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2349 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2350 | assert(Size <= 8 && "does not support size > 8"); |
| 2351 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2352 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2353 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2354 | } |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2355 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2356 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
| 2357 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn, |
| 2358 | src, dst, "assignivar"); |
| 2359 | return; |
| 2360 | } |
| 2361 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2362 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 2363 | /// objc_assign_strongCast (id src, id *dst) |
| 2364 | /// |
| 2365 | void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 2366 | llvm::Value *src, llvm::Value *dst) |
| 2367 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2368 | const llvm::Type * SrcTy = src->getType(); |
| 2369 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2370 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2371 | assert(Size <= 8 && "does not support size > 8"); |
| 2372 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2373 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2374 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2375 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2376 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2377 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2378 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn, |
| 2379 | src, dst, "weakassign"); |
| 2380 | return; |
| 2381 | } |
| 2382 | |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2383 | /// EmitObjCValueForIvar - Code Gen for ivar reference. |
| 2384 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2385 | LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 2386 | QualType ObjectTy, |
| 2387 | llvm::Value *BaseValue, |
| 2388 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2389 | unsigned CVRQualifiers) { |
Daniel Dunbar | 525c9b7 | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 2390 | const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl(); |
| 2391 | const FieldDecl *Field = ID->lookupFieldDeclForIvar(CGM.getContext(), Ivar); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2392 | if (Ivar->isBitField()) |
| 2393 | return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field), |
| 2394 | CVRQualifiers); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2395 | // TODO: Add a special case for isa (index 0) |
| 2396 | unsigned Index = CGM.getTypes().getLLVMFieldNo(Field); |
| 2397 | llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp"); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2398 | LValue LV = LValue::MakeAddr(V, |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 2399 | Ivar->getType().getCVRQualifiers()|CVRQualifiers, |
| 2400 | CGM.getContext().getObjCGCAttrKind(Ivar->getType())); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2401 | LValue::SetObjCIvar(LV, true); |
| 2402 | return LV; |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2403 | } |
| 2404 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2405 | llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
| 2406 | ObjCInterfaceDecl *Interface, |
| 2407 | const ObjCIvarDecl *Ivar) { |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 2408 | const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(Interface); |
Daniel Dunbar | 60952f9 | 2009-04-20 00:37:55 +0000 | [diff] [blame] | 2409 | const FieldDecl *Field = |
| 2410 | Interface->lookupFieldDeclForIvar(CGM.getContext(), Ivar); |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 2411 | uint64_t Offset = GetIvarBaseOffset(Layout, Field); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2412 | return llvm::ConstantInt::get( |
| 2413 | CGM.getTypes().ConvertType(CGM.getContext().LongTy), |
| 2414 | Offset); |
| 2415 | } |
| 2416 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2417 | /* *** Private Interface *** */ |
| 2418 | |
| 2419 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 2420 | /// level information. |
| 2421 | /// |
| 2422 | /// See: <rdr://4810609&4810587&4810587> |
| 2423 | /// struct IMAGE_INFO { |
| 2424 | /// unsigned version; |
| 2425 | /// unsigned flags; |
| 2426 | /// }; |
| 2427 | enum ImageInfoFlags { |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 2428 | eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what |
| 2429 | // this implies. |
| 2430 | eImageInfo_GarbageCollected = (1 << 1), |
| 2431 | eImageInfo_GCOnly = (1 << 2), |
| 2432 | eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set. |
| 2433 | |
| 2434 | // A flag indicating that the module has no instances of an |
| 2435 | // @synthesize of a superclass variable. <rdar://problem/6803242> |
| 2436 | eImageInfo_CorrectedSynthesize = (1 << 4) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2437 | }; |
| 2438 | |
| 2439 | void CGObjCMac::EmitImageInfo() { |
| 2440 | unsigned version = 0; // Version is unused? |
| 2441 | unsigned flags = 0; |
| 2442 | |
| 2443 | // FIXME: Fix and continue? |
| 2444 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 2445 | flags |= eImageInfo_GarbageCollected; |
| 2446 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 2447 | flags |= eImageInfo_GCOnly; |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 2448 | |
| 2449 | // We never allow @synthesize of a superclass property. |
| 2450 | flags |= eImageInfo_CorrectedSynthesize; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2451 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2452 | // Emitted as int[2]; |
| 2453 | llvm::Constant *values[2] = { |
| 2454 | llvm::ConstantInt::get(llvm::Type::Int32Ty, version), |
| 2455 | llvm::ConstantInt::get(llvm::Type::Int32Ty, flags) |
| 2456 | }; |
| 2457 | llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2458 | |
| 2459 | const char *Section; |
| 2460 | if (ObjCABI == 1) |
| 2461 | Section = "__OBJC, __image_info,regular"; |
| 2462 | else |
| 2463 | Section = "__DATA, __objc_imageinfo, regular, no_dead_strip"; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2464 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2465 | CreateMetadataVar("\01L_OBJC_IMAGE_INFO", |
| 2466 | llvm::ConstantArray::get(AT, values, 2), |
| 2467 | Section, |
| 2468 | 0, |
| 2469 | true); |
| 2470 | GV->setConstant(true); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2471 | } |
| 2472 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2473 | |
| 2474 | // struct objc_module { |
| 2475 | // unsigned long version; |
| 2476 | // unsigned long size; |
| 2477 | // const char *name; |
| 2478 | // Symtab symtab; |
| 2479 | // }; |
| 2480 | |
| 2481 | // FIXME: Get from somewhere |
| 2482 | static const int ModuleVersion = 7; |
| 2483 | |
| 2484 | void CGObjCMac::EmitModuleInfo() { |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 2485 | uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2486 | |
| 2487 | std::vector<llvm::Constant*> Values(4); |
| 2488 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion); |
| 2489 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2490 | // This used to be the filename, now it is unused. <rdr://4327263> |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2491 | Values[2] = GetClassName(&CGM.getContext().Idents.get("")); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2492 | Values[3] = EmitModuleSymbols(); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2493 | CreateMetadataVar("\01L_OBJC_MODULES", |
| 2494 | llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values), |
| 2495 | "__OBJC,__module_info,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2496 | 4, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2497 | } |
| 2498 | |
| 2499 | llvm::Constant *CGObjCMac::EmitModuleSymbols() { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2500 | unsigned NumClasses = DefinedClasses.size(); |
| 2501 | unsigned NumCategories = DefinedCategories.size(); |
| 2502 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2503 | // Return null if no symbols were defined. |
| 2504 | if (!NumClasses && !NumCategories) |
| 2505 | return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy); |
| 2506 | |
| 2507 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2508 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2509 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy); |
| 2510 | Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses); |
| 2511 | Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories); |
| 2512 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2513 | // The runtime expects exactly the list of defined classes followed |
| 2514 | // by the list of defined categories, in a single array. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2515 | std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2516 | for (unsigned i=0; i<NumClasses; i++) |
| 2517 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
| 2518 | ObjCTypes.Int8PtrTy); |
| 2519 | for (unsigned i=0; i<NumCategories; i++) |
| 2520 | Symbols[NumClasses + i] = |
| 2521 | llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
| 2522 | ObjCTypes.Int8PtrTy); |
| 2523 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2524 | Values[4] = |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2525 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2526 | NumClasses + NumCategories), |
| 2527 | Symbols); |
| 2528 | |
| 2529 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 2530 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2531 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2532 | CreateMetadataVar("\01L_OBJC_SYMBOLS", Init, |
| 2533 | "__OBJC,__symbols,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2534 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2535 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy); |
| 2536 | } |
| 2537 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2538 | llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2539 | const ObjCInterfaceDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2540 | LazySymbols.insert(ID->getIdentifier()); |
| 2541 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2542 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 2543 | |
| 2544 | if (!Entry) { |
| 2545 | llvm::Constant *Casted = |
| 2546 | llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()), |
| 2547 | ObjCTypes.ClassPtrTy); |
| 2548 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2549 | CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted, |
| 2550 | "__OBJC,__cls_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2551 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2552 | } |
| 2553 | |
| 2554 | return Builder.CreateLoad(Entry, false, "tmp"); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2555 | } |
| 2556 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2557 | llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2558 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 2559 | |
| 2560 | if (!Entry) { |
| 2561 | llvm::Constant *Casted = |
| 2562 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 2563 | ObjCTypes.SelectorPtrTy); |
| 2564 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2565 | CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted, |
| 2566 | "__OBJC,__message_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2567 | 4, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2568 | } |
| 2569 | |
| 2570 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 2571 | } |
| 2572 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 2573 | llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2574 | llvm::GlobalVariable *&Entry = ClassNames[Ident]; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2575 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2576 | if (!Entry) |
| 2577 | Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
| 2578 | llvm::ConstantArray::get(Ident->getName()), |
| 2579 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 2580 | 1, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2581 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2582 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2583 | } |
| 2584 | |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 2585 | /// GetInterfaceDeclStructLayout - Get layout for ivars of given |
| 2586 | /// interface declaration. |
| 2587 | const llvm::StructLayout *CGObjCCommonMac::GetInterfaceDeclStructLayout( |
| 2588 | const ObjCInterfaceDecl *OID) const { |
Daniel Dunbar | 24c8991 | 2009-04-21 21:41:56 +0000 | [diff] [blame] | 2589 | assert(!OID->isForwardDecl() && "Invalid interface decl!"); |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 2590 | QualType T = |
| 2591 | CGM.getContext().getObjCInterfaceType(const_cast<ObjCInterfaceDecl*>(OID)); |
| 2592 | const llvm::StructType *InterfaceTy = |
| 2593 | cast<llvm::StructType>(CGM.getTypes().ConvertType(T)); |
| 2594 | return CGM.getTargetData().getStructLayout(InterfaceTy); |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 2595 | } |
| 2596 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 2597 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 2598 | /// ivar layout bitmap. |
| 2599 | llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident, |
| 2600 | const ObjCCommonTypesHelper &ObjCTypes) { |
| 2601 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 2602 | } |
| 2603 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2604 | void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCInterfaceDecl *OI, |
| 2605 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2606 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2607 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2608 | unsigned int BytePos, bool ForStrongLayout, |
| 2609 | int &Index, int &SkIndex, bool &HasUnion) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2610 | bool IsUnion = (RD && RD->isUnion()); |
| 2611 | uint64_t MaxUnionIvarSize = 0; |
| 2612 | uint64_t MaxSkippedUnionIvarSize = 0; |
| 2613 | FieldDecl *MaxField = 0; |
| 2614 | FieldDecl *MaxSkippedField = 0; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2615 | FieldDecl *LastFieldBitfield = 0; |
| 2616 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2617 | unsigned base = 0; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2618 | if (RecFields.empty()) |
| 2619 | return; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2620 | if (IsUnion) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2621 | base = BytePos + GetFieldBaseOffset(OI, Layout, RecFields[0]); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2622 | unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0); |
| 2623 | unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth(); |
| 2624 | |
| 2625 | llvm::SmallVector<FieldDecl*, 16> TmpRecFields; |
| 2626 | |
| 2627 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2628 | FieldDecl *Field = RecFields[i]; |
| 2629 | // Skip over unnamed or bitfields |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2630 | if (!Field->getIdentifier() || Field->isBitField()) { |
| 2631 | LastFieldBitfield = Field; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2632 | continue; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2633 | } |
| 2634 | LastFieldBitfield = 0; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2635 | QualType FQT = Field->getType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2636 | if (FQT->isRecordType() || FQT->isUnionType()) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2637 | if (FQT->isUnionType()) |
| 2638 | HasUnion = true; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2639 | else |
| 2640 | assert(FQT->isRecordType() && |
| 2641 | "only union/record is supported for ivar layout bitmap"); |
| 2642 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2643 | const RecordType *RT = FQT->getAsRecordType(); |
| 2644 | const RecordDecl *RD = RT->getDecl(); |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 2645 | // FIXME - Find a more efficient way of passing records down. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2646 | TmpRecFields.append(RD->field_begin(CGM.getContext()), |
| 2647 | RD->field_end(CGM.getContext())); |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 2648 | const llvm::Type *Ty = CGM.getTypes().ConvertType(FQT); |
| 2649 | const llvm::StructLayout *RecLayout = |
| 2650 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty)); |
| 2651 | |
| 2652 | BuildAggrIvarLayout(0, RecLayout, RD, TmpRecFields, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2653 | BytePos + GetFieldBaseOffset(OI, Layout, Field), |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2654 | ForStrongLayout, Index, SkIndex, |
| 2655 | HasUnion); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2656 | TmpRecFields.clear(); |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2657 | continue; |
| 2658 | } |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2659 | |
| 2660 | if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2661 | const ConstantArrayType *CArray = |
| 2662 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2663 | uint64_t ElCount = CArray->getSize().getZExtValue(); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2664 | assert(CArray && "only array with know element size is supported"); |
| 2665 | FQT = CArray->getElementType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2666 | while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 2667 | const ConstantArrayType *CArray = |
| 2668 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2669 | ElCount *= CArray->getSize().getZExtValue(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2670 | FQT = CArray->getElementType(); |
| 2671 | } |
| 2672 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2673 | assert(!FQT->isUnionType() && |
| 2674 | "layout for array of unions not supported"); |
| 2675 | if (FQT->isRecordType()) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2676 | int OldIndex = Index; |
| 2677 | int OldSkIndex = SkIndex; |
| 2678 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2679 | // FIXME - Use a common routine with the above! |
| 2680 | const RecordType *RT = FQT->getAsRecordType(); |
| 2681 | const RecordDecl *RD = RT->getDecl(); |
| 2682 | // FIXME - Find a more efficiant way of passing records down. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2683 | TmpRecFields.append(RD->field_begin(CGM.getContext()), |
| 2684 | RD->field_end(CGM.getContext())); |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 2685 | const llvm::Type *Ty = CGM.getTypes().ConvertType(FQT); |
| 2686 | const llvm::StructLayout *RecLayout = |
| 2687 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty)); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2688 | |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 2689 | BuildAggrIvarLayout(0, RecLayout, RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2690 | TmpRecFields, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2691 | BytePos + GetFieldBaseOffset(OI, Layout, Field), |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2692 | ForStrongLayout, Index, SkIndex, |
| 2693 | HasUnion); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2694 | TmpRecFields.clear(); |
| 2695 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2696 | // Replicate layout information for each array element. Note that |
| 2697 | // one element is already done. |
| 2698 | uint64_t ElIx = 1; |
| 2699 | for (int FirstIndex = Index, FirstSkIndex = SkIndex; |
| 2700 | ElIx < ElCount; ElIx++) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2701 | uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2702 | for (int i = OldIndex+1; i <= FirstIndex; ++i) |
| 2703 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2704 | GC_IVAR gcivar; |
| 2705 | gcivar.ivar_bytepos = IvarsInfo[i].ivar_bytepos + Size*ElIx; |
| 2706 | gcivar.ivar_size = IvarsInfo[i].ivar_size; |
| 2707 | IvarsInfo.push_back(gcivar); ++Index; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2708 | } |
| 2709 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2710 | for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2711 | GC_IVAR skivar; |
| 2712 | skivar.ivar_bytepos = SkipIvars[i].ivar_bytepos + Size*ElIx; |
| 2713 | skivar.ivar_size = SkipIvars[i].ivar_size; |
| 2714 | SkipIvars.push_back(skivar); ++SkIndex; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2715 | } |
| 2716 | } |
| 2717 | continue; |
| 2718 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2719 | } |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2720 | // At this point, we are done with Record/Union and array there of. |
| 2721 | // For other arrays we are down to its element type. |
| 2722 | QualType::GCAttrTypes GCAttr = QualType::GCNone; |
| 2723 | do { |
| 2724 | if (FQT.isObjCGCStrong() || FQT.isObjCGCWeak()) { |
| 2725 | GCAttr = FQT.isObjCGCStrong() ? QualType::Strong : QualType::Weak; |
| 2726 | break; |
| 2727 | } |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2728 | else if (CGM.getContext().isObjCObjectPointerType(FQT)) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2729 | GCAttr = QualType::Strong; |
| 2730 | break; |
| 2731 | } |
| 2732 | else if (const PointerType *PT = FQT->getAsPointerType()) { |
| 2733 | FQT = PT->getPointeeType(); |
| 2734 | } |
| 2735 | else { |
| 2736 | break; |
| 2737 | } |
| 2738 | } while (true); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2739 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2740 | if ((ForStrongLayout && GCAttr == QualType::Strong) |
| 2741 | || (!ForStrongLayout && GCAttr == QualType::Weak)) { |
| 2742 | if (IsUnion) |
| 2743 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2744 | uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType()) |
| 2745 | / WordSizeInBits; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2746 | if (UnionIvarSize > MaxUnionIvarSize) |
| 2747 | { |
| 2748 | MaxUnionIvarSize = UnionIvarSize; |
| 2749 | MaxField = Field; |
| 2750 | } |
| 2751 | } |
| 2752 | else |
| 2753 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2754 | GC_IVAR gcivar; |
| 2755 | gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field); |
| 2756 | gcivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) / |
| 2757 | WordSizeInBits; |
| 2758 | IvarsInfo.push_back(gcivar); ++Index; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2759 | } |
| 2760 | } |
| 2761 | else if ((ForStrongLayout && |
| 2762 | (GCAttr == QualType::GCNone || GCAttr == QualType::Weak)) |
| 2763 | || (!ForStrongLayout && GCAttr != QualType::Weak)) { |
| 2764 | if (IsUnion) |
| 2765 | { |
| 2766 | uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType()); |
| 2767 | if (UnionIvarSize > MaxSkippedUnionIvarSize) |
| 2768 | { |
| 2769 | MaxSkippedUnionIvarSize = UnionIvarSize; |
| 2770 | MaxSkippedField = Field; |
| 2771 | } |
| 2772 | } |
| 2773 | else |
| 2774 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2775 | GC_IVAR skivar; |
| 2776 | skivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field); |
| 2777 | skivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) / |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2778 | ByteSizeInBits; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2779 | SkipIvars.push_back(skivar); ++SkIndex; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2780 | } |
| 2781 | } |
| 2782 | } |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 2783 | if (LastFieldBitfield) { |
| 2784 | // Last field was a bitfield. Must update skip info. |
| 2785 | GC_IVAR skivar; |
| 2786 | skivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, |
| 2787 | LastFieldBitfield); |
| 2788 | Expr *BitWidth = LastFieldBitfield->getBitWidth(); |
| 2789 | uint64_t BitFieldSize = |
| 2790 | BitWidth->getIntegerConstantExprValue(CGM.getContext()).getZExtValue(); |
| 2791 | skivar.ivar_size = (BitFieldSize / ByteSizeInBits) |
| 2792 | + ((BitFieldSize % ByteSizeInBits) != 0); |
| 2793 | SkipIvars.push_back(skivar); ++SkIndex; |
| 2794 | } |
| 2795 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2796 | if (MaxField) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2797 | GC_IVAR gcivar; |
| 2798 | gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, MaxField); |
| 2799 | gcivar.ivar_size = MaxUnionIvarSize; |
| 2800 | IvarsInfo.push_back(gcivar); ++Index; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2801 | } |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2802 | |
| 2803 | if (MaxSkippedField) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2804 | GC_IVAR skivar; |
| 2805 | skivar.ivar_bytepos = BytePos + |
| 2806 | GetFieldBaseOffset(OI, Layout, MaxSkippedField); |
| 2807 | skivar.ivar_size = MaxSkippedUnionIvarSize; |
| 2808 | SkipIvars.push_back(skivar); ++SkIndex; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2809 | } |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2810 | } |
| 2811 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2812 | static int |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2813 | IvarBytePosCompare(const void *a, const void *b) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2814 | { |
| 2815 | unsigned int sa = ((CGObjCCommonMac::GC_IVAR *)a)->ivar_bytepos; |
| 2816 | unsigned int sb = ((CGObjCCommonMac::GC_IVAR *)b)->ivar_bytepos; |
| 2817 | |
| 2818 | if (sa < sb) |
| 2819 | return -1; |
| 2820 | if (sa > sb) |
| 2821 | return 1; |
| 2822 | return 0; |
| 2823 | } |
| 2824 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2825 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 2826 | /// implementation for the __strong or __weak case. |
| 2827 | /// The layout map displays which words in ivar list must be skipped |
| 2828 | /// and which must be scanned by GC (see below). String is built of bytes. |
| 2829 | /// Each byte is divided up in two nibbles (4-bit each). Left nibble is count |
| 2830 | /// of words to skip and right nibble is count of words to scan. So, each |
| 2831 | /// nibble represents up to 15 workds to skip or scan. Skipping the rest is |
| 2832 | /// represented by a 0x00 byte which also ends the string. |
| 2833 | /// 1. when ForStrongLayout is true, following ivars are scanned: |
| 2834 | /// - id, Class |
| 2835 | /// - object * |
| 2836 | /// - __strong anything |
| 2837 | /// |
| 2838 | /// 2. When ForStrongLayout is false, following ivars are scanned: |
| 2839 | /// - __weak anything |
| 2840 | /// |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2841 | llvm::Constant *CGObjCCommonMac::BuildIvarLayout( |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2842 | const ObjCImplementationDecl *OMD, |
| 2843 | bool ForStrongLayout) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2844 | int Index = -1; |
| 2845 | int SkIndex = -1; |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2846 | bool hasUnion = false; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2847 | int SkipScan; |
| 2848 | unsigned int WordsToScan, WordsToSkip; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2849 | const llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 2850 | if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC) |
| 2851 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2852 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2853 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2854 | const ObjCInterfaceDecl *OI = OMD->getClassInterface(); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2855 | CGM.getContext().CollectObjCIvars(OI, RecFields); |
| 2856 | if (RecFields.empty()) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2857 | return llvm::Constant::getNullValue(PtrTy); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2858 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2859 | SkipIvars.clear(); |
| 2860 | IvarsInfo.clear(); |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 2861 | |
| 2862 | const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OI); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2863 | BuildAggrIvarLayout(OI, Layout, 0, RecFields, 0, ForStrongLayout, |
| 2864 | Index, SkIndex, hasUnion); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2865 | if (Index == -1) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2866 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2867 | |
| 2868 | // Sort on byte position in case we encounterred a union nested in |
| 2869 | // the ivar list. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2870 | if (hasUnion && !IvarsInfo.empty()) |
| 2871 | qsort(&IvarsInfo[0], Index+1, sizeof(GC_IVAR), IvarBytePosCompare); |
| 2872 | if (hasUnion && !SkipIvars.empty()) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2873 | qsort(&SkipIvars[0], Index+1, sizeof(GC_IVAR), IvarBytePosCompare); |
| 2874 | |
| 2875 | // Build the string of skip/scan nibbles |
| 2876 | SkipScan = -1; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2877 | SkipScanIvars.clear(); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2878 | unsigned int WordSize = |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2879 | CGM.getTypes().getTargetData().getTypePaddedSize(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2880 | if (IvarsInfo[0].ivar_bytepos == 0) { |
| 2881 | WordsToSkip = 0; |
| 2882 | WordsToScan = IvarsInfo[0].ivar_size; |
| 2883 | } |
| 2884 | else { |
| 2885 | WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize; |
| 2886 | WordsToScan = IvarsInfo[0].ivar_size; |
| 2887 | } |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2888 | for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2889 | { |
| 2890 | unsigned int TailPrevGCObjC = |
| 2891 | IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize; |
| 2892 | if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) |
| 2893 | { |
| 2894 | // consecutive 'scanned' object pointers. |
| 2895 | WordsToScan += IvarsInfo[i].ivar_size; |
| 2896 | } |
| 2897 | else |
| 2898 | { |
| 2899 | // Skip over 'gc'able object pointer which lay over each other. |
| 2900 | if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos) |
| 2901 | continue; |
| 2902 | // Must skip over 1 or more words. We save current skip/scan values |
| 2903 | // and start a new pair. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2904 | SKIP_SCAN SkScan; |
| 2905 | SkScan.skip = WordsToSkip; |
| 2906 | SkScan.scan = WordsToScan; |
| 2907 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
| 2908 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2909 | // Skip the hole. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2910 | SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize; |
| 2911 | SkScan.scan = 0; |
| 2912 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2913 | WordsToSkip = 0; |
| 2914 | WordsToScan = IvarsInfo[i].ivar_size; |
| 2915 | } |
| 2916 | } |
| 2917 | if (WordsToScan > 0) |
| 2918 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2919 | SKIP_SCAN SkScan; |
| 2920 | SkScan.skip = WordsToSkip; |
| 2921 | SkScan.scan = WordsToScan; |
| 2922 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2923 | } |
| 2924 | |
| 2925 | bool BytesSkipped = false; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2926 | if (!SkipIvars.empty()) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2927 | { |
| 2928 | int LastByteSkipped = |
| 2929 | SkipIvars[SkIndex].ivar_bytepos + SkipIvars[SkIndex].ivar_size; |
| 2930 | int LastByteScanned = |
| 2931 | IvarsInfo[Index].ivar_bytepos + IvarsInfo[Index].ivar_size * WordSize; |
| 2932 | BytesSkipped = (LastByteSkipped > LastByteScanned); |
| 2933 | // Compute number of bytes to skip at the tail end of the last ivar scanned. |
| 2934 | if (BytesSkipped) |
| 2935 | { |
| 2936 | unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2937 | SKIP_SCAN SkScan; |
| 2938 | SkScan.skip = TotalWords - (LastByteScanned/WordSize); |
| 2939 | SkScan.scan = 0; |
| 2940 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2941 | } |
| 2942 | } |
| 2943 | // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced |
| 2944 | // as 0xMN. |
| 2945 | for (int i = 0; i <= SkipScan; i++) |
| 2946 | { |
| 2947 | if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0 |
| 2948 | && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) { |
| 2949 | // 0xM0 followed by 0x0N detected. |
| 2950 | SkipScanIvars[i].scan = SkipScanIvars[i+1].scan; |
| 2951 | for (int j = i+1; j < SkipScan; j++) |
| 2952 | SkipScanIvars[j] = SkipScanIvars[j+1]; |
| 2953 | --SkipScan; |
| 2954 | } |
| 2955 | } |
| 2956 | |
| 2957 | // Generate the string. |
| 2958 | std::string BitMap; |
| 2959 | for (int i = 0; i <= SkipScan; i++) |
| 2960 | { |
| 2961 | unsigned char byte; |
| 2962 | unsigned int skip_small = SkipScanIvars[i].skip % 0xf; |
| 2963 | unsigned int scan_small = SkipScanIvars[i].scan % 0xf; |
| 2964 | unsigned int skip_big = SkipScanIvars[i].skip / 0xf; |
| 2965 | unsigned int scan_big = SkipScanIvars[i].scan / 0xf; |
| 2966 | |
| 2967 | if (skip_small > 0 || skip_big > 0) |
| 2968 | BytesSkipped = true; |
| 2969 | // first skip big. |
| 2970 | for (unsigned int ix = 0; ix < skip_big; ix++) |
| 2971 | BitMap += (unsigned char)(0xf0); |
| 2972 | |
| 2973 | // next (skip small, scan) |
| 2974 | if (skip_small) |
| 2975 | { |
| 2976 | byte = skip_small << 4; |
| 2977 | if (scan_big > 0) |
| 2978 | { |
| 2979 | byte |= 0xf; |
| 2980 | --scan_big; |
| 2981 | } |
| 2982 | else if (scan_small) |
| 2983 | { |
| 2984 | byte |= scan_small; |
| 2985 | scan_small = 0; |
| 2986 | } |
| 2987 | BitMap += byte; |
| 2988 | } |
| 2989 | // next scan big |
| 2990 | for (unsigned int ix = 0; ix < scan_big; ix++) |
| 2991 | BitMap += (unsigned char)(0x0f); |
| 2992 | // last scan small |
| 2993 | if (scan_small) |
| 2994 | { |
| 2995 | byte = scan_small; |
| 2996 | BitMap += byte; |
| 2997 | } |
| 2998 | } |
| 2999 | // null terminate string. |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3000 | unsigned char zero = 0; |
| 3001 | BitMap += zero; |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 3002 | |
| 3003 | if (CGM.getLangOptions().ObjCGCBitmapPrint) { |
| 3004 | printf("\n%s ivar layout for class '%s': ", |
| 3005 | ForStrongLayout ? "strong" : "weak", |
| 3006 | OMD->getClassInterface()->getNameAsCString()); |
| 3007 | const unsigned char *s = (unsigned char*)BitMap.c_str(); |
| 3008 | for (unsigned i = 0; i < BitMap.size(); i++) |
| 3009 | if (!(s[i] & 0xf0)) |
| 3010 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 3011 | else |
| 3012 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 3013 | printf("\n"); |
| 3014 | } |
| 3015 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3016 | // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as |
| 3017 | // final layout. |
| 3018 | if (ForStrongLayout && !BytesSkipped) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3019 | return llvm::Constant::getNullValue(PtrTy); |
| 3020 | llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
| 3021 | llvm::ConstantArray::get(BitMap.c_str()), |
| 3022 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3023 | 1, true); |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 3024 | return getConstantGEP(Entry, 0, 0); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3025 | } |
| 3026 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3027 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3028 | llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; |
| 3029 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3030 | // FIXME: Avoid std::string copying. |
| 3031 | if (!Entry) |
| 3032 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_", |
| 3033 | llvm::ConstantArray::get(Sel.getAsString()), |
| 3034 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3035 | 1, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3036 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3037 | return getConstantGEP(Entry, 0, 0); |
| 3038 | } |
| 3039 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3040 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3041 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3042 | return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); |
| 3043 | } |
| 3044 | |
| 3045 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3046 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3047 | return GetMethodVarName(&CGM.getContext().Idents.get(Name)); |
| 3048 | } |
| 3049 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 3050 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) { |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3051 | std::string TypeStr; |
| 3052 | CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); |
| 3053 | |
| 3054 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3055 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3056 | if (!Entry) |
| 3057 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
| 3058 | llvm::ConstantArray::get(TypeStr), |
| 3059 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3060 | 1, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3061 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3062 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3063 | } |
| 3064 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3065 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3066 | std::string TypeStr; |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3067 | CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D), |
| 3068 | TypeStr); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3069 | |
| 3070 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
| 3071 | |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3072 | if (!Entry) |
| 3073 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
| 3074 | llvm::ConstantArray::get(TypeStr), |
| 3075 | "__TEXT,__cstring,cstring_literals", |
| 3076 | 1, true); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3077 | |
| 3078 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3079 | } |
| 3080 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3081 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3082 | llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3083 | llvm::GlobalVariable *&Entry = PropertyNames[Ident]; |
| 3084 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3085 | if (!Entry) |
| 3086 | Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_", |
| 3087 | llvm::ConstantArray::get(Ident->getName()), |
| 3088 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3089 | 1, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3090 | |
| 3091 | return getConstantGEP(Entry, 0, 0); |
| 3092 | } |
| 3093 | |
| 3094 | // FIXME: Merge into a single cstring creation function. |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3095 | // FIXME: This Decl should be more precise. |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3096 | llvm::Constant * |
| 3097 | CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 3098 | const Decl *Container) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3099 | std::string TypeStr; |
| 3100 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3101 | return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); |
| 3102 | } |
| 3103 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3104 | void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D, |
| 3105 | const ObjCContainerDecl *CD, |
| 3106 | std::string &NameOut) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 3107 | NameOut = '\01'; |
| 3108 | NameOut += (D->isInstanceMethod() ? '-' : '+'); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3109 | NameOut += '['; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 3110 | assert (CD && "Missing container decl in GetNameForMethod"); |
| 3111 | NameOut += CD->getNameAsString(); |
Fariborz Jahanian | 1e9aef3 | 2009-04-16 18:34:20 +0000 | [diff] [blame] | 3112 | if (const ObjCCategoryImplDecl *CID = |
| 3113 | dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) { |
| 3114 | NameOut += '('; |
| 3115 | NameOut += CID->getNameAsString(); |
| 3116 | NameOut+= ')'; |
| 3117 | } |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3118 | NameOut += ' '; |
| 3119 | NameOut += D->getSelector().getAsString(); |
| 3120 | NameOut += ']'; |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3121 | } |
| 3122 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3123 | void CGObjCMac::FinishModule() { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3124 | EmitModuleInfo(); |
| 3125 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3126 | // Emit the dummy bodies for any protocols which were referenced but |
| 3127 | // never defined. |
| 3128 | for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator |
| 3129 | i = Protocols.begin(), e = Protocols.end(); i != e; ++i) { |
| 3130 | if (i->second->hasInitializer()) |
| 3131 | continue; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3132 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3133 | std::vector<llvm::Constant*> Values(5); |
| 3134 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
| 3135 | Values[1] = GetClassName(i->first); |
| 3136 | Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 3137 | Values[3] = Values[4] = |
| 3138 | llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
| 3139 | i->second->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 3140 | i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 3141 | Values)); |
| 3142 | } |
| 3143 | |
| 3144 | std::vector<llvm::Constant*> Used; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3145 | for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3146 | e = UsedGlobals.end(); i != e; ++i) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3147 | Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3148 | } |
| 3149 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3150 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3151 | llvm::GlobalValue *GV = |
| 3152 | new llvm::GlobalVariable(AT, false, |
| 3153 | llvm::GlobalValue::AppendingLinkage, |
| 3154 | llvm::ConstantArray::get(AT, Used), |
| 3155 | "llvm.used", |
| 3156 | &CGM.getModule()); |
| 3157 | |
| 3158 | GV->setSection("llvm.metadata"); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3159 | |
| 3160 | // Add assembler directives to add lazy undefined symbol references |
| 3161 | // for classes which are referenced but not defined. This is |
| 3162 | // important for correct linker interaction. |
| 3163 | |
| 3164 | // FIXME: Uh, this isn't particularly portable. |
| 3165 | std::stringstream s; |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 3166 | |
| 3167 | if (!CGM.getModule().getModuleInlineAsm().empty()) |
| 3168 | s << "\n"; |
| 3169 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3170 | for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(), |
| 3171 | e = LazySymbols.end(); i != e; ++i) { |
| 3172 | s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n"; |
| 3173 | } |
| 3174 | for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(), |
| 3175 | e = DefinedSymbols.end(); i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3176 | s << "\t.objc_class_name_" << (*i)->getName() << "=0\n" |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3177 | << "\t.globl .objc_class_name_" << (*i)->getName() << "\n"; |
| 3178 | } |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 3179 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3180 | CGM.getModule().appendModuleInlineAsm(s.str()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3181 | } |
| 3182 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3183 | CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3184 | : CGObjCCommonMac(cgm), |
| 3185 | ObjCTypes(cgm) |
| 3186 | { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3187 | ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3188 | ObjCABI = 2; |
| 3189 | } |
| 3190 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3191 | /* *** */ |
| 3192 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3193 | ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm) |
| 3194 | : CGM(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3195 | { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3196 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3197 | ASTContext &Ctx = CGM.getContext(); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3198 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3199 | ShortTy = Types.ConvertType(Ctx.ShortTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3200 | IntTy = Types.ConvertType(Ctx.IntTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3201 | LongTy = Types.ConvertType(Ctx.LongTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3202 | LongLongTy = Types.ConvertType(Ctx.LongLongTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3203 | Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 3204 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3205 | ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType()); |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 3206 | PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3207 | SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3208 | |
| 3209 | // FIXME: It would be nice to unify this with the opaque type, so |
| 3210 | // that the IR comes out a bit cleaner. |
| 3211 | const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); |
| 3212 | ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3213 | |
| 3214 | // I'm not sure I like this. The implicit coordination is a bit |
| 3215 | // gross. We should solve this in a reasonable fashion because this |
| 3216 | // is a pretty common task (match some runtime data structure with |
| 3217 | // an LLVM data structure). |
| 3218 | |
| 3219 | // FIXME: This is leaked. |
| 3220 | // FIXME: Merge with rewriter code? |
| 3221 | |
| 3222 | // struct _objc_super { |
| 3223 | // id self; |
| 3224 | // Class cls; |
| 3225 | // } |
| 3226 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 3227 | SourceLocation(), |
| 3228 | &Ctx.Idents.get("_objc_super")); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3229 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3230 | Ctx.getObjCIdType(), 0, false)); |
| 3231 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3232 | Ctx.getObjCClassType(), 0, false)); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3233 | RD->completeDefinition(Ctx); |
| 3234 | |
| 3235 | SuperCTy = Ctx.getTagDeclType(RD); |
| 3236 | SuperPtrCTy = Ctx.getPointerType(SuperCTy); |
| 3237 | |
| 3238 | SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy)); |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3239 | SuperPtrTy = llvm::PointerType::getUnqual(SuperTy); |
| 3240 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3241 | // struct _prop_t { |
| 3242 | // char *name; |
| 3243 | // char *attributes; |
| 3244 | // } |
| 3245 | PropertyTy = llvm::StructType::get(Int8PtrTy, |
| 3246 | Int8PtrTy, |
| 3247 | NULL); |
| 3248 | CGM.getModule().addTypeName("struct._prop_t", |
| 3249 | PropertyTy); |
| 3250 | |
| 3251 | // struct _prop_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3252 | // uint32_t entsize; // sizeof(struct _prop_t) |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3253 | // uint32_t count_of_properties; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3254 | // struct _prop_t prop_list[count_of_properties]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3255 | // } |
| 3256 | PropertyListTy = llvm::StructType::get(IntTy, |
| 3257 | IntTy, |
| 3258 | llvm::ArrayType::get(PropertyTy, 0), |
| 3259 | NULL); |
| 3260 | CGM.getModule().addTypeName("struct._prop_list_t", |
| 3261 | PropertyListTy); |
| 3262 | // struct _prop_list_t * |
| 3263 | PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy); |
| 3264 | |
| 3265 | // struct _objc_method { |
| 3266 | // SEL _cmd; |
| 3267 | // char *method_type; |
| 3268 | // char *_imp; |
| 3269 | // } |
| 3270 | MethodTy = llvm::StructType::get(SelectorPtrTy, |
| 3271 | Int8PtrTy, |
| 3272 | Int8PtrTy, |
| 3273 | NULL); |
| 3274 | CGM.getModule().addTypeName("struct._objc_method", MethodTy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3275 | |
| 3276 | // struct _objc_cache * |
| 3277 | CacheTy = llvm::OpaqueType::get(); |
| 3278 | CGM.getModule().addTypeName("struct._objc_cache", CacheTy); |
| 3279 | CachePtrTy = llvm::PointerType::getUnqual(CacheTy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3280 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3281 | // Property manipulation functions. |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3282 | |
| 3283 | QualType IdType = Ctx.getObjCIdType(); |
| 3284 | QualType SelType = Ctx.getObjCSelType(); |
| 3285 | llvm::SmallVector<QualType,16> Params; |
| 3286 | const llvm::FunctionType *FTy; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3287 | |
| 3288 | // id objc_getProperty (id, SEL, ptrdiff_t, bool) |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3289 | Params.push_back(IdType); |
| 3290 | Params.push_back(SelType); |
| 3291 | Params.push_back(Ctx.LongTy); |
| 3292 | Params.push_back(Ctx.BoolTy); |
| 3293 | FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), |
| 3294 | false); |
| 3295 | GetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_getProperty"); |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3296 | |
| 3297 | // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool) |
| 3298 | Params.clear(); |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3299 | Params.push_back(IdType); |
| 3300 | Params.push_back(SelType); |
| 3301 | Params.push_back(Ctx.LongTy); |
| 3302 | Params.push_back(IdType); |
| 3303 | Params.push_back(Ctx.BoolTy); |
| 3304 | Params.push_back(Ctx.BoolTy); |
| 3305 | FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
| 3306 | SetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_setProperty"); |
| 3307 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3308 | // Enumeration mutation. |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3309 | |
| 3310 | // void objc_enumerationMutation (id) |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3311 | Params.clear(); |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3312 | Params.push_back(IdType); |
| 3313 | FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
| 3314 | EnumerationMutationFn = CGM.CreateRuntimeFunction(FTy, |
| 3315 | "objc_enumerationMutation"); |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3316 | |
| 3317 | // gc's API |
| 3318 | // id objc_read_weak (id *) |
| 3319 | Params.clear(); |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3320 | Params.push_back(Ctx.getPointerType(IdType)); |
| 3321 | FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false); |
| 3322 | GcReadWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_read_weak"); |
| 3323 | |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 3324 | // id objc_assign_global (id, id *) |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3325 | Params.clear(); |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3326 | Params.push_back(IdType); |
| 3327 | Params.push_back(Ctx.getPointerType(IdType)); |
| 3328 | |
| 3329 | FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false); |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3330 | GcAssignGlobalFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_global"); |
| 3331 | GcAssignIvarFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar"); |
| 3332 | GcAssignStrongCastFn = |
| 3333 | CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast"); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 3334 | |
| 3335 | // void objc_exception_throw(id) |
| 3336 | Params.clear(); |
| 3337 | Params.push_back(IdType); |
| 3338 | |
| 3339 | FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 3340 | ExceptionThrowFn = |
| 3341 | CGM.CreateRuntimeFunction(FTy, "objc_exception_throw"); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3342 | |
| 3343 | // synchronized APIs |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3344 | // void objc_sync_exit (id) |
| 3345 | Params.clear(); |
| 3346 | Params.push_back(IdType); |
| 3347 | |
| 3348 | FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3349 | SyncExitFn = CGM.CreateRuntimeFunction(FTy, "objc_sync_exit"); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3350 | } |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3351 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3352 | ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) |
| 3353 | : ObjCCommonTypesHelper(cgm) |
| 3354 | { |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3355 | // struct _objc_method_description { |
| 3356 | // SEL name; |
| 3357 | // char *types; |
| 3358 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3359 | MethodDescriptionTy = |
| 3360 | llvm::StructType::get(SelectorPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3361 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3362 | NULL); |
| 3363 | CGM.getModule().addTypeName("struct._objc_method_description", |
| 3364 | MethodDescriptionTy); |
| 3365 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3366 | // struct _objc_method_description_list { |
| 3367 | // int count; |
| 3368 | // struct _objc_method_description[1]; |
| 3369 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3370 | MethodDescriptionListTy = |
| 3371 | llvm::StructType::get(IntTy, |
| 3372 | llvm::ArrayType::get(MethodDescriptionTy, 0), |
| 3373 | NULL); |
| 3374 | CGM.getModule().addTypeName("struct._objc_method_description_list", |
| 3375 | MethodDescriptionListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3376 | |
| 3377 | // struct _objc_method_description_list * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3378 | MethodDescriptionListPtrTy = |
| 3379 | llvm::PointerType::getUnqual(MethodDescriptionListTy); |
| 3380 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3381 | // Protocol description structures |
| 3382 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3383 | // struct _objc_protocol_extension { |
| 3384 | // uint32_t size; // sizeof(struct _objc_protocol_extension) |
| 3385 | // struct _objc_method_description_list *optional_instance_methods; |
| 3386 | // struct _objc_method_description_list *optional_class_methods; |
| 3387 | // struct _objc_property_list *instance_properties; |
| 3388 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3389 | ProtocolExtensionTy = |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3390 | llvm::StructType::get(IntTy, |
| 3391 | MethodDescriptionListPtrTy, |
| 3392 | MethodDescriptionListPtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3393 | PropertyListPtrTy, |
| 3394 | NULL); |
| 3395 | CGM.getModule().addTypeName("struct._objc_protocol_extension", |
| 3396 | ProtocolExtensionTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3397 | |
| 3398 | // struct _objc_protocol_extension * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3399 | ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy); |
| 3400 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3401 | // Handle recursive construction of Protocol and ProtocolList types |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3402 | |
| 3403 | llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(); |
| 3404 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 3405 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3406 | const llvm::Type *T = |
| 3407 | llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 3408 | LongTy, |
| 3409 | llvm::ArrayType::get(ProtocolTyHolder, 0), |
| 3410 | NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3411 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T); |
| 3412 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3413 | // struct _objc_protocol { |
| 3414 | // struct _objc_protocol_extension *isa; |
| 3415 | // char *protocol_name; |
| 3416 | // struct _objc_protocol **_objc_protocol_list; |
| 3417 | // struct _objc_method_description_list *instance_methods; |
| 3418 | // struct _objc_method_description_list *class_methods; |
| 3419 | // } |
| 3420 | T = llvm::StructType::get(ProtocolExtensionPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3421 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3422 | llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 3423 | MethodDescriptionListPtrTy, |
| 3424 | MethodDescriptionListPtrTy, |
| 3425 | NULL); |
| 3426 | cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T); |
| 3427 | |
| 3428 | ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get()); |
| 3429 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 3430 | ProtocolListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3431 | // struct _objc_protocol_list * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3432 | ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy); |
| 3433 | |
| 3434 | ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get()); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3435 | CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3436 | ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3437 | |
| 3438 | // Class description structures |
| 3439 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3440 | // struct _objc_ivar { |
| 3441 | // char *ivar_name; |
| 3442 | // char *ivar_type; |
| 3443 | // int ivar_offset; |
| 3444 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3445 | IvarTy = llvm::StructType::get(Int8PtrTy, |
| 3446 | Int8PtrTy, |
| 3447 | IntTy, |
| 3448 | NULL); |
| 3449 | CGM.getModule().addTypeName("struct._objc_ivar", IvarTy); |
| 3450 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3451 | // struct _objc_ivar_list * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3452 | IvarListTy = llvm::OpaqueType::get(); |
| 3453 | CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy); |
| 3454 | IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy); |
| 3455 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3456 | // struct _objc_method_list * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3457 | MethodListTy = llvm::OpaqueType::get(); |
| 3458 | CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy); |
| 3459 | MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy); |
| 3460 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3461 | // struct _objc_class_extension * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3462 | ClassExtensionTy = |
| 3463 | llvm::StructType::get(IntTy, |
| 3464 | Int8PtrTy, |
| 3465 | PropertyListPtrTy, |
| 3466 | NULL); |
| 3467 | CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy); |
| 3468 | ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy); |
| 3469 | |
| 3470 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 3471 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3472 | // struct _objc_class { |
| 3473 | // Class isa; |
| 3474 | // Class super_class; |
| 3475 | // char *name; |
| 3476 | // long version; |
| 3477 | // long info; |
| 3478 | // long instance_size; |
| 3479 | // struct _objc_ivar_list *ivars; |
| 3480 | // struct _objc_method_list *methods; |
| 3481 | // struct _objc_cache *cache; |
| 3482 | // struct _objc_protocol_list *protocols; |
| 3483 | // char *ivar_layout; |
| 3484 | // struct _objc_class_ext *ext; |
| 3485 | // }; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3486 | T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 3487 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 3488 | Int8PtrTy, |
| 3489 | LongTy, |
| 3490 | LongTy, |
| 3491 | LongTy, |
| 3492 | IvarListPtrTy, |
| 3493 | MethodListPtrTy, |
| 3494 | CachePtrTy, |
| 3495 | ProtocolListPtrTy, |
| 3496 | Int8PtrTy, |
| 3497 | ClassExtensionPtrTy, |
| 3498 | NULL); |
| 3499 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T); |
| 3500 | |
| 3501 | ClassTy = cast<llvm::StructType>(ClassTyHolder.get()); |
| 3502 | CGM.getModule().addTypeName("struct._objc_class", ClassTy); |
| 3503 | ClassPtrTy = llvm::PointerType::getUnqual(ClassTy); |
| 3504 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3505 | // struct _objc_category { |
| 3506 | // char *category_name; |
| 3507 | // char *class_name; |
| 3508 | // struct _objc_method_list *instance_method; |
| 3509 | // struct _objc_method_list *class_method; |
| 3510 | // uint32_t size; // sizeof(struct _objc_category) |
| 3511 | // struct _objc_property_list *instance_properties;// category's @property |
| 3512 | // } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3513 | CategoryTy = llvm::StructType::get(Int8PtrTy, |
| 3514 | Int8PtrTy, |
| 3515 | MethodListPtrTy, |
| 3516 | MethodListPtrTy, |
| 3517 | ProtocolListPtrTy, |
| 3518 | IntTy, |
| 3519 | PropertyListPtrTy, |
| 3520 | NULL); |
| 3521 | CGM.getModule().addTypeName("struct._objc_category", CategoryTy); |
| 3522 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3523 | // Global metadata structures |
| 3524 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3525 | // struct _objc_symtab { |
| 3526 | // long sel_ref_cnt; |
| 3527 | // SEL *refs; |
| 3528 | // short cls_def_cnt; |
| 3529 | // short cat_def_cnt; |
| 3530 | // char *defs[cls_def_cnt + cat_def_cnt]; |
| 3531 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3532 | SymtabTy = llvm::StructType::get(LongTy, |
| 3533 | SelectorPtrTy, |
| 3534 | ShortTy, |
| 3535 | ShortTy, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3536 | llvm::ArrayType::get(Int8PtrTy, 0), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3537 | NULL); |
| 3538 | CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy); |
| 3539 | SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy); |
| 3540 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3541 | // struct _objc_module { |
| 3542 | // long version; |
| 3543 | // long size; // sizeof(struct _objc_module) |
| 3544 | // char *name; |
| 3545 | // struct _objc_symtab* symtab; |
| 3546 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3547 | ModuleTy = |
| 3548 | llvm::StructType::get(LongTy, |
| 3549 | LongTy, |
| 3550 | Int8PtrTy, |
| 3551 | SymtabPtrTy, |
| 3552 | NULL); |
| 3553 | CGM.getModule().addTypeName("struct._objc_module", ModuleTy); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3554 | |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 3555 | // Message send functions. |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3556 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3557 | // id objc_msgSend (id, SEL, ...) |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3558 | std::vector<const llvm::Type*> Params; |
| 3559 | Params.push_back(ObjectPtrTy); |
| 3560 | Params.push_back(SelectorPtrTy); |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3561 | MessageSendFn = |
| 3562 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3563 | Params, |
| 3564 | true), |
| 3565 | "objc_msgSend"); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3566 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3567 | // id objc_msgSend_stret (id, SEL, ...) |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3568 | Params.clear(); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3569 | Params.push_back(ObjectPtrTy); |
| 3570 | Params.push_back(SelectorPtrTy); |
| 3571 | MessageSendStretFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3572 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3573 | Params, |
| 3574 | true), |
| 3575 | "objc_msgSend_stret"); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 3576 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3577 | // |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 3578 | Params.clear(); |
| 3579 | Params.push_back(ObjectPtrTy); |
| 3580 | Params.push_back(SelectorPtrTy); |
| 3581 | // FIXME: This should be long double on x86_64? |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3582 | // [double | long double] objc_msgSend_fpret(id self, SEL op, ...) |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 3583 | MessageSendFpretFn = |
| 3584 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy, |
| 3585 | Params, |
| 3586 | true), |
| 3587 | "objc_msgSend_fpret"); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3588 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3589 | // id objc_msgSendSuper(struct objc_super *super, SEL op, ...) |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3590 | Params.clear(); |
| 3591 | Params.push_back(SuperPtrTy); |
| 3592 | Params.push_back(SelectorPtrTy); |
| 3593 | MessageSendSuperFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3594 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3595 | Params, |
| 3596 | true), |
| 3597 | "objc_msgSendSuper"); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3598 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3599 | // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super, |
| 3600 | // SEL op, ...) |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3601 | Params.clear(); |
| 3602 | Params.push_back(Int8PtrTy); |
| 3603 | Params.push_back(SuperPtrTy); |
| 3604 | Params.push_back(SelectorPtrTy); |
| 3605 | MessageSendSuperStretFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3606 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3607 | Params, |
| 3608 | true), |
| 3609 | "objc_msgSendSuper_stret"); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 3610 | |
| 3611 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 3612 | MessageSendSuperFpretFn = MessageSendSuperFn; |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 3613 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3614 | // FIXME: This is the size of the setjmp buffer and should be |
| 3615 | // target specific. 18 is what's used on 32-bit X86. |
| 3616 | uint64_t SetJmpBufferSize = 18; |
| 3617 | |
| 3618 | // Exceptions |
| 3619 | const llvm::Type *StackPtrTy = |
Daniel Dunbar | 1000491 | 2008-09-27 06:32:25 +0000 | [diff] [blame] | 3620 | llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3621 | |
| 3622 | ExceptionDataTy = |
| 3623 | llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty, |
| 3624 | SetJmpBufferSize), |
| 3625 | StackPtrTy, NULL); |
| 3626 | CGM.getModule().addTypeName("struct._objc_exception_data", |
| 3627 | ExceptionDataTy); |
| 3628 | |
| 3629 | Params.clear(); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3630 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
| 3631 | ExceptionTryEnterFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3632 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3633 | Params, |
| 3634 | false), |
| 3635 | "objc_exception_try_enter"); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3636 | ExceptionTryExitFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3637 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3638 | Params, |
| 3639 | false), |
| 3640 | "objc_exception_try_exit"); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3641 | ExceptionExtractFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3642 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3643 | Params, |
| 3644 | false), |
| 3645 | "objc_exception_extract"); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3646 | |
| 3647 | Params.clear(); |
| 3648 | Params.push_back(ClassPtrTy); |
| 3649 | Params.push_back(ObjectPtrTy); |
| 3650 | ExceptionMatchFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3651 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 3652 | Params, |
| 3653 | false), |
| 3654 | "objc_exception_match"); |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 3655 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3656 | Params.clear(); |
| 3657 | Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty)); |
| 3658 | SetJmpFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3659 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 3660 | Params, |
| 3661 | false), |
| 3662 | "_setjmp"); |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 3663 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3664 | } |
| 3665 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3666 | ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3667 | : ObjCCommonTypesHelper(cgm) |
| 3668 | { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3669 | // struct _method_list_t { |
| 3670 | // uint32_t entsize; // sizeof(struct _objc_method) |
| 3671 | // uint32_t method_count; |
| 3672 | // struct _objc_method method_list[method_count]; |
| 3673 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3674 | MethodListnfABITy = llvm::StructType::get(IntTy, |
| 3675 | IntTy, |
| 3676 | llvm::ArrayType::get(MethodTy, 0), |
| 3677 | NULL); |
| 3678 | CGM.getModule().addTypeName("struct.__method_list_t", |
| 3679 | MethodListnfABITy); |
| 3680 | // struct method_list_t * |
| 3681 | MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3682 | |
| 3683 | // struct _protocol_t { |
| 3684 | // id isa; // NULL |
| 3685 | // const char * const protocol_name; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3686 | // const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3687 | // const struct method_list_t * const instance_methods; |
| 3688 | // const struct method_list_t * const class_methods; |
| 3689 | // const struct method_list_t *optionalInstanceMethods; |
| 3690 | // const struct method_list_t *optionalClassMethods; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3691 | // const struct _prop_list_t * properties; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3692 | // const uint32_t size; // sizeof(struct _protocol_t) |
| 3693 | // const uint32_t flags; // = 0 |
| 3694 | // } |
| 3695 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3696 | // Holder for struct _protocol_list_t * |
| 3697 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 3698 | |
| 3699 | ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy, |
| 3700 | Int8PtrTy, |
| 3701 | llvm::PointerType::getUnqual( |
| 3702 | ProtocolListTyHolder), |
| 3703 | MethodListnfABIPtrTy, |
| 3704 | MethodListnfABIPtrTy, |
| 3705 | MethodListnfABIPtrTy, |
| 3706 | MethodListnfABIPtrTy, |
| 3707 | PropertyListPtrTy, |
| 3708 | IntTy, |
| 3709 | IntTy, |
| 3710 | NULL); |
| 3711 | CGM.getModule().addTypeName("struct._protocol_t", |
| 3712 | ProtocolnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3713 | |
| 3714 | // struct _protocol_t* |
| 3715 | ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3716 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 3717 | // struct _protocol_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3718 | // long protocol_count; // Note, this is 32/64 bit |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3719 | // struct _protocol_t *[protocol_count]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3720 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3721 | ProtocolListnfABITy = llvm::StructType::get(LongTy, |
| 3722 | llvm::ArrayType::get( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3723 | ProtocolnfABIPtrTy, 0), |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3724 | NULL); |
| 3725 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 3726 | ProtocolListnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3727 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo( |
| 3728 | ProtocolListnfABITy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3729 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3730 | // struct _objc_protocol_list* |
| 3731 | ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3732 | |
| 3733 | // struct _ivar_t { |
| 3734 | // unsigned long int *offset; // pointer to ivar offset location |
| 3735 | // char *name; |
| 3736 | // char *type; |
| 3737 | // uint32_t alignment; |
| 3738 | // uint32_t size; |
| 3739 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3740 | IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy), |
| 3741 | Int8PtrTy, |
| 3742 | Int8PtrTy, |
| 3743 | IntTy, |
| 3744 | IntTy, |
| 3745 | NULL); |
| 3746 | CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy); |
| 3747 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3748 | // struct _ivar_list_t { |
| 3749 | // uint32 entsize; // sizeof(struct _ivar_t) |
| 3750 | // uint32 count; |
| 3751 | // struct _iver_t list[count]; |
| 3752 | // } |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3753 | IvarListnfABITy = llvm::StructType::get(IntTy, |
| 3754 | IntTy, |
| 3755 | llvm::ArrayType::get( |
| 3756 | IvarnfABITy, 0), |
| 3757 | NULL); |
| 3758 | CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy); |
| 3759 | |
| 3760 | IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3761 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3762 | // struct _class_ro_t { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3763 | // uint32_t const flags; |
| 3764 | // uint32_t const instanceStart; |
| 3765 | // uint32_t const instanceSize; |
| 3766 | // uint32_t const reserved; // only when building for 64bit targets |
| 3767 | // const uint8_t * const ivarLayout; |
| 3768 | // const char *const name; |
| 3769 | // const struct _method_list_t * const baseMethods; |
| 3770 | // const struct _objc_protocol_list *const baseProtocols; |
| 3771 | // const struct _ivar_list_t *const ivars; |
| 3772 | // const uint8_t * const weakIvarLayout; |
| 3773 | // const struct _prop_list_t * const properties; |
| 3774 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3775 | |
| 3776 | // FIXME. Add 'reserved' field in 64bit abi mode! |
| 3777 | ClassRonfABITy = llvm::StructType::get(IntTy, |
| 3778 | IntTy, |
| 3779 | IntTy, |
| 3780 | Int8PtrTy, |
| 3781 | Int8PtrTy, |
| 3782 | MethodListnfABIPtrTy, |
| 3783 | ProtocolListnfABIPtrTy, |
| 3784 | IvarListnfABIPtrTy, |
| 3785 | Int8PtrTy, |
| 3786 | PropertyListPtrTy, |
| 3787 | NULL); |
| 3788 | CGM.getModule().addTypeName("struct._class_ro_t", |
| 3789 | ClassRonfABITy); |
| 3790 | |
| 3791 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 3792 | std::vector<const llvm::Type*> Params; |
| 3793 | Params.push_back(ObjectPtrTy); |
| 3794 | Params.push_back(SelectorPtrTy); |
| 3795 | ImpnfABITy = llvm::PointerType::getUnqual( |
| 3796 | llvm::FunctionType::get(ObjectPtrTy, Params, false)); |
| 3797 | |
| 3798 | // struct _class_t { |
| 3799 | // struct _class_t *isa; |
| 3800 | // struct _class_t * const superclass; |
| 3801 | // void *cache; |
| 3802 | // IMP *vtable; |
| 3803 | // struct class_ro_t *ro; |
| 3804 | // } |
| 3805 | |
| 3806 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 3807 | ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 3808 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 3809 | CachePtrTy, |
| 3810 | llvm::PointerType::getUnqual(ImpnfABITy), |
| 3811 | llvm::PointerType::getUnqual( |
| 3812 | ClassRonfABITy), |
| 3813 | NULL); |
| 3814 | CGM.getModule().addTypeName("struct._class_t", ClassnfABITy); |
| 3815 | |
| 3816 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo( |
| 3817 | ClassnfABITy); |
| 3818 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3819 | // LLVM for struct _class_t * |
| 3820 | ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy); |
| 3821 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3822 | // struct _category_t { |
| 3823 | // const char * const name; |
| 3824 | // struct _class_t *const cls; |
| 3825 | // const struct _method_list_t * const instance_methods; |
| 3826 | // const struct _method_list_t * const class_methods; |
| 3827 | // const struct _protocol_list_t * const protocols; |
| 3828 | // const struct _prop_list_t * const properties; |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3829 | // } |
| 3830 | CategorynfABITy = llvm::StructType::get(Int8PtrTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3831 | ClassnfABIPtrTy, |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3832 | MethodListnfABIPtrTy, |
| 3833 | MethodListnfABIPtrTy, |
| 3834 | ProtocolListnfABIPtrTy, |
| 3835 | PropertyListPtrTy, |
| 3836 | NULL); |
| 3837 | CGM.getModule().addTypeName("struct._category_t", CategorynfABITy); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3838 | |
| 3839 | // New types for nonfragile abi messaging. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3840 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3841 | ASTContext &Ctx = CGM.getContext(); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3842 | |
| 3843 | // MessageRefTy - LLVM for: |
| 3844 | // struct _message_ref_t { |
| 3845 | // IMP messenger; |
| 3846 | // SEL name; |
| 3847 | // }; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3848 | |
| 3849 | // First the clang type for struct _message_ref_t |
| 3850 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 3851 | SourceLocation(), |
| 3852 | &Ctx.Idents.get("_message_ref_t")); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3853 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3854 | Ctx.VoidPtrTy, 0, false)); |
| 3855 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3856 | Ctx.getObjCSelType(), 0, false)); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3857 | RD->completeDefinition(Ctx); |
| 3858 | |
| 3859 | MessageRefCTy = Ctx.getTagDeclType(RD); |
| 3860 | MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy); |
| 3861 | MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy)); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3862 | |
| 3863 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
| 3864 | MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy); |
| 3865 | |
| 3866 | // SuperMessageRefTy - LLVM for: |
| 3867 | // struct _super_message_ref_t { |
| 3868 | // SUPER_IMP messenger; |
| 3869 | // SEL name; |
| 3870 | // }; |
| 3871 | SuperMessageRefTy = llvm::StructType::get(ImpnfABITy, |
| 3872 | SelectorPtrTy, |
| 3873 | NULL); |
| 3874 | CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy); |
| 3875 | |
| 3876 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
| 3877 | SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy); |
| 3878 | |
| 3879 | // id objc_msgSend_fixup (id, struct message_ref_t*, ...) |
| 3880 | Params.clear(); |
| 3881 | Params.push_back(ObjectPtrTy); |
| 3882 | Params.push_back(MessageRefPtrTy); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 3883 | MessengerTy = llvm::FunctionType::get(ObjectPtrTy, |
| 3884 | Params, |
| 3885 | true); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3886 | MessageSendFixupFn = |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 3887 | CGM.CreateRuntimeFunction(MessengerTy, |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3888 | "objc_msgSend_fixup"); |
| 3889 | |
| 3890 | // id objc_msgSend_fpret_fixup (id, struct message_ref_t*, ...) |
| 3891 | MessageSendFpretFixupFn = |
| 3892 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3893 | Params, |
| 3894 | true), |
| 3895 | "objc_msgSend_fpret_fixup"); |
| 3896 | |
| 3897 | // id objc_msgSend_stret_fixup (id, struct message_ref_t*, ...) |
| 3898 | MessageSendStretFixupFn = |
| 3899 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3900 | Params, |
| 3901 | true), |
| 3902 | "objc_msgSend_stret_fixup"); |
| 3903 | |
| 3904 | // id objc_msgSendId_fixup (id, struct message_ref_t*, ...) |
| 3905 | MessageSendIdFixupFn = |
| 3906 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3907 | Params, |
| 3908 | true), |
| 3909 | "objc_msgSendId_fixup"); |
| 3910 | |
| 3911 | |
| 3912 | // id objc_msgSendId_stret_fixup (id, struct message_ref_t*, ...) |
| 3913 | MessageSendIdStretFixupFn = |
| 3914 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3915 | Params, |
| 3916 | true), |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3917 | "objc_msgSendId_stret_fixup"); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3918 | |
| 3919 | // id objc_msgSendSuper2_fixup (struct objc_super *, |
| 3920 | // struct _super_message_ref_t*, ...) |
| 3921 | Params.clear(); |
| 3922 | Params.push_back(SuperPtrTy); |
| 3923 | Params.push_back(SuperMessageRefPtrTy); |
| 3924 | MessageSendSuper2FixupFn = |
| 3925 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3926 | Params, |
| 3927 | true), |
| 3928 | "objc_msgSendSuper2_fixup"); |
| 3929 | |
| 3930 | |
| 3931 | // id objc_msgSendSuper2_stret_fixup (struct objc_super *, |
| 3932 | // struct _super_message_ref_t*, ...) |
| 3933 | MessageSendSuper2StretFixupFn = |
| 3934 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3935 | Params, |
| 3936 | true), |
| 3937 | "objc_msgSendSuper2_stret_fixup"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 3938 | |
| 3939 | Params.clear(); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 3940 | |
| 3941 | // struct objc_typeinfo { |
| 3942 | // const void** vtable; // objc_ehtype_vtable + 2 |
| 3943 | // const char* name; // c++ typeinfo string |
| 3944 | // Class cls; |
| 3945 | // }; |
| 3946 | EHTypeTy = llvm::StructType::get(llvm::PointerType::getUnqual(Int8PtrTy), |
| 3947 | Int8PtrTy, |
| 3948 | ClassnfABIPtrTy, |
| 3949 | NULL); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 3950 | CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 3951 | EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3952 | } |
| 3953 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3954 | llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() { |
| 3955 | FinishNonFragileABIModule(); |
| 3956 | |
| 3957 | return NULL; |
| 3958 | } |
| 3959 | |
| 3960 | void CGObjCNonFragileABIMac::FinishNonFragileABIModule() { |
| 3961 | // nonfragile abi has no module definition. |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 3962 | |
| 3963 | // Build list of all implemented classe addresses in array |
| 3964 | // L_OBJC_LABEL_CLASS_$. |
| 3965 | // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$ |
| 3966 | // list of 'nonlazy' implementations (defined as those with a +load{} |
| 3967 | // method!!). |
| 3968 | unsigned NumClasses = DefinedClasses.size(); |
| 3969 | if (NumClasses) { |
| 3970 | std::vector<llvm::Constant*> Symbols(NumClasses); |
| 3971 | for (unsigned i=0; i<NumClasses; i++) |
| 3972 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
| 3973 | ObjCTypes.Int8PtrTy); |
| 3974 | llvm::Constant* Init = |
| 3975 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 3976 | NumClasses), |
| 3977 | Symbols); |
| 3978 | |
| 3979 | llvm::GlobalVariable *GV = |
| 3980 | new llvm::GlobalVariable(Init->getType(), false, |
| 3981 | llvm::GlobalValue::InternalLinkage, |
| 3982 | Init, |
| 3983 | "\01L_OBJC_LABEL_CLASS_$", |
| 3984 | &CGM.getModule()); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 3985 | GV->setAlignment(8); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 3986 | GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip"); |
| 3987 | UsedGlobals.push_back(GV); |
| 3988 | } |
| 3989 | |
| 3990 | // Build list of all implemented category addresses in array |
| 3991 | // L_OBJC_LABEL_CATEGORY_$. |
| 3992 | // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$ |
| 3993 | // list of 'nonlazy' category implementations (defined as those with a +load{} |
| 3994 | // method!!). |
| 3995 | unsigned NumCategory = DefinedCategories.size(); |
| 3996 | if (NumCategory) { |
| 3997 | std::vector<llvm::Constant*> Symbols(NumCategory); |
| 3998 | for (unsigned i=0; i<NumCategory; i++) |
| 3999 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
| 4000 | ObjCTypes.Int8PtrTy); |
| 4001 | llvm::Constant* Init = |
| 4002 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 4003 | NumCategory), |
| 4004 | Symbols); |
| 4005 | |
| 4006 | llvm::GlobalVariable *GV = |
| 4007 | new llvm::GlobalVariable(Init->getType(), false, |
| 4008 | llvm::GlobalValue::InternalLinkage, |
| 4009 | Init, |
| 4010 | "\01L_OBJC_LABEL_CATEGORY_$", |
| 4011 | &CGM.getModule()); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 4012 | GV->setAlignment(8); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4013 | GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip"); |
| 4014 | UsedGlobals.push_back(GV); |
| 4015 | } |
| 4016 | |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4017 | // static int L_OBJC_IMAGE_INFO[2] = { 0, flags }; |
| 4018 | // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0 |
| 4019 | std::vector<llvm::Constant*> Values(2); |
| 4020 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0); |
Fariborz Jahanian | 067986e | 2009-02-24 21:08:09 +0000 | [diff] [blame] | 4021 | unsigned int flags = 0; |
Fariborz Jahanian | 66a5c2c | 2009-02-24 23:34:44 +0000 | [diff] [blame] | 4022 | // FIXME: Fix and continue? |
| 4023 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 4024 | flags |= eImageInfo_GarbageCollected; |
| 4025 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 4026 | flags |= eImageInfo_GCOnly; |
Fariborz Jahanian | 067986e | 2009-02-24 21:08:09 +0000 | [diff] [blame] | 4027 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4028 | llvm::Constant* Init = llvm::ConstantArray::get( |
| 4029 | llvm::ArrayType::get(ObjCTypes.IntTy, 2), |
| 4030 | Values); |
| 4031 | llvm::GlobalVariable *IMGV = |
| 4032 | new llvm::GlobalVariable(Init->getType(), false, |
| 4033 | llvm::GlobalValue::InternalLinkage, |
| 4034 | Init, |
| 4035 | "\01L_OBJC_IMAGE_INFO", |
| 4036 | &CGM.getModule()); |
| 4037 | IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip"); |
| 4038 | UsedGlobals.push_back(IMGV); |
| 4039 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4040 | std::vector<llvm::Constant*> Used; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4041 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4042 | for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), |
| 4043 | e = UsedGlobals.end(); i != e; ++i) { |
| 4044 | Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); |
| 4045 | } |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4046 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4047 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); |
| 4048 | llvm::GlobalValue *GV = |
| 4049 | new llvm::GlobalVariable(AT, false, |
| 4050 | llvm::GlobalValue::AppendingLinkage, |
| 4051 | llvm::ConstantArray::get(AT, Used), |
| 4052 | "llvm.used", |
| 4053 | &CGM.getModule()); |
| 4054 | |
| 4055 | GV->setSection("llvm.metadata"); |
| 4056 | |
| 4057 | } |
| 4058 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4059 | // Metadata flags |
| 4060 | enum MetaDataDlags { |
| 4061 | CLS = 0x0, |
| 4062 | CLS_META = 0x1, |
| 4063 | CLS_ROOT = 0x2, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4064 | OBJC2_CLS_HIDDEN = 0x10, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4065 | CLS_EXCEPTION = 0x20 |
| 4066 | }; |
| 4067 | /// BuildClassRoTInitializer - generate meta-data for: |
| 4068 | /// struct _class_ro_t { |
| 4069 | /// uint32_t const flags; |
| 4070 | /// uint32_t const instanceStart; |
| 4071 | /// uint32_t const instanceSize; |
| 4072 | /// uint32_t const reserved; // only when building for 64bit targets |
| 4073 | /// const uint8_t * const ivarLayout; |
| 4074 | /// const char *const name; |
| 4075 | /// const struct _method_list_t * const baseMethods; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4076 | /// const struct _protocol_list_t *const baseProtocols; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4077 | /// const struct _ivar_list_t *const ivars; |
| 4078 | /// const uint8_t * const weakIvarLayout; |
| 4079 | /// const struct _prop_list_t * const properties; |
| 4080 | /// } |
| 4081 | /// |
| 4082 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer( |
| 4083 | unsigned flags, |
| 4084 | unsigned InstanceStart, |
| 4085 | unsigned InstanceSize, |
| 4086 | const ObjCImplementationDecl *ID) { |
| 4087 | std::string ClassName = ID->getNameAsString(); |
| 4088 | std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets! |
| 4089 | Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
| 4090 | Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart); |
| 4091 | Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize); |
| 4092 | // FIXME. For 64bit targets add 0 here. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4093 | // FIXME. ivarLayout is currently null! |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 4094 | // Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4095 | // : BuildIvarLayout(ID, true); |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 4096 | Values[ 3] = GetIvarLayoutName(0, ObjCTypes); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4097 | Values[ 4] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4098 | // const struct _method_list_t * const baseMethods; |
| 4099 | std::vector<llvm::Constant*> Methods; |
| 4100 | std::string MethodListName("\01l_OBJC_$_"); |
| 4101 | if (flags & CLS_META) { |
| 4102 | MethodListName += "CLASS_METHODS_" + ID->getNameAsString(); |
| 4103 | for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(), |
| 4104 | e = ID->classmeth_end(); i != e; ++i) { |
| 4105 | // Class methods should always be defined. |
| 4106 | Methods.push_back(GetMethodConstant(*i)); |
| 4107 | } |
| 4108 | } else { |
| 4109 | MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString(); |
| 4110 | for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(), |
| 4111 | e = ID->instmeth_end(); i != e; ++i) { |
| 4112 | // Instance methods should always be defined. |
| 4113 | Methods.push_back(GetMethodConstant(*i)); |
| 4114 | } |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4115 | for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(), |
| 4116 | e = ID->propimpl_end(); i != e; ++i) { |
| 4117 | ObjCPropertyImplDecl *PID = *i; |
| 4118 | |
| 4119 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){ |
| 4120 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 4121 | |
| 4122 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 4123 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4124 | Methods.push_back(C); |
| 4125 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 4126 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4127 | Methods.push_back(C); |
| 4128 | } |
| 4129 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4130 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4131 | Values[ 5] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4132 | "__DATA, __objc_const", Methods); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4133 | |
| 4134 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4135 | assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer"); |
| 4136 | Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_" |
| 4137 | + OID->getNameAsString(), |
| 4138 | OID->protocol_begin(), |
| 4139 | OID->protocol_end()); |
| 4140 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4141 | if (flags & CLS_META) |
| 4142 | Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
| 4143 | else |
| 4144 | Values[ 7] = EmitIvarList(ID); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4145 | // FIXME. weakIvarLayout is currently null. |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 4146 | // Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4147 | // : BuildIvarLayout(ID, false); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 4148 | Values[ 8] = GetIvarLayoutName(0, ObjCTypes); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4149 | if (flags & CLS_META) |
| 4150 | Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 4151 | else |
| 4152 | Values[ 9] = |
| 4153 | EmitPropertyList( |
| 4154 | "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
| 4155 | ID, ID->getClassInterface(), ObjCTypes); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4156 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy, |
| 4157 | Values); |
| 4158 | llvm::GlobalVariable *CLASS_RO_GV = |
| 4159 | new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false, |
| 4160 | llvm::GlobalValue::InternalLinkage, |
| 4161 | Init, |
| 4162 | (flags & CLS_META) ? |
| 4163 | std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName : |
| 4164 | std::string("\01l_OBJC_CLASS_RO_$_")+ClassName, |
| 4165 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4166 | CLASS_RO_GV->setAlignment( |
| 4167 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4168 | CLASS_RO_GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4169 | return CLASS_RO_GV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4170 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4171 | } |
| 4172 | |
| 4173 | /// BuildClassMetaData - This routine defines that to-level meta-data |
| 4174 | /// for the given ClassName for: |
| 4175 | /// struct _class_t { |
| 4176 | /// struct _class_t *isa; |
| 4177 | /// struct _class_t * const superclass; |
| 4178 | /// void *cache; |
| 4179 | /// IMP *vtable; |
| 4180 | /// struct class_ro_t *ro; |
| 4181 | /// } |
| 4182 | /// |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4183 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData( |
| 4184 | std::string &ClassName, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4185 | llvm::Constant *IsAGV, |
| 4186 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4187 | llvm::Constant *ClassRoGV, |
| 4188 | bool HiddenVisibility) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4189 | std::vector<llvm::Constant*> Values(5); |
| 4190 | Values[0] = IsAGV; |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4191 | Values[1] = SuperClassGV |
| 4192 | ? SuperClassGV |
| 4193 | : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4194 | Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar |
| 4195 | Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar |
| 4196 | Values[4] = ClassRoGV; // &CLASS_RO_GV |
| 4197 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy, |
| 4198 | Values); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4199 | llvm::GlobalVariable *GV = GetClassGlobal(ClassName); |
| 4200 | GV->setInitializer(Init); |
Fariborz Jahanian | dd0db2a | 2009-01-31 01:07:39 +0000 | [diff] [blame] | 4201 | GV->setSection("__DATA, __objc_data"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4202 | GV->setAlignment( |
| 4203 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy)); |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4204 | if (HiddenVisibility) |
| 4205 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4206 | return GV; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4207 | } |
| 4208 | |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 4209 | /// countInheritedIvars - count number of ivars in class and its super class(s) |
| 4210 | /// |
| 4211 | static int countInheritedIvars(const ObjCInterfaceDecl *OI, |
| 4212 | ASTContext &Context) { |
| 4213 | int count = 0; |
| 4214 | if (!OI) |
| 4215 | return 0; |
| 4216 | const ObjCInterfaceDecl *SuperClass = OI->getSuperClass(); |
| 4217 | if (SuperClass) |
| 4218 | count += countInheritedIvars(SuperClass, Context); |
| 4219 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 4220 | E = OI->ivar_end(); I != E; ++I) |
| 4221 | ++count; |
| 4222 | // look into properties. |
| 4223 | for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(Context), |
| 4224 | E = OI->prop_end(Context); I != E; ++I) { |
| 4225 | if ((*I)->getPropertyIvarDecl()) |
| 4226 | ++count; |
| 4227 | } |
| 4228 | return count; |
| 4229 | } |
| 4230 | |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4231 | void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCInterfaceDecl *OID, |
| 4232 | uint32_t &InstanceStart, |
| 4233 | uint32_t &InstanceSize) { |
Daniel Dunbar | 24c8991 | 2009-04-21 21:41:56 +0000 | [diff] [blame] | 4234 | assert(!OID->isForwardDecl() && "Invalid interface decl!"); |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4235 | const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OID); |
| 4236 | |
Daniel Dunbar | 6ec0716 | 2009-04-20 07:18:49 +0000 | [diff] [blame] | 4237 | int countSuperClassIvars = countInheritedIvars(OID->getSuperClass(), |
| 4238 | CGM.getContext()); |
| 4239 | const RecordDecl *RD = CGM.getContext().addRecordToClass(OID); |
| 4240 | RecordDecl::field_iterator firstField = RD->field_begin(CGM.getContext()); |
| 4241 | RecordDecl::field_iterator lastField = RD->field_end(CGM.getContext()); |
| 4242 | while (countSuperClassIvars-- > 0) { |
| 4243 | lastField = firstField; |
| 4244 | ++firstField; |
| 4245 | } |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4246 | |
| 4247 | for (RecordDecl::field_iterator e = RD->field_end(CGM.getContext()), |
| 4248 | ifield = firstField; ifield != e; ++ifield) |
| 4249 | lastField = ifield; |
| 4250 | |
| 4251 | InstanceStart = InstanceSize = 0; |
| 4252 | if (lastField != RD->field_end(CGM.getContext())) { |
| 4253 | FieldDecl *Field = *lastField; |
| 4254 | const llvm::Type *FieldTy = |
| 4255 | CGM.getTypes().ConvertTypeForMem(Field->getType()); |
| 4256 | unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy); |
| 4257 | InstanceSize = GetIvarBaseOffset(Layout, Field) + Size; |
| 4258 | if (firstField == RD->field_end(CGM.getContext())) |
| 4259 | InstanceStart = InstanceSize; |
| 4260 | else { |
| 4261 | Field = *firstField; |
| 4262 | InstanceStart = GetIvarBaseOffset(Layout, Field); |
| 4263 | } |
| 4264 | } |
| 4265 | } |
| 4266 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4267 | void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) { |
| 4268 | std::string ClassName = ID->getNameAsString(); |
| 4269 | if (!ObjCEmptyCacheVar) { |
| 4270 | ObjCEmptyCacheVar = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4271 | ObjCTypes.CacheTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4272 | false, |
| 4273 | llvm::GlobalValue::ExternalLinkage, |
| 4274 | 0, |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4275 | "_objc_empty_cache", |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4276 | &CGM.getModule()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4277 | |
| 4278 | ObjCEmptyVtableVar = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4279 | ObjCTypes.ImpnfABITy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4280 | false, |
| 4281 | llvm::GlobalValue::ExternalLinkage, |
| 4282 | 0, |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4283 | "_objc_empty_vtable", |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4284 | &CGM.getModule()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4285 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4286 | assert(ID->getClassInterface() && |
| 4287 | "CGObjCNonFragileABIMac::GenerateClass - class is 0"); |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 4288 | // FIXME: Is this correct (that meta class size is never computed)? |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4289 | uint32_t InstanceStart = |
| 4290 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy); |
| 4291 | uint32_t InstanceSize = InstanceStart; |
| 4292 | uint32_t flags = CLS_META; |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4293 | std::string ObjCMetaClassName(getMetaclassSymbolPrefix()); |
| 4294 | std::string ObjCClassName(getClassSymbolPrefix()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4295 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4296 | llvm::GlobalVariable *SuperClassGV, *IsAGV; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4297 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4298 | bool classIsHidden = |
| 4299 | CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4300 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4301 | flags |= OBJC2_CLS_HIDDEN; |
| 4302 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4303 | // class is root |
| 4304 | flags |= CLS_ROOT; |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4305 | SuperClassGV = GetClassGlobal(ObjCClassName + ClassName); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4306 | IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4307 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4308 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4309 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 4310 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 4311 | Root = Super; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4312 | IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString()); |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4313 | // work on super class metadata symbol. |
| 4314 | std::string SuperClassName = |
| 4315 | ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4316 | SuperClassGV = GetClassGlobal(SuperClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4317 | } |
| 4318 | llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags, |
| 4319 | InstanceStart, |
| 4320 | InstanceSize,ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4321 | std::string TClassName = ObjCMetaClassName + ClassName; |
| 4322 | llvm::GlobalVariable *MetaTClass = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4323 | BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV, |
| 4324 | classIsHidden); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4325 | |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4326 | // Metadata for the class |
| 4327 | flags = CLS; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4328 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4329 | flags |= OBJC2_CLS_HIDDEN; |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4330 | |
| 4331 | if (hasObjCExceptionAttribute(ID->getClassInterface())) |
| 4332 | flags |= CLS_EXCEPTION; |
| 4333 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4334 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4335 | flags |= CLS_ROOT; |
| 4336 | SuperClassGV = 0; |
Chris Lattner | b7b58b1 | 2009-04-19 06:02:28 +0000 | [diff] [blame] | 4337 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4338 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4339 | std::string RootClassName = |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4340 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4341 | SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4342 | } |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4343 | GetClassSizeInfo(ID->getClassInterface(), InstanceStart, InstanceSize); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4344 | CLASS_RO_GV = BuildClassRoTInitializer(flags, |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4345 | InstanceStart, |
| 4346 | InstanceSize, |
| 4347 | ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4348 | |
| 4349 | TClassName = ObjCClassName + ClassName; |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4350 | llvm::GlobalVariable *ClassMD = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4351 | BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV, |
| 4352 | classIsHidden); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4353 | DefinedClasses.push_back(ClassMD); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4354 | |
| 4355 | // Force the definition of the EHType if necessary. |
| 4356 | if (flags & CLS_EXCEPTION) |
| 4357 | GetInterfaceEHType(ID->getClassInterface(), true); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4358 | } |
| 4359 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4360 | /// GenerateProtocolRef - This routine is called to generate code for |
| 4361 | /// a protocol reference expression; as in: |
| 4362 | /// @code |
| 4363 | /// @protocol(Proto1); |
| 4364 | /// @endcode |
| 4365 | /// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1 |
| 4366 | /// which will hold address of the protocol meta-data. |
| 4367 | /// |
| 4368 | llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder, |
| 4369 | const ObjCProtocolDecl *PD) { |
| 4370 | |
Fariborz Jahanian | 960cd06 | 2009-04-10 18:47:34 +0000 | [diff] [blame] | 4371 | // This routine is called for @protocol only. So, we must build definition |
| 4372 | // of protocol's meta-data (not a reference to it!) |
| 4373 | // |
| 4374 | llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD), |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4375 | ObjCTypes.ExternalProtocolPtrTy); |
| 4376 | |
| 4377 | std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_"); |
| 4378 | ProtocolName += PD->getNameAsCString(); |
| 4379 | |
| 4380 | llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName); |
| 4381 | if (PTGV) |
| 4382 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 4383 | PTGV = new llvm::GlobalVariable( |
| 4384 | Init->getType(), false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4385 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4386 | Init, |
| 4387 | ProtocolName, |
| 4388 | &CGM.getModule()); |
| 4389 | PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip"); |
| 4390 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4391 | UsedGlobals.push_back(PTGV); |
| 4392 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 4393 | } |
| 4394 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4395 | /// GenerateCategory - Build metadata for a category implementation. |
| 4396 | /// struct _category_t { |
| 4397 | /// const char * const name; |
| 4398 | /// struct _class_t *const cls; |
| 4399 | /// const struct _method_list_t * const instance_methods; |
| 4400 | /// const struct _method_list_t * const class_methods; |
| 4401 | /// const struct _protocol_list_t * const protocols; |
| 4402 | /// const struct _prop_list_t * const properties; |
| 4403 | /// } |
| 4404 | /// |
| 4405 | void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) |
| 4406 | { |
| 4407 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4408 | const char *Prefix = "\01l_OBJC_$_CATEGORY_"; |
| 4409 | std::string ExtCatName(Prefix + Interface->getNameAsString()+ |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4410 | "_$_" + OCD->getNameAsString()); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4411 | std::string ExtClassName(getClassSymbolPrefix() + |
| 4412 | Interface->getNameAsString()); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4413 | |
| 4414 | std::vector<llvm::Constant*> Values(6); |
| 4415 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 4416 | // meta-class entry symbol |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4417 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4418 | Values[1] = ClassGV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4419 | std::vector<llvm::Constant*> Methods; |
| 4420 | std::string MethodListName(Prefix); |
| 4421 | MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() + |
| 4422 | "_$_" + OCD->getNameAsString(); |
| 4423 | |
| 4424 | for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(), |
| 4425 | e = OCD->instmeth_end(); i != e; ++i) { |
| 4426 | // Instance methods should always be defined. |
| 4427 | Methods.push_back(GetMethodConstant(*i)); |
| 4428 | } |
| 4429 | |
| 4430 | Values[2] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4431 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4432 | Methods); |
| 4433 | |
| 4434 | MethodListName = Prefix; |
| 4435 | MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" + |
| 4436 | OCD->getNameAsString(); |
| 4437 | Methods.clear(); |
| 4438 | for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(), |
| 4439 | e = OCD->classmeth_end(); i != e; ++i) { |
| 4440 | // Class methods should always be defined. |
| 4441 | Methods.push_back(GetMethodConstant(*i)); |
| 4442 | } |
| 4443 | |
| 4444 | Values[3] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4445 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4446 | Methods); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4447 | const ObjCCategoryDecl *Category = |
| 4448 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 4449 | if (Category) { |
| 4450 | std::string ExtName(Interface->getNameAsString() + "_$_" + |
| 4451 | OCD->getNameAsString()); |
| 4452 | Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_" |
| 4453 | + Interface->getNameAsString() + "_$_" |
| 4454 | + Category->getNameAsString(), |
| 4455 | Category->protocol_begin(), |
| 4456 | Category->protocol_end()); |
| 4457 | Values[5] = |
| 4458 | EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName, |
| 4459 | OCD, Category, ObjCTypes); |
| 4460 | } |
| 4461 | else { |
| 4462 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 4463 | Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 4464 | } |
| 4465 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4466 | llvm::Constant *Init = |
| 4467 | llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy, |
| 4468 | Values); |
| 4469 | llvm::GlobalVariable *GCATV |
| 4470 | = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy, |
| 4471 | false, |
| 4472 | llvm::GlobalValue::InternalLinkage, |
| 4473 | Init, |
| 4474 | ExtCatName, |
| 4475 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4476 | GCATV->setAlignment( |
| 4477 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4478 | GCATV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4479 | UsedGlobals.push_back(GCATV); |
| 4480 | DefinedCategories.push_back(GCATV); |
| 4481 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4482 | |
| 4483 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 4484 | /// given method if it has been defined. The result is null if the |
| 4485 | /// method has not been defined. The return value has type MethodPtrTy. |
| 4486 | llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant( |
| 4487 | const ObjCMethodDecl *MD) { |
| 4488 | // FIXME: Use DenseMap::lookup |
| 4489 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 4490 | if (!Fn) |
| 4491 | return 0; |
| 4492 | |
| 4493 | std::vector<llvm::Constant*> Method(3); |
| 4494 | Method[0] = |
| 4495 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 4496 | ObjCTypes.SelectorPtrTy); |
| 4497 | Method[1] = GetMethodVarType(MD); |
| 4498 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 4499 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 4500 | } |
| 4501 | |
| 4502 | /// EmitMethodList - Build meta-data for method declarations |
| 4503 | /// struct _method_list_t { |
| 4504 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 4505 | /// uint32_t method_count; |
| 4506 | /// struct _objc_method method_list[method_count]; |
| 4507 | /// } |
| 4508 | /// |
| 4509 | llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList( |
| 4510 | const std::string &Name, |
| 4511 | const char *Section, |
| 4512 | const ConstantVector &Methods) { |
| 4513 | // Return null for empty list. |
| 4514 | if (Methods.empty()) |
| 4515 | return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy); |
| 4516 | |
| 4517 | std::vector<llvm::Constant*> Values(3); |
| 4518 | // sizeof(struct _objc_method) |
| 4519 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy); |
| 4520 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4521 | // method_count |
| 4522 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 4523 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 4524 | Methods.size()); |
| 4525 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 4526 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4527 | |
| 4528 | llvm::GlobalVariable *GV = |
| 4529 | new llvm::GlobalVariable(Init->getType(), false, |
| 4530 | llvm::GlobalValue::InternalLinkage, |
| 4531 | Init, |
| 4532 | Name, |
| 4533 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4534 | GV->setAlignment( |
| 4535 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4536 | GV->setSection(Section); |
| 4537 | UsedGlobals.push_back(GV); |
| 4538 | return llvm::ConstantExpr::getBitCast(GV, |
| 4539 | ObjCTypes.MethodListnfABIPtrTy); |
| 4540 | } |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4541 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4542 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 4543 | /// the given ivar. |
| 4544 | /// |
| 4545 | llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable( |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 4546 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4547 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 4548 | std::string Name = "OBJC_IVAR_$_" + |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4549 | getInterfaceDeclForIvar(ID, Ivar, CGM.getContext())->getNameAsString() + |
| 4550 | '.' + Ivar->getNameAsString(); |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4551 | llvm::GlobalVariable *IvarOffsetGV = |
| 4552 | CGM.getModule().getGlobalVariable(Name); |
| 4553 | if (!IvarOffsetGV) |
| 4554 | IvarOffsetGV = |
| 4555 | new llvm::GlobalVariable(ObjCTypes.LongTy, |
| 4556 | false, |
| 4557 | llvm::GlobalValue::ExternalLinkage, |
| 4558 | 0, |
| 4559 | Name, |
| 4560 | &CGM.getModule()); |
| 4561 | return IvarOffsetGV; |
| 4562 | } |
| 4563 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4564 | llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar( |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4565 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4566 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4567 | unsigned long int Offset) { |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4568 | llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar); |
| 4569 | IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy, |
| 4570 | Offset)); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4571 | IvarOffsetGV->setAlignment( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4572 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy)); |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4573 | |
| 4574 | // FIXME: This matches gcc, but shouldn't the visibility be set on |
| 4575 | // the use as well (i.e., in ObjCIvarOffsetVariable). |
| 4576 | if (Ivar->getAccessControl() == ObjCIvarDecl::Private || |
| 4577 | Ivar->getAccessControl() == ObjCIvarDecl::Package || |
| 4578 | CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden) |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4579 | IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4580 | else |
Fariborz Jahanian | 77c9fd2 | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 4581 | IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4582 | IvarOffsetGV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4583 | return IvarOffsetGV; |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4584 | } |
| 4585 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4586 | /// EmitIvarList - Emit the ivar list for the given |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 4587 | /// implementation. The return value has type |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4588 | /// IvarListnfABIPtrTy. |
| 4589 | /// struct _ivar_t { |
| 4590 | /// unsigned long int *offset; // pointer to ivar offset location |
| 4591 | /// char *name; |
| 4592 | /// char *type; |
| 4593 | /// uint32_t alignment; |
| 4594 | /// uint32_t size; |
| 4595 | /// } |
| 4596 | /// struct _ivar_list_t { |
| 4597 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 4598 | /// uint32 count; |
| 4599 | /// struct _iver_t list[count]; |
| 4600 | /// } |
| 4601 | /// |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4602 | |
| 4603 | void CGObjCCommonMac::GetNamedIvarList(const ObjCInterfaceDecl *OID, |
| 4604 | llvm::SmallVector<ObjCIvarDecl*, 16> &Res) const { |
| 4605 | for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(), |
| 4606 | E = OID->ivar_end(); I != E; ++I) { |
| 4607 | // Ignore unnamed bit-fields. |
| 4608 | if (!(*I)->getDeclName()) |
| 4609 | continue; |
| 4610 | |
| 4611 | Res.push_back(*I); |
| 4612 | } |
| 4613 | |
| 4614 | for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(CGM.getContext()), |
| 4615 | E = OID->prop_end(CGM.getContext()); I != E; ++I) |
| 4616 | if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl()) |
| 4617 | Res.push_back(IV); |
| 4618 | } |
| 4619 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4620 | llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList( |
| 4621 | const ObjCImplementationDecl *ID) { |
| 4622 | |
| 4623 | std::vector<llvm::Constant*> Ivars, Ivar(5); |
| 4624 | |
| 4625 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4626 | assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface"); |
| 4627 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4628 | // FIXME. Consolidate this with similar code in GenerateClass. |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 4629 | const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OID); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 4630 | |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 4631 | // Collect declared and synthesized ivars in a small vector. |
Fariborz Jahanian | 1819188 | 2009-03-31 18:11:23 +0000 | [diff] [blame] | 4632 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4633 | GetNamedIvarList(OID, OIvars); |
Fariborz Jahanian | 99eee36 | 2009-04-01 19:37:34 +0000 | [diff] [blame] | 4634 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4635 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 4636 | ObjCIvarDecl *IVD = OIvars[i]; |
| 4637 | const FieldDecl *Field = OID->lookupFieldDeclForIvar(CGM.getContext(), IVD); |
Daniel Dunbar | 3eec8aa | 2009-04-20 05:53:40 +0000 | [diff] [blame] | 4638 | Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD, |
Daniel Dunbar | 48fa064 | 2009-04-19 02:03:42 +0000 | [diff] [blame] | 4639 | GetIvarBaseOffset(Layout, Field)); |
Daniel Dunbar | 3eec8aa | 2009-04-20 05:53:40 +0000 | [diff] [blame] | 4640 | Ivar[1] = GetMethodVarName(Field->getIdentifier()); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4641 | Ivar[2] = GetMethodVarType(Field); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4642 | const llvm::Type *FieldTy = |
| 4643 | CGM.getTypes().ConvertTypeForMem(Field->getType()); |
| 4644 | unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy); |
| 4645 | unsigned Align = CGM.getContext().getPreferredTypeAlign( |
| 4646 | Field->getType().getTypePtr()) >> 3; |
| 4647 | Align = llvm::Log2_32(Align); |
| 4648 | Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align); |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 4649 | // NOTE. Size of a bitfield does not match gcc's, because of the |
| 4650 | // way bitfields are treated special in each. But I am told that |
| 4651 | // 'size' for bitfield ivars is ignored by the runtime so it does |
| 4652 | // not matter. If it matters, there is enough info to get the |
| 4653 | // bitfield right! |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4654 | Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4655 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar)); |
| 4656 | } |
| 4657 | // Return null for empty list. |
| 4658 | if (Ivars.empty()) |
| 4659 | return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
| 4660 | std::vector<llvm::Constant*> Values(3); |
| 4661 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy); |
| 4662 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4663 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 4664 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy, |
| 4665 | Ivars.size()); |
| 4666 | Values[2] = llvm::ConstantArray::get(AT, Ivars); |
| 4667 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4668 | const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_"; |
| 4669 | llvm::GlobalVariable *GV = |
| 4670 | new llvm::GlobalVariable(Init->getType(), false, |
| 4671 | llvm::GlobalValue::InternalLinkage, |
| 4672 | Init, |
| 4673 | Prefix + OID->getNameAsString(), |
| 4674 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4675 | GV->setAlignment( |
| 4676 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4677 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4678 | |
| 4679 | UsedGlobals.push_back(GV); |
| 4680 | return llvm::ConstantExpr::getBitCast(GV, |
| 4681 | ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4682 | } |
| 4683 | |
| 4684 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef( |
| 4685 | const ObjCProtocolDecl *PD) { |
| 4686 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4687 | |
| 4688 | if (!Entry) { |
| 4689 | // We use the initializer as a marker of whether this is a forward |
| 4690 | // reference or not. At module finalization we add the empty |
| 4691 | // contents for protocols which were referenced but never defined. |
| 4692 | Entry = |
| 4693 | new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false, |
| 4694 | llvm::GlobalValue::ExternalLinkage, |
| 4695 | 0, |
| 4696 | "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(), |
| 4697 | &CGM.getModule()); |
| 4698 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 4699 | UsedGlobals.push_back(Entry); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4700 | } |
| 4701 | |
| 4702 | return Entry; |
| 4703 | } |
| 4704 | |
| 4705 | /// GetOrEmitProtocol - Generate the protocol meta-data: |
| 4706 | /// @code |
| 4707 | /// struct _protocol_t { |
| 4708 | /// id isa; // NULL |
| 4709 | /// const char * const protocol_name; |
| 4710 | /// const struct _protocol_list_t * protocol_list; // super protocols |
| 4711 | /// const struct method_list_t * const instance_methods; |
| 4712 | /// const struct method_list_t * const class_methods; |
| 4713 | /// const struct method_list_t *optionalInstanceMethods; |
| 4714 | /// const struct method_list_t *optionalClassMethods; |
| 4715 | /// const struct _prop_list_t * properties; |
| 4716 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 4717 | /// const uint32_t flags; // = 0 |
| 4718 | /// } |
| 4719 | /// @endcode |
| 4720 | /// |
| 4721 | |
| 4722 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol( |
| 4723 | const ObjCProtocolDecl *PD) { |
| 4724 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4725 | |
| 4726 | // Early exit if a defining object has already been generated. |
| 4727 | if (Entry && Entry->hasInitializer()) |
| 4728 | return Entry; |
| 4729 | |
| 4730 | const char *ProtocolName = PD->getNameAsCString(); |
| 4731 | |
| 4732 | // Construct method lists. |
| 4733 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 4734 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4735 | for (ObjCProtocolDecl::instmeth_iterator |
| 4736 | i = PD->instmeth_begin(CGM.getContext()), |
| 4737 | e = PD->instmeth_end(CGM.getContext()); |
| 4738 | i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4739 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4740 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4741 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4742 | OptInstanceMethods.push_back(C); |
| 4743 | } else { |
| 4744 | InstanceMethods.push_back(C); |
| 4745 | } |
| 4746 | } |
| 4747 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4748 | for (ObjCProtocolDecl::classmeth_iterator |
| 4749 | i = PD->classmeth_begin(CGM.getContext()), |
| 4750 | e = PD->classmeth_end(CGM.getContext()); |
| 4751 | i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4752 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4753 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4754 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4755 | OptClassMethods.push_back(C); |
| 4756 | } else { |
| 4757 | ClassMethods.push_back(C); |
| 4758 | } |
| 4759 | } |
| 4760 | |
| 4761 | std::vector<llvm::Constant*> Values(10); |
| 4762 | // isa is NULL |
| 4763 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy); |
| 4764 | Values[1] = GetClassName(PD->getIdentifier()); |
| 4765 | Values[2] = EmitProtocolList( |
| 4766 | "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(), |
| 4767 | PD->protocol_begin(), |
| 4768 | PD->protocol_end()); |
| 4769 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4770 | Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4771 | + PD->getNameAsString(), |
| 4772 | "__DATA, __objc_const", |
| 4773 | InstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4774 | Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4775 | + PD->getNameAsString(), |
| 4776 | "__DATA, __objc_const", |
| 4777 | ClassMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4778 | Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4779 | + PD->getNameAsString(), |
| 4780 | "__DATA, __objc_const", |
| 4781 | OptInstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4782 | Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4783 | + PD->getNameAsString(), |
| 4784 | "__DATA, __objc_const", |
| 4785 | OptClassMethods); |
| 4786 | Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(), |
| 4787 | 0, PD, ObjCTypes); |
| 4788 | uint32_t Size = |
| 4789 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy); |
| 4790 | Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4791 | Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy); |
| 4792 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy, |
| 4793 | Values); |
| 4794 | |
| 4795 | if (Entry) { |
| 4796 | // Already created, fix the linkage and update the initializer. |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4797 | Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4798 | Entry->setInitializer(Init); |
| 4799 | } else { |
| 4800 | Entry = |
| 4801 | new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4802 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4803 | Init, |
| 4804 | std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName, |
| 4805 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4806 | Entry->setAlignment( |
| 4807 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4808 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4809 | } |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4810 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4811 | |
| 4812 | // Use this protocol meta-data to build protocol list table in section |
| 4813 | // __DATA, __objc_protolist |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4814 | llvm::GlobalVariable *PTGV = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4815 | ObjCTypes.ProtocolnfABIPtrTy, false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4816 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4817 | Entry, |
| 4818 | std::string("\01l_OBJC_LABEL_PROTOCOL_$_") |
| 4819 | +ProtocolName, |
| 4820 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4821 | PTGV->setAlignment( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4822 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy)); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 4823 | PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip"); |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4824 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4825 | UsedGlobals.push_back(PTGV); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4826 | return Entry; |
| 4827 | } |
| 4828 | |
| 4829 | /// EmitProtocolList - Generate protocol list meta-data: |
| 4830 | /// @code |
| 4831 | /// struct _protocol_list_t { |
| 4832 | /// long protocol_count; // Note, this is 32/64 bit |
| 4833 | /// struct _protocol_t[protocol_count]; |
| 4834 | /// } |
| 4835 | /// @endcode |
| 4836 | /// |
| 4837 | llvm::Constant * |
| 4838 | CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name, |
| 4839 | ObjCProtocolDecl::protocol_iterator begin, |
| 4840 | ObjCProtocolDecl::protocol_iterator end) { |
| 4841 | std::vector<llvm::Constant*> ProtocolRefs; |
| 4842 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4843 | // Just return null for empty protocol lists |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4844 | if (begin == end) |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4845 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 4846 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4847 | // FIXME: We shouldn't need to do this lookup here, should we? |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4848 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true); |
| 4849 | if (GV) |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4850 | return llvm::ConstantExpr::getBitCast(GV, |
| 4851 | ObjCTypes.ProtocolListnfABIPtrTy); |
| 4852 | |
| 4853 | for (; begin != end; ++begin) |
| 4854 | ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented??? |
| 4855 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4856 | // This list is null terminated. |
| 4857 | ProtocolRefs.push_back(llvm::Constant::getNullValue( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4858 | ObjCTypes.ProtocolnfABIPtrTy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4859 | |
| 4860 | std::vector<llvm::Constant*> Values(2); |
| 4861 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
| 4862 | Values[1] = |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4863 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4864 | ProtocolRefs.size()), |
| 4865 | ProtocolRefs); |
| 4866 | |
| 4867 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4868 | GV = new llvm::GlobalVariable(Init->getType(), false, |
| 4869 | llvm::GlobalValue::InternalLinkage, |
| 4870 | Init, |
| 4871 | Name, |
| 4872 | &CGM.getModule()); |
| 4873 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4874 | GV->setAlignment( |
| 4875 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4876 | UsedGlobals.push_back(GV); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4877 | return llvm::ConstantExpr::getBitCast(GV, |
| 4878 | ObjCTypes.ProtocolListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4879 | } |
| 4880 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4881 | /// GetMethodDescriptionConstant - This routine build following meta-data: |
| 4882 | /// struct _objc_method { |
| 4883 | /// SEL _cmd; |
| 4884 | /// char *method_type; |
| 4885 | /// char *_imp; |
| 4886 | /// } |
| 4887 | |
| 4888 | llvm::Constant * |
| 4889 | CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 4890 | std::vector<llvm::Constant*> Desc(3); |
| 4891 | Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 4892 | ObjCTypes.SelectorPtrTy); |
| 4893 | Desc[1] = GetMethodVarType(MD); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4894 | // Protocol methods have no implementation. So, this entry is always NULL. |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4895 | Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 4896 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc); |
| 4897 | } |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4898 | |
| 4899 | /// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference. |
| 4900 | /// This code gen. amounts to generating code for: |
| 4901 | /// @code |
| 4902 | /// (type *)((char *)base + _OBJC_IVAR_$_.ivar; |
| 4903 | /// @encode |
| 4904 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4905 | LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4906 | CodeGen::CodeGenFunction &CGF, |
| 4907 | QualType ObjectTy, |
| 4908 | llvm::Value *BaseValue, |
| 4909 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4910 | unsigned CVRQualifiers) { |
Daniel Dunbar | 525c9b7 | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 4911 | const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl(); |
| 4912 | const FieldDecl *Field = ID->lookupFieldDeclForIvar(CGM.getContext(), Ivar); |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 4913 | llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4914 | |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4915 | // (char *) BaseValue |
Chris Lattner | 51123fe | 2009-04-17 17:46:19 +0000 | [diff] [blame] | 4916 | llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4917 | llvm::Value *Offset = CGF.Builder.CreateLoad(IvarOffsetGV); |
| 4918 | // (char*)BaseValue + Offset_symbol |
| 4919 | V = CGF.Builder.CreateGEP(V, Offset, "add.ptr"); |
| 4920 | // (type *)((char*)BaseValue + Offset_symbol) |
| 4921 | const llvm::Type *IvarTy = |
Chris Lattner | 51123fe | 2009-04-17 17:46:19 +0000 | [diff] [blame] | 4922 | CGM.getTypes().ConvertTypeForMem(Ivar->getType()); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4923 | llvm::Type *ptrIvarTy = llvm::PointerType::getUnqual(IvarTy); |
| 4924 | V = CGF.Builder.CreateBitCast(V, ptrIvarTy); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4925 | |
Fariborz Jahanian | a6681ae | 2009-03-09 20:44:22 +0000 | [diff] [blame] | 4926 | if (Ivar->isBitField()) { |
Chris Lattner | 51123fe | 2009-04-17 17:46:19 +0000 | [diff] [blame] | 4927 | QualType FieldTy = Field->getType(); |
Fariborz Jahanian | a6681ae | 2009-03-09 20:44:22 +0000 | [diff] [blame] | 4928 | CodeGenTypes::BitFieldInfo bitFieldInfo = |
| 4929 | CGM.getTypes().getBitFieldInfo(Field); |
| 4930 | return LValue::MakeBitfield(V, bitFieldInfo.Begin, bitFieldInfo.Size, |
Chris Lattner | 51123fe | 2009-04-17 17:46:19 +0000 | [diff] [blame] | 4931 | FieldTy->isSignedIntegerType(), |
| 4932 | FieldTy.getCVRQualifiers()|CVRQualifiers); |
Fariborz Jahanian | a6681ae | 2009-03-09 20:44:22 +0000 | [diff] [blame] | 4933 | } |
| 4934 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4935 | LValue LV = LValue::MakeAddr(V, |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 4936 | Ivar->getType().getCVRQualifiers()|CVRQualifiers, |
| 4937 | CGM.getContext().getObjCGCAttrKind(Ivar->getType())); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4938 | LValue::SetObjCIvar(LV, true); |
| 4939 | return LV; |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4940 | } |
| 4941 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4942 | llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset( |
| 4943 | CodeGen::CodeGenFunction &CGF, |
| 4944 | ObjCInterfaceDecl *Interface, |
| 4945 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 4946 | return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar), |
| 4947 | false, "ivar"); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4948 | } |
| 4949 | |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 4950 | CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend( |
| 4951 | CodeGen::CodeGenFunction &CGF, |
| 4952 | QualType ResultType, |
| 4953 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4954 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 4955 | QualType Arg0Ty, |
| 4956 | bool IsSuper, |
| 4957 | const CallArgList &CallArgs) { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4958 | // FIXME. Even though IsSuper is passes. This function doese not |
| 4959 | // handle calls to 'super' receivers. |
| 4960 | CodeGenTypes &Types = CGM.getTypes(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4961 | llvm::Value *Arg0 = Receiver; |
| 4962 | if (!IsSuper) |
| 4963 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4964 | |
| 4965 | // Find the message function name. |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 4966 | // FIXME. This is too much work to get the ABI-specific result type |
| 4967 | // needed to find the message name. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4968 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, |
| 4969 | llvm::SmallVector<QualType, 16>()); |
| 4970 | llvm::Constant *Fn; |
| 4971 | std::string Name("\01l_"); |
| 4972 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4973 | #if 0 |
| 4974 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4975 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
| 4976 | Fn = ObjCTypes.MessageSendIdStretFixupFn; |
| 4977 | // FIXME. Is there a better way of getting these names. |
| 4978 | // They are available in RuntimeFunctions vector pair. |
| 4979 | Name += "objc_msgSendId_stret_fixup"; |
| 4980 | } |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4981 | else |
| 4982 | #endif |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4983 | if (IsSuper) { |
| 4984 | Fn = ObjCTypes.MessageSendSuper2StretFixupFn; |
| 4985 | Name += "objc_msgSendSuper2_stret_fixup"; |
| 4986 | } |
| 4987 | else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4988 | { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4989 | Fn = ObjCTypes.MessageSendStretFixupFn; |
| 4990 | Name += "objc_msgSend_stret_fixup"; |
| 4991 | } |
| 4992 | } |
Fariborz Jahanian | 1a6b368 | 2009-02-05 19:35:43 +0000 | [diff] [blame] | 4993 | else if (ResultType->isFloatingType() && |
| 4994 | // Selection of frret API only happens in 32bit nonfragile ABI. |
| 4995 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.LongTy) == 4) { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4996 | Fn = ObjCTypes.MessageSendFpretFixupFn; |
| 4997 | Name += "objc_msgSend_fpret_fixup"; |
| 4998 | } |
| 4999 | else { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5000 | #if 0 |
| 5001 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5002 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
| 5003 | Fn = ObjCTypes.MessageSendIdFixupFn; |
| 5004 | Name += "objc_msgSendId_fixup"; |
| 5005 | } |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5006 | else |
| 5007 | #endif |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5008 | if (IsSuper) { |
| 5009 | Fn = ObjCTypes.MessageSendSuper2FixupFn; |
| 5010 | Name += "objc_msgSendSuper2_fixup"; |
| 5011 | } |
| 5012 | else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5013 | { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5014 | Fn = ObjCTypes.MessageSendFixupFn; |
| 5015 | Name += "objc_msgSend_fixup"; |
| 5016 | } |
| 5017 | } |
| 5018 | Name += '_'; |
| 5019 | std::string SelName(Sel.getAsString()); |
| 5020 | // Replace all ':' in selector name with '_' ouch! |
| 5021 | for(unsigned i = 0; i < SelName.size(); i++) |
| 5022 | if (SelName[i] == ':') |
| 5023 | SelName[i] = '_'; |
| 5024 | Name += SelName; |
| 5025 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5026 | if (!GV) { |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5027 | // Build message ref table entry. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5028 | std::vector<llvm::Constant*> Values(2); |
| 5029 | Values[0] = Fn; |
| 5030 | Values[1] = GetMethodVarName(Sel); |
| 5031 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 5032 | GV = new llvm::GlobalVariable(Init->getType(), false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 5033 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5034 | Init, |
| 5035 | Name, |
| 5036 | &CGM.getModule()); |
| 5037 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | f59c1a6 | 2009-04-15 19:04:46 +0000 | [diff] [blame] | 5038 | GV->setAlignment(16); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5039 | GV->setSection("__DATA, __objc_msgrefs, coalesced"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5040 | } |
| 5041 | llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5042 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5043 | CallArgList ActualArgs; |
| 5044 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
| 5045 | ActualArgs.push_back(std::make_pair(RValue::get(Arg1), |
| 5046 | ObjCTypes.MessageRefCPtrTy)); |
| 5047 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5048 | const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs); |
| 5049 | llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0); |
| 5050 | Callee = CGF.Builder.CreateLoad(Callee); |
Fariborz Jahanian | 3ab75bd | 2009-02-14 21:25:36 +0000 | [diff] [blame] | 5051 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5052 | Callee = CGF.Builder.CreateBitCast(Callee, |
| 5053 | llvm::PointerType::getUnqual(FTy)); |
| 5054 | return CGF.EmitCall(FnInfo1, Callee, ActualArgs); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5055 | } |
| 5056 | |
| 5057 | /// Generate code for a message send expression in the nonfragile abi. |
| 5058 | CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend( |
| 5059 | CodeGen::CodeGenFunction &CGF, |
| 5060 | QualType ResultType, |
| 5061 | Selector Sel, |
| 5062 | llvm::Value *Receiver, |
| 5063 | bool IsClassMessage, |
| 5064 | const CallArgList &CallArgs) { |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5065 | return EmitMessageSend(CGF, ResultType, Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5066 | Receiver, CGF.getContext().getObjCIdType(), |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5067 | false, CallArgs); |
| 5068 | } |
| 5069 | |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5070 | llvm::GlobalVariable * |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5071 | CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5072 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5073 | |
Daniel Dunbar | dfff230 | 2009-03-02 05:18:14 +0000 | [diff] [blame] | 5074 | if (!GV) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5075 | GV = new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, |
| 5076 | llvm::GlobalValue::ExternalLinkage, |
| 5077 | 0, Name, &CGM.getModule()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5078 | } |
| 5079 | |
| 5080 | return GV; |
| 5081 | } |
| 5082 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5083 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5084 | const ObjCInterfaceDecl *ID) { |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5085 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 5086 | |
| 5087 | if (!Entry) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5088 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5089 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5090 | Entry = |
| 5091 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5092 | llvm::GlobalValue::InternalLinkage, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5093 | ClassGV, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5094 | "\01L_OBJC_CLASSLIST_REFERENCES_$_", |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5095 | &CGM.getModule()); |
| 5096 | Entry->setAlignment( |
| 5097 | CGM.getTargetData().getPrefTypeAlignment( |
| 5098 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5099 | Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip"); |
| 5100 | UsedGlobals.push_back(Entry); |
| 5101 | } |
| 5102 | |
| 5103 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5104 | } |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5105 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5106 | llvm::Value * |
| 5107 | CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder, |
| 5108 | const ObjCInterfaceDecl *ID) { |
| 5109 | llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()]; |
| 5110 | |
| 5111 | if (!Entry) { |
| 5112 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
| 5113 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
| 5114 | Entry = |
| 5115 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5116 | llvm::GlobalValue::InternalLinkage, |
| 5117 | ClassGV, |
| 5118 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_", |
| 5119 | &CGM.getModule()); |
| 5120 | Entry->setAlignment( |
| 5121 | CGM.getTargetData().getPrefTypeAlignment( |
| 5122 | ObjCTypes.ClassnfABIPtrTy)); |
| 5123 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5124 | UsedGlobals.push_back(Entry); |
| 5125 | } |
| 5126 | |
| 5127 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5128 | } |
| 5129 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5130 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 5131 | /// meta-data |
| 5132 | /// |
| 5133 | llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder, |
| 5134 | const ObjCInterfaceDecl *ID) { |
| 5135 | llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()]; |
| 5136 | if (Entry) |
| 5137 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5138 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5139 | std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5140 | llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5141 | Entry = |
| 5142 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5143 | llvm::GlobalValue::InternalLinkage, |
| 5144 | MetaClassGV, |
| 5145 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_", |
| 5146 | &CGM.getModule()); |
| 5147 | Entry->setAlignment( |
| 5148 | CGM.getTargetData().getPrefTypeAlignment( |
| 5149 | ObjCTypes.ClassnfABIPtrTy)); |
| 5150 | |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5151 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5152 | UsedGlobals.push_back(Entry); |
| 5153 | |
| 5154 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5155 | } |
| 5156 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5157 | /// GetClass - Return a reference to the class for the given interface |
| 5158 | /// decl. |
| 5159 | llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder, |
| 5160 | const ObjCInterfaceDecl *ID) { |
| 5161 | return EmitClassRef(Builder, ID); |
| 5162 | } |
| 5163 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5164 | /// Generates a message send where the super is the receiver. This is |
| 5165 | /// a message send to self with special delivery semantics indicating |
| 5166 | /// which class's method should be called. |
| 5167 | CodeGen::RValue |
| 5168 | CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 5169 | QualType ResultType, |
| 5170 | Selector Sel, |
| 5171 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5172 | bool isCategoryImpl, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5173 | llvm::Value *Receiver, |
| 5174 | bool IsClassMessage, |
| 5175 | const CodeGen::CallArgList &CallArgs) { |
| 5176 | // ... |
| 5177 | // Create and init a super structure; this is a (receiver, class) |
| 5178 | // pair we will pass to objc_msgSendSuper. |
| 5179 | llvm::Value *ObjCSuper = |
| 5180 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 5181 | |
| 5182 | llvm::Value *ReceiverAsObject = |
| 5183 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 5184 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 5185 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
| 5186 | |
| 5187 | // If this is a class message the metaclass is passed as the target. |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5188 | llvm::Value *Target; |
| 5189 | if (IsClassMessage) { |
| 5190 | if (isCategoryImpl) { |
| 5191 | // Message sent to "super' in a class method defined in |
| 5192 | // a category implementation. |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5193 | Target = EmitClassRef(CGF.Builder, Class); |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5194 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 5195 | Target = CGF.Builder.CreateLoad(Target); |
| 5196 | } |
| 5197 | else |
| 5198 | Target = EmitMetaClassRef(CGF.Builder, Class); |
| 5199 | } |
| 5200 | else |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5201 | Target = EmitSuperClassRef(CGF.Builder, Class); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5202 | |
| 5203 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext |
| 5204 | // and ObjCTypes types. |
| 5205 | const llvm::Type *ClassTy = |
| 5206 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
| 5207 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
| 5208 | CGF.Builder.CreateStore(Target, |
| 5209 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
| 5210 | |
| 5211 | return EmitMessageSend(CGF, ResultType, Sel, |
| 5212 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 5213 | true, CallArgs); |
| 5214 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5215 | |
| 5216 | llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder, |
| 5217 | Selector Sel) { |
| 5218 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 5219 | |
| 5220 | if (!Entry) { |
| 5221 | llvm::Constant *Casted = |
| 5222 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 5223 | ObjCTypes.SelectorPtrTy); |
| 5224 | Entry = |
| 5225 | new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false, |
| 5226 | llvm::GlobalValue::InternalLinkage, |
| 5227 | Casted, "\01L_OBJC_SELECTOR_REFERENCES_", |
| 5228 | &CGM.getModule()); |
| 5229 | Entry->setSection("__DATA,__objc_selrefs,literal_pointers,no_dead_strip"); |
| 5230 | UsedGlobals.push_back(Entry); |
| 5231 | } |
| 5232 | |
| 5233 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5234 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5235 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 5236 | /// objc_assign_ivar (id src, id *dst) |
| 5237 | /// |
| 5238 | void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 5239 | llvm::Value *src, llvm::Value *dst) |
| 5240 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5241 | const llvm::Type * SrcTy = src->getType(); |
| 5242 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5243 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5244 | assert(Size <= 8 && "does not support size > 8"); |
| 5245 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5246 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5247 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5248 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5249 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5250 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
| 5251 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn, |
| 5252 | src, dst, "assignivar"); |
| 5253 | return; |
| 5254 | } |
| 5255 | |
| 5256 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 5257 | /// objc_assign_strongCast (id src, id *dst) |
| 5258 | /// |
| 5259 | void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign( |
| 5260 | CodeGen::CodeGenFunction &CGF, |
| 5261 | llvm::Value *src, llvm::Value *dst) |
| 5262 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5263 | const llvm::Type * SrcTy = src->getType(); |
| 5264 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5265 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5266 | assert(Size <= 8 && "does not support size > 8"); |
| 5267 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5268 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5269 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5270 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5271 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5272 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
| 5273 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn, |
| 5274 | src, dst, "weakassign"); |
| 5275 | return; |
| 5276 | } |
| 5277 | |
| 5278 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
| 5279 | /// object: objc_read_weak (id *src) |
| 5280 | /// |
| 5281 | llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead( |
| 5282 | CodeGen::CodeGenFunction &CGF, |
| 5283 | llvm::Value *AddrWeakObj) |
| 5284 | { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5285 | const llvm::Type* DestTy = |
| 5286 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5287 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
| 5288 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn, |
| 5289 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5290 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5291 | return read_weak; |
| 5292 | } |
| 5293 | |
| 5294 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 5295 | /// objc_assign_weak (id src, id *dst) |
| 5296 | /// |
| 5297 | void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 5298 | llvm::Value *src, llvm::Value *dst) |
| 5299 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5300 | const llvm::Type * SrcTy = src->getType(); |
| 5301 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5302 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5303 | assert(Size <= 8 && "does not support size > 8"); |
| 5304 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5305 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5306 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5307 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5308 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5309 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 5310 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5311 | src, dst, "weakassign"); |
| 5312 | return; |
| 5313 | } |
| 5314 | |
| 5315 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 5316 | /// objc_assign_global (id src, id *dst) |
| 5317 | /// |
| 5318 | void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 5319 | llvm::Value *src, llvm::Value *dst) |
| 5320 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5321 | const llvm::Type * SrcTy = src->getType(); |
| 5322 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5323 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5324 | assert(Size <= 8 && "does not support size > 8"); |
| 5325 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5326 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5327 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5328 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5329 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5330 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
| 5331 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn, |
| 5332 | src, dst, "globalassign"); |
| 5333 | return; |
| 5334 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5335 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5336 | void |
| 5337 | CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 5338 | const Stmt &S) { |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5339 | bool isTry = isa<ObjCAtTryStmt>(S); |
| 5340 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 5341 | llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5342 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5343 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5344 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5345 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
| 5346 | |
| 5347 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 5348 | // evaluation of the expression must occur before we enter the |
| 5349 | // @synchronized. We can safely avoid a temp here because jumps into |
| 5350 | // @synchronized are illegal & this will dominate uses. |
| 5351 | llvm::Value *SyncArg = 0; |
| 5352 | if (!isTry) { |
| 5353 | SyncArg = |
| 5354 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 5355 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5356 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5357 | } |
| 5358 | |
| 5359 | // Push an EH context entry, used for handling rethrows and jumps |
| 5360 | // through finally. |
| 5361 | CGF.PushCleanupBlock(FinallyBlock); |
| 5362 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5363 | CGF.setInvokeDest(TryHandler); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5364 | |
| 5365 | CGF.EmitBlock(TryBlock); |
| 5366 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 5367 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
| 5368 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 5369 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5370 | // Emit the exception handler. |
| 5371 | |
| 5372 | CGF.EmitBlock(TryHandler); |
| 5373 | |
| 5374 | llvm::Value *llvm_eh_exception = |
| 5375 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception); |
| 5376 | llvm::Value *llvm_eh_selector_i64 = |
| 5377 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64); |
| 5378 | llvm::Value *llvm_eh_typeid_for_i64 = |
| 5379 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64); |
| 5380 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5381 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow"); |
| 5382 | |
| 5383 | llvm::SmallVector<llvm::Value*, 8> SelectorArgs; |
| 5384 | SelectorArgs.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5385 | SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5386 | |
| 5387 | // Construct the lists of (type, catch body) to handle. |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5388 | llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5389 | bool HasCatchAll = false; |
| 5390 | if (isTry) { |
| 5391 | if (const ObjCAtCatchStmt* CatchStmt = |
| 5392 | cast<ObjCAtTryStmt>(S).getCatchStmts()) { |
| 5393 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5394 | const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl(); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5395 | Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody())); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5396 | |
| 5397 | // catch(...) always matches. |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5398 | if (!CatchDecl) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5399 | // Use i8* null here to signal this is a catch all, not a cleanup. |
| 5400 | llvm::Value *Null = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 5401 | SelectorArgs.push_back(Null); |
| 5402 | HasCatchAll = true; |
| 5403 | break; |
| 5404 | } |
| 5405 | |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5406 | if (CGF.getContext().isObjCIdType(CatchDecl->getType()) || |
| 5407 | CatchDecl->getType()->isObjCQualifiedIdType()) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5408 | llvm::Value *IDEHType = |
| 5409 | CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id"); |
| 5410 | if (!IDEHType) |
| 5411 | IDEHType = |
| 5412 | new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5413 | llvm::GlobalValue::ExternalLinkage, |
| 5414 | 0, "OBJC_EHTYPE_id", &CGM.getModule()); |
| 5415 | SelectorArgs.push_back(IDEHType); |
| 5416 | HasCatchAll = true; |
| 5417 | break; |
| 5418 | } |
| 5419 | |
| 5420 | // All other types should be Objective-C interface pointer types. |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5421 | const PointerType *PT = CatchDecl->getType()->getAsPointerType(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5422 | assert(PT && "Invalid @catch type."); |
| 5423 | const ObjCInterfaceType *IT = |
| 5424 | PT->getPointeeType()->getAsObjCInterfaceType(); |
| 5425 | assert(IT && "Invalid @catch type."); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5426 | llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5427 | SelectorArgs.push_back(EHType); |
| 5428 | } |
| 5429 | } |
| 5430 | } |
| 5431 | |
| 5432 | // We use a cleanup unless there was already a catch all. |
| 5433 | if (!HasCatchAll) { |
| 5434 | SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0)); |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5435 | Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5436 | } |
| 5437 | |
| 5438 | llvm::Value *Selector = |
| 5439 | CGF.Builder.CreateCall(llvm_eh_selector_i64, |
| 5440 | SelectorArgs.begin(), SelectorArgs.end(), |
| 5441 | "selector"); |
| 5442 | for (unsigned i = 0, e = Handlers.size(); i != e; ++i) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5443 | const ParmVarDecl *CatchParam = Handlers[i].first; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5444 | const Stmt *CatchBody = Handlers[i].second; |
| 5445 | |
| 5446 | llvm::BasicBlock *Next = 0; |
| 5447 | |
| 5448 | // The last handler always matches. |
| 5449 | if (i + 1 != e) { |
| 5450 | assert(CatchParam && "Only last handler can be a catch all."); |
| 5451 | |
| 5452 | llvm::BasicBlock *Match = CGF.createBasicBlock("match"); |
| 5453 | Next = CGF.createBasicBlock("catch.next"); |
| 5454 | llvm::Value *Id = |
| 5455 | CGF.Builder.CreateCall(llvm_eh_typeid_for_i64, |
| 5456 | CGF.Builder.CreateBitCast(SelectorArgs[i+2], |
| 5457 | ObjCTypes.Int8PtrTy)); |
| 5458 | CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id), |
| 5459 | Match, Next); |
| 5460 | |
| 5461 | CGF.EmitBlock(Match); |
| 5462 | } |
| 5463 | |
| 5464 | if (CatchBody) { |
| 5465 | llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end"); |
| 5466 | llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler"); |
| 5467 | |
| 5468 | // Cleanups must call objc_end_catch. |
| 5469 | // |
| 5470 | // FIXME: It seems incorrect for objc_begin_catch to be inside |
| 5471 | // this context, but this matches gcc. |
| 5472 | CGF.PushCleanupBlock(MatchEnd); |
| 5473 | CGF.setInvokeDest(MatchHandler); |
| 5474 | |
| 5475 | llvm::Value *ExcObject = |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame^] | 5476 | CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), Exc); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5477 | |
| 5478 | // Bind the catch parameter if it exists. |
| 5479 | if (CatchParam) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5480 | ExcObject = |
| 5481 | CGF.Builder.CreateBitCast(ExcObject, |
| 5482 | CGF.ConvertType(CatchParam->getType())); |
| 5483 | // CatchParam is a ParmVarDecl because of the grammar |
| 5484 | // construction used to handle this, but for codegen purposes |
| 5485 | // we treat this as a local decl. |
| 5486 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
| 5487 | CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5488 | } |
| 5489 | |
| 5490 | CGF.ObjCEHValueStack.push_back(ExcObject); |
| 5491 | CGF.EmitStmt(CatchBody); |
| 5492 | CGF.ObjCEHValueStack.pop_back(); |
| 5493 | |
| 5494 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 5495 | |
| 5496 | CGF.EmitBlock(MatchHandler); |
| 5497 | |
| 5498 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5499 | // We are required to emit this call to satisfy LLVM, even |
| 5500 | // though we don't use the result. |
| 5501 | llvm::SmallVector<llvm::Value*, 8> Args; |
| 5502 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5503 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5504 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 5505 | 0)); |
| 5506 | CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end()); |
| 5507 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5508 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5509 | |
| 5510 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 5511 | |
| 5512 | CGF.EmitBlock(MatchEnd); |
| 5513 | |
| 5514 | // Unfortunately, we also have to generate another EH frame here |
| 5515 | // in case this throws. |
| 5516 | llvm::BasicBlock *MatchEndHandler = |
| 5517 | CGF.createBasicBlock("match.end.handler"); |
| 5518 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame^] | 5519 | CGF.Builder.CreateInvoke(ObjCTypes.getObjCEndCatchFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5520 | Cont, MatchEndHandler, |
| 5521 | Args.begin(), Args.begin()); |
| 5522 | |
| 5523 | CGF.EmitBlock(Cont); |
| 5524 | if (Info.SwitchBlock) |
| 5525 | CGF.EmitBlock(Info.SwitchBlock); |
| 5526 | if (Info.EndBlock) |
| 5527 | CGF.EmitBlock(Info.EndBlock); |
| 5528 | |
| 5529 | CGF.EmitBlock(MatchEndHandler); |
| 5530 | Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5531 | // We are required to emit this call to satisfy LLVM, even |
| 5532 | // though we don't use the result. |
| 5533 | Args.clear(); |
| 5534 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5535 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5536 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 5537 | 0)); |
| 5538 | CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end()); |
| 5539 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5540 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5541 | |
| 5542 | if (Next) |
| 5543 | CGF.EmitBlock(Next); |
| 5544 | } else { |
| 5545 | assert(!Next && "catchup should be last handler."); |
| 5546 | |
| 5547 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5548 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5549 | } |
| 5550 | } |
| 5551 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5552 | // Pop the cleanup entry, the @finally is outside this cleanup |
| 5553 | // scope. |
| 5554 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 5555 | CGF.setInvokeDest(PrevLandingPad); |
| 5556 | |
| 5557 | CGF.EmitBlock(FinallyBlock); |
| 5558 | |
| 5559 | if (isTry) { |
| 5560 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 5561 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 5562 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
| 5563 | } else { |
| 5564 | // Emit 'objc_sync_exit(expr)' as finally's sole statement for |
| 5565 | // @synchronized. |
| 5566 | CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, SyncArg); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5567 | } |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5568 | |
| 5569 | if (Info.SwitchBlock) |
| 5570 | CGF.EmitBlock(Info.SwitchBlock); |
| 5571 | if (Info.EndBlock) |
| 5572 | CGF.EmitBlock(Info.EndBlock); |
| 5573 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5574 | // Branch around the rethrow code. |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5575 | CGF.EmitBranch(FinallyEnd); |
| 5576 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5577 | CGF.EmitBlock(FinallyRethrow); |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame^] | 5578 | CGF.Builder.CreateCall(ObjCTypes.getUnwindResumeOrRethrowFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5579 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5580 | CGF.Builder.CreateUnreachable(); |
| 5581 | |
| 5582 | CGF.EmitBlock(FinallyEnd); |
| 5583 | } |
| 5584 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5585 | /// EmitThrowStmt - Generate code for a throw statement. |
| 5586 | void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 5587 | const ObjCAtThrowStmt &S) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5588 | llvm::Value *Exception; |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5589 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5590 | Exception = CGF.EmitScalarExpr(ThrowExpr); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5591 | } else { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5592 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
| 5593 | "Unexpected rethrow outside @catch block."); |
| 5594 | Exception = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5595 | } |
| 5596 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5597 | llvm::Value *ExceptionAsObject = |
| 5598 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 5599 | llvm::BasicBlock *InvokeDest = CGF.getInvokeDest(); |
| 5600 | if (InvokeDest) { |
| 5601 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
| 5602 | CGF.Builder.CreateInvoke(ObjCTypes.ExceptionThrowFn, |
| 5603 | Cont, InvokeDest, |
| 5604 | &ExceptionAsObject, &ExceptionAsObject + 1); |
| 5605 | CGF.EmitBlock(Cont); |
| 5606 | } else |
| 5607 | CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject); |
| 5608 | CGF.Builder.CreateUnreachable(); |
| 5609 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5610 | // Clear the insertion point to indicate we are in unreachable code. |
| 5611 | CGF.Builder.ClearInsertionPoint(); |
| 5612 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5613 | |
| 5614 | llvm::Value * |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5615 | CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
| 5616 | bool ForDefinition) { |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5617 | llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()]; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5618 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5619 | // If we don't need a definition, return the entry if found or check |
| 5620 | // if we use an external reference. |
| 5621 | if (!ForDefinition) { |
| 5622 | if (Entry) |
| 5623 | return Entry; |
Daniel Dunbar | 7e075cb | 2009-04-07 06:43:45 +0000 | [diff] [blame] | 5624 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5625 | // If this type (or a super class) has the __objc_exception__ |
| 5626 | // attribute, emit an external reference. |
| 5627 | if (hasObjCExceptionAttribute(ID)) |
| 5628 | return Entry = |
| 5629 | new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5630 | llvm::GlobalValue::ExternalLinkage, |
| 5631 | 0, |
| 5632 | (std::string("OBJC_EHTYPE_$_") + |
| 5633 | ID->getIdentifier()->getName()), |
| 5634 | &CGM.getModule()); |
| 5635 | } |
| 5636 | |
| 5637 | // Otherwise we need to either make a new entry or fill in the |
| 5638 | // initializer. |
| 5639 | assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition"); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5640 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5641 | std::string VTableName = "objc_ehtype_vtable"; |
| 5642 | llvm::GlobalVariable *VTableGV = |
| 5643 | CGM.getModule().getGlobalVariable(VTableName); |
| 5644 | if (!VTableGV) |
| 5645 | VTableGV = new llvm::GlobalVariable(ObjCTypes.Int8PtrTy, false, |
| 5646 | llvm::GlobalValue::ExternalLinkage, |
| 5647 | 0, VTableName, &CGM.getModule()); |
| 5648 | |
| 5649 | llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2); |
| 5650 | |
| 5651 | std::vector<llvm::Constant*> Values(3); |
| 5652 | Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1); |
| 5653 | Values[1] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5654 | Values[2] = GetClassGlobal(ClassName); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5655 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values); |
| 5656 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5657 | if (Entry) { |
| 5658 | Entry->setInitializer(Init); |
| 5659 | } else { |
| 5660 | Entry = new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5661 | llvm::GlobalValue::WeakAnyLinkage, |
| 5662 | Init, |
| 5663 | (std::string("OBJC_EHTYPE_$_") + |
| 5664 | ID->getIdentifier()->getName()), |
| 5665 | &CGM.getModule()); |
| 5666 | } |
| 5667 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 5668 | if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden) |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5669 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5670 | Entry->setAlignment(8); |
| 5671 | |
| 5672 | if (ForDefinition) { |
| 5673 | Entry->setSection("__DATA,__objc_const"); |
| 5674 | Entry->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 5675 | } else { |
| 5676 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 5677 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5678 | |
| 5679 | return Entry; |
| 5680 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5681 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 5682 | /* *** */ |
| 5683 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5684 | CodeGen::CGObjCRuntime * |
| 5685 | CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 5686 | return new CGObjCMac(CGM); |
| 5687 | } |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5688 | |
| 5689 | CodeGen::CGObjCRuntime * |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5690 | CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5691 | return new CGObjCNonFragileABIMac(CGM); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5692 | } |