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 | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 95 | llvm::Constant *GcAssignWeakFn; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 96 | |
| 97 | /// GcAssignGlobalFn -- LLVM objc_assign_global function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 98 | llvm::Constant *GcAssignGlobalFn; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 99 | |
| 100 | /// GcAssignIvarFn -- LLVM objc_assign_ivar function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 101 | llvm::Constant *GcAssignIvarFn; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 102 | |
| 103 | /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 104 | llvm::Constant *GcAssignStrongCastFn; |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 105 | |
| 106 | /// ExceptionThrowFn - LLVM objc_exception_throw function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 107 | llvm::Constant *ExceptionThrowFn; |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 108 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 109 | /// SyncEnterFn - LLVM object_sync_enter function. |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 110 | llvm::Constant *getSyncEnterFn() { |
| 111 | // void objc_sync_enter (id) |
| 112 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 113 | llvm::FunctionType *FTy = |
| 114 | llvm::FunctionType::get(llvm::Type::VoidTy, Args, false); |
| 115 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter"); |
| 116 | } |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 117 | |
| 118 | /// SyncExitFn - LLVM object_sync_exit function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 119 | llvm::Constant *SyncExitFn; |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 120 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 121 | ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm); |
| 122 | ~ObjCCommonTypesHelper(){} |
| 123 | }; |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 124 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 125 | /// ObjCTypesHelper - Helper class that encapsulates lazy |
| 126 | /// construction of varies types used during ObjC generation. |
| 127 | class ObjCTypesHelper : public ObjCCommonTypesHelper { |
| 128 | private: |
| 129 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 130 | llvm::Constant *MessageSendFn, *MessageSendStretFn, *MessageSendFpretFn; |
| 131 | llvm::Constant *MessageSendSuperFn, *MessageSendSuperStretFn, |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 132 | *MessageSendSuperFpretFn; |
| 133 | |
| 134 | public: |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 135 | /// SymtabTy - LLVM type for struct objc_symtab. |
| 136 | const llvm::StructType *SymtabTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 137 | /// SymtabPtrTy - LLVM type for struct objc_symtab *. |
| 138 | const llvm::Type *SymtabPtrTy; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 139 | /// ModuleTy - LLVM type for struct objc_module. |
| 140 | const llvm::StructType *ModuleTy; |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 141 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 142 | /// ProtocolTy - LLVM type for struct objc_protocol. |
| 143 | const llvm::StructType *ProtocolTy; |
| 144 | /// ProtocolPtrTy - LLVM type for struct objc_protocol *. |
| 145 | const llvm::Type *ProtocolPtrTy; |
| 146 | /// ProtocolExtensionTy - LLVM type for struct |
| 147 | /// objc_protocol_extension. |
| 148 | const llvm::StructType *ProtocolExtensionTy; |
| 149 | /// ProtocolExtensionTy - LLVM type for struct |
| 150 | /// objc_protocol_extension *. |
| 151 | const llvm::Type *ProtocolExtensionPtrTy; |
| 152 | /// MethodDescriptionTy - LLVM type for struct |
| 153 | /// objc_method_description. |
| 154 | const llvm::StructType *MethodDescriptionTy; |
| 155 | /// MethodDescriptionListTy - LLVM type for struct |
| 156 | /// objc_method_description_list. |
| 157 | const llvm::StructType *MethodDescriptionListTy; |
| 158 | /// MethodDescriptionListPtrTy - LLVM type for struct |
| 159 | /// objc_method_description_list *. |
| 160 | const llvm::Type *MethodDescriptionListPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 161 | /// ProtocolListTy - LLVM type for struct objc_property_list. |
| 162 | const llvm::Type *ProtocolListTy; |
| 163 | /// ProtocolListPtrTy - LLVM type for struct objc_property_list*. |
| 164 | const llvm::Type *ProtocolListPtrTy; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 165 | /// CategoryTy - LLVM type for struct objc_category. |
| 166 | const llvm::StructType *CategoryTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 167 | /// ClassTy - LLVM type for struct objc_class. |
| 168 | const llvm::StructType *ClassTy; |
| 169 | /// ClassPtrTy - LLVM type for struct objc_class *. |
| 170 | const llvm::Type *ClassPtrTy; |
| 171 | /// ClassExtensionTy - LLVM type for struct objc_class_ext. |
| 172 | const llvm::StructType *ClassExtensionTy; |
| 173 | /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *. |
| 174 | const llvm::Type *ClassExtensionPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 175 | // IvarTy - LLVM type for struct objc_ivar. |
| 176 | const llvm::StructType *IvarTy; |
| 177 | /// IvarListTy - LLVM type for struct objc_ivar_list. |
| 178 | const llvm::Type *IvarListTy; |
| 179 | /// IvarListPtrTy - LLVM type for struct objc_ivar_list *. |
| 180 | const llvm::Type *IvarListPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 181 | /// MethodListTy - LLVM type for struct objc_method_list. |
| 182 | const llvm::Type *MethodListTy; |
| 183 | /// MethodListPtrTy - LLVM type for struct objc_method_list *. |
| 184 | const llvm::Type *MethodListPtrTy; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 185 | |
| 186 | /// ExceptionDataTy - LLVM type for struct _objc_exception_data. |
| 187 | const llvm::Type *ExceptionDataTy; |
| 188 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 189 | /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 190 | llvm::Constant *ExceptionTryEnterFn; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 191 | |
| 192 | /// ExceptionTryExitFn - LLVM objc_exception_try_exit function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 193 | llvm::Constant *ExceptionTryExitFn; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 194 | |
| 195 | /// ExceptionExtractFn - LLVM objc_exception_extract function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 196 | llvm::Constant *ExceptionExtractFn; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 197 | |
| 198 | /// ExceptionMatchFn - LLVM objc_exception_match function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 199 | llvm::Constant *ExceptionMatchFn; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 200 | |
| 201 | /// SetJmpFn - LLVM _setjmp function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 202 | llvm::Constant *SetJmpFn; |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 203 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 204 | public: |
| 205 | ObjCTypesHelper(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 206 | ~ObjCTypesHelper() {} |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 207 | |
| 208 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 209 | llvm::Constant *getSendFn(bool IsSuper) { |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 210 | return IsSuper ? MessageSendSuperFn : MessageSendFn; |
| 211 | } |
| 212 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 213 | llvm::Constant *getSendStretFn(bool IsSuper) { |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 214 | return IsSuper ? MessageSendSuperStretFn : MessageSendStretFn; |
| 215 | } |
| 216 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 217 | llvm::Constant *getSendFpretFn(bool IsSuper) { |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 218 | return IsSuper ? MessageSendSuperFpretFn : MessageSendFpretFn; |
| 219 | } |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 220 | }; |
| 221 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 222 | /// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 223 | /// modern abi |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 224 | class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 225 | public: |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 226 | llvm::Constant *MessageSendFixupFn, *MessageSendFpretFixupFn, |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 227 | *MessageSendStretFixupFn, *MessageSendIdFixupFn, |
| 228 | *MessageSendIdStretFixupFn, *MessageSendSuper2FixupFn, |
| 229 | *MessageSendSuper2StretFixupFn; |
| 230 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 231 | // MethodListnfABITy - LLVM for struct _method_list_t |
| 232 | const llvm::StructType *MethodListnfABITy; |
| 233 | |
| 234 | // MethodListnfABIPtrTy - LLVM for struct _method_list_t* |
| 235 | const llvm::Type *MethodListnfABIPtrTy; |
| 236 | |
| 237 | // ProtocolnfABITy = LLVM for struct _protocol_t |
| 238 | const llvm::StructType *ProtocolnfABITy; |
| 239 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 240 | // ProtocolnfABIPtrTy = LLVM for struct _protocol_t* |
| 241 | const llvm::Type *ProtocolnfABIPtrTy; |
| 242 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 243 | // ProtocolListnfABITy - LLVM for struct _objc_protocol_list |
| 244 | const llvm::StructType *ProtocolListnfABITy; |
| 245 | |
| 246 | // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list* |
| 247 | const llvm::Type *ProtocolListnfABIPtrTy; |
| 248 | |
| 249 | // ClassnfABITy - LLVM for struct _class_t |
| 250 | const llvm::StructType *ClassnfABITy; |
| 251 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 252 | // ClassnfABIPtrTy - LLVM for struct _class_t* |
| 253 | const llvm::Type *ClassnfABIPtrTy; |
| 254 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 255 | // IvarnfABITy - LLVM for struct _ivar_t |
| 256 | const llvm::StructType *IvarnfABITy; |
| 257 | |
| 258 | // IvarListnfABITy - LLVM for struct _ivar_list_t |
| 259 | const llvm::StructType *IvarListnfABITy; |
| 260 | |
| 261 | // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t* |
| 262 | const llvm::Type *IvarListnfABIPtrTy; |
| 263 | |
| 264 | // ClassRonfABITy - LLVM for struct _class_ro_t |
| 265 | const llvm::StructType *ClassRonfABITy; |
| 266 | |
| 267 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 268 | const llvm::Type *ImpnfABITy; |
| 269 | |
| 270 | // CategorynfABITy - LLVM for struct _category_t |
| 271 | const llvm::StructType *CategorynfABITy; |
| 272 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 273 | // New types for nonfragile abi messaging. |
| 274 | |
| 275 | // MessageRefTy - LLVM for: |
| 276 | // struct _message_ref_t { |
| 277 | // IMP messenger; |
| 278 | // SEL name; |
| 279 | // }; |
| 280 | const llvm::StructType *MessageRefTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 281 | // MessageRefCTy - clang type for struct _message_ref_t |
| 282 | QualType MessageRefCTy; |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 283 | |
| 284 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
| 285 | const llvm::Type *MessageRefPtrTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 286 | // MessageRefCPtrTy - clang type for struct _message_ref_t* |
| 287 | QualType MessageRefCPtrTy; |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 288 | |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 289 | // MessengerTy - Type of the messenger (shown as IMP above) |
| 290 | const llvm::FunctionType *MessengerTy; |
| 291 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 292 | // SuperMessageRefTy - LLVM for: |
| 293 | // struct _super_message_ref_t { |
| 294 | // SUPER_IMP messenger; |
| 295 | // SEL name; |
| 296 | // }; |
| 297 | const llvm::StructType *SuperMessageRefTy; |
| 298 | |
| 299 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
| 300 | const llvm::Type *SuperMessageRefPtrTy; |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 301 | |
| 302 | /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C |
| 303 | /// exception personality function. |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 304 | llvm::Value *getEHPersonalityPtr() { |
| 305 | llvm::Constant *Personality = |
| 306 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 307 | std::vector<const llvm::Type*>(), |
| 308 | true), |
| 309 | "__objc_personality_v0"); |
| 310 | return llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy); |
| 311 | } |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 312 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 313 | llvm::Constant *UnwindResumeOrRethrowFn, *ObjCBeginCatchFn, *ObjCEndCatchFn; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 314 | |
| 315 | const llvm::StructType *EHTypeTy; |
| 316 | const llvm::Type *EHTypePtrTy; |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 317 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 318 | ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm); |
| 319 | ~ObjCNonFragileABITypesHelper(){} |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 320 | }; |
| 321 | |
| 322 | class CGObjCCommonMac : public CodeGen::CGObjCRuntime { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 323 | public: |
| 324 | // FIXME - accessibility |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 325 | class GC_IVAR { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 326 | public: |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 327 | unsigned int ivar_bytepos; |
| 328 | unsigned int ivar_size; |
| 329 | GC_IVAR() : ivar_bytepos(0), ivar_size(0) {} |
| 330 | }; |
| 331 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 332 | class SKIP_SCAN { |
| 333 | public: |
| 334 | unsigned int skip; |
| 335 | unsigned int scan; |
| 336 | SKIP_SCAN() : skip(0), scan(0) {} |
| 337 | }; |
| 338 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 339 | protected: |
| 340 | CodeGen::CodeGenModule &CGM; |
| 341 | // FIXME! May not be needing this after all. |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 342 | unsigned ObjCABI; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 343 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 344 | // gc ivar layout bitmap calculation helper caches. |
| 345 | llvm::SmallVector<GC_IVAR, 16> SkipIvars; |
| 346 | llvm::SmallVector<GC_IVAR, 16> IvarsInfo; |
| 347 | llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 348 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 349 | /// LazySymbols - Symbols to generate a lazy reference for. See |
| 350 | /// DefinedSymbols and FinishModule(). |
| 351 | std::set<IdentifierInfo*> LazySymbols; |
| 352 | |
| 353 | /// DefinedSymbols - External symbols which are defined by this |
| 354 | /// module. The symbols in this list and LazySymbols are used to add |
| 355 | /// special linker symbols which ensure that Objective-C modules are |
| 356 | /// linked properly. |
| 357 | std::set<IdentifierInfo*> DefinedSymbols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 358 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 359 | /// ClassNames - uniqued class names. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 360 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 361 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 362 | /// MethodVarNames - uniqued method variable names. |
| 363 | llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 364 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 365 | /// MethodVarTypes - uniqued method type signatures. We have to use |
| 366 | /// a StringMap here because have no other unique reference. |
| 367 | llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 368 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 369 | /// MethodDefinitions - map of methods which have been defined in |
| 370 | /// this translation unit. |
| 371 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 372 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 373 | /// PropertyNames - uniqued method variable names. |
| 374 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 375 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 376 | /// ClassReferences - uniqued class references. |
| 377 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 378 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 379 | /// SelectorReferences - uniqued selector references. |
| 380 | llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 381 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 382 | /// Protocols - Protocols for which an objc_protocol structure has |
| 383 | /// been emitted. Forward declarations are handled by creating an |
| 384 | /// empty structure whose initializer is filled in when/if defined. |
| 385 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 386 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 387 | /// DefinedProtocols - Protocols which have actually been |
| 388 | /// defined. We should not need this, see FIXME in GenerateProtocol. |
| 389 | llvm::DenseSet<IdentifierInfo*> DefinedProtocols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 390 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 391 | /// DefinedClasses - List of defined classes. |
| 392 | std::vector<llvm::GlobalValue*> DefinedClasses; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 393 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 394 | /// DefinedCategories - List of defined categories. |
| 395 | std::vector<llvm::GlobalValue*> DefinedCategories; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 396 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 397 | /// UsedGlobals - List of globals to pack into the llvm.used metadata |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 398 | /// to prevent them from being clobbered. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 399 | std::vector<llvm::GlobalVariable*> UsedGlobals; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 400 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 401 | /// GetNameForMethod - Return a name for the given method. |
| 402 | /// \param[out] NameOut - The return value. |
| 403 | void GetNameForMethod(const ObjCMethodDecl *OMD, |
| 404 | const ObjCContainerDecl *CD, |
| 405 | std::string &NameOut); |
| 406 | |
| 407 | /// GetMethodVarName - Return a unique constant for the given |
| 408 | /// selector's name. The return value has type char *. |
| 409 | llvm::Constant *GetMethodVarName(Selector Sel); |
| 410 | llvm::Constant *GetMethodVarName(IdentifierInfo *Ident); |
| 411 | llvm::Constant *GetMethodVarName(const std::string &Name); |
| 412 | |
| 413 | /// GetMethodVarType - Return a unique constant for the given |
| 414 | /// selector's name. The return value has type char *. |
| 415 | |
| 416 | // FIXME: This is a horrible name. |
| 417 | llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 418 | llvm::Constant *GetMethodVarType(FieldDecl *D); |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 419 | |
| 420 | /// GetPropertyName - Return a unique constant for the given |
| 421 | /// name. The return value has type char *. |
| 422 | llvm::Constant *GetPropertyName(IdentifierInfo *Ident); |
| 423 | |
| 424 | // FIXME: This can be dropped once string functions are unified. |
| 425 | llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 426 | const Decl *Container); |
| 427 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 428 | /// GetClassName - Return a unique constant for the given selector's |
| 429 | /// name. The return value has type char *. |
| 430 | llvm::Constant *GetClassName(IdentifierInfo *Ident); |
| 431 | |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 432 | /// GetInterfaceDeclStructLayout - Get layout for ivars of given |
| 433 | /// interface declaration. |
| 434 | const llvm::StructLayout *GetInterfaceDeclStructLayout( |
| 435 | const ObjCInterfaceDecl *ID) const; |
| 436 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 437 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 438 | /// implementation for the __strong or __weak case. |
| 439 | /// |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 440 | llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI, |
| 441 | bool ForStrongLayout); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 442 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 443 | void BuildAggrIvarLayout(const ObjCInterfaceDecl *OI, |
| 444 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 445 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 446 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 447 | unsigned int BytePos, bool ForStrongLayout, |
| 448 | int &Index, int &SkIndex, bool &HasUnion); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 449 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 450 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 451 | /// ivar layout bitmap. |
| 452 | llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident, |
| 453 | const ObjCCommonTypesHelper &ObjCTypes); |
| 454 | |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 455 | const RecordDecl *GetFirstIvarInRecord(const ObjCInterfaceDecl *OID, |
| 456 | RecordDecl::field_iterator &FIV, |
| 457 | RecordDecl::field_iterator &PIV); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 458 | /// EmitPropertyList - Emit the given property list. The return |
| 459 | /// value has type PropertyListPtrTy. |
| 460 | llvm::Constant *EmitPropertyList(const std::string &Name, |
| 461 | const Decl *Container, |
| 462 | const ObjCContainerDecl *OCD, |
| 463 | const ObjCCommonTypesHelper &ObjCTypes); |
| 464 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 465 | /// GetProtocolRef - Return a reference to the internal protocol |
| 466 | /// description, creating an empty one if it has not been |
| 467 | /// defined. The return value has type ProtocolPtrTy. |
| 468 | llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD); |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 469 | |
| 470 | /// GetIvarBaseOffset - returns ivars byte offset. |
| 471 | uint64_t GetIvarBaseOffset(const llvm::StructLayout *Layout, |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 472 | const FieldDecl *Field); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 473 | |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 474 | /// GetFieldBaseOffset - return's field byte offset. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 475 | uint64_t GetFieldBaseOffset(const ObjCInterfaceDecl *OI, |
| 476 | const llvm::StructLayout *Layout, |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 477 | const FieldDecl *Field); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 478 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 479 | /// CreateMetadataVar - Create a global variable with internal |
| 480 | /// linkage for use by the Objective-C runtime. |
| 481 | /// |
| 482 | /// This is a convenience wrapper which not only creates the |
| 483 | /// variable, but also sets the section and alignment and adds the |
| 484 | /// global to the UsedGlobals list. |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 485 | /// |
| 486 | /// \param Name - The variable name. |
| 487 | /// \param Init - The variable initializer; this is also used to |
| 488 | /// define the type of the variable. |
| 489 | /// \param Section - The section the variable should go into, or 0. |
| 490 | /// \param Align - The alignment for the variable, or 0. |
| 491 | /// \param AddToUsed - Whether the variable should be added to |
Daniel Dunbar | c158306 | 2009-04-14 17:42:51 +0000 | [diff] [blame] | 492 | /// "llvm.used". |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 493 | llvm::GlobalVariable *CreateMetadataVar(const std::string &Name, |
| 494 | llvm::Constant *Init, |
| 495 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 496 | unsigned Align, |
| 497 | bool AddToUsed); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 498 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 499 | public: |
| 500 | CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm) |
| 501 | { } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 502 | |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 503 | virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *SL); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 504 | |
| 505 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
| 506 | const ObjCContainerDecl *CD=0); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 507 | |
| 508 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); |
| 509 | |
| 510 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 511 | /// declaration, emitting it if necessary. The return value has type |
| 512 | /// ProtocolPtrTy. |
| 513 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0; |
| 514 | |
| 515 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 516 | /// object for the given declaration, emitting it if needed. These |
| 517 | /// forward references will be filled in with empty bodies if no |
| 518 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 519 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 520 | }; |
| 521 | |
| 522 | class CGObjCMac : public CGObjCCommonMac { |
| 523 | private: |
| 524 | ObjCTypesHelper ObjCTypes; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 525 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 526 | /// level information. |
| 527 | void EmitImageInfo(); |
| 528 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 529 | /// EmitModuleInfo - Another marker encoding module level |
| 530 | /// information. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 531 | void EmitModuleInfo(); |
| 532 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 533 | /// EmitModuleSymols - Emit module symbols, the list of defined |
| 534 | /// classes and categories. The result has type SymtabPtrTy. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 535 | llvm::Constant *EmitModuleSymbols(); |
| 536 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 537 | /// FinishModule - Write out global data structures at the end of |
| 538 | /// processing a translation unit. |
| 539 | void FinishModule(); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 540 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 541 | /// EmitClassExtension - Generate the class extension structure used |
| 542 | /// to store the weak ivar layout and properties. The return value |
| 543 | /// has type ClassExtensionPtrTy. |
| 544 | llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID); |
| 545 | |
| 546 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 547 | /// for the given class. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 548 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 549 | const ObjCInterfaceDecl *ID); |
| 550 | |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 551 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 552 | QualType ResultType, |
| 553 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 554 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 555 | QualType Arg0Ty, |
| 556 | bool IsSuper, |
| 557 | const CallArgList &CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 558 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 559 | /// EmitIvarList - Emit the ivar list for the given |
| 560 | /// implementation. If ForClass is true the list of class ivars |
| 561 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 562 | /// interface ivars will be emitted. The return value has type |
| 563 | /// IvarListPtrTy. |
| 564 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 565 | bool ForClass); |
| 566 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 567 | /// EmitMetaClass - Emit a forward reference to the class structure |
| 568 | /// for the metaclass of the given interface. The return value has |
| 569 | /// type ClassPtrTy. |
| 570 | llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID); |
| 571 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 572 | /// EmitMetaClass - Emit a class structure for the metaclass of the |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 573 | /// given implementation. The return value has type ClassPtrTy. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 574 | llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID, |
| 575 | llvm::Constant *Protocols, |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 576 | const llvm::Type *InterfaceTy, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 577 | const ConstantVector &Methods); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 578 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 579 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 580 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 581 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 582 | |
| 583 | /// EmitMethodList - Emit the method list for the given |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 584 | /// implementation. The return value has type MethodListPtrTy. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 585 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 586 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 587 | const ConstantVector &Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 588 | |
| 589 | /// EmitMethodDescList - Emit a method description list for a list of |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 590 | /// method declarations. |
| 591 | /// - TypeName: The name for the type containing the methods. |
| 592 | /// - IsProtocol: True iff these methods are for a protocol. |
| 593 | /// - ClassMethds: True iff these are class methods. |
| 594 | /// - Required: When true, only "required" methods are |
| 595 | /// listed. Similarly, when false only "optional" methods are |
| 596 | /// listed. For classes this should always be true. |
| 597 | /// - begin, end: The method list to output. |
| 598 | /// |
| 599 | /// The return value has type MethodDescriptionListPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 600 | llvm::Constant *EmitMethodDescList(const std::string &Name, |
| 601 | const char *Section, |
| 602 | const ConstantVector &Methods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 603 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 604 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 605 | /// declaration, emitting it if necessary. The return value has type |
| 606 | /// ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 607 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 608 | |
| 609 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 610 | /// object for the given declaration, emitting it if needed. These |
| 611 | /// forward references will be filled in with empty bodies if no |
| 612 | /// definition is seen. The return value has type ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 613 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 614 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 615 | /// EmitProtocolExtension - Generate the protocol extension |
| 616 | /// structure used to store optional instance and class methods, and |
| 617 | /// protocol properties. The return value has type |
| 618 | /// ProtocolExtensionPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 619 | llvm::Constant * |
| 620 | EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 621 | const ConstantVector &OptInstanceMethods, |
| 622 | const ConstantVector &OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 623 | |
| 624 | /// EmitProtocolList - Generate the list of referenced |
| 625 | /// protocols. The return value has type ProtocolListPtrTy. |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 626 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 627 | ObjCProtocolDecl::protocol_iterator begin, |
| 628 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 629 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 630 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 631 | /// for the given selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 632 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 633 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 634 | public: |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 635 | CGObjCMac(CodeGen::CodeGenModule &cgm); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 636 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 637 | virtual llvm::Function *ModuleInitFunction(); |
| 638 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 639 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 640 | QualType ResultType, |
| 641 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 642 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 643 | bool IsClassMessage, |
| 644 | const CallArgList &CallArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 645 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 646 | virtual CodeGen::RValue |
| 647 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 648 | QualType ResultType, |
| 649 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 650 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 651 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 652 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 653 | bool IsClassMessage, |
| 654 | const CallArgList &CallArgs); |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 655 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 656 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 657 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 658 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 659 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 660 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 661 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 662 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 663 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 664 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 665 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 666 | const ObjCProtocolDecl *PD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 667 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 668 | virtual llvm::Constant *GetPropertyGetFunction(); |
| 669 | virtual llvm::Constant *GetPropertySetFunction(); |
| 670 | virtual llvm::Constant *EnumerationMutationFunction(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 671 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 672 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 673 | const Stmt &S); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 674 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 675 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 676 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 677 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 678 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 679 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 680 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 681 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 682 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 683 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 684 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 685 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 686 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 687 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 688 | QualType ObjectTy, |
| 689 | llvm::Value *BaseValue, |
| 690 | const ObjCIvarDecl *Ivar, |
| 691 | const FieldDecl *Field, |
| 692 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 693 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
| 694 | ObjCInterfaceDecl *Interface, |
| 695 | const ObjCIvarDecl *Ivar); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 696 | }; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 697 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 698 | class CGObjCNonFragileABIMac : public CGObjCCommonMac { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 699 | private: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 700 | ObjCNonFragileABITypesHelper ObjCTypes; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 701 | llvm::GlobalVariable* ObjCEmptyCacheVar; |
| 702 | llvm::GlobalVariable* ObjCEmptyVtableVar; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 703 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 704 | /// MetaClassReferences - uniqued meta class references. |
| 705 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 706 | |
| 707 | /// EHTypeReferences - uniqued class ehtype references. |
| 708 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 709 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 710 | /// FinishNonFragileABIModule - Write out global data structures at the end of |
| 711 | /// processing a translation unit. |
| 712 | void FinishNonFragileABIModule(); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 713 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 714 | llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags, |
| 715 | unsigned InstanceStart, |
| 716 | unsigned InstanceSize, |
| 717 | const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 718 | llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName, |
| 719 | llvm::Constant *IsAGV, |
| 720 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 721 | llvm::Constant *ClassRoGV, |
| 722 | bool HiddenVisibility); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 723 | |
| 724 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
| 725 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 726 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
| 727 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 728 | /// EmitMethodList - Emit the method list for the given |
| 729 | /// implementation. The return value has type MethodListnfABITy. |
| 730 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 731 | const char *Section, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 732 | const ConstantVector &Methods); |
| 733 | /// EmitIvarList - Emit the ivar list for the given |
| 734 | /// implementation. If ForClass is true the list of class ivars |
| 735 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 736 | /// interface ivars will be emitted. The return value has type |
| 737 | /// IvarListnfABIPtrTy. |
| 738 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 739 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 740 | llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 741 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 742 | unsigned long int offset); |
| 743 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 744 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 745 | /// declaration, emitting it if necessary. The return value has type |
| 746 | /// ProtocolPtrTy. |
| 747 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
| 748 | |
| 749 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 750 | /// object for the given declaration, emitting it if needed. These |
| 751 | /// forward references will be filled in with empty bodies if no |
| 752 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 753 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
| 754 | |
| 755 | /// EmitProtocolList - Generate the list of referenced |
| 756 | /// protocols. The return value has type ProtocolListPtrTy. |
| 757 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 758 | ObjCProtocolDecl::protocol_iterator begin, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 759 | ObjCProtocolDecl::protocol_iterator end); |
| 760 | |
| 761 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 762 | QualType ResultType, |
| 763 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 764 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 765 | QualType Arg0Ty, |
| 766 | bool IsSuper, |
| 767 | const CallArgList &CallArgs); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 768 | |
| 769 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 770 | /// class of the given name. |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 771 | llvm::GlobalVariable *GetClassGlobal(const std::string &Name); |
| 772 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 773 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 774 | /// for the given class. |
| 775 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 776 | const ObjCInterfaceDecl *ID, |
| 777 | bool IsSuper = false); |
| 778 | |
| 779 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 780 | /// meta-data |
| 781 | llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder, |
| 782 | const ObjCInterfaceDecl *ID); |
| 783 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 784 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 785 | /// the given ivar. |
| 786 | /// |
| 787 | llvm::GlobalVariable * ObjCIvarOffsetVariable(std::string &Name, |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 788 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 789 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 790 | |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 791 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 792 | /// for the given selector. |
| 793 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 794 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 795 | /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 796 | /// interface. The return value has type EHTypePtrTy. |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 797 | llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
| 798 | bool ForDefinition); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 799 | |
| 800 | const char *getMetaclassSymbolPrefix() const { |
| 801 | return "OBJC_METACLASS_$_"; |
| 802 | } |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 803 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 804 | const char *getClassSymbolPrefix() const { |
| 805 | return "OBJC_CLASS_$_"; |
| 806 | } |
| 807 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 808 | public: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 809 | CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 810 | // FIXME. All stubs for now! |
| 811 | virtual llvm::Function *ModuleInitFunction(); |
| 812 | |
| 813 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
| 814 | QualType ResultType, |
| 815 | Selector Sel, |
| 816 | llvm::Value *Receiver, |
| 817 | bool IsClassMessage, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 818 | const CallArgList &CallArgs); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 819 | |
| 820 | virtual CodeGen::RValue |
| 821 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 822 | QualType ResultType, |
| 823 | Selector Sel, |
| 824 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 825 | bool isCategoryImpl, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 826 | llvm::Value *Receiver, |
| 827 | bool IsClassMessage, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 828 | const CallArgList &CallArgs); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 829 | |
| 830 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 831 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 832 | |
| 833 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel) |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 834 | { return EmitSelector(Builder, Sel); } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 835 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 836 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 837 | |
| 838 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 839 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 840 | const ObjCProtocolDecl *PD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 841 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 842 | virtual llvm::Constant *GetPropertyGetFunction() { |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 843 | return ObjCTypes.GetPropertyFn; |
| 844 | } |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 845 | virtual llvm::Constant *GetPropertySetFunction() { |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 846 | return ObjCTypes.SetPropertyFn; |
| 847 | } |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 848 | virtual llvm::Constant *EnumerationMutationFunction() { |
Daniel Dunbar | 28ed084 | 2009-02-16 18:48:45 +0000 | [diff] [blame] | 849 | return ObjCTypes.EnumerationMutationFn; |
| 850 | } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 851 | |
| 852 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 853 | const Stmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 854 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 855 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 856 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 857 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 858 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 859 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 860 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 861 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 862 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 863 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 864 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 865 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 866 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 867 | QualType ObjectTy, |
| 868 | llvm::Value *BaseValue, |
| 869 | const ObjCIvarDecl *Ivar, |
| 870 | const FieldDecl *Field, |
| 871 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 872 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
| 873 | ObjCInterfaceDecl *Interface, |
| 874 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 875 | }; |
| 876 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 877 | } // end anonymous namespace |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 878 | |
| 879 | /* *** Helper Functions *** */ |
| 880 | |
| 881 | /// getConstantGEP() - Help routine to construct simple GEPs. |
| 882 | static llvm::Constant *getConstantGEP(llvm::Constant *C, |
| 883 | unsigned idx0, |
| 884 | unsigned idx1) { |
| 885 | llvm::Value *Idxs[] = { |
| 886 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0), |
| 887 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1) |
| 888 | }; |
| 889 | return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2); |
| 890 | } |
| 891 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 892 | /// hasObjCExceptionAttribute - Return true if this class or any super |
| 893 | /// class has the __objc_exception__ attribute. |
| 894 | static bool hasObjCExceptionAttribute(const ObjCInterfaceDecl *OID) { |
Daniel Dunbar | b11fa0d | 2009-04-13 21:08:27 +0000 | [diff] [blame] | 895 | if (OID->hasAttr<ObjCExceptionAttr>()) |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 896 | return true; |
| 897 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
| 898 | return hasObjCExceptionAttribute(Super); |
| 899 | return false; |
| 900 | } |
| 901 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 902 | /* *** CGObjCMac Public Interface *** */ |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 903 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 904 | CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm), |
| 905 | ObjCTypes(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 906 | { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 907 | ObjCABI = 1; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 908 | EmitImageInfo(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 909 | } |
| 910 | |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 911 | /// GetClass - Return a reference to the class for the given interface |
| 912 | /// decl. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 913 | llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 914 | const ObjCInterfaceDecl *ID) { |
| 915 | return EmitClassRef(Builder, ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | /// GetSelector - Return the pointer to the unique'd string for this selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 919 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 920 | return EmitSelector(Builder, Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 921 | } |
| 922 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 923 | /// Generate a constant CFString object. |
| 924 | /* |
| 925 | struct __builtin_CFString { |
| 926 | const int *isa; // point to __CFConstantStringClassReference |
| 927 | int flags; |
| 928 | const char *str; |
| 929 | long length; |
| 930 | }; |
| 931 | */ |
| 932 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 933 | llvm::Constant *CGObjCCommonMac::GenerateConstantString( |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 934 | const ObjCStringLiteral *SL) { |
Steve Naroff | 8d4141f | 2009-04-01 13:55:36 +0000 | [diff] [blame] | 935 | return CGM.GetAddrOfConstantCFString(SL->getString()); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | /// Generates a message send where the super is the receiver. This is |
| 939 | /// a message send to self with special delivery semantics indicating |
| 940 | /// which class's method should be called. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 941 | CodeGen::RValue |
| 942 | CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 943 | QualType ResultType, |
| 944 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 945 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 946 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 947 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 948 | bool IsClassMessage, |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 949 | const CodeGen::CallArgList &CallArgs) { |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 950 | // Create and init a super structure; this is a (receiver, class) |
| 951 | // pair we will pass to objc_msgSendSuper. |
| 952 | llvm::Value *ObjCSuper = |
| 953 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 954 | llvm::Value *ReceiverAsObject = |
| 955 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 956 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 957 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 958 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 959 | // If this is a class message the metaclass is passed as the target. |
| 960 | llvm::Value *Target; |
| 961 | if (IsClassMessage) { |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 962 | if (isCategoryImpl) { |
| 963 | // Message sent to 'super' in a class method defined in a category |
| 964 | // implementation requires an odd treatment. |
| 965 | // If we are in a class method, we must retrieve the |
| 966 | // _metaclass_ for the current class, pointed at by |
| 967 | // the class's "isa" pointer. The following assumes that |
| 968 | // isa" is the first ivar in a class (which it must be). |
| 969 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 970 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 971 | Target = CGF.Builder.CreateLoad(Target); |
| 972 | } |
| 973 | else { |
| 974 | llvm::Value *MetaClassPtr = EmitMetaClassRef(Class); |
| 975 | llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1); |
| 976 | llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr); |
| 977 | Target = Super; |
| 978 | } |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 979 | } else { |
| 980 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 981 | } |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 982 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext |
| 983 | // and ObjCTypes types. |
| 984 | const llvm::Type *ClassTy = |
| 985 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 986 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 987 | CGF.Builder.CreateStore(Target, |
| 988 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
| 989 | |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 990 | return EmitMessageSend(CGF, ResultType, Sel, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 991 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 992 | true, CallArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 993 | } |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 994 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 995 | /// Generate code for a message send expression. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 996 | CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 997 | QualType ResultType, |
| 998 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 999 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1000 | bool IsClassMessage, |
| 1001 | const CallArgList &CallArgs) { |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1002 | llvm::Value *Arg0 = |
| 1003 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp"); |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1004 | return EmitMessageSend(CGF, ResultType, Sel, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1005 | Arg0, CGF.getContext().getObjCIdType(), |
| 1006 | false, CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1010 | QualType ResultType, |
| 1011 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1012 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1013 | QualType Arg0Ty, |
| 1014 | bool IsSuper, |
| 1015 | const CallArgList &CallArgs) { |
| 1016 | CallArgList ActualArgs; |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 1017 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
| 1018 | ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder, |
| 1019 | Sel)), |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1020 | CGF.getContext().getObjCSelType())); |
| 1021 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1022 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1023 | CodeGenTypes &Types = CGM.getTypes(); |
| 1024 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs); |
| 1025 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1026 | |
| 1027 | llvm::Constant *Fn; |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1028 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1029 | Fn = ObjCTypes.getSendStretFn(IsSuper); |
| 1030 | } else if (ResultType->isFloatingType()) { |
| 1031 | // FIXME: Sadly, this is wrong. This actually depends on the |
| 1032 | // architecture. This happens to be right for x86-32 though. |
| 1033 | Fn = ObjCTypes.getSendFpretFn(IsSuper); |
| 1034 | } else { |
| 1035 | Fn = ObjCTypes.getSendFn(IsSuper); |
| 1036 | } |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 1037 | Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy)); |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1038 | return CGF.EmitCall(FnInfo, Fn, ActualArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1041 | llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1042 | const ObjCProtocolDecl *PD) { |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1043 | // FIXME: I don't understand why gcc generates this, or where it is |
| 1044 | // resolved. Investigate. Its also wasteful to look this up over and |
| 1045 | // over. |
| 1046 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1047 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1048 | return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), |
| 1049 | ObjCTypes.ExternalProtocolPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1052 | void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1053 | // FIXME: We shouldn't need this, the protocol decl should contain |
| 1054 | // enough information to tell us whether this was a declaration or a |
| 1055 | // definition. |
| 1056 | DefinedProtocols.insert(PD->getIdentifier()); |
| 1057 | |
| 1058 | // If we have generated a forward reference to this protocol, emit |
| 1059 | // it now. Otherwise do nothing, the protocol objects are lazily |
| 1060 | // emitted. |
| 1061 | if (Protocols.count(PD->getIdentifier())) |
| 1062 | GetOrEmitProtocol(PD); |
| 1063 | } |
| 1064 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1065 | llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1066 | if (DefinedProtocols.count(PD->getIdentifier())) |
| 1067 | return GetOrEmitProtocol(PD); |
| 1068 | return GetOrEmitProtocolRef(PD); |
| 1069 | } |
| 1070 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1071 | /* |
| 1072 | // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions |
| 1073 | struct _objc_protocol { |
| 1074 | struct _objc_protocol_extension *isa; |
| 1075 | char *protocol_name; |
| 1076 | struct _objc_protocol_list *protocol_list; |
| 1077 | struct _objc__method_prototype_list *instance_methods; |
| 1078 | struct _objc__method_prototype_list *class_methods |
| 1079 | }; |
| 1080 | |
| 1081 | See EmitProtocolExtension(). |
| 1082 | */ |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1083 | llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) { |
| 1084 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1085 | |
| 1086 | // Early exit if a defining object has already been generated. |
| 1087 | if (Entry && Entry->hasInitializer()) |
| 1088 | return Entry; |
| 1089 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1090 | // FIXME: I don't understand why gcc generates this, or where it is |
| 1091 | // resolved. Investigate. Its also wasteful to look this up over and |
| 1092 | // over. |
| 1093 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1094 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1095 | const char *ProtocolName = PD->getNameAsCString(); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1096 | |
| 1097 | // Construct method lists. |
| 1098 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1099 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1100 | for (ObjCProtocolDecl::instmeth_iterator |
| 1101 | i = PD->instmeth_begin(CGM.getContext()), |
| 1102 | e = PD->instmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1103 | ObjCMethodDecl *MD = *i; |
| 1104 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1105 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1106 | OptInstanceMethods.push_back(C); |
| 1107 | } else { |
| 1108 | InstanceMethods.push_back(C); |
| 1109 | } |
| 1110 | } |
| 1111 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1112 | for (ObjCProtocolDecl::classmeth_iterator |
| 1113 | i = PD->classmeth_begin(CGM.getContext()), |
| 1114 | e = PD->classmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1115 | ObjCMethodDecl *MD = *i; |
| 1116 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1117 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1118 | OptClassMethods.push_back(C); |
| 1119 | } else { |
| 1120 | ClassMethods.push_back(C); |
| 1121 | } |
| 1122 | } |
| 1123 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1124 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1125 | Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1126 | Values[1] = GetClassName(PD->getIdentifier()); |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1127 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1128 | EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1129 | PD->protocol_begin(), |
| 1130 | PD->protocol_end()); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1131 | Values[3] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1132 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" |
| 1133 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1134 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1135 | InstanceMethods); |
| 1136 | Values[4] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1137 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" |
| 1138 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1139 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1140 | ClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1141 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 1142 | Values); |
| 1143 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1144 | if (Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1145 | // Already created, fix the linkage and update the initializer. |
| 1146 | Entry->setLinkage(llvm::GlobalValue::InternalLinkage); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1147 | Entry->setInitializer(Init); |
| 1148 | } else { |
| 1149 | Entry = |
| 1150 | new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, |
| 1151 | llvm::GlobalValue::InternalLinkage, |
| 1152 | Init, |
| 1153 | std::string("\01L_OBJC_PROTOCOL_")+ProtocolName, |
| 1154 | &CGM.getModule()); |
| 1155 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1156 | Entry->setAlignment(4); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1157 | UsedGlobals.push_back(Entry); |
| 1158 | // FIXME: Is this necessary? Why only for protocol? |
| 1159 | Entry->setAlignment(4); |
| 1160 | } |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1161 | |
| 1162 | return Entry; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1165 | llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1166 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1167 | |
| 1168 | if (!Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1169 | // We use the initializer as a marker of whether this is a forward |
| 1170 | // reference or not. At module finalization we add the empty |
| 1171 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1172 | Entry = |
| 1173 | new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1174 | llvm::GlobalValue::ExternalLinkage, |
| 1175 | 0, |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1176 | "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1177 | &CGM.getModule()); |
| 1178 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1179 | Entry->setAlignment(4); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1180 | UsedGlobals.push_back(Entry); |
| 1181 | // FIXME: Is this necessary? Why only for protocol? |
| 1182 | Entry->setAlignment(4); |
| 1183 | } |
| 1184 | |
| 1185 | return Entry; |
| 1186 | } |
| 1187 | |
| 1188 | /* |
| 1189 | struct _objc_protocol_extension { |
| 1190 | uint32_t size; |
| 1191 | struct objc_method_description_list *optional_instance_methods; |
| 1192 | struct objc_method_description_list *optional_class_methods; |
| 1193 | struct objc_property_list *instance_properties; |
| 1194 | }; |
| 1195 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1196 | llvm::Constant * |
| 1197 | CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 1198 | const ConstantVector &OptInstanceMethods, |
| 1199 | const ConstantVector &OptClassMethods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1200 | uint64_t Size = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1201 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1202 | std::vector<llvm::Constant*> Values(4); |
| 1203 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1204 | Values[1] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1205 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_" |
| 1206 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1207 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1208 | OptInstanceMethods); |
| 1209 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1210 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" |
| 1211 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1212 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1213 | OptClassMethods); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1214 | Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + |
| 1215 | PD->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1216 | 0, PD, ObjCTypes); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1217 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1218 | // Return null if no extension bits are used. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1219 | if (Values[1]->isNullValue() && Values[2]->isNullValue() && |
| 1220 | Values[3]->isNullValue()) |
| 1221 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
| 1222 | |
| 1223 | llvm::Constant *Init = |
| 1224 | llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1225 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1226 | // No special section, but goes in llvm.used |
| 1227 | return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(), |
| 1228 | Init, |
| 1229 | 0, 0, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | /* |
| 1233 | struct objc_protocol_list { |
| 1234 | struct objc_protocol_list *next; |
| 1235 | long count; |
| 1236 | Protocol *list[]; |
| 1237 | }; |
| 1238 | */ |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1239 | llvm::Constant * |
| 1240 | CGObjCMac::EmitProtocolList(const std::string &Name, |
| 1241 | ObjCProtocolDecl::protocol_iterator begin, |
| 1242 | ObjCProtocolDecl::protocol_iterator end) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1243 | std::vector<llvm::Constant*> ProtocolRefs; |
| 1244 | |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1245 | for (; begin != end; ++begin) |
| 1246 | ProtocolRefs.push_back(GetProtocolRef(*begin)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1247 | |
| 1248 | // Just return null for empty protocol lists |
| 1249 | if (ProtocolRefs.empty()) |
| 1250 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1251 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1252 | // This list is null terminated. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1253 | ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy)); |
| 1254 | |
| 1255 | std::vector<llvm::Constant*> Values(3); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1256 | // This field is only used by the runtime. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1257 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1258 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
| 1259 | Values[2] = |
| 1260 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy, |
| 1261 | ProtocolRefs.size()), |
| 1262 | ProtocolRefs); |
| 1263 | |
| 1264 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1265 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1266 | CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1267 | 4, false); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1268 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy); |
| 1269 | } |
| 1270 | |
| 1271 | /* |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1272 | struct _objc_property { |
| 1273 | const char * const name; |
| 1274 | const char * const attributes; |
| 1275 | }; |
| 1276 | |
| 1277 | struct _objc_property_list { |
| 1278 | uint32_t entsize; // sizeof (struct _objc_property) |
| 1279 | uint32_t prop_count; |
| 1280 | struct _objc_property[prop_count]; |
| 1281 | }; |
| 1282 | */ |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1283 | llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name, |
| 1284 | const Decl *Container, |
| 1285 | const ObjCContainerDecl *OCD, |
| 1286 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1287 | std::vector<llvm::Constant*> Properties, Prop(2); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1288 | for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(CGM.getContext()), |
| 1289 | E = OCD->prop_end(CGM.getContext()); I != E; ++I) { |
Steve Naroff | 93983f8 | 2009-01-11 12:47:58 +0000 | [diff] [blame] | 1290 | const ObjCPropertyDecl *PD = *I; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1291 | Prop[0] = GetPropertyName(PD->getIdentifier()); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1292 | Prop[1] = GetPropertyTypeString(PD, Container); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1293 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, |
| 1294 | Prop)); |
| 1295 | } |
| 1296 | |
| 1297 | // Return null for empty list. |
| 1298 | if (Properties.empty()) |
| 1299 | return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 1300 | |
| 1301 | unsigned PropertySize = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1302 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1303 | std::vector<llvm::Constant*> Values(3); |
| 1304 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize); |
| 1305 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size()); |
| 1306 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy, |
| 1307 | Properties.size()); |
| 1308 | Values[2] = llvm::ConstantArray::get(AT, Properties); |
| 1309 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1310 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1311 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1312 | CreateMetadataVar(Name, Init, |
| 1313 | (ObjCABI == 2) ? "__DATA, __objc_const" : |
| 1314 | "__OBJC,__property,regular,no_dead_strip", |
| 1315 | (ObjCABI == 2) ? 8 : 4, |
| 1316 | true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1317 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | /* |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1321 | struct objc_method_description_list { |
| 1322 | int count; |
| 1323 | struct objc_method_description list[]; |
| 1324 | }; |
| 1325 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1326 | llvm::Constant * |
| 1327 | CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 1328 | std::vector<llvm::Constant*> Desc(2); |
| 1329 | Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 1330 | ObjCTypes.SelectorPtrTy); |
| 1331 | Desc[1] = GetMethodVarType(MD); |
| 1332 | return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy, |
| 1333 | Desc); |
| 1334 | } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1335 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1336 | llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name, |
| 1337 | const char *Section, |
| 1338 | const ConstantVector &Methods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1339 | // Return null for empty list. |
| 1340 | if (Methods.empty()) |
| 1341 | return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
| 1342 | |
| 1343 | std::vector<llvm::Constant*> Values(2); |
| 1344 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 1345 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy, |
| 1346 | Methods.size()); |
| 1347 | Values[1] = llvm::ConstantArray::get(AT, Methods); |
| 1348 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1349 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1350 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1351 | return llvm::ConstantExpr::getBitCast(GV, |
| 1352 | ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1355 | /* |
| 1356 | struct _objc_category { |
| 1357 | char *category_name; |
| 1358 | char *class_name; |
| 1359 | struct _objc_method_list *instance_methods; |
| 1360 | struct _objc_method_list *class_methods; |
| 1361 | struct _objc_protocol_list *protocols; |
| 1362 | uint32_t size; // <rdar://4585769> |
| 1363 | struct _objc_property_list *instance_properties; |
| 1364 | }; |
| 1365 | */ |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1366 | void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1367 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1368 | |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1369 | // FIXME: This is poor design, the OCD should have a pointer to the |
| 1370 | // category decl. Additionally, note that Category can be null for |
| 1371 | // the @implementation w/o an @interface case. Sema should just |
| 1372 | // create one for us as it does for @implementation so everyone else |
| 1373 | // can live life under a clear blue sky. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1374 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1375 | const ObjCCategoryDecl *Category = |
| 1376 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1377 | std::string ExtName(Interface->getNameAsString() + "_" + |
| 1378 | OCD->getNameAsString()); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1379 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1380 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1381 | for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(), |
| 1382 | e = OCD->instmeth_end(); i != e; ++i) { |
| 1383 | // Instance methods should always be defined. |
| 1384 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1385 | } |
| 1386 | for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(), |
| 1387 | e = OCD->classmeth_end(); i != e; ++i) { |
| 1388 | // Class methods should always be defined. |
| 1389 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1390 | } |
| 1391 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1392 | std::vector<llvm::Constant*> Values(7); |
| 1393 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 1394 | Values[1] = GetClassName(Interface->getIdentifier()); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1395 | Values[2] = |
| 1396 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") + |
| 1397 | ExtName, |
| 1398 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1399 | InstanceMethods); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1400 | Values[3] = |
| 1401 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName, |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1402 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1403 | ClassMethods); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1404 | if (Category) { |
| 1405 | Values[4] = |
| 1406 | EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName, |
| 1407 | Category->protocol_begin(), |
| 1408 | Category->protocol_end()); |
| 1409 | } else { |
| 1410 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1411 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1412 | Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1413 | |
| 1414 | // If there is no category @interface then there can be no properties. |
| 1415 | if (Category) { |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1416 | Values[6] = EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName, |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1417 | OCD, Category, ObjCTypes); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1418 | } else { |
| 1419 | Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 1420 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1421 | |
| 1422 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy, |
| 1423 | Values); |
| 1424 | |
| 1425 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1426 | CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init, |
| 1427 | "__OBJC,__category,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1428 | 4, true); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1429 | DefinedCategories.push_back(GV); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1432 | // FIXME: Get from somewhere? |
| 1433 | enum ClassFlags { |
| 1434 | eClassFlags_Factory = 0x00001, |
| 1435 | eClassFlags_Meta = 0x00002, |
| 1436 | // <rdr://5142207> |
| 1437 | eClassFlags_HasCXXStructors = 0x02000, |
| 1438 | eClassFlags_Hidden = 0x20000, |
| 1439 | eClassFlags_ABI2_Hidden = 0x00010, |
| 1440 | eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634> |
| 1441 | }; |
| 1442 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1443 | /* |
| 1444 | struct _objc_class { |
| 1445 | Class isa; |
| 1446 | Class super_class; |
| 1447 | const char *name; |
| 1448 | long version; |
| 1449 | long info; |
| 1450 | long instance_size; |
| 1451 | struct _objc_ivar_list *ivars; |
| 1452 | struct _objc_method_list *methods; |
| 1453 | struct _objc_cache *cache; |
| 1454 | struct _objc_protocol_list *protocols; |
| 1455 | // Objective-C 1.0 extensions (<rdr://4585769>) |
| 1456 | const char *ivar_layout; |
| 1457 | struct _objc_class_ext *ext; |
| 1458 | }; |
| 1459 | |
| 1460 | See EmitClassExtension(); |
| 1461 | */ |
| 1462 | void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1463 | DefinedSymbols.insert(ID->getIdentifier()); |
| 1464 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1465 | std::string ClassName = ID->getNameAsString(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1466 | // FIXME: Gross |
| 1467 | ObjCInterfaceDecl *Interface = |
| 1468 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1469 | llvm::Constant *Protocols = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1470 | EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1471 | Interface->protocol_begin(), |
| 1472 | Interface->protocol_end()); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1473 | const llvm::Type *InterfaceTy = |
Chris Lattner | 03d9f34 | 2009-04-01 06:23:52 +0000 | [diff] [blame] | 1474 | CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1475 | unsigned Flags = eClassFlags_Factory; |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1476 | unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1477 | |
| 1478 | // FIXME: Set CXX-structors flag. |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 1479 | if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1480 | Flags |= eClassFlags_Hidden; |
| 1481 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1482 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1483 | for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(), |
| 1484 | e = ID->instmeth_end(); i != e; ++i) { |
| 1485 | // Instance methods should always be defined. |
| 1486 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1487 | } |
| 1488 | for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(), |
| 1489 | e = ID->classmeth_end(); i != e; ++i) { |
| 1490 | // Class methods should always be defined. |
| 1491 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1492 | } |
| 1493 | |
| 1494 | for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(), |
| 1495 | e = ID->propimpl_end(); i != e; ++i) { |
| 1496 | ObjCPropertyImplDecl *PID = *i; |
| 1497 | |
| 1498 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 1499 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 1500 | |
| 1501 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 1502 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 1503 | InstanceMethods.push_back(C); |
| 1504 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 1505 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 1506 | InstanceMethods.push_back(C); |
| 1507 | } |
| 1508 | } |
| 1509 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1510 | std::vector<llvm::Constant*> Values(12); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1511 | Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1512 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1513 | // Record a reference to the super class. |
| 1514 | LazySymbols.insert(Super->getIdentifier()); |
| 1515 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1516 | Values[ 1] = |
| 1517 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 1518 | ObjCTypes.ClassPtrTy); |
| 1519 | } else { |
| 1520 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
| 1521 | } |
| 1522 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 1523 | // Version is always 0. |
| 1524 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 1525 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 1526 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1527 | Values[ 6] = EmitIvarList(ID, false); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1528 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1529 | EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1530 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1531 | InstanceMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1532 | // cache is always NULL. |
| 1533 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
| 1534 | Values[ 9] = Protocols; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1535 | // FIXME: Set ivar_layout |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 1536 | // Values[10] = BuildIvarLayout(ID, true); |
| 1537 | Values[10] = GetIvarLayoutName(0, ObjCTypes); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1538 | Values[11] = EmitClassExtension(ID); |
| 1539 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 1540 | Values); |
| 1541 | |
| 1542 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1543 | CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init, |
| 1544 | "__OBJC,__class,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1545 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1546 | DefinedClasses.push_back(GV); |
| 1547 | } |
| 1548 | |
| 1549 | llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID, |
| 1550 | llvm::Constant *Protocols, |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1551 | const llvm::Type *InterfaceTy, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1552 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1553 | unsigned Flags = eClassFlags_Meta; |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1554 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1555 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 1556 | if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1557 | Flags |= eClassFlags_Hidden; |
| 1558 | |
| 1559 | std::vector<llvm::Constant*> Values(12); |
| 1560 | // The isa for the metaclass is the root of the hierarchy. |
| 1561 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 1562 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 1563 | Root = Super; |
| 1564 | Values[ 0] = |
| 1565 | llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()), |
| 1566 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1567 | // The super class for the metaclass is emitted as the name of the |
| 1568 | // super class. The runtime fixes this up to point to the |
| 1569 | // *metaclass* for the super class. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1570 | if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) { |
| 1571 | Values[ 1] = |
| 1572 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 1573 | ObjCTypes.ClassPtrTy); |
| 1574 | } else { |
| 1575 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
| 1576 | } |
| 1577 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 1578 | // Version is always 0. |
| 1579 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 1580 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 1581 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1582 | Values[ 6] = EmitIvarList(ID, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1583 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1584 | EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1585 | "__OBJC,__cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1586 | Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1587 | // cache is always NULL. |
| 1588 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
| 1589 | Values[ 9] = Protocols; |
| 1590 | // ivar_layout for metaclass is always NULL. |
| 1591 | Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 1592 | // The class extension is always unused for metaclasses. |
| 1593 | Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
| 1594 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 1595 | Values); |
| 1596 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1597 | std::string Name("\01L_OBJC_METACLASS_"); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1598 | Name += ID->getNameAsCString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1599 | |
| 1600 | // Check for a forward reference. |
| 1601 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 1602 | if (GV) { |
| 1603 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 1604 | "Forward metaclass reference has incorrect type."); |
| 1605 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 1606 | GV->setInitializer(Init); |
| 1607 | } else { |
| 1608 | GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 1609 | llvm::GlobalValue::InternalLinkage, |
| 1610 | Init, Name, |
| 1611 | &CGM.getModule()); |
| 1612 | } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1613 | GV->setSection("__OBJC,__meta_class,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1614 | GV->setAlignment(4); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1615 | UsedGlobals.push_back(GV); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1616 | |
| 1617 | return GV; |
| 1618 | } |
| 1619 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1620 | llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1621 | std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1622 | |
| 1623 | // FIXME: Should we look these up somewhere other than the |
| 1624 | // module. Its a bit silly since we only generate these while |
| 1625 | // processing an implementation, so exactly one pointer would work |
| 1626 | // if know when we entered/exitted an implementation block. |
| 1627 | |
| 1628 | // Check for an existing forward reference. |
Fariborz Jahanian | b0d2794 | 2009-01-07 20:11:22 +0000 | [diff] [blame] | 1629 | // Previously, metaclass with internal linkage may have been defined. |
| 1630 | // pass 'true' as 2nd argument so it is returned. |
| 1631 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) { |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1632 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 1633 | "Forward metaclass reference has incorrect type."); |
| 1634 | return GV; |
| 1635 | } else { |
| 1636 | // Generate as an external reference to keep a consistent |
| 1637 | // module. This will be patched up when we emit the metaclass. |
| 1638 | return new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 1639 | llvm::GlobalValue::ExternalLinkage, |
| 1640 | 0, |
| 1641 | Name, |
| 1642 | &CGM.getModule()); |
| 1643 | } |
| 1644 | } |
| 1645 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1646 | /* |
| 1647 | struct objc_class_ext { |
| 1648 | uint32_t size; |
| 1649 | const char *weak_ivar_layout; |
| 1650 | struct _objc_property_list *properties; |
| 1651 | }; |
| 1652 | */ |
| 1653 | llvm::Constant * |
| 1654 | CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) { |
| 1655 | uint64_t Size = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1656 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1657 | |
| 1658 | std::vector<llvm::Constant*> Values(3); |
| 1659 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1660 | // FIXME: Output weak_ivar_layout string. |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 1661 | // Values[1] = BuildIvarLayout(ID, false); |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 1662 | Values[1] = GetIvarLayoutName(0, ObjCTypes); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1663 | Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1664 | ID, ID->getClassInterface(), ObjCTypes); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1665 | |
| 1666 | // Return null if no extension bits are used. |
| 1667 | if (Values[1]->isNullValue() && Values[2]->isNullValue()) |
| 1668 | return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
| 1669 | |
| 1670 | llvm::Constant *Init = |
| 1671 | llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1672 | return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1673 | Init, "__OBJC,__class_ext,regular,no_dead_strip", |
| 1674 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
Fariborz Jahanian | f54b194 | 2009-01-17 19:36:33 +0000 | [diff] [blame] | 1677 | /// countInheritedIvars - count number of ivars in class and its super class(s) |
| 1678 | /// |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1679 | static int countInheritedIvars(const ObjCInterfaceDecl *OI, |
| 1680 | ASTContext &Context) { |
Fariborz Jahanian | f54b194 | 2009-01-17 19:36:33 +0000 | [diff] [blame] | 1681 | int count = 0; |
| 1682 | if (!OI) |
| 1683 | return 0; |
| 1684 | const ObjCInterfaceDecl *SuperClass = OI->getSuperClass(); |
| 1685 | if (SuperClass) |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1686 | count += countInheritedIvars(SuperClass, Context); |
Fariborz Jahanian | f54b194 | 2009-01-17 19:36:33 +0000 | [diff] [blame] | 1687 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 1688 | E = OI->ivar_end(); I != E; ++I) |
| 1689 | ++count; |
Fariborz Jahanian | 1819188 | 2009-03-31 18:11:23 +0000 | [diff] [blame] | 1690 | // look into properties. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1691 | for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(Context), |
| 1692 | E = OI->prop_end(Context); I != E; ++I) { |
Fariborz Jahanian | 1819188 | 2009-03-31 18:11:23 +0000 | [diff] [blame] | 1693 | if ((*I)->getPropertyIvarDecl()) |
| 1694 | ++count; |
| 1695 | } |
Fariborz Jahanian | f54b194 | 2009-01-17 19:36:33 +0000 | [diff] [blame] | 1696 | return count; |
| 1697 | } |
| 1698 | |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 1699 | /// getInterfaceDeclForIvar - Get the interface declaration node where |
| 1700 | /// this ivar is declared in. |
| 1701 | /// FIXME. Ideally, this info should be in the ivar node. But currently |
| 1702 | /// it is not and prevailing wisdom is that ASTs should not have more |
| 1703 | /// info than is absolutely needed, even though this info reflects the |
| 1704 | /// source language. |
| 1705 | /// |
| 1706 | static const ObjCInterfaceDecl *getInterfaceDeclForIvar( |
| 1707 | const ObjCInterfaceDecl *OI, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1708 | const ObjCIvarDecl *IVD, |
| 1709 | ASTContext &Context) { |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 1710 | if (!OI) |
| 1711 | return 0; |
| 1712 | assert(isa<ObjCInterfaceDecl>(OI) && "OI is not an interface"); |
| 1713 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 1714 | E = OI->ivar_end(); I != E; ++I) |
| 1715 | if ((*I)->getIdentifier() == IVD->getIdentifier()) |
| 1716 | return OI; |
Fariborz Jahanian | 5a4b453 | 2009-03-31 17:00:52 +0000 | [diff] [blame] | 1717 | // look into properties. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1718 | for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(Context), |
| 1719 | E = OI->prop_end(Context); I != E; ++I) { |
Fariborz Jahanian | 5a4b453 | 2009-03-31 17:00:52 +0000 | [diff] [blame] | 1720 | ObjCPropertyDecl *PDecl = (*I); |
| 1721 | if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl()) |
| 1722 | if (IV->getIdentifier() == IVD->getIdentifier()) |
| 1723 | return OI; |
| 1724 | } |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1725 | return getInterfaceDeclForIvar(OI->getSuperClass(), IVD, Context); |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1728 | /* |
| 1729 | struct objc_ivar { |
| 1730 | char *ivar_name; |
| 1731 | char *ivar_type; |
| 1732 | int ivar_offset; |
| 1733 | }; |
| 1734 | |
| 1735 | struct objc_ivar_list { |
| 1736 | int ivar_count; |
| 1737 | struct objc_ivar list[count]; |
| 1738 | }; |
| 1739 | */ |
| 1740 | llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1741 | bool ForClass) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1742 | std::vector<llvm::Constant*> Ivars, Ivar(3); |
| 1743 | |
| 1744 | // When emitting the root class GCC emits ivar entries for the |
| 1745 | // actual class structure. It is not clear if we need to follow this |
| 1746 | // behavior; for now lets try and get away with not doing it. If so, |
| 1747 | // the cleanest solution would be to make up an ObjCInterfaceDecl |
| 1748 | // for the class. |
| 1749 | if (ForClass) |
| 1750 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1751 | |
| 1752 | ObjCInterfaceDecl *OID = |
| 1753 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 1754 | const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OID); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1755 | |
| 1756 | RecordDecl::field_iterator ifield, pfield; |
| 1757 | const RecordDecl *RD = GetFirstIvarInRecord(OID, ifield, pfield); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1758 | for (RecordDecl::field_iterator e = RD->field_end(CGM.getContext()); |
| 1759 | ifield != e; ++ifield) { |
Fariborz Jahanian | f54b194 | 2009-01-17 19:36:33 +0000 | [diff] [blame] | 1760 | FieldDecl *Field = *ifield; |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 1761 | uint64_t Offset = GetIvarBaseOffset(Layout, Field); |
Fariborz Jahanian | f54b194 | 2009-01-17 19:36:33 +0000 | [diff] [blame] | 1762 | if (Field->getIdentifier()) |
| 1763 | Ivar[0] = GetMethodVarName(Field->getIdentifier()); |
| 1764 | else |
| 1765 | Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 1766 | Ivar[1] = GetMethodVarType(Field); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1767 | Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1768 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1769 | } |
| 1770 | |
| 1771 | // Return null for empty list. |
| 1772 | if (Ivars.empty()) |
| 1773 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
| 1774 | |
| 1775 | std::vector<llvm::Constant*> Values(2); |
| 1776 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 1777 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy, |
| 1778 | Ivars.size()); |
| 1779 | Values[1] = llvm::ConstantArray::get(AT, Ivars); |
| 1780 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1781 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1782 | llvm::GlobalVariable *GV; |
| 1783 | if (ForClass) |
| 1784 | GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(), |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1785 | Init, "__OBJC,__class_vars,regular,no_dead_strip", |
| 1786 | 4, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1787 | else |
| 1788 | GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" |
| 1789 | + ID->getNameAsString(), |
| 1790 | Init, "__OBJC,__instance_vars,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1791 | 4, true); |
| 1792 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | /* |
| 1796 | struct objc_method { |
| 1797 | SEL method_name; |
| 1798 | char *method_types; |
| 1799 | void *method; |
| 1800 | }; |
| 1801 | |
| 1802 | struct objc_method_list { |
| 1803 | struct objc_method_list *obsolete; |
| 1804 | int count; |
| 1805 | struct objc_method methods_list[count]; |
| 1806 | }; |
| 1807 | */ |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1808 | |
| 1809 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 1810 | /// given method if it has been defined. The result is null if the |
| 1811 | /// method has not been defined. The return value has type MethodPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1812 | llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1813 | // FIXME: Use DenseMap::lookup |
| 1814 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 1815 | if (!Fn) |
| 1816 | return 0; |
| 1817 | |
| 1818 | std::vector<llvm::Constant*> Method(3); |
| 1819 | Method[0] = |
| 1820 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 1821 | ObjCTypes.SelectorPtrTy); |
| 1822 | Method[1] = GetMethodVarType(MD); |
| 1823 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 1824 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 1825 | } |
| 1826 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1827 | llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name, |
| 1828 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1829 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1830 | // Return null for empty list. |
| 1831 | if (Methods.empty()) |
| 1832 | return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy); |
| 1833 | |
| 1834 | std::vector<llvm::Constant*> Values(3); |
| 1835 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 1836 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 1837 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 1838 | Methods.size()); |
| 1839 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 1840 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1841 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1842 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1843 | return llvm::ConstantExpr::getBitCast(GV, |
| 1844 | ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1845 | } |
| 1846 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1847 | llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD, |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 1848 | const ObjCContainerDecl *CD) { |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1849 | std::string Name; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 1850 | GetNameForMethod(OMD, CD, Name); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1851 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1852 | CodeGenTypes &Types = CGM.getTypes(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1853 | const llvm::FunctionType *MethodTy = |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1854 | Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic()); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1855 | llvm::Function *Method = |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1856 | llvm::Function::Create(MethodTy, |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1857 | llvm::GlobalValue::InternalLinkage, |
| 1858 | Name, |
| 1859 | &CGM.getModule()); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1860 | MethodDefinitions.insert(std::make_pair(OMD, Method)); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1861 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1862 | return Method; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 1865 | uint64_t CGObjCCommonMac::GetIvarBaseOffset(const llvm::StructLayout *Layout, |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 1866 | const FieldDecl *Field) { |
Fariborz Jahanian | a6681ae | 2009-03-09 20:44:22 +0000 | [diff] [blame] | 1867 | if (!Field->isBitField()) |
| 1868 | return Layout->getElementOffset( |
| 1869 | CGM.getTypes().getLLVMFieldNo(Field)); |
| 1870 | // FIXME. Must be a better way of getting a bitfield base offset. |
| 1871 | uint64_t offset = CGM.getTypes().getLLVMFieldNo(Field); |
| 1872 | const llvm::Type *Ty = CGM.getTypes().ConvertTypeForMemRecursive(Field->getType()); |
| 1873 | uint64_t size = CGM.getTypes().getTargetData().getTypePaddedSizeInBits(Ty); |
| 1874 | offset = (offset*size)/8; |
| 1875 | return offset; |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 1876 | } |
| 1877 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 1878 | /// GetFieldBaseOffset - return's field byt offset. |
| 1879 | uint64_t CGObjCCommonMac::GetFieldBaseOffset(const ObjCInterfaceDecl *OI, |
| 1880 | const llvm::StructLayout *Layout, |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 1881 | const FieldDecl *Field) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 1882 | const ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field); |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 1883 | const FieldDecl *FD = OI->lookupFieldDeclForIvar(CGM.getContext(), Ivar); |
| 1884 | return GetIvarBaseOffset(Layout, FD); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 1887 | llvm::GlobalVariable * |
| 1888 | CGObjCCommonMac::CreateMetadataVar(const std::string &Name, |
| 1889 | llvm::Constant *Init, |
| 1890 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 1891 | unsigned Align, |
| 1892 | bool AddToUsed) { |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 1893 | const llvm::Type *Ty = Init->getType(); |
| 1894 | llvm::GlobalVariable *GV = |
| 1895 | new llvm::GlobalVariable(Ty, false, |
| 1896 | llvm::GlobalValue::InternalLinkage, |
| 1897 | Init, |
| 1898 | Name, |
| 1899 | &CGM.getModule()); |
| 1900 | if (Section) |
| 1901 | GV->setSection(Section); |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 1902 | if (Align) |
| 1903 | GV->setAlignment(Align); |
| 1904 | if (AddToUsed) |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 1905 | UsedGlobals.push_back(GV); |
| 1906 | return GV; |
| 1907 | } |
| 1908 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1909 | llvm::Function *CGObjCMac::ModuleInitFunction() { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1910 | // Abuse this interface function as a place to finalize. |
| 1911 | FinishModule(); |
| 1912 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1913 | return NULL; |
| 1914 | } |
| 1915 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1916 | llvm::Constant *CGObjCMac::GetPropertyGetFunction() { |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 1917 | return ObjCTypes.GetPropertyFn; |
| 1918 | } |
| 1919 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1920 | llvm::Constant *CGObjCMac::GetPropertySetFunction() { |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 1921 | return ObjCTypes.SetPropertyFn; |
| 1922 | } |
| 1923 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1924 | llvm::Constant *CGObjCMac::EnumerationMutationFunction() { |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 1925 | return ObjCTypes.EnumerationMutationFn; |
| 1926 | } |
| 1927 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1928 | /* |
| 1929 | |
| 1930 | Objective-C setjmp-longjmp (sjlj) Exception Handling |
| 1931 | -- |
| 1932 | |
| 1933 | The basic framework for a @try-catch-finally is as follows: |
| 1934 | { |
| 1935 | objc_exception_data d; |
| 1936 | id _rethrow = null; |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 1937 | bool _call_try_exit = true; |
| 1938 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1939 | objc_exception_try_enter(&d); |
| 1940 | if (!setjmp(d.jmp_buf)) { |
| 1941 | ... try body ... |
| 1942 | } else { |
| 1943 | // exception path |
| 1944 | id _caught = objc_exception_extract(&d); |
| 1945 | |
| 1946 | // enter new try scope for handlers |
| 1947 | if (!setjmp(d.jmp_buf)) { |
| 1948 | ... match exception and execute catch blocks ... |
| 1949 | |
| 1950 | // fell off end, rethrow. |
| 1951 | _rethrow = _caught; |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1952 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1953 | } else { |
| 1954 | // exception in catch block |
| 1955 | _rethrow = objc_exception_extract(&d); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 1956 | _call_try_exit = false; |
| 1957 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1958 | } |
| 1959 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1960 | ... jump-through-finally to finally_end ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1961 | |
| 1962 | finally: |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 1963 | if (_call_try_exit) |
| 1964 | objc_exception_try_exit(&d); |
| 1965 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1966 | ... finally block .... |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1967 | ... dispatch to finally destination ... |
| 1968 | |
| 1969 | finally_rethrow: |
| 1970 | objc_exception_throw(_rethrow); |
| 1971 | |
| 1972 | finally_end: |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1973 | } |
| 1974 | |
| 1975 | This framework differs slightly from the one gcc uses, in that gcc |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1976 | uses _rethrow to determine if objc_exception_try_exit should be called |
| 1977 | and if the object should be rethrown. This breaks in the face of |
| 1978 | throwing nil and introduces unnecessary branches. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1979 | |
| 1980 | We specialize this framework for a few particular circumstances: |
| 1981 | |
| 1982 | - If there are no catch blocks, then we avoid emitting the second |
| 1983 | exception handling context. |
| 1984 | |
| 1985 | - If there is a catch-all catch block (i.e. @catch(...) or @catch(id |
| 1986 | e)) we avoid emitting the code to rethrow an uncaught exception. |
| 1987 | |
| 1988 | - FIXME: If there is no @finally block we can do a few more |
| 1989 | simplifications. |
| 1990 | |
| 1991 | Rethrows and Jumps-Through-Finally |
| 1992 | -- |
| 1993 | |
| 1994 | Support for implicit rethrows and jumping through the finally block is |
| 1995 | handled by storing the current exception-handling context in |
| 1996 | ObjCEHStack. |
| 1997 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1998 | In order to implement proper @finally semantics, we support one basic |
| 1999 | mechanism for jumping through the finally block to an arbitrary |
| 2000 | destination. Constructs which generate exits from a @try or @catch |
| 2001 | block use this mechanism to implement the proper semantics by chaining |
| 2002 | jumps, as necessary. |
| 2003 | |
| 2004 | This mechanism works like the one used for indirect goto: we |
| 2005 | arbitrarily assign an ID to each destination and store the ID for the |
| 2006 | destination in a variable prior to entering the finally block. At the |
| 2007 | end of the finally block we simply create a switch to the proper |
| 2008 | destination. |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2009 | |
| 2010 | Code gen for @synchronized(expr) stmt; |
| 2011 | Effectively generating code for: |
| 2012 | objc_sync_enter(expr); |
| 2013 | @try stmt @finally { objc_sync_exit(expr); } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2014 | */ |
| 2015 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2016 | void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 2017 | const Stmt &S) { |
| 2018 | bool isTry = isa<ObjCAtTryStmt>(S); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2019 | // Create various blocks we refer to for handling @finally. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2020 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2021 | llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit"); |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2022 | llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit"); |
| 2023 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
| 2024 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2025 | |
| 2026 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 2027 | // evaluation of the expression must occur before we enter the |
| 2028 | // @synchronized. We can safely avoid a temp here because jumps into |
| 2029 | // @synchronized are illegal & this will dominate uses. |
| 2030 | llvm::Value *SyncArg = 0; |
| 2031 | if (!isTry) { |
| 2032 | SyncArg = |
| 2033 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 2034 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 2035 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2036 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2037 | |
| 2038 | // Push an EH context entry, used for handling rethrows and jumps |
| 2039 | // through finally. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2040 | CGF.PushCleanupBlock(FinallyBlock); |
| 2041 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2042 | CGF.ObjCEHValueStack.push_back(0); |
| 2043 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2044 | // Allocate memory for the exception data and rethrow pointer. |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2045 | llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy, |
| 2046 | "exceptiondata.ptr"); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2047 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy, |
| 2048 | "_rethrow"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2049 | llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty, |
| 2050 | "_call_try_exit"); |
| 2051 | CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr); |
| 2052 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2053 | // Enter a new try block and call setjmp. |
| 2054 | CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData); |
| 2055 | llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0, |
| 2056 | "jmpbufarray"); |
| 2057 | JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp"); |
| 2058 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn, |
| 2059 | JmpBufPtr, "result"); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2060 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2061 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 2062 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2063 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2064 | TryHandler, TryBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2065 | |
| 2066 | // Emit the @try block. |
| 2067 | CGF.EmitBlock(TryBlock); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2068 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 2069 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2070 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2071 | |
| 2072 | // Emit the "exception in @try" block. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2073 | CGF.EmitBlock(TryHandler); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2074 | |
| 2075 | // Retrieve the exception object. We may emit multiple blocks but |
| 2076 | // nothing can cross this so the value is already in SSA form. |
| 2077 | llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn, |
| 2078 | ExceptionData, |
| 2079 | "caught"); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2080 | CGF.ObjCEHValueStack.back() = Caught; |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2081 | if (!isTry) |
| 2082 | { |
| 2083 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2084 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2085 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2086 | } |
| 2087 | else if (const ObjCAtCatchStmt* CatchStmt = |
| 2088 | cast<ObjCAtTryStmt>(S).getCatchStmts()) |
| 2089 | { |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2090 | // Enter a new exception try block (in case a @catch block throws |
| 2091 | // an exception). |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2092 | CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2093 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2094 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn, |
| 2095 | JmpBufPtr, "result"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2096 | llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2097 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2098 | llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch"); |
| 2099 | llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler"); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2100 | CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2101 | |
| 2102 | CGF.EmitBlock(CatchBlock); |
| 2103 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2104 | // Handle catch list. As a special case we check if everything is |
| 2105 | // matched and avoid generating code for falling off the end if |
| 2106 | // so. |
| 2107 | bool AllMatched = false; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2108 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2109 | llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2110 | |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2111 | const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl(); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2112 | const PointerType *PT = 0; |
| 2113 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2114 | // catch(...) always matches. |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2115 | if (!CatchParam) { |
| 2116 | AllMatched = true; |
| 2117 | } else { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2118 | PT = CatchParam->getType()->getAsPointerType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2119 | |
Daniel Dunbar | 97f61d1 | 2008-09-27 22:21:14 +0000 | [diff] [blame] | 2120 | // catch(id e) always matches. |
| 2121 | // FIXME: For the time being we also match id<X>; this should |
| 2122 | // be rejected by Sema instead. |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 2123 | if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) || |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2124 | CatchParam->getType()->isObjCQualifiedIdType()) |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2125 | AllMatched = true; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2126 | } |
| 2127 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2128 | if (AllMatched) { |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2129 | if (CatchParam) { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2130 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2131 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2132 | CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2133 | } |
Anders Carlsson | 1452f55 | 2008-09-11 08:21:54 +0000 | [diff] [blame] | 2134 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2135 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2136 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2137 | break; |
| 2138 | } |
| 2139 | |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2140 | assert(PT && "Unexpected non-pointer type in @catch"); |
| 2141 | QualType T = PT->getPointeeType(); |
Anders Carlsson | 4b7ff6e | 2008-09-11 06:35:14 +0000 | [diff] [blame] | 2142 | const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2143 | assert(ObjCType && "Catch parameter must have Objective-C type!"); |
| 2144 | |
| 2145 | // Check if the @catch block matches the exception object. |
| 2146 | llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl()); |
| 2147 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2148 | llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn, |
| 2149 | Class, Caught, "match"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2150 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2151 | llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2152 | |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2153 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2154 | MatchedBlock, NextCatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2155 | |
| 2156 | // Emit the @catch block. |
| 2157 | CGF.EmitBlock(MatchedBlock); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2158 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2159 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2160 | |
| 2161 | llvm::Value *Tmp = |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2162 | CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()), |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2163 | "tmp"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2164 | CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2165 | |
| 2166 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2167 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2168 | |
| 2169 | CGF.EmitBlock(NextCatchBlock); |
| 2170 | } |
| 2171 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2172 | if (!AllMatched) { |
| 2173 | // None of the handlers caught the exception, so store it to be |
| 2174 | // rethrown at the end of the @finally block. |
| 2175 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2176 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2177 | } |
| 2178 | |
| 2179 | // Emit the exception handler for the @catch blocks. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2180 | CGF.EmitBlock(CatchHandler); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2181 | CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn, |
| 2182 | ExceptionData), |
| 2183 | RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2184 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2185 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2186 | } else { |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2187 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2188 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2189 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2190 | } |
| 2191 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2192 | // Pop the exception-handling stack entry. It is important to do |
| 2193 | // this now, because the code in the @finally block is not in this |
| 2194 | // context. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2195 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 2196 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2197 | CGF.ObjCEHValueStack.pop_back(); |
| 2198 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2199 | // Emit the @finally block. |
| 2200 | CGF.EmitBlock(FinallyBlock); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2201 | llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp"); |
| 2202 | |
| 2203 | CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit); |
| 2204 | |
| 2205 | CGF.EmitBlock(FinallyExit); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2206 | CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2207 | |
| 2208 | CGF.EmitBlock(FinallyNoExit); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2209 | if (isTry) { |
| 2210 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 2211 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 2212 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2213 | } else { |
| 2214 | // Emit objc_sync_exit(expr); as finally's sole statement for |
| 2215 | // @synchronized. |
| 2216 | CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, SyncArg); |
Fariborz Jahanian | f2878e5 | 2008-11-21 19:21:53 +0000 | [diff] [blame] | 2217 | } |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2218 | |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2219 | // Emit the switch block |
| 2220 | if (Info.SwitchBlock) |
| 2221 | CGF.EmitBlock(Info.SwitchBlock); |
| 2222 | if (Info.EndBlock) |
| 2223 | CGF.EmitBlock(Info.EndBlock); |
| 2224 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2225 | CGF.EmitBlock(FinallyRethrow); |
| 2226 | CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, |
| 2227 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2228 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2229 | |
| 2230 | CGF.EmitBlock(FinallyEnd); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2231 | } |
| 2232 | |
| 2233 | void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2234 | const ObjCAtThrowStmt &S) { |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2235 | llvm::Value *ExceptionAsObject; |
| 2236 | |
| 2237 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
| 2238 | llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr); |
| 2239 | ExceptionAsObject = |
| 2240 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 2241 | } else { |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2242 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2243 | "Unexpected rethrow outside @catch block."); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2244 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2245 | } |
| 2246 | |
| 2247 | CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2248 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2249 | |
| 2250 | // Clear the insertion point to indicate we are in unreachable code. |
| 2251 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2252 | } |
| 2253 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2254 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2255 | /// object: objc_read_weak (id *src) |
| 2256 | /// |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2257 | llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2258 | llvm::Value *AddrWeakObj) |
| 2259 | { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2260 | const llvm::Type* DestTy = |
| 2261 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2262 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2263 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn, |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2264 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2265 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2266 | return read_weak; |
| 2267 | } |
| 2268 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2269 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 2270 | /// objc_assign_weak (id src, id *dst) |
| 2271 | /// |
| 2272 | void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 2273 | llvm::Value *src, llvm::Value *dst) |
| 2274 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2275 | const llvm::Type * SrcTy = src->getType(); |
| 2276 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2277 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2278 | assert(Size <= 8 && "does not support size > 8"); |
| 2279 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2280 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2281 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2282 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2283 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2284 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2285 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn, |
| 2286 | src, dst, "weakassign"); |
| 2287 | return; |
| 2288 | } |
| 2289 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2290 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 2291 | /// objc_assign_global (id src, id *dst) |
| 2292 | /// |
| 2293 | void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 2294 | llvm::Value *src, llvm::Value *dst) |
| 2295 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2296 | const llvm::Type * SrcTy = src->getType(); |
| 2297 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2298 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2299 | assert(Size <= 8 && "does not support size > 8"); |
| 2300 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2301 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2302 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2303 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2304 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2305 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2306 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn, |
| 2307 | src, dst, "globalassign"); |
| 2308 | return; |
| 2309 | } |
| 2310 | |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2311 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 2312 | /// objc_assign_ivar (id src, id *dst) |
| 2313 | /// |
| 2314 | void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 2315 | llvm::Value *src, llvm::Value *dst) |
| 2316 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2317 | const llvm::Type * SrcTy = src->getType(); |
| 2318 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2319 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2320 | assert(Size <= 8 && "does not support size > 8"); |
| 2321 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2322 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2323 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2324 | } |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2325 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2326 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
| 2327 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn, |
| 2328 | src, dst, "assignivar"); |
| 2329 | return; |
| 2330 | } |
| 2331 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2332 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 2333 | /// objc_assign_strongCast (id src, id *dst) |
| 2334 | /// |
| 2335 | void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 2336 | llvm::Value *src, llvm::Value *dst) |
| 2337 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2338 | const llvm::Type * SrcTy = src->getType(); |
| 2339 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2340 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2341 | assert(Size <= 8 && "does not support size > 8"); |
| 2342 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2343 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2344 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2345 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2346 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2347 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2348 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn, |
| 2349 | src, dst, "weakassign"); |
| 2350 | return; |
| 2351 | } |
| 2352 | |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2353 | /// EmitObjCValueForIvar - Code Gen for ivar reference. |
| 2354 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2355 | LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 2356 | QualType ObjectTy, |
| 2357 | llvm::Value *BaseValue, |
| 2358 | const ObjCIvarDecl *Ivar, |
| 2359 | const FieldDecl *Field, |
| 2360 | unsigned CVRQualifiers) { |
| 2361 | if (Ivar->isBitField()) |
| 2362 | return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field), |
| 2363 | CVRQualifiers); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2364 | // TODO: Add a special case for isa (index 0) |
| 2365 | unsigned Index = CGM.getTypes().getLLVMFieldNo(Field); |
| 2366 | llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp"); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2367 | LValue LV = LValue::MakeAddr(V, |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 2368 | Ivar->getType().getCVRQualifiers()|CVRQualifiers, |
| 2369 | CGM.getContext().getObjCGCAttrKind(Ivar->getType())); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2370 | LValue::SetObjCIvar(LV, true); |
| 2371 | return LV; |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2372 | } |
| 2373 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2374 | llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
| 2375 | ObjCInterfaceDecl *Interface, |
| 2376 | const ObjCIvarDecl *Ivar) { |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 2377 | const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(Interface); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2378 | FieldDecl *Field = Interface->lookupFieldDeclForIvar(CGM.getContext(), Ivar); |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 2379 | uint64_t Offset = GetIvarBaseOffset(Layout, Field); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2380 | return llvm::ConstantInt::get( |
| 2381 | CGM.getTypes().ConvertType(CGM.getContext().LongTy), |
| 2382 | Offset); |
| 2383 | } |
| 2384 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2385 | /* *** Private Interface *** */ |
| 2386 | |
| 2387 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 2388 | /// level information. |
| 2389 | /// |
| 2390 | /// See: <rdr://4810609&4810587&4810587> |
| 2391 | /// struct IMAGE_INFO { |
| 2392 | /// unsigned version; |
| 2393 | /// unsigned flags; |
| 2394 | /// }; |
| 2395 | enum ImageInfoFlags { |
| 2396 | eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies |
| 2397 | eImageInfo_GarbageCollected = (1 << 1), |
| 2398 | eImageInfo_GCOnly = (1 << 2) |
| 2399 | }; |
| 2400 | |
| 2401 | void CGObjCMac::EmitImageInfo() { |
| 2402 | unsigned version = 0; // Version is unused? |
| 2403 | unsigned flags = 0; |
| 2404 | |
| 2405 | // FIXME: Fix and continue? |
| 2406 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 2407 | flags |= eImageInfo_GarbageCollected; |
| 2408 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 2409 | flags |= eImageInfo_GCOnly; |
| 2410 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2411 | // Emitted as int[2]; |
| 2412 | llvm::Constant *values[2] = { |
| 2413 | llvm::ConstantInt::get(llvm::Type::Int32Ty, version), |
| 2414 | llvm::ConstantInt::get(llvm::Type::Int32Ty, flags) |
| 2415 | }; |
| 2416 | llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2417 | |
| 2418 | const char *Section; |
| 2419 | if (ObjCABI == 1) |
| 2420 | Section = "__OBJC, __image_info,regular"; |
| 2421 | else |
| 2422 | Section = "__DATA, __objc_imageinfo, regular, no_dead_strip"; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2423 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2424 | CreateMetadataVar("\01L_OBJC_IMAGE_INFO", |
| 2425 | llvm::ConstantArray::get(AT, values, 2), |
| 2426 | Section, |
| 2427 | 0, |
| 2428 | true); |
| 2429 | GV->setConstant(true); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2430 | } |
| 2431 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2432 | |
| 2433 | // struct objc_module { |
| 2434 | // unsigned long version; |
| 2435 | // unsigned long size; |
| 2436 | // const char *name; |
| 2437 | // Symtab symtab; |
| 2438 | // }; |
| 2439 | |
| 2440 | // FIXME: Get from somewhere |
| 2441 | static const int ModuleVersion = 7; |
| 2442 | |
| 2443 | void CGObjCMac::EmitModuleInfo() { |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 2444 | uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2445 | |
| 2446 | std::vector<llvm::Constant*> Values(4); |
| 2447 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion); |
| 2448 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2449 | // This used to be the filename, now it is unused. <rdr://4327263> |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2450 | Values[2] = GetClassName(&CGM.getContext().Idents.get("")); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2451 | Values[3] = EmitModuleSymbols(); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2452 | CreateMetadataVar("\01L_OBJC_MODULES", |
| 2453 | llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values), |
| 2454 | "__OBJC,__module_info,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2455 | 4, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2456 | } |
| 2457 | |
| 2458 | llvm::Constant *CGObjCMac::EmitModuleSymbols() { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2459 | unsigned NumClasses = DefinedClasses.size(); |
| 2460 | unsigned NumCategories = DefinedCategories.size(); |
| 2461 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2462 | // Return null if no symbols were defined. |
| 2463 | if (!NumClasses && !NumCategories) |
| 2464 | return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy); |
| 2465 | |
| 2466 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2467 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2468 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy); |
| 2469 | Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses); |
| 2470 | Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories); |
| 2471 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2472 | // The runtime expects exactly the list of defined classes followed |
| 2473 | // by the list of defined categories, in a single array. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2474 | std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2475 | for (unsigned i=0; i<NumClasses; i++) |
| 2476 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
| 2477 | ObjCTypes.Int8PtrTy); |
| 2478 | for (unsigned i=0; i<NumCategories; i++) |
| 2479 | Symbols[NumClasses + i] = |
| 2480 | llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
| 2481 | ObjCTypes.Int8PtrTy); |
| 2482 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2483 | Values[4] = |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2484 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2485 | NumClasses + NumCategories), |
| 2486 | Symbols); |
| 2487 | |
| 2488 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 2489 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2490 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2491 | CreateMetadataVar("\01L_OBJC_SYMBOLS", Init, |
| 2492 | "__OBJC,__symbols,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2493 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2494 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy); |
| 2495 | } |
| 2496 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2497 | llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2498 | const ObjCInterfaceDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2499 | LazySymbols.insert(ID->getIdentifier()); |
| 2500 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2501 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 2502 | |
| 2503 | if (!Entry) { |
| 2504 | llvm::Constant *Casted = |
| 2505 | llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()), |
| 2506 | ObjCTypes.ClassPtrTy); |
| 2507 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2508 | CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted, |
| 2509 | "__OBJC,__cls_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2510 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2511 | } |
| 2512 | |
| 2513 | return Builder.CreateLoad(Entry, false, "tmp"); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2514 | } |
| 2515 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2516 | llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2517 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 2518 | |
| 2519 | if (!Entry) { |
| 2520 | llvm::Constant *Casted = |
| 2521 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 2522 | ObjCTypes.SelectorPtrTy); |
| 2523 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2524 | CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted, |
| 2525 | "__OBJC,__message_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2526 | 4, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2527 | } |
| 2528 | |
| 2529 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 2530 | } |
| 2531 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 2532 | llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2533 | llvm::GlobalVariable *&Entry = ClassNames[Ident]; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2534 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2535 | if (!Entry) |
| 2536 | Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
| 2537 | llvm::ConstantArray::get(Ident->getName()), |
| 2538 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 2539 | 1, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2540 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2541 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2542 | } |
| 2543 | |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 2544 | /// GetInterfaceDeclStructLayout - Get layout for ivars of given |
| 2545 | /// interface declaration. |
| 2546 | const llvm::StructLayout *CGObjCCommonMac::GetInterfaceDeclStructLayout( |
| 2547 | const ObjCInterfaceDecl *OID) const { |
| 2548 | const llvm::Type *InterfaceTy = |
| 2549 | CGM.getTypes().ConvertType( |
| 2550 | CGM.getContext().getObjCInterfaceType( |
| 2551 | const_cast<ObjCInterfaceDecl*>(OID))); |
| 2552 | const llvm::StructLayout *Layout = |
| 2553 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy)); |
| 2554 | return Layout; |
| 2555 | } |
| 2556 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 2557 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 2558 | /// ivar layout bitmap. |
| 2559 | llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident, |
| 2560 | const ObjCCommonTypesHelper &ObjCTypes) { |
| 2561 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 2562 | } |
| 2563 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2564 | void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCInterfaceDecl *OI, |
| 2565 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2566 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2567 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2568 | unsigned int BytePos, bool ForStrongLayout, |
| 2569 | int &Index, int &SkIndex, bool &HasUnion) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2570 | bool IsUnion = (RD && RD->isUnion()); |
| 2571 | uint64_t MaxUnionIvarSize = 0; |
| 2572 | uint64_t MaxSkippedUnionIvarSize = 0; |
| 2573 | FieldDecl *MaxField = 0; |
| 2574 | FieldDecl *MaxSkippedField = 0; |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2575 | unsigned base = 0; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2576 | if (RecFields.empty()) |
| 2577 | return; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2578 | if (IsUnion) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2579 | base = BytePos + GetFieldBaseOffset(OI, Layout, RecFields[0]); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2580 | unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0); |
| 2581 | unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth(); |
| 2582 | |
| 2583 | llvm::SmallVector<FieldDecl*, 16> TmpRecFields; |
| 2584 | |
| 2585 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2586 | FieldDecl *Field = RecFields[i]; |
| 2587 | // Skip over unnamed or bitfields |
| 2588 | if (!Field->getIdentifier() || Field->isBitField()) |
| 2589 | continue; |
| 2590 | QualType FQT = Field->getType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2591 | if (FQT->isRecordType() || FQT->isUnionType()) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2592 | if (FQT->isUnionType()) |
| 2593 | HasUnion = true; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2594 | else |
| 2595 | assert(FQT->isRecordType() && |
| 2596 | "only union/record is supported for ivar layout bitmap"); |
| 2597 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2598 | const RecordType *RT = FQT->getAsRecordType(); |
| 2599 | const RecordDecl *RD = RT->getDecl(); |
| 2600 | // FIXME - Find a more efficiant way of passing records down. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2601 | TmpRecFields.append(RD->field_begin(CGM.getContext()), |
| 2602 | RD->field_end(CGM.getContext())); |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2603 | // FIXME - Is Layout correct? |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2604 | BuildAggrIvarLayout(OI, Layout, RD, TmpRecFields, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2605 | BytePos + GetFieldBaseOffset(OI, Layout, Field), |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2606 | ForStrongLayout, Index, SkIndex, |
| 2607 | HasUnion); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2608 | TmpRecFields.clear(); |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2609 | continue; |
| 2610 | } |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2611 | |
| 2612 | if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2613 | const ConstantArrayType *CArray = |
| 2614 | dyn_cast_or_null<ConstantArrayType>(Array); |
| 2615 | assert(CArray && "only array with know element size is supported"); |
| 2616 | FQT = CArray->getElementType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2617 | while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 2618 | const ConstantArrayType *CArray = |
| 2619 | dyn_cast_or_null<ConstantArrayType>(Array); |
| 2620 | FQT = CArray->getElementType(); |
| 2621 | } |
| 2622 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2623 | assert(!FQT->isUnionType() && |
| 2624 | "layout for array of unions not supported"); |
| 2625 | if (FQT->isRecordType()) { |
| 2626 | uint64_t ElCount = CArray->getSize().getZExtValue(); |
| 2627 | int OldIndex = Index; |
| 2628 | int OldSkIndex = SkIndex; |
| 2629 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2630 | // FIXME - Use a common routine with the above! |
| 2631 | const RecordType *RT = FQT->getAsRecordType(); |
| 2632 | const RecordDecl *RD = RT->getDecl(); |
| 2633 | // FIXME - Find a more efficiant way of passing records down. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2634 | TmpRecFields.append(RD->field_begin(CGM.getContext()), |
| 2635 | RD->field_end(CGM.getContext())); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2636 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2637 | BuildAggrIvarLayout(OI, Layout, RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2638 | TmpRecFields, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2639 | BytePos + GetFieldBaseOffset(OI, Layout, Field), |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2640 | ForStrongLayout, Index, SkIndex, |
| 2641 | HasUnion); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2642 | TmpRecFields.clear(); |
| 2643 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2644 | // Replicate layout information for each array element. Note that |
| 2645 | // one element is already done. |
| 2646 | uint64_t ElIx = 1; |
| 2647 | for (int FirstIndex = Index, FirstSkIndex = SkIndex; |
| 2648 | ElIx < ElCount; ElIx++) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2649 | uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2650 | for (int i = OldIndex+1; i <= FirstIndex; ++i) |
| 2651 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2652 | GC_IVAR gcivar; |
| 2653 | gcivar.ivar_bytepos = IvarsInfo[i].ivar_bytepos + Size*ElIx; |
| 2654 | gcivar.ivar_size = IvarsInfo[i].ivar_size; |
| 2655 | IvarsInfo.push_back(gcivar); ++Index; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2656 | } |
| 2657 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2658 | for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2659 | GC_IVAR skivar; |
| 2660 | skivar.ivar_bytepos = SkipIvars[i].ivar_bytepos + Size*ElIx; |
| 2661 | skivar.ivar_size = SkipIvars[i].ivar_size; |
| 2662 | SkipIvars.push_back(skivar); ++SkIndex; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2663 | } |
| 2664 | } |
| 2665 | continue; |
| 2666 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2667 | } |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2668 | // At this point, we are done with Record/Union and array there of. |
| 2669 | // For other arrays we are down to its element type. |
| 2670 | QualType::GCAttrTypes GCAttr = QualType::GCNone; |
| 2671 | do { |
| 2672 | if (FQT.isObjCGCStrong() || FQT.isObjCGCWeak()) { |
| 2673 | GCAttr = FQT.isObjCGCStrong() ? QualType::Strong : QualType::Weak; |
| 2674 | break; |
| 2675 | } |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2676 | else if (CGM.getContext().isObjCObjectPointerType(FQT)) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2677 | GCAttr = QualType::Strong; |
| 2678 | break; |
| 2679 | } |
| 2680 | else if (const PointerType *PT = FQT->getAsPointerType()) { |
| 2681 | FQT = PT->getPointeeType(); |
| 2682 | } |
| 2683 | else { |
| 2684 | break; |
| 2685 | } |
| 2686 | } while (true); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2687 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2688 | if ((ForStrongLayout && GCAttr == QualType::Strong) |
| 2689 | || (!ForStrongLayout && GCAttr == QualType::Weak)) { |
| 2690 | if (IsUnion) |
| 2691 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2692 | uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType()) |
| 2693 | / WordSizeInBits; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2694 | if (UnionIvarSize > MaxUnionIvarSize) |
| 2695 | { |
| 2696 | MaxUnionIvarSize = UnionIvarSize; |
| 2697 | MaxField = Field; |
| 2698 | } |
| 2699 | } |
| 2700 | else |
| 2701 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2702 | GC_IVAR gcivar; |
| 2703 | gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field); |
| 2704 | gcivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) / |
| 2705 | WordSizeInBits; |
| 2706 | IvarsInfo.push_back(gcivar); ++Index; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2707 | } |
| 2708 | } |
| 2709 | else if ((ForStrongLayout && |
| 2710 | (GCAttr == QualType::GCNone || GCAttr == QualType::Weak)) |
| 2711 | || (!ForStrongLayout && GCAttr != QualType::Weak)) { |
| 2712 | if (IsUnion) |
| 2713 | { |
| 2714 | uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType()); |
| 2715 | if (UnionIvarSize > MaxSkippedUnionIvarSize) |
| 2716 | { |
| 2717 | MaxSkippedUnionIvarSize = UnionIvarSize; |
| 2718 | MaxSkippedField = Field; |
| 2719 | } |
| 2720 | } |
| 2721 | else |
| 2722 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2723 | GC_IVAR skivar; |
| 2724 | skivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field); |
| 2725 | skivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) / |
| 2726 | WordSizeInBits; |
| 2727 | SkipIvars.push_back(skivar); ++SkIndex; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2728 | } |
| 2729 | } |
| 2730 | } |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2731 | if (MaxField) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2732 | GC_IVAR gcivar; |
| 2733 | gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, MaxField); |
| 2734 | gcivar.ivar_size = MaxUnionIvarSize; |
| 2735 | IvarsInfo.push_back(gcivar); ++Index; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2736 | } |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2737 | |
| 2738 | if (MaxSkippedField) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2739 | GC_IVAR skivar; |
| 2740 | skivar.ivar_bytepos = BytePos + |
| 2741 | GetFieldBaseOffset(OI, Layout, MaxSkippedField); |
| 2742 | skivar.ivar_size = MaxSkippedUnionIvarSize; |
| 2743 | SkipIvars.push_back(skivar); ++SkIndex; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2744 | } |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2745 | } |
| 2746 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2747 | static int |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2748 | IvarBytePosCompare(const void *a, const void *b) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2749 | { |
| 2750 | unsigned int sa = ((CGObjCCommonMac::GC_IVAR *)a)->ivar_bytepos; |
| 2751 | unsigned int sb = ((CGObjCCommonMac::GC_IVAR *)b)->ivar_bytepos; |
| 2752 | |
| 2753 | if (sa < sb) |
| 2754 | return -1; |
| 2755 | if (sa > sb) |
| 2756 | return 1; |
| 2757 | return 0; |
| 2758 | } |
| 2759 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2760 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 2761 | /// implementation for the __strong or __weak case. |
| 2762 | /// The layout map displays which words in ivar list must be skipped |
| 2763 | /// and which must be scanned by GC (see below). String is built of bytes. |
| 2764 | /// Each byte is divided up in two nibbles (4-bit each). Left nibble is count |
| 2765 | /// of words to skip and right nibble is count of words to scan. So, each |
| 2766 | /// nibble represents up to 15 workds to skip or scan. Skipping the rest is |
| 2767 | /// represented by a 0x00 byte which also ends the string. |
| 2768 | /// 1. when ForStrongLayout is true, following ivars are scanned: |
| 2769 | /// - id, Class |
| 2770 | /// - object * |
| 2771 | /// - __strong anything |
| 2772 | /// |
| 2773 | /// 2. When ForStrongLayout is false, following ivars are scanned: |
| 2774 | /// - __weak anything |
| 2775 | /// |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2776 | llvm::Constant *CGObjCCommonMac::BuildIvarLayout( |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2777 | const ObjCImplementationDecl *OMD, |
| 2778 | bool ForStrongLayout) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2779 | int Index = -1; |
| 2780 | int SkIndex = -1; |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2781 | bool hasUnion = false; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2782 | int SkipScan; |
| 2783 | unsigned int WordsToScan, WordsToSkip; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2784 | const llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 2785 | if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC) |
| 2786 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2787 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2788 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2789 | const ObjCInterfaceDecl *OI = OMD->getClassInterface(); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2790 | CGM.getContext().CollectObjCIvars(OI, RecFields); |
| 2791 | if (RecFields.empty()) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2792 | return llvm::Constant::getNullValue(PtrTy); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2793 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2794 | SkipIvars.clear(); |
| 2795 | IvarsInfo.clear(); |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 2796 | |
| 2797 | const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OI); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2798 | BuildAggrIvarLayout(OI, Layout, 0, RecFields, 0, ForStrongLayout, |
| 2799 | Index, SkIndex, hasUnion); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2800 | if (Index == -1) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2801 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2802 | |
| 2803 | // Sort on byte position in case we encounterred a union nested in |
| 2804 | // the ivar list. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2805 | if (hasUnion && !IvarsInfo.empty()) |
| 2806 | qsort(&IvarsInfo[0], Index+1, sizeof(GC_IVAR), IvarBytePosCompare); |
| 2807 | if (hasUnion && !SkipIvars.empty()) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2808 | qsort(&SkipIvars[0], Index+1, sizeof(GC_IVAR), IvarBytePosCompare); |
| 2809 | |
| 2810 | // Build the string of skip/scan nibbles |
| 2811 | SkipScan = -1; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2812 | SkipScanIvars.clear(); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2813 | unsigned int WordSize = |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2814 | CGM.getTypes().getTargetData().getTypePaddedSize(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2815 | if (IvarsInfo[0].ivar_bytepos == 0) { |
| 2816 | WordsToSkip = 0; |
| 2817 | WordsToScan = IvarsInfo[0].ivar_size; |
| 2818 | } |
| 2819 | else { |
| 2820 | WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize; |
| 2821 | WordsToScan = IvarsInfo[0].ivar_size; |
| 2822 | } |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2823 | for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2824 | { |
| 2825 | unsigned int TailPrevGCObjC = |
| 2826 | IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize; |
| 2827 | if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) |
| 2828 | { |
| 2829 | // consecutive 'scanned' object pointers. |
| 2830 | WordsToScan += IvarsInfo[i].ivar_size; |
| 2831 | } |
| 2832 | else |
| 2833 | { |
| 2834 | // Skip over 'gc'able object pointer which lay over each other. |
| 2835 | if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos) |
| 2836 | continue; |
| 2837 | // Must skip over 1 or more words. We save current skip/scan values |
| 2838 | // and start a new pair. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2839 | SKIP_SCAN SkScan; |
| 2840 | SkScan.skip = WordsToSkip; |
| 2841 | SkScan.scan = WordsToScan; |
| 2842 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
| 2843 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2844 | // Skip the hole. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2845 | SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize; |
| 2846 | SkScan.scan = 0; |
| 2847 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2848 | WordsToSkip = 0; |
| 2849 | WordsToScan = IvarsInfo[i].ivar_size; |
| 2850 | } |
| 2851 | } |
| 2852 | if (WordsToScan > 0) |
| 2853 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2854 | SKIP_SCAN SkScan; |
| 2855 | SkScan.skip = WordsToSkip; |
| 2856 | SkScan.scan = WordsToScan; |
| 2857 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2858 | } |
| 2859 | |
| 2860 | bool BytesSkipped = false; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2861 | if (!SkipIvars.empty()) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2862 | { |
| 2863 | int LastByteSkipped = |
| 2864 | SkipIvars[SkIndex].ivar_bytepos + SkipIvars[SkIndex].ivar_size; |
| 2865 | int LastByteScanned = |
| 2866 | IvarsInfo[Index].ivar_bytepos + IvarsInfo[Index].ivar_size * WordSize; |
| 2867 | BytesSkipped = (LastByteSkipped > LastByteScanned); |
| 2868 | // Compute number of bytes to skip at the tail end of the last ivar scanned. |
| 2869 | if (BytesSkipped) |
| 2870 | { |
| 2871 | unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2872 | SKIP_SCAN SkScan; |
| 2873 | SkScan.skip = TotalWords - (LastByteScanned/WordSize); |
| 2874 | SkScan.scan = 0; |
| 2875 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2876 | } |
| 2877 | } |
| 2878 | // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced |
| 2879 | // as 0xMN. |
| 2880 | for (int i = 0; i <= SkipScan; i++) |
| 2881 | { |
| 2882 | if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0 |
| 2883 | && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) { |
| 2884 | // 0xM0 followed by 0x0N detected. |
| 2885 | SkipScanIvars[i].scan = SkipScanIvars[i+1].scan; |
| 2886 | for (int j = i+1; j < SkipScan; j++) |
| 2887 | SkipScanIvars[j] = SkipScanIvars[j+1]; |
| 2888 | --SkipScan; |
| 2889 | } |
| 2890 | } |
| 2891 | |
| 2892 | // Generate the string. |
| 2893 | std::string BitMap; |
| 2894 | for (int i = 0; i <= SkipScan; i++) |
| 2895 | { |
| 2896 | unsigned char byte; |
| 2897 | unsigned int skip_small = SkipScanIvars[i].skip % 0xf; |
| 2898 | unsigned int scan_small = SkipScanIvars[i].scan % 0xf; |
| 2899 | unsigned int skip_big = SkipScanIvars[i].skip / 0xf; |
| 2900 | unsigned int scan_big = SkipScanIvars[i].scan / 0xf; |
| 2901 | |
| 2902 | if (skip_small > 0 || skip_big > 0) |
| 2903 | BytesSkipped = true; |
| 2904 | // first skip big. |
| 2905 | for (unsigned int ix = 0; ix < skip_big; ix++) |
| 2906 | BitMap += (unsigned char)(0xf0); |
| 2907 | |
| 2908 | // next (skip small, scan) |
| 2909 | if (skip_small) |
| 2910 | { |
| 2911 | byte = skip_small << 4; |
| 2912 | if (scan_big > 0) |
| 2913 | { |
| 2914 | byte |= 0xf; |
| 2915 | --scan_big; |
| 2916 | } |
| 2917 | else if (scan_small) |
| 2918 | { |
| 2919 | byte |= scan_small; |
| 2920 | scan_small = 0; |
| 2921 | } |
| 2922 | BitMap += byte; |
| 2923 | } |
| 2924 | // next scan big |
| 2925 | for (unsigned int ix = 0; ix < scan_big; ix++) |
| 2926 | BitMap += (unsigned char)(0x0f); |
| 2927 | // last scan small |
| 2928 | if (scan_small) |
| 2929 | { |
| 2930 | byte = scan_small; |
| 2931 | BitMap += byte; |
| 2932 | } |
| 2933 | } |
| 2934 | // null terminate string. |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2935 | unsigned char zero = 0; |
| 2936 | BitMap += zero; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2937 | // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as |
| 2938 | // final layout. |
| 2939 | if (ForStrongLayout && !BytesSkipped) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2940 | return llvm::Constant::getNullValue(PtrTy); |
| 2941 | llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
| 2942 | llvm::ConstantArray::get(BitMap.c_str()), |
| 2943 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 2944 | 1, true); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2945 | // FIXME. Need a commomand-line option for this eventually. |
| 2946 | if (ForStrongLayout) |
| 2947 | printf("\nstrong ivar layout: "); |
| 2948 | else |
| 2949 | printf("\nweak ivar layout: "); |
| 2950 | const unsigned char *s = (unsigned char*)BitMap.c_str(); |
| 2951 | for (unsigned i = 0; i < BitMap.size(); i++) |
Fariborz Jahanian | dbf15cb | 2009-03-26 19:10:36 +0000 | [diff] [blame] | 2952 | if (!(s[i] & 0xf0)) |
| 2953 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 2954 | else |
| 2955 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2956 | printf("\n"); |
| 2957 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2958 | return getConstantGEP(Entry, 0, 0); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2959 | } |
| 2960 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 2961 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2962 | llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; |
| 2963 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2964 | // FIXME: Avoid std::string copying. |
| 2965 | if (!Entry) |
| 2966 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_", |
| 2967 | llvm::ConstantArray::get(Sel.getAsString()), |
| 2968 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 2969 | 1, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2970 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2971 | return getConstantGEP(Entry, 0, 0); |
| 2972 | } |
| 2973 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2974 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 2975 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2976 | return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); |
| 2977 | } |
| 2978 | |
| 2979 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 2980 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2981 | return GetMethodVarName(&CGM.getContext().Idents.get(Name)); |
| 2982 | } |
| 2983 | |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 2984 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(FieldDecl *Field) { |
| 2985 | std::string TypeStr; |
| 2986 | CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); |
| 2987 | |
| 2988 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2989 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2990 | if (!Entry) |
| 2991 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
| 2992 | llvm::ConstantArray::get(TypeStr), |
| 2993 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 2994 | 1, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2995 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2996 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2997 | } |
| 2998 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 2999 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3000 | std::string TypeStr; |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3001 | CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D), |
| 3002 | TypeStr); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3003 | |
| 3004 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
| 3005 | |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3006 | if (!Entry) |
| 3007 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
| 3008 | llvm::ConstantArray::get(TypeStr), |
| 3009 | "__TEXT,__cstring,cstring_literals", |
| 3010 | 1, true); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3011 | |
| 3012 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3013 | } |
| 3014 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3015 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3016 | llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3017 | llvm::GlobalVariable *&Entry = PropertyNames[Ident]; |
| 3018 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3019 | if (!Entry) |
| 3020 | Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_", |
| 3021 | llvm::ConstantArray::get(Ident->getName()), |
| 3022 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3023 | 1, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3024 | |
| 3025 | return getConstantGEP(Entry, 0, 0); |
| 3026 | } |
| 3027 | |
| 3028 | // FIXME: Merge into a single cstring creation function. |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3029 | // FIXME: This Decl should be more precise. |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3030 | llvm::Constant * |
| 3031 | CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 3032 | const Decl *Container) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3033 | std::string TypeStr; |
| 3034 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3035 | return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); |
| 3036 | } |
| 3037 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3038 | void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D, |
| 3039 | const ObjCContainerDecl *CD, |
| 3040 | std::string &NameOut) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 3041 | NameOut = '\01'; |
| 3042 | NameOut += (D->isInstanceMethod() ? '-' : '+'); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3043 | NameOut += '['; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 3044 | assert (CD && "Missing container decl in GetNameForMethod"); |
| 3045 | NameOut += CD->getNameAsString(); |
Fariborz Jahanian | 5284733 | 2009-01-26 23:49:05 +0000 | [diff] [blame] | 3046 | // FIXME. For a method in a category, (CAT_NAME) is inserted here. |
| 3047 | // Right now! there is not enough info. to do this. |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3048 | NameOut += ' '; |
| 3049 | NameOut += D->getSelector().getAsString(); |
| 3050 | NameOut += ']'; |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3051 | } |
| 3052 | |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 3053 | /// GetFirstIvarInRecord - This routine returns the record for the |
| 3054 | /// implementation of the fiven class OID. It also returns field |
| 3055 | /// corresponding to the first ivar in the class in FIV. It also |
| 3056 | /// returns the one before the first ivar. |
| 3057 | /// |
| 3058 | const RecordDecl *CGObjCCommonMac::GetFirstIvarInRecord( |
| 3059 | const ObjCInterfaceDecl *OID, |
| 3060 | RecordDecl::field_iterator &FIV, |
| 3061 | RecordDecl::field_iterator &PIV) { |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3062 | int countSuperClassIvars = countInheritedIvars(OID->getSuperClass(), |
| 3063 | CGM.getContext()); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 3064 | const RecordDecl *RD = CGM.getContext().addRecordToClass(OID); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3065 | RecordDecl::field_iterator ifield = RD->field_begin(CGM.getContext()); |
| 3066 | RecordDecl::field_iterator pfield = RD->field_end(CGM.getContext()); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 3067 | while (countSuperClassIvars-- > 0) { |
| 3068 | pfield = ifield; |
| 3069 | ++ifield; |
| 3070 | } |
| 3071 | FIV = ifield; |
| 3072 | PIV = pfield; |
| 3073 | return RD; |
| 3074 | } |
| 3075 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3076 | void CGObjCMac::FinishModule() { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3077 | EmitModuleInfo(); |
| 3078 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3079 | // Emit the dummy bodies for any protocols which were referenced but |
| 3080 | // never defined. |
| 3081 | for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator |
| 3082 | i = Protocols.begin(), e = Protocols.end(); i != e; ++i) { |
| 3083 | if (i->second->hasInitializer()) |
| 3084 | continue; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3085 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3086 | std::vector<llvm::Constant*> Values(5); |
| 3087 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
| 3088 | Values[1] = GetClassName(i->first); |
| 3089 | Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 3090 | Values[3] = Values[4] = |
| 3091 | llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
| 3092 | i->second->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 3093 | i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 3094 | Values)); |
| 3095 | } |
| 3096 | |
| 3097 | std::vector<llvm::Constant*> Used; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3098 | for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3099 | e = UsedGlobals.end(); i != e; ++i) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3100 | Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3101 | } |
| 3102 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3103 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3104 | llvm::GlobalValue *GV = |
| 3105 | new llvm::GlobalVariable(AT, false, |
| 3106 | llvm::GlobalValue::AppendingLinkage, |
| 3107 | llvm::ConstantArray::get(AT, Used), |
| 3108 | "llvm.used", |
| 3109 | &CGM.getModule()); |
| 3110 | |
| 3111 | GV->setSection("llvm.metadata"); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3112 | |
| 3113 | // Add assembler directives to add lazy undefined symbol references |
| 3114 | // for classes which are referenced but not defined. This is |
| 3115 | // important for correct linker interaction. |
| 3116 | |
| 3117 | // FIXME: Uh, this isn't particularly portable. |
| 3118 | std::stringstream s; |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 3119 | |
| 3120 | if (!CGM.getModule().getModuleInlineAsm().empty()) |
| 3121 | s << "\n"; |
| 3122 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3123 | for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(), |
| 3124 | e = LazySymbols.end(); i != e; ++i) { |
| 3125 | s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n"; |
| 3126 | } |
| 3127 | for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(), |
| 3128 | e = DefinedSymbols.end(); i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3129 | s << "\t.objc_class_name_" << (*i)->getName() << "=0\n" |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3130 | << "\t.globl .objc_class_name_" << (*i)->getName() << "\n"; |
| 3131 | } |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 3132 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3133 | CGM.getModule().appendModuleInlineAsm(s.str()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3134 | } |
| 3135 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3136 | CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3137 | : CGObjCCommonMac(cgm), |
| 3138 | ObjCTypes(cgm) |
| 3139 | { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3140 | ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3141 | ObjCABI = 2; |
| 3142 | } |
| 3143 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3144 | /* *** */ |
| 3145 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3146 | ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm) |
| 3147 | : CGM(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3148 | { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3149 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3150 | ASTContext &Ctx = CGM.getContext(); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3151 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3152 | ShortTy = Types.ConvertType(Ctx.ShortTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3153 | IntTy = Types.ConvertType(Ctx.IntTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3154 | LongTy = Types.ConvertType(Ctx.LongTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3155 | LongLongTy = Types.ConvertType(Ctx.LongLongTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3156 | Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 3157 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3158 | ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType()); |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 3159 | PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3160 | SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3161 | |
| 3162 | // FIXME: It would be nice to unify this with the opaque type, so |
| 3163 | // that the IR comes out a bit cleaner. |
| 3164 | const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); |
| 3165 | ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3166 | |
| 3167 | // I'm not sure I like this. The implicit coordination is a bit |
| 3168 | // gross. We should solve this in a reasonable fashion because this |
| 3169 | // is a pretty common task (match some runtime data structure with |
| 3170 | // an LLVM data structure). |
| 3171 | |
| 3172 | // FIXME: This is leaked. |
| 3173 | // FIXME: Merge with rewriter code? |
| 3174 | |
| 3175 | // struct _objc_super { |
| 3176 | // id self; |
| 3177 | // Class cls; |
| 3178 | // } |
| 3179 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 3180 | SourceLocation(), |
| 3181 | &Ctx.Idents.get("_objc_super")); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3182 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3183 | Ctx.getObjCIdType(), 0, false)); |
| 3184 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3185 | Ctx.getObjCClassType(), 0, false)); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3186 | RD->completeDefinition(Ctx); |
| 3187 | |
| 3188 | SuperCTy = Ctx.getTagDeclType(RD); |
| 3189 | SuperPtrCTy = Ctx.getPointerType(SuperCTy); |
| 3190 | |
| 3191 | SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy)); |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3192 | SuperPtrTy = llvm::PointerType::getUnqual(SuperTy); |
| 3193 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3194 | // struct _prop_t { |
| 3195 | // char *name; |
| 3196 | // char *attributes; |
| 3197 | // } |
| 3198 | PropertyTy = llvm::StructType::get(Int8PtrTy, |
| 3199 | Int8PtrTy, |
| 3200 | NULL); |
| 3201 | CGM.getModule().addTypeName("struct._prop_t", |
| 3202 | PropertyTy); |
| 3203 | |
| 3204 | // struct _prop_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3205 | // uint32_t entsize; // sizeof(struct _prop_t) |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3206 | // uint32_t count_of_properties; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3207 | // struct _prop_t prop_list[count_of_properties]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3208 | // } |
| 3209 | PropertyListTy = llvm::StructType::get(IntTy, |
| 3210 | IntTy, |
| 3211 | llvm::ArrayType::get(PropertyTy, 0), |
| 3212 | NULL); |
| 3213 | CGM.getModule().addTypeName("struct._prop_list_t", |
| 3214 | PropertyListTy); |
| 3215 | // struct _prop_list_t * |
| 3216 | PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy); |
| 3217 | |
| 3218 | // struct _objc_method { |
| 3219 | // SEL _cmd; |
| 3220 | // char *method_type; |
| 3221 | // char *_imp; |
| 3222 | // } |
| 3223 | MethodTy = llvm::StructType::get(SelectorPtrTy, |
| 3224 | Int8PtrTy, |
| 3225 | Int8PtrTy, |
| 3226 | NULL); |
| 3227 | CGM.getModule().addTypeName("struct._objc_method", MethodTy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3228 | |
| 3229 | // struct _objc_cache * |
| 3230 | CacheTy = llvm::OpaqueType::get(); |
| 3231 | CGM.getModule().addTypeName("struct._objc_cache", CacheTy); |
| 3232 | CachePtrTy = llvm::PointerType::getUnqual(CacheTy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3233 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3234 | // Property manipulation functions. |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3235 | |
| 3236 | QualType IdType = Ctx.getObjCIdType(); |
| 3237 | QualType SelType = Ctx.getObjCSelType(); |
| 3238 | llvm::SmallVector<QualType,16> Params; |
| 3239 | const llvm::FunctionType *FTy; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3240 | |
| 3241 | // id objc_getProperty (id, SEL, ptrdiff_t, bool) |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3242 | Params.push_back(IdType); |
| 3243 | Params.push_back(SelType); |
| 3244 | Params.push_back(Ctx.LongTy); |
| 3245 | Params.push_back(Ctx.BoolTy); |
| 3246 | FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), |
| 3247 | false); |
| 3248 | GetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_getProperty"); |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3249 | |
| 3250 | // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool) |
| 3251 | Params.clear(); |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3252 | Params.push_back(IdType); |
| 3253 | Params.push_back(SelType); |
| 3254 | Params.push_back(Ctx.LongTy); |
| 3255 | Params.push_back(IdType); |
| 3256 | Params.push_back(Ctx.BoolTy); |
| 3257 | Params.push_back(Ctx.BoolTy); |
| 3258 | FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
| 3259 | SetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_setProperty"); |
| 3260 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3261 | // Enumeration mutation. |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3262 | |
| 3263 | // void objc_enumerationMutation (id) |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3264 | Params.clear(); |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3265 | Params.push_back(IdType); |
| 3266 | FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
| 3267 | EnumerationMutationFn = CGM.CreateRuntimeFunction(FTy, |
| 3268 | "objc_enumerationMutation"); |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3269 | |
| 3270 | // gc's API |
| 3271 | // id objc_read_weak (id *) |
| 3272 | Params.clear(); |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3273 | Params.push_back(Ctx.getPointerType(IdType)); |
| 3274 | FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false); |
| 3275 | GcReadWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_read_weak"); |
| 3276 | |
| 3277 | // id objc_assign_weak (id, id *) |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3278 | Params.clear(); |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3279 | Params.push_back(IdType); |
| 3280 | Params.push_back(Ctx.getPointerType(IdType)); |
| 3281 | |
| 3282 | FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false); |
| 3283 | GcAssignWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_weak"); |
| 3284 | GcAssignGlobalFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_global"); |
| 3285 | GcAssignIvarFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar"); |
| 3286 | GcAssignStrongCastFn = |
| 3287 | CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast"); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 3288 | |
| 3289 | // void objc_exception_throw(id) |
| 3290 | Params.clear(); |
| 3291 | Params.push_back(IdType); |
| 3292 | |
| 3293 | FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 3294 | ExceptionThrowFn = |
| 3295 | CGM.CreateRuntimeFunction(FTy, "objc_exception_throw"); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3296 | |
| 3297 | // synchronized APIs |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3298 | // void objc_sync_exit (id) |
| 3299 | Params.clear(); |
| 3300 | Params.push_back(IdType); |
| 3301 | |
| 3302 | FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3303 | SyncExitFn = CGM.CreateRuntimeFunction(FTy, "objc_sync_exit"); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3304 | } |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3305 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3306 | ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) |
| 3307 | : ObjCCommonTypesHelper(cgm) |
| 3308 | { |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3309 | // struct _objc_method_description { |
| 3310 | // SEL name; |
| 3311 | // char *types; |
| 3312 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3313 | MethodDescriptionTy = |
| 3314 | llvm::StructType::get(SelectorPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3315 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3316 | NULL); |
| 3317 | CGM.getModule().addTypeName("struct._objc_method_description", |
| 3318 | MethodDescriptionTy); |
| 3319 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3320 | // struct _objc_method_description_list { |
| 3321 | // int count; |
| 3322 | // struct _objc_method_description[1]; |
| 3323 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3324 | MethodDescriptionListTy = |
| 3325 | llvm::StructType::get(IntTy, |
| 3326 | llvm::ArrayType::get(MethodDescriptionTy, 0), |
| 3327 | NULL); |
| 3328 | CGM.getModule().addTypeName("struct._objc_method_description_list", |
| 3329 | MethodDescriptionListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3330 | |
| 3331 | // struct _objc_method_description_list * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3332 | MethodDescriptionListPtrTy = |
| 3333 | llvm::PointerType::getUnqual(MethodDescriptionListTy); |
| 3334 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3335 | // Protocol description structures |
| 3336 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3337 | // struct _objc_protocol_extension { |
| 3338 | // uint32_t size; // sizeof(struct _objc_protocol_extension) |
| 3339 | // struct _objc_method_description_list *optional_instance_methods; |
| 3340 | // struct _objc_method_description_list *optional_class_methods; |
| 3341 | // struct _objc_property_list *instance_properties; |
| 3342 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3343 | ProtocolExtensionTy = |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3344 | llvm::StructType::get(IntTy, |
| 3345 | MethodDescriptionListPtrTy, |
| 3346 | MethodDescriptionListPtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3347 | PropertyListPtrTy, |
| 3348 | NULL); |
| 3349 | CGM.getModule().addTypeName("struct._objc_protocol_extension", |
| 3350 | ProtocolExtensionTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3351 | |
| 3352 | // struct _objc_protocol_extension * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3353 | ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy); |
| 3354 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3355 | // Handle recursive construction of Protocol and ProtocolList types |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3356 | |
| 3357 | llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(); |
| 3358 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 3359 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3360 | const llvm::Type *T = |
| 3361 | llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 3362 | LongTy, |
| 3363 | llvm::ArrayType::get(ProtocolTyHolder, 0), |
| 3364 | NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3365 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T); |
| 3366 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3367 | // struct _objc_protocol { |
| 3368 | // struct _objc_protocol_extension *isa; |
| 3369 | // char *protocol_name; |
| 3370 | // struct _objc_protocol **_objc_protocol_list; |
| 3371 | // struct _objc_method_description_list *instance_methods; |
| 3372 | // struct _objc_method_description_list *class_methods; |
| 3373 | // } |
| 3374 | T = llvm::StructType::get(ProtocolExtensionPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3375 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3376 | llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 3377 | MethodDescriptionListPtrTy, |
| 3378 | MethodDescriptionListPtrTy, |
| 3379 | NULL); |
| 3380 | cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T); |
| 3381 | |
| 3382 | ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get()); |
| 3383 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 3384 | ProtocolListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3385 | // struct _objc_protocol_list * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3386 | ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy); |
| 3387 | |
| 3388 | ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get()); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3389 | CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3390 | ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3391 | |
| 3392 | // Class description structures |
| 3393 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3394 | // struct _objc_ivar { |
| 3395 | // char *ivar_name; |
| 3396 | // char *ivar_type; |
| 3397 | // int ivar_offset; |
| 3398 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3399 | IvarTy = llvm::StructType::get(Int8PtrTy, |
| 3400 | Int8PtrTy, |
| 3401 | IntTy, |
| 3402 | NULL); |
| 3403 | CGM.getModule().addTypeName("struct._objc_ivar", IvarTy); |
| 3404 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3405 | // struct _objc_ivar_list * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3406 | IvarListTy = llvm::OpaqueType::get(); |
| 3407 | CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy); |
| 3408 | IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy); |
| 3409 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3410 | // struct _objc_method_list * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3411 | MethodListTy = llvm::OpaqueType::get(); |
| 3412 | CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy); |
| 3413 | MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy); |
| 3414 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3415 | // struct _objc_class_extension * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3416 | ClassExtensionTy = |
| 3417 | llvm::StructType::get(IntTy, |
| 3418 | Int8PtrTy, |
| 3419 | PropertyListPtrTy, |
| 3420 | NULL); |
| 3421 | CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy); |
| 3422 | ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy); |
| 3423 | |
| 3424 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 3425 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3426 | // struct _objc_class { |
| 3427 | // Class isa; |
| 3428 | // Class super_class; |
| 3429 | // char *name; |
| 3430 | // long version; |
| 3431 | // long info; |
| 3432 | // long instance_size; |
| 3433 | // struct _objc_ivar_list *ivars; |
| 3434 | // struct _objc_method_list *methods; |
| 3435 | // struct _objc_cache *cache; |
| 3436 | // struct _objc_protocol_list *protocols; |
| 3437 | // char *ivar_layout; |
| 3438 | // struct _objc_class_ext *ext; |
| 3439 | // }; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3440 | T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 3441 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 3442 | Int8PtrTy, |
| 3443 | LongTy, |
| 3444 | LongTy, |
| 3445 | LongTy, |
| 3446 | IvarListPtrTy, |
| 3447 | MethodListPtrTy, |
| 3448 | CachePtrTy, |
| 3449 | ProtocolListPtrTy, |
| 3450 | Int8PtrTy, |
| 3451 | ClassExtensionPtrTy, |
| 3452 | NULL); |
| 3453 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T); |
| 3454 | |
| 3455 | ClassTy = cast<llvm::StructType>(ClassTyHolder.get()); |
| 3456 | CGM.getModule().addTypeName("struct._objc_class", ClassTy); |
| 3457 | ClassPtrTy = llvm::PointerType::getUnqual(ClassTy); |
| 3458 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3459 | // struct _objc_category { |
| 3460 | // char *category_name; |
| 3461 | // char *class_name; |
| 3462 | // struct _objc_method_list *instance_method; |
| 3463 | // struct _objc_method_list *class_method; |
| 3464 | // uint32_t size; // sizeof(struct _objc_category) |
| 3465 | // struct _objc_property_list *instance_properties;// category's @property |
| 3466 | // } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3467 | CategoryTy = llvm::StructType::get(Int8PtrTy, |
| 3468 | Int8PtrTy, |
| 3469 | MethodListPtrTy, |
| 3470 | MethodListPtrTy, |
| 3471 | ProtocolListPtrTy, |
| 3472 | IntTy, |
| 3473 | PropertyListPtrTy, |
| 3474 | NULL); |
| 3475 | CGM.getModule().addTypeName("struct._objc_category", CategoryTy); |
| 3476 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3477 | // Global metadata structures |
| 3478 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3479 | // struct _objc_symtab { |
| 3480 | // long sel_ref_cnt; |
| 3481 | // SEL *refs; |
| 3482 | // short cls_def_cnt; |
| 3483 | // short cat_def_cnt; |
| 3484 | // char *defs[cls_def_cnt + cat_def_cnt]; |
| 3485 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3486 | SymtabTy = llvm::StructType::get(LongTy, |
| 3487 | SelectorPtrTy, |
| 3488 | ShortTy, |
| 3489 | ShortTy, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3490 | llvm::ArrayType::get(Int8PtrTy, 0), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3491 | NULL); |
| 3492 | CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy); |
| 3493 | SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy); |
| 3494 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3495 | // struct _objc_module { |
| 3496 | // long version; |
| 3497 | // long size; // sizeof(struct _objc_module) |
| 3498 | // char *name; |
| 3499 | // struct _objc_symtab* symtab; |
| 3500 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3501 | ModuleTy = |
| 3502 | llvm::StructType::get(LongTy, |
| 3503 | LongTy, |
| 3504 | Int8PtrTy, |
| 3505 | SymtabPtrTy, |
| 3506 | NULL); |
| 3507 | CGM.getModule().addTypeName("struct._objc_module", ModuleTy); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3508 | |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 3509 | // Message send functions. |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3510 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3511 | // id objc_msgSend (id, SEL, ...) |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3512 | std::vector<const llvm::Type*> Params; |
| 3513 | Params.push_back(ObjectPtrTy); |
| 3514 | Params.push_back(SelectorPtrTy); |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3515 | MessageSendFn = |
| 3516 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3517 | Params, |
| 3518 | true), |
| 3519 | "objc_msgSend"); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3520 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3521 | // id objc_msgSend_stret (id, SEL, ...) |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3522 | Params.clear(); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3523 | Params.push_back(ObjectPtrTy); |
| 3524 | Params.push_back(SelectorPtrTy); |
| 3525 | MessageSendStretFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3526 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3527 | Params, |
| 3528 | true), |
| 3529 | "objc_msgSend_stret"); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 3530 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3531 | // |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 3532 | Params.clear(); |
| 3533 | Params.push_back(ObjectPtrTy); |
| 3534 | Params.push_back(SelectorPtrTy); |
| 3535 | // FIXME: This should be long double on x86_64? |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3536 | // [double | long double] objc_msgSend_fpret(id self, SEL op, ...) |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 3537 | MessageSendFpretFn = |
| 3538 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy, |
| 3539 | Params, |
| 3540 | true), |
| 3541 | "objc_msgSend_fpret"); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3542 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3543 | // id objc_msgSendSuper(struct objc_super *super, SEL op, ...) |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3544 | Params.clear(); |
| 3545 | Params.push_back(SuperPtrTy); |
| 3546 | Params.push_back(SelectorPtrTy); |
| 3547 | MessageSendSuperFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3548 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3549 | Params, |
| 3550 | true), |
| 3551 | "objc_msgSendSuper"); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3552 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3553 | // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super, |
| 3554 | // SEL op, ...) |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3555 | Params.clear(); |
| 3556 | Params.push_back(Int8PtrTy); |
| 3557 | Params.push_back(SuperPtrTy); |
| 3558 | Params.push_back(SelectorPtrTy); |
| 3559 | MessageSendSuperStretFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3560 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3561 | Params, |
| 3562 | true), |
| 3563 | "objc_msgSendSuper_stret"); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 3564 | |
| 3565 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 3566 | MessageSendSuperFpretFn = MessageSendSuperFn; |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 3567 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3568 | // FIXME: This is the size of the setjmp buffer and should be |
| 3569 | // target specific. 18 is what's used on 32-bit X86. |
| 3570 | uint64_t SetJmpBufferSize = 18; |
| 3571 | |
| 3572 | // Exceptions |
| 3573 | const llvm::Type *StackPtrTy = |
Daniel Dunbar | 1000491 | 2008-09-27 06:32:25 +0000 | [diff] [blame] | 3574 | llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3575 | |
| 3576 | ExceptionDataTy = |
| 3577 | llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty, |
| 3578 | SetJmpBufferSize), |
| 3579 | StackPtrTy, NULL); |
| 3580 | CGM.getModule().addTypeName("struct._objc_exception_data", |
| 3581 | ExceptionDataTy); |
| 3582 | |
| 3583 | Params.clear(); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3584 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
| 3585 | ExceptionTryEnterFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3586 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3587 | Params, |
| 3588 | false), |
| 3589 | "objc_exception_try_enter"); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3590 | ExceptionTryExitFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3591 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3592 | Params, |
| 3593 | false), |
| 3594 | "objc_exception_try_exit"); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3595 | ExceptionExtractFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3596 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3597 | Params, |
| 3598 | false), |
| 3599 | "objc_exception_extract"); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3600 | |
| 3601 | Params.clear(); |
| 3602 | Params.push_back(ClassPtrTy); |
| 3603 | Params.push_back(ObjectPtrTy); |
| 3604 | ExceptionMatchFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3605 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 3606 | Params, |
| 3607 | false), |
| 3608 | "objc_exception_match"); |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 3609 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3610 | Params.clear(); |
| 3611 | Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty)); |
| 3612 | SetJmpFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3613 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 3614 | Params, |
| 3615 | false), |
| 3616 | "_setjmp"); |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 3617 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3618 | } |
| 3619 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3620 | ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3621 | : ObjCCommonTypesHelper(cgm) |
| 3622 | { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3623 | // struct _method_list_t { |
| 3624 | // uint32_t entsize; // sizeof(struct _objc_method) |
| 3625 | // uint32_t method_count; |
| 3626 | // struct _objc_method method_list[method_count]; |
| 3627 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3628 | MethodListnfABITy = llvm::StructType::get(IntTy, |
| 3629 | IntTy, |
| 3630 | llvm::ArrayType::get(MethodTy, 0), |
| 3631 | NULL); |
| 3632 | CGM.getModule().addTypeName("struct.__method_list_t", |
| 3633 | MethodListnfABITy); |
| 3634 | // struct method_list_t * |
| 3635 | MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3636 | |
| 3637 | // struct _protocol_t { |
| 3638 | // id isa; // NULL |
| 3639 | // const char * const protocol_name; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3640 | // const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3641 | // const struct method_list_t * const instance_methods; |
| 3642 | // const struct method_list_t * const class_methods; |
| 3643 | // const struct method_list_t *optionalInstanceMethods; |
| 3644 | // const struct method_list_t *optionalClassMethods; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3645 | // const struct _prop_list_t * properties; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3646 | // const uint32_t size; // sizeof(struct _protocol_t) |
| 3647 | // const uint32_t flags; // = 0 |
| 3648 | // } |
| 3649 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3650 | // Holder for struct _protocol_list_t * |
| 3651 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 3652 | |
| 3653 | ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy, |
| 3654 | Int8PtrTy, |
| 3655 | llvm::PointerType::getUnqual( |
| 3656 | ProtocolListTyHolder), |
| 3657 | MethodListnfABIPtrTy, |
| 3658 | MethodListnfABIPtrTy, |
| 3659 | MethodListnfABIPtrTy, |
| 3660 | MethodListnfABIPtrTy, |
| 3661 | PropertyListPtrTy, |
| 3662 | IntTy, |
| 3663 | IntTy, |
| 3664 | NULL); |
| 3665 | CGM.getModule().addTypeName("struct._protocol_t", |
| 3666 | ProtocolnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3667 | |
| 3668 | // struct _protocol_t* |
| 3669 | ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3670 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 3671 | // struct _protocol_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3672 | // long protocol_count; // Note, this is 32/64 bit |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3673 | // struct _protocol_t *[protocol_count]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3674 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3675 | ProtocolListnfABITy = llvm::StructType::get(LongTy, |
| 3676 | llvm::ArrayType::get( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3677 | ProtocolnfABIPtrTy, 0), |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3678 | NULL); |
| 3679 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 3680 | ProtocolListnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3681 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo( |
| 3682 | ProtocolListnfABITy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3683 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3684 | // struct _objc_protocol_list* |
| 3685 | ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3686 | |
| 3687 | // struct _ivar_t { |
| 3688 | // unsigned long int *offset; // pointer to ivar offset location |
| 3689 | // char *name; |
| 3690 | // char *type; |
| 3691 | // uint32_t alignment; |
| 3692 | // uint32_t size; |
| 3693 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3694 | IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy), |
| 3695 | Int8PtrTy, |
| 3696 | Int8PtrTy, |
| 3697 | IntTy, |
| 3698 | IntTy, |
| 3699 | NULL); |
| 3700 | CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy); |
| 3701 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3702 | // struct _ivar_list_t { |
| 3703 | // uint32 entsize; // sizeof(struct _ivar_t) |
| 3704 | // uint32 count; |
| 3705 | // struct _iver_t list[count]; |
| 3706 | // } |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3707 | IvarListnfABITy = llvm::StructType::get(IntTy, |
| 3708 | IntTy, |
| 3709 | llvm::ArrayType::get( |
| 3710 | IvarnfABITy, 0), |
| 3711 | NULL); |
| 3712 | CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy); |
| 3713 | |
| 3714 | IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3715 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3716 | // struct _class_ro_t { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3717 | // uint32_t const flags; |
| 3718 | // uint32_t const instanceStart; |
| 3719 | // uint32_t const instanceSize; |
| 3720 | // uint32_t const reserved; // only when building for 64bit targets |
| 3721 | // const uint8_t * const ivarLayout; |
| 3722 | // const char *const name; |
| 3723 | // const struct _method_list_t * const baseMethods; |
| 3724 | // const struct _objc_protocol_list *const baseProtocols; |
| 3725 | // const struct _ivar_list_t *const ivars; |
| 3726 | // const uint8_t * const weakIvarLayout; |
| 3727 | // const struct _prop_list_t * const properties; |
| 3728 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3729 | |
| 3730 | // FIXME. Add 'reserved' field in 64bit abi mode! |
| 3731 | ClassRonfABITy = llvm::StructType::get(IntTy, |
| 3732 | IntTy, |
| 3733 | IntTy, |
| 3734 | Int8PtrTy, |
| 3735 | Int8PtrTy, |
| 3736 | MethodListnfABIPtrTy, |
| 3737 | ProtocolListnfABIPtrTy, |
| 3738 | IvarListnfABIPtrTy, |
| 3739 | Int8PtrTy, |
| 3740 | PropertyListPtrTy, |
| 3741 | NULL); |
| 3742 | CGM.getModule().addTypeName("struct._class_ro_t", |
| 3743 | ClassRonfABITy); |
| 3744 | |
| 3745 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 3746 | std::vector<const llvm::Type*> Params; |
| 3747 | Params.push_back(ObjectPtrTy); |
| 3748 | Params.push_back(SelectorPtrTy); |
| 3749 | ImpnfABITy = llvm::PointerType::getUnqual( |
| 3750 | llvm::FunctionType::get(ObjectPtrTy, Params, false)); |
| 3751 | |
| 3752 | // struct _class_t { |
| 3753 | // struct _class_t *isa; |
| 3754 | // struct _class_t * const superclass; |
| 3755 | // void *cache; |
| 3756 | // IMP *vtable; |
| 3757 | // struct class_ro_t *ro; |
| 3758 | // } |
| 3759 | |
| 3760 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 3761 | ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 3762 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 3763 | CachePtrTy, |
| 3764 | llvm::PointerType::getUnqual(ImpnfABITy), |
| 3765 | llvm::PointerType::getUnqual( |
| 3766 | ClassRonfABITy), |
| 3767 | NULL); |
| 3768 | CGM.getModule().addTypeName("struct._class_t", ClassnfABITy); |
| 3769 | |
| 3770 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo( |
| 3771 | ClassnfABITy); |
| 3772 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3773 | // LLVM for struct _class_t * |
| 3774 | ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy); |
| 3775 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3776 | // struct _category_t { |
| 3777 | // const char * const name; |
| 3778 | // struct _class_t *const cls; |
| 3779 | // const struct _method_list_t * const instance_methods; |
| 3780 | // const struct _method_list_t * const class_methods; |
| 3781 | // const struct _protocol_list_t * const protocols; |
| 3782 | // const struct _prop_list_t * const properties; |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3783 | // } |
| 3784 | CategorynfABITy = llvm::StructType::get(Int8PtrTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3785 | ClassnfABIPtrTy, |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3786 | MethodListnfABIPtrTy, |
| 3787 | MethodListnfABIPtrTy, |
| 3788 | ProtocolListnfABIPtrTy, |
| 3789 | PropertyListPtrTy, |
| 3790 | NULL); |
| 3791 | CGM.getModule().addTypeName("struct._category_t", CategorynfABITy); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3792 | |
| 3793 | // New types for nonfragile abi messaging. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3794 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3795 | ASTContext &Ctx = CGM.getContext(); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3796 | |
| 3797 | // MessageRefTy - LLVM for: |
| 3798 | // struct _message_ref_t { |
| 3799 | // IMP messenger; |
| 3800 | // SEL name; |
| 3801 | // }; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3802 | |
| 3803 | // First the clang type for struct _message_ref_t |
| 3804 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 3805 | SourceLocation(), |
| 3806 | &Ctx.Idents.get("_message_ref_t")); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3807 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3808 | Ctx.VoidPtrTy, 0, false)); |
| 3809 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3810 | Ctx.getObjCSelType(), 0, false)); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3811 | RD->completeDefinition(Ctx); |
| 3812 | |
| 3813 | MessageRefCTy = Ctx.getTagDeclType(RD); |
| 3814 | MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy); |
| 3815 | MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy)); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3816 | |
| 3817 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
| 3818 | MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy); |
| 3819 | |
| 3820 | // SuperMessageRefTy - LLVM for: |
| 3821 | // struct _super_message_ref_t { |
| 3822 | // SUPER_IMP messenger; |
| 3823 | // SEL name; |
| 3824 | // }; |
| 3825 | SuperMessageRefTy = llvm::StructType::get(ImpnfABITy, |
| 3826 | SelectorPtrTy, |
| 3827 | NULL); |
| 3828 | CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy); |
| 3829 | |
| 3830 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
| 3831 | SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy); |
| 3832 | |
| 3833 | // id objc_msgSend_fixup (id, struct message_ref_t*, ...) |
| 3834 | Params.clear(); |
| 3835 | Params.push_back(ObjectPtrTy); |
| 3836 | Params.push_back(MessageRefPtrTy); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 3837 | MessengerTy = llvm::FunctionType::get(ObjectPtrTy, |
| 3838 | Params, |
| 3839 | true); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3840 | MessageSendFixupFn = |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 3841 | CGM.CreateRuntimeFunction(MessengerTy, |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3842 | "objc_msgSend_fixup"); |
| 3843 | |
| 3844 | // id objc_msgSend_fpret_fixup (id, struct message_ref_t*, ...) |
| 3845 | MessageSendFpretFixupFn = |
| 3846 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3847 | Params, |
| 3848 | true), |
| 3849 | "objc_msgSend_fpret_fixup"); |
| 3850 | |
| 3851 | // id objc_msgSend_stret_fixup (id, struct message_ref_t*, ...) |
| 3852 | MessageSendStretFixupFn = |
| 3853 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3854 | Params, |
| 3855 | true), |
| 3856 | "objc_msgSend_stret_fixup"); |
| 3857 | |
| 3858 | // id objc_msgSendId_fixup (id, struct message_ref_t*, ...) |
| 3859 | MessageSendIdFixupFn = |
| 3860 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3861 | Params, |
| 3862 | true), |
| 3863 | "objc_msgSendId_fixup"); |
| 3864 | |
| 3865 | |
| 3866 | // id objc_msgSendId_stret_fixup (id, struct message_ref_t*, ...) |
| 3867 | MessageSendIdStretFixupFn = |
| 3868 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3869 | Params, |
| 3870 | true), |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3871 | "objc_msgSendId_stret_fixup"); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3872 | |
| 3873 | // id objc_msgSendSuper2_fixup (struct objc_super *, |
| 3874 | // struct _super_message_ref_t*, ...) |
| 3875 | Params.clear(); |
| 3876 | Params.push_back(SuperPtrTy); |
| 3877 | Params.push_back(SuperMessageRefPtrTy); |
| 3878 | MessageSendSuper2FixupFn = |
| 3879 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3880 | Params, |
| 3881 | true), |
| 3882 | "objc_msgSendSuper2_fixup"); |
| 3883 | |
| 3884 | |
| 3885 | // id objc_msgSendSuper2_stret_fixup (struct objc_super *, |
| 3886 | // struct _super_message_ref_t*, ...) |
| 3887 | MessageSendSuper2StretFixupFn = |
| 3888 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3889 | Params, |
| 3890 | true), |
| 3891 | "objc_msgSendSuper2_stret_fixup"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 3892 | |
| 3893 | Params.clear(); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 3894 | Params.push_back(Int8PtrTy); |
| 3895 | UnwindResumeOrRethrowFn = |
| 3896 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3897 | Params, |
| 3898 | false), |
| 3899 | "_Unwind_Resume_or_Rethrow"); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 3900 | ObjCBeginCatchFn = |
| 3901 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy, |
| 3902 | Params, |
| 3903 | false), |
| 3904 | "objc_begin_catch"); |
| 3905 | |
| 3906 | Params.clear(); |
| 3907 | ObjCEndCatchFn = |
| 3908 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3909 | Params, |
| 3910 | false), |
| 3911 | "objc_end_catch"); |
| 3912 | |
| 3913 | // struct objc_typeinfo { |
| 3914 | // const void** vtable; // objc_ehtype_vtable + 2 |
| 3915 | // const char* name; // c++ typeinfo string |
| 3916 | // Class cls; |
| 3917 | // }; |
| 3918 | EHTypeTy = llvm::StructType::get(llvm::PointerType::getUnqual(Int8PtrTy), |
| 3919 | Int8PtrTy, |
| 3920 | ClassnfABIPtrTy, |
| 3921 | NULL); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 3922 | CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 3923 | EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3924 | } |
| 3925 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3926 | llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() { |
| 3927 | FinishNonFragileABIModule(); |
| 3928 | |
| 3929 | return NULL; |
| 3930 | } |
| 3931 | |
| 3932 | void CGObjCNonFragileABIMac::FinishNonFragileABIModule() { |
| 3933 | // nonfragile abi has no module definition. |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 3934 | |
| 3935 | // Build list of all implemented classe addresses in array |
| 3936 | // L_OBJC_LABEL_CLASS_$. |
| 3937 | // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$ |
| 3938 | // list of 'nonlazy' implementations (defined as those with a +load{} |
| 3939 | // method!!). |
| 3940 | unsigned NumClasses = DefinedClasses.size(); |
| 3941 | if (NumClasses) { |
| 3942 | std::vector<llvm::Constant*> Symbols(NumClasses); |
| 3943 | for (unsigned i=0; i<NumClasses; i++) |
| 3944 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
| 3945 | ObjCTypes.Int8PtrTy); |
| 3946 | llvm::Constant* Init = |
| 3947 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 3948 | NumClasses), |
| 3949 | Symbols); |
| 3950 | |
| 3951 | llvm::GlobalVariable *GV = |
| 3952 | new llvm::GlobalVariable(Init->getType(), false, |
| 3953 | llvm::GlobalValue::InternalLinkage, |
| 3954 | Init, |
| 3955 | "\01L_OBJC_LABEL_CLASS_$", |
| 3956 | &CGM.getModule()); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 3957 | GV->setAlignment(8); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 3958 | GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip"); |
| 3959 | UsedGlobals.push_back(GV); |
| 3960 | } |
| 3961 | |
| 3962 | // Build list of all implemented category addresses in array |
| 3963 | // L_OBJC_LABEL_CATEGORY_$. |
| 3964 | // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$ |
| 3965 | // list of 'nonlazy' category implementations (defined as those with a +load{} |
| 3966 | // method!!). |
| 3967 | unsigned NumCategory = DefinedCategories.size(); |
| 3968 | if (NumCategory) { |
| 3969 | std::vector<llvm::Constant*> Symbols(NumCategory); |
| 3970 | for (unsigned i=0; i<NumCategory; i++) |
| 3971 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
| 3972 | ObjCTypes.Int8PtrTy); |
| 3973 | llvm::Constant* Init = |
| 3974 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 3975 | NumCategory), |
| 3976 | Symbols); |
| 3977 | |
| 3978 | llvm::GlobalVariable *GV = |
| 3979 | new llvm::GlobalVariable(Init->getType(), false, |
| 3980 | llvm::GlobalValue::InternalLinkage, |
| 3981 | Init, |
| 3982 | "\01L_OBJC_LABEL_CATEGORY_$", |
| 3983 | &CGM.getModule()); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 3984 | GV->setAlignment(8); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 3985 | GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip"); |
| 3986 | UsedGlobals.push_back(GV); |
| 3987 | } |
| 3988 | |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 3989 | // static int L_OBJC_IMAGE_INFO[2] = { 0, flags }; |
| 3990 | // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0 |
| 3991 | std::vector<llvm::Constant*> Values(2); |
| 3992 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0); |
Fariborz Jahanian | 067986e | 2009-02-24 21:08:09 +0000 | [diff] [blame] | 3993 | unsigned int flags = 0; |
Fariborz Jahanian | 66a5c2c | 2009-02-24 23:34:44 +0000 | [diff] [blame] | 3994 | // FIXME: Fix and continue? |
| 3995 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 3996 | flags |= eImageInfo_GarbageCollected; |
| 3997 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 3998 | flags |= eImageInfo_GCOnly; |
Fariborz Jahanian | 067986e | 2009-02-24 21:08:09 +0000 | [diff] [blame] | 3999 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4000 | llvm::Constant* Init = llvm::ConstantArray::get( |
| 4001 | llvm::ArrayType::get(ObjCTypes.IntTy, 2), |
| 4002 | Values); |
| 4003 | llvm::GlobalVariable *IMGV = |
| 4004 | new llvm::GlobalVariable(Init->getType(), false, |
| 4005 | llvm::GlobalValue::InternalLinkage, |
| 4006 | Init, |
| 4007 | "\01L_OBJC_IMAGE_INFO", |
| 4008 | &CGM.getModule()); |
| 4009 | IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip"); |
| 4010 | UsedGlobals.push_back(IMGV); |
| 4011 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4012 | std::vector<llvm::Constant*> Used; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4013 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4014 | for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), |
| 4015 | e = UsedGlobals.end(); i != e; ++i) { |
| 4016 | Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); |
| 4017 | } |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4018 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4019 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); |
| 4020 | llvm::GlobalValue *GV = |
| 4021 | new llvm::GlobalVariable(AT, false, |
| 4022 | llvm::GlobalValue::AppendingLinkage, |
| 4023 | llvm::ConstantArray::get(AT, Used), |
| 4024 | "llvm.used", |
| 4025 | &CGM.getModule()); |
| 4026 | |
| 4027 | GV->setSection("llvm.metadata"); |
| 4028 | |
| 4029 | } |
| 4030 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4031 | // Metadata flags |
| 4032 | enum MetaDataDlags { |
| 4033 | CLS = 0x0, |
| 4034 | CLS_META = 0x1, |
| 4035 | CLS_ROOT = 0x2, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4036 | OBJC2_CLS_HIDDEN = 0x10, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4037 | CLS_EXCEPTION = 0x20 |
| 4038 | }; |
| 4039 | /// BuildClassRoTInitializer - generate meta-data for: |
| 4040 | /// struct _class_ro_t { |
| 4041 | /// uint32_t const flags; |
| 4042 | /// uint32_t const instanceStart; |
| 4043 | /// uint32_t const instanceSize; |
| 4044 | /// uint32_t const reserved; // only when building for 64bit targets |
| 4045 | /// const uint8_t * const ivarLayout; |
| 4046 | /// const char *const name; |
| 4047 | /// const struct _method_list_t * const baseMethods; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4048 | /// const struct _protocol_list_t *const baseProtocols; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4049 | /// const struct _ivar_list_t *const ivars; |
| 4050 | /// const uint8_t * const weakIvarLayout; |
| 4051 | /// const struct _prop_list_t * const properties; |
| 4052 | /// } |
| 4053 | /// |
| 4054 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer( |
| 4055 | unsigned flags, |
| 4056 | unsigned InstanceStart, |
| 4057 | unsigned InstanceSize, |
| 4058 | const ObjCImplementationDecl *ID) { |
| 4059 | std::string ClassName = ID->getNameAsString(); |
| 4060 | std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets! |
| 4061 | Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
| 4062 | Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart); |
| 4063 | Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize); |
| 4064 | // FIXME. For 64bit targets add 0 here. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4065 | // FIXME. ivarLayout is currently null! |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 4066 | Values[ 3] = GetIvarLayoutName(0, ObjCTypes); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4067 | Values[ 4] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4068 | // const struct _method_list_t * const baseMethods; |
| 4069 | std::vector<llvm::Constant*> Methods; |
| 4070 | std::string MethodListName("\01l_OBJC_$_"); |
| 4071 | if (flags & CLS_META) { |
| 4072 | MethodListName += "CLASS_METHODS_" + ID->getNameAsString(); |
| 4073 | for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(), |
| 4074 | e = ID->classmeth_end(); i != e; ++i) { |
| 4075 | // Class methods should always be defined. |
| 4076 | Methods.push_back(GetMethodConstant(*i)); |
| 4077 | } |
| 4078 | } else { |
| 4079 | MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString(); |
| 4080 | for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(), |
| 4081 | e = ID->instmeth_end(); i != e; ++i) { |
| 4082 | // Instance methods should always be defined. |
| 4083 | Methods.push_back(GetMethodConstant(*i)); |
| 4084 | } |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4085 | for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(), |
| 4086 | e = ID->propimpl_end(); i != e; ++i) { |
| 4087 | ObjCPropertyImplDecl *PID = *i; |
| 4088 | |
| 4089 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){ |
| 4090 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 4091 | |
| 4092 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 4093 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4094 | Methods.push_back(C); |
| 4095 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 4096 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4097 | Methods.push_back(C); |
| 4098 | } |
| 4099 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4100 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4101 | Values[ 5] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4102 | "__DATA, __objc_const", Methods); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4103 | |
| 4104 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4105 | assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer"); |
| 4106 | Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_" |
| 4107 | + OID->getNameAsString(), |
| 4108 | OID->protocol_begin(), |
| 4109 | OID->protocol_end()); |
| 4110 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4111 | if (flags & CLS_META) |
| 4112 | Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
| 4113 | else |
| 4114 | Values[ 7] = EmitIvarList(ID); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4115 | // FIXME. weakIvarLayout is currently null. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 4116 | Values[ 8] = GetIvarLayoutName(0, ObjCTypes); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4117 | if (flags & CLS_META) |
| 4118 | Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 4119 | else |
| 4120 | Values[ 9] = |
| 4121 | EmitPropertyList( |
| 4122 | "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
| 4123 | ID, ID->getClassInterface(), ObjCTypes); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4124 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy, |
| 4125 | Values); |
| 4126 | llvm::GlobalVariable *CLASS_RO_GV = |
| 4127 | new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false, |
| 4128 | llvm::GlobalValue::InternalLinkage, |
| 4129 | Init, |
| 4130 | (flags & CLS_META) ? |
| 4131 | std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName : |
| 4132 | std::string("\01l_OBJC_CLASS_RO_$_")+ClassName, |
| 4133 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4134 | CLASS_RO_GV->setAlignment( |
| 4135 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4136 | CLASS_RO_GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4137 | return CLASS_RO_GV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4138 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4139 | } |
| 4140 | |
| 4141 | /// BuildClassMetaData - This routine defines that to-level meta-data |
| 4142 | /// for the given ClassName for: |
| 4143 | /// struct _class_t { |
| 4144 | /// struct _class_t *isa; |
| 4145 | /// struct _class_t * const superclass; |
| 4146 | /// void *cache; |
| 4147 | /// IMP *vtable; |
| 4148 | /// struct class_ro_t *ro; |
| 4149 | /// } |
| 4150 | /// |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4151 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData( |
| 4152 | std::string &ClassName, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4153 | llvm::Constant *IsAGV, |
| 4154 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4155 | llvm::Constant *ClassRoGV, |
| 4156 | bool HiddenVisibility) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4157 | std::vector<llvm::Constant*> Values(5); |
| 4158 | Values[0] = IsAGV; |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4159 | Values[1] = SuperClassGV |
| 4160 | ? SuperClassGV |
| 4161 | : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4162 | Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar |
| 4163 | Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar |
| 4164 | Values[4] = ClassRoGV; // &CLASS_RO_GV |
| 4165 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy, |
| 4166 | Values); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4167 | llvm::GlobalVariable *GV = GetClassGlobal(ClassName); |
| 4168 | GV->setInitializer(Init); |
Fariborz Jahanian | dd0db2a | 2009-01-31 01:07:39 +0000 | [diff] [blame] | 4169 | GV->setSection("__DATA, __objc_data"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4170 | GV->setAlignment( |
| 4171 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy)); |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4172 | if (HiddenVisibility) |
| 4173 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4174 | return GV; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4175 | } |
| 4176 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4177 | void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) { |
| 4178 | std::string ClassName = ID->getNameAsString(); |
| 4179 | if (!ObjCEmptyCacheVar) { |
| 4180 | ObjCEmptyCacheVar = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4181 | ObjCTypes.CacheTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4182 | false, |
| 4183 | llvm::GlobalValue::ExternalLinkage, |
| 4184 | 0, |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4185 | "_objc_empty_cache", |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4186 | &CGM.getModule()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4187 | |
| 4188 | ObjCEmptyVtableVar = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4189 | ObjCTypes.ImpnfABITy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4190 | false, |
| 4191 | llvm::GlobalValue::ExternalLinkage, |
| 4192 | 0, |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4193 | "_objc_empty_vtable", |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4194 | &CGM.getModule()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4195 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4196 | assert(ID->getClassInterface() && |
| 4197 | "CGObjCNonFragileABIMac::GenerateClass - class is 0"); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4198 | uint32_t InstanceStart = |
| 4199 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy); |
| 4200 | uint32_t InstanceSize = InstanceStart; |
| 4201 | uint32_t flags = CLS_META; |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4202 | std::string ObjCMetaClassName(getMetaclassSymbolPrefix()); |
| 4203 | std::string ObjCClassName(getClassSymbolPrefix()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4204 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4205 | llvm::GlobalVariable *SuperClassGV, *IsAGV; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4206 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4207 | bool classIsHidden = |
| 4208 | CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4209 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4210 | flags |= OBJC2_CLS_HIDDEN; |
| 4211 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4212 | // class is root |
| 4213 | flags |= CLS_ROOT; |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4214 | SuperClassGV = GetClassGlobal(ObjCClassName + ClassName); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4215 | IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4216 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4217 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4218 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 4219 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 4220 | Root = Super; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4221 | IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString()); |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4222 | // work on super class metadata symbol. |
| 4223 | std::string SuperClassName = |
| 4224 | ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4225 | SuperClassGV = GetClassGlobal(SuperClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4226 | } |
| 4227 | llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags, |
| 4228 | InstanceStart, |
| 4229 | InstanceSize,ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4230 | std::string TClassName = ObjCMetaClassName + ClassName; |
| 4231 | llvm::GlobalVariable *MetaTClass = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4232 | BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV, |
| 4233 | classIsHidden); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4234 | |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4235 | // Metadata for the class |
| 4236 | flags = CLS; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4237 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4238 | flags |= OBJC2_CLS_HIDDEN; |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4239 | |
| 4240 | if (hasObjCExceptionAttribute(ID->getClassInterface())) |
| 4241 | flags |= CLS_EXCEPTION; |
| 4242 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4243 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4244 | flags |= CLS_ROOT; |
| 4245 | SuperClassGV = 0; |
| 4246 | } |
| 4247 | else { |
| 4248 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4249 | std::string RootClassName = |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4250 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4251 | SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4252 | } |
Fariborz Jahanian | ebf9ed3 | 2009-03-20 20:48:19 +0000 | [diff] [blame] | 4253 | // FIXME: Gross |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4254 | InstanceStart = InstanceSize = 0; |
| 4255 | if (ObjCInterfaceDecl *OID = |
| 4256 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface())) { |
| 4257 | // FIXME. Share this with the one in EmitIvarList. |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 4258 | const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OID); |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4259 | |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 4260 | RecordDecl::field_iterator firstField, lastField; |
| 4261 | const RecordDecl *RD = GetFirstIvarInRecord(OID, firstField, lastField); |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4262 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4263 | for (RecordDecl::field_iterator e = RD->field_end(CGM.getContext()), |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4264 | ifield = firstField; ifield != e; ++ifield) |
| 4265 | lastField = ifield; |
| 4266 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4267 | if (lastField != RD->field_end(CGM.getContext())) { |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4268 | FieldDecl *Field = *lastField; |
| 4269 | const llvm::Type *FieldTy = |
| 4270 | CGM.getTypes().ConvertTypeForMem(Field->getType()); |
| 4271 | unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy); |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 4272 | InstanceSize = GetIvarBaseOffset(Layout, Field) + Size; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4273 | if (firstField == RD->field_end(CGM.getContext())) |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4274 | InstanceStart = InstanceSize; |
Fariborz Jahanian | 11894a4 | 2009-03-07 19:43:20 +0000 | [diff] [blame] | 4275 | else { |
| 4276 | Field = *firstField; |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 4277 | InstanceStart = GetIvarBaseOffset(Layout, Field); |
Fariborz Jahanian | 11894a4 | 2009-03-07 19:43:20 +0000 | [diff] [blame] | 4278 | } |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4279 | } |
| 4280 | } |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4281 | CLASS_RO_GV = BuildClassRoTInitializer(flags, |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4282 | InstanceStart, |
| 4283 | InstanceSize, |
| 4284 | ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4285 | |
| 4286 | TClassName = ObjCClassName + ClassName; |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4287 | llvm::GlobalVariable *ClassMD = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4288 | BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV, |
| 4289 | classIsHidden); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4290 | DefinedClasses.push_back(ClassMD); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4291 | |
| 4292 | // Force the definition of the EHType if necessary. |
| 4293 | if (flags & CLS_EXCEPTION) |
| 4294 | GetInterfaceEHType(ID->getClassInterface(), true); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4295 | } |
| 4296 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4297 | /// GenerateProtocolRef - This routine is called to generate code for |
| 4298 | /// a protocol reference expression; as in: |
| 4299 | /// @code |
| 4300 | /// @protocol(Proto1); |
| 4301 | /// @endcode |
| 4302 | /// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1 |
| 4303 | /// which will hold address of the protocol meta-data. |
| 4304 | /// |
| 4305 | llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder, |
| 4306 | const ObjCProtocolDecl *PD) { |
| 4307 | |
Fariborz Jahanian | 960cd06 | 2009-04-10 18:47:34 +0000 | [diff] [blame] | 4308 | // This routine is called for @protocol only. So, we must build definition |
| 4309 | // of protocol's meta-data (not a reference to it!) |
| 4310 | // |
| 4311 | llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD), |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4312 | ObjCTypes.ExternalProtocolPtrTy); |
| 4313 | |
| 4314 | std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_"); |
| 4315 | ProtocolName += PD->getNameAsCString(); |
| 4316 | |
| 4317 | llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName); |
| 4318 | if (PTGV) |
| 4319 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 4320 | PTGV = new llvm::GlobalVariable( |
| 4321 | Init->getType(), false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4322 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4323 | Init, |
| 4324 | ProtocolName, |
| 4325 | &CGM.getModule()); |
| 4326 | PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip"); |
| 4327 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4328 | UsedGlobals.push_back(PTGV); |
| 4329 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 4330 | } |
| 4331 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4332 | /// GenerateCategory - Build metadata for a category implementation. |
| 4333 | /// struct _category_t { |
| 4334 | /// const char * const name; |
| 4335 | /// struct _class_t *const cls; |
| 4336 | /// const struct _method_list_t * const instance_methods; |
| 4337 | /// const struct _method_list_t * const class_methods; |
| 4338 | /// const struct _protocol_list_t * const protocols; |
| 4339 | /// const struct _prop_list_t * const properties; |
| 4340 | /// } |
| 4341 | /// |
| 4342 | void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) |
| 4343 | { |
| 4344 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4345 | const char *Prefix = "\01l_OBJC_$_CATEGORY_"; |
| 4346 | std::string ExtCatName(Prefix + Interface->getNameAsString()+ |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4347 | "_$_" + OCD->getNameAsString()); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4348 | std::string ExtClassName(getClassSymbolPrefix() + |
| 4349 | Interface->getNameAsString()); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4350 | |
| 4351 | std::vector<llvm::Constant*> Values(6); |
| 4352 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 4353 | // meta-class entry symbol |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4354 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4355 | Values[1] = ClassGV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4356 | std::vector<llvm::Constant*> Methods; |
| 4357 | std::string MethodListName(Prefix); |
| 4358 | MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() + |
| 4359 | "_$_" + OCD->getNameAsString(); |
| 4360 | |
| 4361 | for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(), |
| 4362 | e = OCD->instmeth_end(); i != e; ++i) { |
| 4363 | // Instance methods should always be defined. |
| 4364 | Methods.push_back(GetMethodConstant(*i)); |
| 4365 | } |
| 4366 | |
| 4367 | Values[2] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4368 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4369 | Methods); |
| 4370 | |
| 4371 | MethodListName = Prefix; |
| 4372 | MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" + |
| 4373 | OCD->getNameAsString(); |
| 4374 | Methods.clear(); |
| 4375 | for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(), |
| 4376 | e = OCD->classmeth_end(); i != e; ++i) { |
| 4377 | // Class methods should always be defined. |
| 4378 | Methods.push_back(GetMethodConstant(*i)); |
| 4379 | } |
| 4380 | |
| 4381 | Values[3] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4382 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4383 | Methods); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4384 | const ObjCCategoryDecl *Category = |
| 4385 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 4386 | if (Category) { |
| 4387 | std::string ExtName(Interface->getNameAsString() + "_$_" + |
| 4388 | OCD->getNameAsString()); |
| 4389 | Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_" |
| 4390 | + Interface->getNameAsString() + "_$_" |
| 4391 | + Category->getNameAsString(), |
| 4392 | Category->protocol_begin(), |
| 4393 | Category->protocol_end()); |
| 4394 | Values[5] = |
| 4395 | EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName, |
| 4396 | OCD, Category, ObjCTypes); |
| 4397 | } |
| 4398 | else { |
| 4399 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 4400 | Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 4401 | } |
| 4402 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4403 | llvm::Constant *Init = |
| 4404 | llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy, |
| 4405 | Values); |
| 4406 | llvm::GlobalVariable *GCATV |
| 4407 | = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy, |
| 4408 | false, |
| 4409 | llvm::GlobalValue::InternalLinkage, |
| 4410 | Init, |
| 4411 | ExtCatName, |
| 4412 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4413 | GCATV->setAlignment( |
| 4414 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4415 | GCATV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4416 | UsedGlobals.push_back(GCATV); |
| 4417 | DefinedCategories.push_back(GCATV); |
| 4418 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4419 | |
| 4420 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 4421 | /// given method if it has been defined. The result is null if the |
| 4422 | /// method has not been defined. The return value has type MethodPtrTy. |
| 4423 | llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant( |
| 4424 | const ObjCMethodDecl *MD) { |
| 4425 | // FIXME: Use DenseMap::lookup |
| 4426 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 4427 | if (!Fn) |
| 4428 | return 0; |
| 4429 | |
| 4430 | std::vector<llvm::Constant*> Method(3); |
| 4431 | Method[0] = |
| 4432 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 4433 | ObjCTypes.SelectorPtrTy); |
| 4434 | Method[1] = GetMethodVarType(MD); |
| 4435 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 4436 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 4437 | } |
| 4438 | |
| 4439 | /// EmitMethodList - Build meta-data for method declarations |
| 4440 | /// struct _method_list_t { |
| 4441 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 4442 | /// uint32_t method_count; |
| 4443 | /// struct _objc_method method_list[method_count]; |
| 4444 | /// } |
| 4445 | /// |
| 4446 | llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList( |
| 4447 | const std::string &Name, |
| 4448 | const char *Section, |
| 4449 | const ConstantVector &Methods) { |
| 4450 | // Return null for empty list. |
| 4451 | if (Methods.empty()) |
| 4452 | return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy); |
| 4453 | |
| 4454 | std::vector<llvm::Constant*> Values(3); |
| 4455 | // sizeof(struct _objc_method) |
| 4456 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy); |
| 4457 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4458 | // method_count |
| 4459 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 4460 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 4461 | Methods.size()); |
| 4462 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 4463 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4464 | |
| 4465 | llvm::GlobalVariable *GV = |
| 4466 | new llvm::GlobalVariable(Init->getType(), false, |
| 4467 | llvm::GlobalValue::InternalLinkage, |
| 4468 | Init, |
| 4469 | Name, |
| 4470 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4471 | GV->setAlignment( |
| 4472 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4473 | GV->setSection(Section); |
| 4474 | UsedGlobals.push_back(GV); |
| 4475 | return llvm::ConstantExpr::getBitCast(GV, |
| 4476 | ObjCTypes.MethodListnfABIPtrTy); |
| 4477 | } |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4478 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4479 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 4480 | /// the given ivar. |
| 4481 | /// |
| 4482 | llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable( |
| 4483 | std::string &Name, |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 4484 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4485 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | dd13198 | 2009-04-14 22:44:26 +0000 | [diff] [blame] | 4486 | Name += "OBJC_IVAR_$_" + |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4487 | getInterfaceDeclForIvar(ID, Ivar, CGM.getContext())->getNameAsString() + |
| 4488 | '.' + Ivar->getNameAsString(); |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4489 | llvm::GlobalVariable *IvarOffsetGV = |
| 4490 | CGM.getModule().getGlobalVariable(Name); |
| 4491 | if (!IvarOffsetGV) |
| 4492 | IvarOffsetGV = |
| 4493 | new llvm::GlobalVariable(ObjCTypes.LongTy, |
| 4494 | false, |
| 4495 | llvm::GlobalValue::ExternalLinkage, |
| 4496 | 0, |
| 4497 | Name, |
| 4498 | &CGM.getModule()); |
| 4499 | return IvarOffsetGV; |
| 4500 | } |
| 4501 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4502 | llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar( |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4503 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4504 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4505 | unsigned long int Offset) { |
| 4506 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4507 | assert(ID && "EmitIvarOffsetVar - null interface decl."); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4508 | std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.' |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4509 | + Ivar->getNameAsString()); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4510 | llvm::Constant *Init = llvm::ConstantInt::get(ObjCTypes.LongTy, Offset); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4511 | llvm::GlobalVariable *IvarOffsetGV = |
| 4512 | CGM.getModule().getGlobalVariable(ExternalName); |
Fariborz Jahanian | 77c9fd2 | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 4513 | if (IvarOffsetGV) |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4514 | // ivar offset symbol already built due to user code referencing it. |
| 4515 | IvarOffsetGV->setInitializer(Init); |
Fariborz Jahanian | 77c9fd2 | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 4516 | else |
| 4517 | IvarOffsetGV = |
| 4518 | new llvm::GlobalVariable(Init->getType(), |
| 4519 | false, |
| 4520 | llvm::GlobalValue::ExternalLinkage, |
| 4521 | Init, |
| 4522 | ExternalName, |
| 4523 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4524 | IvarOffsetGV->setAlignment( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4525 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy)); |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4526 | // @private and @package have hidden visibility. |
| 4527 | bool globalVisibility = (Ivar->getAccessControl() == ObjCIvarDecl::Public || |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4528 | Ivar->getAccessControl() == ObjCIvarDecl::Protected); |
| 4529 | if (!globalVisibility || CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden) |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4530 | IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4531 | else |
Fariborz Jahanian | 77c9fd2 | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 4532 | IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4533 | IvarOffsetGV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4534 | return IvarOffsetGV; |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4535 | } |
| 4536 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4537 | /// EmitIvarList - Emit the ivar list for the given |
| 4538 | /// implementation. If ForClass is true the list of class ivars |
| 4539 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 4540 | /// interface ivars will be emitted. The return value has type |
| 4541 | /// IvarListnfABIPtrTy. |
| 4542 | /// struct _ivar_t { |
| 4543 | /// unsigned long int *offset; // pointer to ivar offset location |
| 4544 | /// char *name; |
| 4545 | /// char *type; |
| 4546 | /// uint32_t alignment; |
| 4547 | /// uint32_t size; |
| 4548 | /// } |
| 4549 | /// struct _ivar_list_t { |
| 4550 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 4551 | /// uint32 count; |
| 4552 | /// struct _iver_t list[count]; |
| 4553 | /// } |
| 4554 | /// |
| 4555 | llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList( |
| 4556 | const ObjCImplementationDecl *ID) { |
| 4557 | |
| 4558 | std::vector<llvm::Constant*> Ivars, Ivar(5); |
| 4559 | |
| 4560 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4561 | assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface"); |
| 4562 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4563 | // FIXME. Consolidate this with similar code in GenerateClass. |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 4564 | const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OID); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 4565 | |
| 4566 | RecordDecl::field_iterator i,p; |
| 4567 | const RecordDecl *RD = GetFirstIvarInRecord(OID, i,p); |
Fariborz Jahanian | 1819188 | 2009-03-31 18:11:23 +0000 | [diff] [blame] | 4568 | // collect declared and synthesized ivars in a small vector. |
| 4569 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
| 4570 | for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(), |
| 4571 | E = OID->ivar_end(); I != E; ++I) |
| 4572 | OIvars.push_back(*I); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4573 | for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(CGM.getContext()), |
| 4574 | E = OID->prop_end(CGM.getContext()); I != E; ++I) |
Fariborz Jahanian | 1819188 | 2009-03-31 18:11:23 +0000 | [diff] [blame] | 4575 | if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl()) |
| 4576 | OIvars.push_back(IV); |
Fariborz Jahanian | 99eee36 | 2009-04-01 19:37:34 +0000 | [diff] [blame] | 4577 | |
Fariborz Jahanian | 1819188 | 2009-03-31 18:11:23 +0000 | [diff] [blame] | 4578 | unsigned iv = 0; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4579 | for (RecordDecl::field_iterator e = RD->field_end(CGM.getContext()); |
| 4580 | i != e; ++i) { |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4581 | FieldDecl *Field = *i; |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 4582 | uint64_t offset = GetIvarBaseOffset(Layout, Field); |
Fariborz Jahanian | 1819188 | 2009-03-31 18:11:23 +0000 | [diff] [blame] | 4583 | Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), OIvars[iv++], offset); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4584 | if (Field->getIdentifier()) |
| 4585 | Ivar[1] = GetMethodVarName(Field->getIdentifier()); |
| 4586 | else |
| 4587 | Ivar[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4588 | Ivar[2] = GetMethodVarType(Field); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4589 | const llvm::Type *FieldTy = |
| 4590 | CGM.getTypes().ConvertTypeForMem(Field->getType()); |
| 4591 | unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy); |
| 4592 | unsigned Align = CGM.getContext().getPreferredTypeAlign( |
| 4593 | Field->getType().getTypePtr()) >> 3; |
| 4594 | Align = llvm::Log2_32(Align); |
| 4595 | Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align); |
Fariborz Jahanian | 07236ba | 2009-01-27 22:27:56 +0000 | [diff] [blame] | 4596 | // NOTE. Size of a bitfield does not match gcc's, because of the way |
| 4597 | // bitfields are treated special in each. But I am told that 'size' |
| 4598 | // for bitfield ivars is ignored by the runtime so it does not matter. |
| 4599 | // (even if it matters, some day, there is enough info. to get the bitfield |
| 4600 | // right! |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4601 | Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4602 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar)); |
| 4603 | } |
| 4604 | // Return null for empty list. |
| 4605 | if (Ivars.empty()) |
| 4606 | return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
| 4607 | std::vector<llvm::Constant*> Values(3); |
| 4608 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy); |
| 4609 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4610 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 4611 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy, |
| 4612 | Ivars.size()); |
| 4613 | Values[2] = llvm::ConstantArray::get(AT, Ivars); |
| 4614 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4615 | const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_"; |
| 4616 | llvm::GlobalVariable *GV = |
| 4617 | new llvm::GlobalVariable(Init->getType(), false, |
| 4618 | llvm::GlobalValue::InternalLinkage, |
| 4619 | Init, |
| 4620 | Prefix + OID->getNameAsString(), |
| 4621 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4622 | GV->setAlignment( |
| 4623 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4624 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4625 | |
| 4626 | UsedGlobals.push_back(GV); |
| 4627 | return llvm::ConstantExpr::getBitCast(GV, |
| 4628 | ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4629 | } |
| 4630 | |
| 4631 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef( |
| 4632 | const ObjCProtocolDecl *PD) { |
| 4633 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4634 | |
| 4635 | if (!Entry) { |
| 4636 | // We use the initializer as a marker of whether this is a forward |
| 4637 | // reference or not. At module finalization we add the empty |
| 4638 | // contents for protocols which were referenced but never defined. |
| 4639 | Entry = |
| 4640 | new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false, |
| 4641 | llvm::GlobalValue::ExternalLinkage, |
| 4642 | 0, |
| 4643 | "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(), |
| 4644 | &CGM.getModule()); |
| 4645 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 4646 | UsedGlobals.push_back(Entry); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4647 | } |
| 4648 | |
| 4649 | return Entry; |
| 4650 | } |
| 4651 | |
| 4652 | /// GetOrEmitProtocol - Generate the protocol meta-data: |
| 4653 | /// @code |
| 4654 | /// struct _protocol_t { |
| 4655 | /// id isa; // NULL |
| 4656 | /// const char * const protocol_name; |
| 4657 | /// const struct _protocol_list_t * protocol_list; // super protocols |
| 4658 | /// const struct method_list_t * const instance_methods; |
| 4659 | /// const struct method_list_t * const class_methods; |
| 4660 | /// const struct method_list_t *optionalInstanceMethods; |
| 4661 | /// const struct method_list_t *optionalClassMethods; |
| 4662 | /// const struct _prop_list_t * properties; |
| 4663 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 4664 | /// const uint32_t flags; // = 0 |
| 4665 | /// } |
| 4666 | /// @endcode |
| 4667 | /// |
| 4668 | |
| 4669 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol( |
| 4670 | const ObjCProtocolDecl *PD) { |
| 4671 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4672 | |
| 4673 | // Early exit if a defining object has already been generated. |
| 4674 | if (Entry && Entry->hasInitializer()) |
| 4675 | return Entry; |
| 4676 | |
| 4677 | const char *ProtocolName = PD->getNameAsCString(); |
| 4678 | |
| 4679 | // Construct method lists. |
| 4680 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 4681 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4682 | for (ObjCProtocolDecl::instmeth_iterator |
| 4683 | i = PD->instmeth_begin(CGM.getContext()), |
| 4684 | e = PD->instmeth_end(CGM.getContext()); |
| 4685 | i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4686 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4687 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4688 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4689 | OptInstanceMethods.push_back(C); |
| 4690 | } else { |
| 4691 | InstanceMethods.push_back(C); |
| 4692 | } |
| 4693 | } |
| 4694 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4695 | for (ObjCProtocolDecl::classmeth_iterator |
| 4696 | i = PD->classmeth_begin(CGM.getContext()), |
| 4697 | e = PD->classmeth_end(CGM.getContext()); |
| 4698 | i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4699 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4700 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4701 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4702 | OptClassMethods.push_back(C); |
| 4703 | } else { |
| 4704 | ClassMethods.push_back(C); |
| 4705 | } |
| 4706 | } |
| 4707 | |
| 4708 | std::vector<llvm::Constant*> Values(10); |
| 4709 | // isa is NULL |
| 4710 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy); |
| 4711 | Values[1] = GetClassName(PD->getIdentifier()); |
| 4712 | Values[2] = EmitProtocolList( |
| 4713 | "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(), |
| 4714 | PD->protocol_begin(), |
| 4715 | PD->protocol_end()); |
| 4716 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4717 | Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4718 | + PD->getNameAsString(), |
| 4719 | "__DATA, __objc_const", |
| 4720 | InstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4721 | Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4722 | + PD->getNameAsString(), |
| 4723 | "__DATA, __objc_const", |
| 4724 | ClassMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4725 | Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4726 | + PD->getNameAsString(), |
| 4727 | "__DATA, __objc_const", |
| 4728 | OptInstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4729 | Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4730 | + PD->getNameAsString(), |
| 4731 | "__DATA, __objc_const", |
| 4732 | OptClassMethods); |
| 4733 | Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(), |
| 4734 | 0, PD, ObjCTypes); |
| 4735 | uint32_t Size = |
| 4736 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy); |
| 4737 | Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4738 | Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy); |
| 4739 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy, |
| 4740 | Values); |
| 4741 | |
| 4742 | if (Entry) { |
| 4743 | // Already created, fix the linkage and update the initializer. |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4744 | Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4745 | Entry->setInitializer(Init); |
| 4746 | } else { |
| 4747 | Entry = |
| 4748 | new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4749 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4750 | Init, |
| 4751 | std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName, |
| 4752 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4753 | Entry->setAlignment( |
| 4754 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4755 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4756 | } |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4757 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4758 | |
| 4759 | // Use this protocol meta-data to build protocol list table in section |
| 4760 | // __DATA, __objc_protolist |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4761 | llvm::GlobalVariable *PTGV = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4762 | ObjCTypes.ProtocolnfABIPtrTy, false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4763 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4764 | Entry, |
| 4765 | std::string("\01l_OBJC_LABEL_PROTOCOL_$_") |
| 4766 | +ProtocolName, |
| 4767 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4768 | PTGV->setAlignment( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4769 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy)); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 4770 | PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip"); |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4771 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4772 | UsedGlobals.push_back(PTGV); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4773 | return Entry; |
| 4774 | } |
| 4775 | |
| 4776 | /// EmitProtocolList - Generate protocol list meta-data: |
| 4777 | /// @code |
| 4778 | /// struct _protocol_list_t { |
| 4779 | /// long protocol_count; // Note, this is 32/64 bit |
| 4780 | /// struct _protocol_t[protocol_count]; |
| 4781 | /// } |
| 4782 | /// @endcode |
| 4783 | /// |
| 4784 | llvm::Constant * |
| 4785 | CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name, |
| 4786 | ObjCProtocolDecl::protocol_iterator begin, |
| 4787 | ObjCProtocolDecl::protocol_iterator end) { |
| 4788 | std::vector<llvm::Constant*> ProtocolRefs; |
| 4789 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4790 | // Just return null for empty protocol lists |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4791 | if (begin == end) |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4792 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 4793 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4794 | // FIXME: We shouldn't need to do this lookup here, should we? |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4795 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true); |
| 4796 | if (GV) |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4797 | return llvm::ConstantExpr::getBitCast(GV, |
| 4798 | ObjCTypes.ProtocolListnfABIPtrTy); |
| 4799 | |
| 4800 | for (; begin != end; ++begin) |
| 4801 | ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented??? |
| 4802 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4803 | // This list is null terminated. |
| 4804 | ProtocolRefs.push_back(llvm::Constant::getNullValue( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4805 | ObjCTypes.ProtocolnfABIPtrTy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4806 | |
| 4807 | std::vector<llvm::Constant*> Values(2); |
| 4808 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
| 4809 | Values[1] = |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4810 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4811 | ProtocolRefs.size()), |
| 4812 | ProtocolRefs); |
| 4813 | |
| 4814 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4815 | GV = new llvm::GlobalVariable(Init->getType(), false, |
| 4816 | llvm::GlobalValue::InternalLinkage, |
| 4817 | Init, |
| 4818 | Name, |
| 4819 | &CGM.getModule()); |
| 4820 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4821 | GV->setAlignment( |
| 4822 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4823 | UsedGlobals.push_back(GV); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4824 | return llvm::ConstantExpr::getBitCast(GV, |
| 4825 | ObjCTypes.ProtocolListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4826 | } |
| 4827 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4828 | /// GetMethodDescriptionConstant - This routine build following meta-data: |
| 4829 | /// struct _objc_method { |
| 4830 | /// SEL _cmd; |
| 4831 | /// char *method_type; |
| 4832 | /// char *_imp; |
| 4833 | /// } |
| 4834 | |
| 4835 | llvm::Constant * |
| 4836 | CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 4837 | std::vector<llvm::Constant*> Desc(3); |
| 4838 | Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 4839 | ObjCTypes.SelectorPtrTy); |
| 4840 | Desc[1] = GetMethodVarType(MD); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4841 | // Protocol methods have no implementation. So, this entry is always NULL. |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4842 | Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 4843 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc); |
| 4844 | } |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4845 | |
| 4846 | /// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference. |
| 4847 | /// This code gen. amounts to generating code for: |
| 4848 | /// @code |
| 4849 | /// (type *)((char *)base + _OBJC_IVAR_$_.ivar; |
| 4850 | /// @encode |
| 4851 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4852 | LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4853 | CodeGen::CodeGenFunction &CGF, |
| 4854 | QualType ObjectTy, |
| 4855 | llvm::Value *BaseValue, |
| 4856 | const ObjCIvarDecl *Ivar, |
| 4857 | const FieldDecl *Field, |
| 4858 | unsigned CVRQualifiers) { |
| 4859 | assert(ObjectTy->isObjCInterfaceType() && |
| 4860 | "CGObjCNonFragileABIMac::EmitObjCValueForIvar"); |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 4861 | ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl(); |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4862 | std::string ExternalName; |
| 4863 | llvm::GlobalVariable *IvarOffsetGV = |
| 4864 | ObjCIvarOffsetVariable(ExternalName, ID, Ivar); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4865 | |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4866 | // (char *) BaseValue |
| 4867 | llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, |
| 4868 | ObjCTypes.Int8PtrTy); |
| 4869 | llvm::Value *Offset = CGF.Builder.CreateLoad(IvarOffsetGV); |
| 4870 | // (char*)BaseValue + Offset_symbol |
| 4871 | V = CGF.Builder.CreateGEP(V, Offset, "add.ptr"); |
| 4872 | // (type *)((char*)BaseValue + Offset_symbol) |
| 4873 | const llvm::Type *IvarTy = |
| 4874 | CGM.getTypes().ConvertType(Ivar->getType()); |
| 4875 | llvm::Type *ptrIvarTy = llvm::PointerType::getUnqual(IvarTy); |
| 4876 | V = CGF.Builder.CreateBitCast(V, ptrIvarTy); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4877 | |
Fariborz Jahanian | a6681ae | 2009-03-09 20:44:22 +0000 | [diff] [blame] | 4878 | if (Ivar->isBitField()) { |
| 4879 | CodeGenTypes::BitFieldInfo bitFieldInfo = |
| 4880 | CGM.getTypes().getBitFieldInfo(Field); |
| 4881 | return LValue::MakeBitfield(V, bitFieldInfo.Begin, bitFieldInfo.Size, |
| 4882 | Field->getType()->isSignedIntegerType(), |
| 4883 | Field->getType().getCVRQualifiers()|CVRQualifiers); |
| 4884 | } |
| 4885 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4886 | LValue LV = LValue::MakeAddr(V, |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 4887 | Ivar->getType().getCVRQualifiers()|CVRQualifiers, |
| 4888 | CGM.getContext().getObjCGCAttrKind(Ivar->getType())); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4889 | LValue::SetObjCIvar(LV, true); |
| 4890 | return LV; |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4891 | } |
| 4892 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4893 | llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset( |
| 4894 | CodeGen::CodeGenFunction &CGF, |
| 4895 | ObjCInterfaceDecl *Interface, |
| 4896 | const ObjCIvarDecl *Ivar) { |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4897 | std::string ExternalName; |
| 4898 | llvm::GlobalVariable *IvarOffsetGV = |
| 4899 | ObjCIvarOffsetVariable(ExternalName, Interface, Ivar); |
| 4900 | |
| 4901 | return CGF.Builder.CreateLoad(IvarOffsetGV, false, "ivar"); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4902 | } |
| 4903 | |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 4904 | CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend( |
| 4905 | CodeGen::CodeGenFunction &CGF, |
| 4906 | QualType ResultType, |
| 4907 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4908 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 4909 | QualType Arg0Ty, |
| 4910 | bool IsSuper, |
| 4911 | const CallArgList &CallArgs) { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4912 | // FIXME. Even though IsSuper is passes. This function doese not |
| 4913 | // handle calls to 'super' receivers. |
| 4914 | CodeGenTypes &Types = CGM.getTypes(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4915 | llvm::Value *Arg0 = Receiver; |
| 4916 | if (!IsSuper) |
| 4917 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4918 | |
| 4919 | // Find the message function name. |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 4920 | // FIXME. This is too much work to get the ABI-specific result type |
| 4921 | // needed to find the message name. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4922 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, |
| 4923 | llvm::SmallVector<QualType, 16>()); |
| 4924 | llvm::Constant *Fn; |
| 4925 | std::string Name("\01l_"); |
| 4926 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4927 | #if 0 |
| 4928 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4929 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
| 4930 | Fn = ObjCTypes.MessageSendIdStretFixupFn; |
| 4931 | // FIXME. Is there a better way of getting these names. |
| 4932 | // They are available in RuntimeFunctions vector pair. |
| 4933 | Name += "objc_msgSendId_stret_fixup"; |
| 4934 | } |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4935 | else |
| 4936 | #endif |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4937 | if (IsSuper) { |
| 4938 | Fn = ObjCTypes.MessageSendSuper2StretFixupFn; |
| 4939 | Name += "objc_msgSendSuper2_stret_fixup"; |
| 4940 | } |
| 4941 | else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4942 | { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4943 | Fn = ObjCTypes.MessageSendStretFixupFn; |
| 4944 | Name += "objc_msgSend_stret_fixup"; |
| 4945 | } |
| 4946 | } |
Fariborz Jahanian | 1a6b368 | 2009-02-05 19:35:43 +0000 | [diff] [blame] | 4947 | else if (ResultType->isFloatingType() && |
| 4948 | // Selection of frret API only happens in 32bit nonfragile ABI. |
| 4949 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.LongTy) == 4) { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4950 | Fn = ObjCTypes.MessageSendFpretFixupFn; |
| 4951 | Name += "objc_msgSend_fpret_fixup"; |
| 4952 | } |
| 4953 | else { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4954 | #if 0 |
| 4955 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4956 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
| 4957 | Fn = ObjCTypes.MessageSendIdFixupFn; |
| 4958 | Name += "objc_msgSendId_fixup"; |
| 4959 | } |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4960 | else |
| 4961 | #endif |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4962 | if (IsSuper) { |
| 4963 | Fn = ObjCTypes.MessageSendSuper2FixupFn; |
| 4964 | Name += "objc_msgSendSuper2_fixup"; |
| 4965 | } |
| 4966 | else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4967 | { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4968 | Fn = ObjCTypes.MessageSendFixupFn; |
| 4969 | Name += "objc_msgSend_fixup"; |
| 4970 | } |
| 4971 | } |
| 4972 | Name += '_'; |
| 4973 | std::string SelName(Sel.getAsString()); |
| 4974 | // Replace all ':' in selector name with '_' ouch! |
| 4975 | for(unsigned i = 0; i < SelName.size(); i++) |
| 4976 | if (SelName[i] == ':') |
| 4977 | SelName[i] = '_'; |
| 4978 | Name += SelName; |
| 4979 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 4980 | if (!GV) { |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 4981 | // Build message ref table entry. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4982 | std::vector<llvm::Constant*> Values(2); |
| 4983 | Values[0] = Fn; |
| 4984 | Values[1] = GetMethodVarName(Sel); |
| 4985 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4986 | GV = new llvm::GlobalVariable(Init->getType(), false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4987 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4988 | Init, |
| 4989 | Name, |
| 4990 | &CGM.getModule()); |
| 4991 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | f59c1a6 | 2009-04-15 19:04:46 +0000 | [diff] [blame^] | 4992 | GV->setAlignment(16); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4993 | GV->setSection("__DATA, __objc_msgrefs, coalesced"); |
| 4994 | UsedGlobals.push_back(GV); |
| 4995 | } |
| 4996 | llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 4997 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4998 | CallArgList ActualArgs; |
| 4999 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
| 5000 | ActualArgs.push_back(std::make_pair(RValue::get(Arg1), |
| 5001 | ObjCTypes.MessageRefCPtrTy)); |
| 5002 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5003 | const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs); |
| 5004 | llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0); |
| 5005 | Callee = CGF.Builder.CreateLoad(Callee); |
Fariborz Jahanian | 3ab75bd | 2009-02-14 21:25:36 +0000 | [diff] [blame] | 5006 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5007 | Callee = CGF.Builder.CreateBitCast(Callee, |
| 5008 | llvm::PointerType::getUnqual(FTy)); |
| 5009 | return CGF.EmitCall(FnInfo1, Callee, ActualArgs); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5010 | } |
| 5011 | |
| 5012 | /// Generate code for a message send expression in the nonfragile abi. |
| 5013 | CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend( |
| 5014 | CodeGen::CodeGenFunction &CGF, |
| 5015 | QualType ResultType, |
| 5016 | Selector Sel, |
| 5017 | llvm::Value *Receiver, |
| 5018 | bool IsClassMessage, |
| 5019 | const CallArgList &CallArgs) { |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5020 | return EmitMessageSend(CGF, ResultType, Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5021 | Receiver, CGF.getContext().getObjCIdType(), |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5022 | false, CallArgs); |
| 5023 | } |
| 5024 | |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5025 | llvm::GlobalVariable * |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5026 | CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5027 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5028 | |
Daniel Dunbar | dfff230 | 2009-03-02 05:18:14 +0000 | [diff] [blame] | 5029 | if (!GV) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5030 | GV = new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, |
| 5031 | llvm::GlobalValue::ExternalLinkage, |
| 5032 | 0, Name, &CGM.getModule()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5033 | } |
| 5034 | |
| 5035 | return GV; |
| 5036 | } |
| 5037 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5038 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5039 | const ObjCInterfaceDecl *ID, |
| 5040 | bool IsSuper) { |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5041 | |
| 5042 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 5043 | |
| 5044 | if (!Entry) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5045 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5046 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5047 | Entry = |
| 5048 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5049 | llvm::GlobalValue::InternalLinkage, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5050 | ClassGV, |
| 5051 | IsSuper ? "\01L_OBJC_CLASSLIST_SUP_REFS_$_" |
| 5052 | : "\01L_OBJC_CLASSLIST_REFERENCES_$_", |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5053 | &CGM.getModule()); |
| 5054 | Entry->setAlignment( |
| 5055 | CGM.getTargetData().getPrefTypeAlignment( |
| 5056 | ObjCTypes.ClassnfABIPtrTy)); |
| 5057 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5058 | if (IsSuper) |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 5059 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5060 | else |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 5061 | Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip"); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5062 | UsedGlobals.push_back(Entry); |
| 5063 | } |
| 5064 | |
| 5065 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5066 | } |
| 5067 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5068 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 5069 | /// meta-data |
| 5070 | /// |
| 5071 | llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder, |
| 5072 | const ObjCInterfaceDecl *ID) { |
| 5073 | llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()]; |
| 5074 | if (Entry) |
| 5075 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5076 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5077 | std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5078 | llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5079 | Entry = |
| 5080 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5081 | llvm::GlobalValue::InternalLinkage, |
| 5082 | MetaClassGV, |
| 5083 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_", |
| 5084 | &CGM.getModule()); |
| 5085 | Entry->setAlignment( |
| 5086 | CGM.getTargetData().getPrefTypeAlignment( |
| 5087 | ObjCTypes.ClassnfABIPtrTy)); |
| 5088 | |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5089 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5090 | UsedGlobals.push_back(Entry); |
| 5091 | |
| 5092 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5093 | } |
| 5094 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5095 | /// GetClass - Return a reference to the class for the given interface |
| 5096 | /// decl. |
| 5097 | llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder, |
| 5098 | const ObjCInterfaceDecl *ID) { |
| 5099 | return EmitClassRef(Builder, ID); |
| 5100 | } |
| 5101 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5102 | /// Generates a message send where the super is the receiver. This is |
| 5103 | /// a message send to self with special delivery semantics indicating |
| 5104 | /// which class's method should be called. |
| 5105 | CodeGen::RValue |
| 5106 | CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 5107 | QualType ResultType, |
| 5108 | Selector Sel, |
| 5109 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5110 | bool isCategoryImpl, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5111 | llvm::Value *Receiver, |
| 5112 | bool IsClassMessage, |
| 5113 | const CodeGen::CallArgList &CallArgs) { |
| 5114 | // ... |
| 5115 | // Create and init a super structure; this is a (receiver, class) |
| 5116 | // pair we will pass to objc_msgSendSuper. |
| 5117 | llvm::Value *ObjCSuper = |
| 5118 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 5119 | |
| 5120 | llvm::Value *ReceiverAsObject = |
| 5121 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 5122 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 5123 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
| 5124 | |
| 5125 | // 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] | 5126 | llvm::Value *Target; |
| 5127 | if (IsClassMessage) { |
| 5128 | if (isCategoryImpl) { |
| 5129 | // Message sent to "super' in a class method defined in |
| 5130 | // a category implementation. |
| 5131 | Target = EmitClassRef(CGF.Builder, Class, false); |
| 5132 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 5133 | Target = CGF.Builder.CreateLoad(Target); |
| 5134 | } |
| 5135 | else |
| 5136 | Target = EmitMetaClassRef(CGF.Builder, Class); |
| 5137 | } |
| 5138 | else |
| 5139 | Target = EmitClassRef(CGF.Builder, Class, true); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5140 | |
| 5141 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext |
| 5142 | // and ObjCTypes types. |
| 5143 | const llvm::Type *ClassTy = |
| 5144 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
| 5145 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
| 5146 | CGF.Builder.CreateStore(Target, |
| 5147 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
| 5148 | |
| 5149 | return EmitMessageSend(CGF, ResultType, Sel, |
| 5150 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 5151 | true, CallArgs); |
| 5152 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5153 | |
| 5154 | llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder, |
| 5155 | Selector Sel) { |
| 5156 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 5157 | |
| 5158 | if (!Entry) { |
| 5159 | llvm::Constant *Casted = |
| 5160 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 5161 | ObjCTypes.SelectorPtrTy); |
| 5162 | Entry = |
| 5163 | new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false, |
| 5164 | llvm::GlobalValue::InternalLinkage, |
| 5165 | Casted, "\01L_OBJC_SELECTOR_REFERENCES_", |
| 5166 | &CGM.getModule()); |
| 5167 | Entry->setSection("__DATA,__objc_selrefs,literal_pointers,no_dead_strip"); |
| 5168 | UsedGlobals.push_back(Entry); |
| 5169 | } |
| 5170 | |
| 5171 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5172 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5173 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 5174 | /// objc_assign_ivar (id src, id *dst) |
| 5175 | /// |
| 5176 | void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 5177 | llvm::Value *src, llvm::Value *dst) |
| 5178 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5179 | const llvm::Type * SrcTy = src->getType(); |
| 5180 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5181 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5182 | assert(Size <= 8 && "does not support size > 8"); |
| 5183 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5184 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5185 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5186 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5187 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5188 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
| 5189 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn, |
| 5190 | src, dst, "assignivar"); |
| 5191 | return; |
| 5192 | } |
| 5193 | |
| 5194 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 5195 | /// objc_assign_strongCast (id src, id *dst) |
| 5196 | /// |
| 5197 | void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign( |
| 5198 | CodeGen::CodeGenFunction &CGF, |
| 5199 | llvm::Value *src, llvm::Value *dst) |
| 5200 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5201 | const llvm::Type * SrcTy = src->getType(); |
| 5202 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5203 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5204 | assert(Size <= 8 && "does not support size > 8"); |
| 5205 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5206 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5207 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5208 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5209 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5210 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
| 5211 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn, |
| 5212 | src, dst, "weakassign"); |
| 5213 | return; |
| 5214 | } |
| 5215 | |
| 5216 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
| 5217 | /// object: objc_read_weak (id *src) |
| 5218 | /// |
| 5219 | llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead( |
| 5220 | CodeGen::CodeGenFunction &CGF, |
| 5221 | llvm::Value *AddrWeakObj) |
| 5222 | { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5223 | const llvm::Type* DestTy = |
| 5224 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5225 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
| 5226 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn, |
| 5227 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5228 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5229 | return read_weak; |
| 5230 | } |
| 5231 | |
| 5232 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 5233 | /// objc_assign_weak (id src, id *dst) |
| 5234 | /// |
| 5235 | void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 5236 | llvm::Value *src, llvm::Value *dst) |
| 5237 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5238 | const llvm::Type * SrcTy = src->getType(); |
| 5239 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5240 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5241 | assert(Size <= 8 && "does not support size > 8"); |
| 5242 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5243 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5244 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5245 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5246 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5247 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
| 5248 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn, |
| 5249 | src, dst, "weakassign"); |
| 5250 | return; |
| 5251 | } |
| 5252 | |
| 5253 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 5254 | /// objc_assign_global (id src, id *dst) |
| 5255 | /// |
| 5256 | void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 5257 | llvm::Value *src, llvm::Value *dst) |
| 5258 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5259 | const llvm::Type * SrcTy = src->getType(); |
| 5260 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5261 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5262 | assert(Size <= 8 && "does not support size > 8"); |
| 5263 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5264 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5265 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5266 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5267 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5268 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
| 5269 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn, |
| 5270 | src, dst, "globalassign"); |
| 5271 | return; |
| 5272 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5273 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5274 | void |
| 5275 | CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 5276 | const Stmt &S) { |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5277 | bool isTry = isa<ObjCAtTryStmt>(S); |
| 5278 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 5279 | llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5280 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5281 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5282 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5283 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
| 5284 | |
| 5285 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 5286 | // evaluation of the expression must occur before we enter the |
| 5287 | // @synchronized. We can safely avoid a temp here because jumps into |
| 5288 | // @synchronized are illegal & this will dominate uses. |
| 5289 | llvm::Value *SyncArg = 0; |
| 5290 | if (!isTry) { |
| 5291 | SyncArg = |
| 5292 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 5293 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5294 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5295 | } |
| 5296 | |
| 5297 | // Push an EH context entry, used for handling rethrows and jumps |
| 5298 | // through finally. |
| 5299 | CGF.PushCleanupBlock(FinallyBlock); |
| 5300 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5301 | CGF.setInvokeDest(TryHandler); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5302 | |
| 5303 | CGF.EmitBlock(TryBlock); |
| 5304 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 5305 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
| 5306 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 5307 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5308 | // Emit the exception handler. |
| 5309 | |
| 5310 | CGF.EmitBlock(TryHandler); |
| 5311 | |
| 5312 | llvm::Value *llvm_eh_exception = |
| 5313 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception); |
| 5314 | llvm::Value *llvm_eh_selector_i64 = |
| 5315 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64); |
| 5316 | llvm::Value *llvm_eh_typeid_for_i64 = |
| 5317 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64); |
| 5318 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5319 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow"); |
| 5320 | |
| 5321 | llvm::SmallVector<llvm::Value*, 8> SelectorArgs; |
| 5322 | SelectorArgs.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5323 | SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5324 | |
| 5325 | // Construct the lists of (type, catch body) to handle. |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5326 | llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5327 | bool HasCatchAll = false; |
| 5328 | if (isTry) { |
| 5329 | if (const ObjCAtCatchStmt* CatchStmt = |
| 5330 | cast<ObjCAtTryStmt>(S).getCatchStmts()) { |
| 5331 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5332 | const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl(); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5333 | Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody())); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5334 | |
| 5335 | // catch(...) always matches. |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5336 | if (!CatchDecl) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5337 | // Use i8* null here to signal this is a catch all, not a cleanup. |
| 5338 | llvm::Value *Null = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 5339 | SelectorArgs.push_back(Null); |
| 5340 | HasCatchAll = true; |
| 5341 | break; |
| 5342 | } |
| 5343 | |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5344 | if (CGF.getContext().isObjCIdType(CatchDecl->getType()) || |
| 5345 | CatchDecl->getType()->isObjCQualifiedIdType()) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5346 | llvm::Value *IDEHType = |
| 5347 | CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id"); |
| 5348 | if (!IDEHType) |
| 5349 | IDEHType = |
| 5350 | new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5351 | llvm::GlobalValue::ExternalLinkage, |
| 5352 | 0, "OBJC_EHTYPE_id", &CGM.getModule()); |
| 5353 | SelectorArgs.push_back(IDEHType); |
| 5354 | HasCatchAll = true; |
| 5355 | break; |
| 5356 | } |
| 5357 | |
| 5358 | // All other types should be Objective-C interface pointer types. |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5359 | const PointerType *PT = CatchDecl->getType()->getAsPointerType(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5360 | assert(PT && "Invalid @catch type."); |
| 5361 | const ObjCInterfaceType *IT = |
| 5362 | PT->getPointeeType()->getAsObjCInterfaceType(); |
| 5363 | assert(IT && "Invalid @catch type."); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5364 | llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5365 | SelectorArgs.push_back(EHType); |
| 5366 | } |
| 5367 | } |
| 5368 | } |
| 5369 | |
| 5370 | // We use a cleanup unless there was already a catch all. |
| 5371 | if (!HasCatchAll) { |
| 5372 | SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0)); |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5373 | Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5374 | } |
| 5375 | |
| 5376 | llvm::Value *Selector = |
| 5377 | CGF.Builder.CreateCall(llvm_eh_selector_i64, |
| 5378 | SelectorArgs.begin(), SelectorArgs.end(), |
| 5379 | "selector"); |
| 5380 | for (unsigned i = 0, e = Handlers.size(); i != e; ++i) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5381 | const ParmVarDecl *CatchParam = Handlers[i].first; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5382 | const Stmt *CatchBody = Handlers[i].second; |
| 5383 | |
| 5384 | llvm::BasicBlock *Next = 0; |
| 5385 | |
| 5386 | // The last handler always matches. |
| 5387 | if (i + 1 != e) { |
| 5388 | assert(CatchParam && "Only last handler can be a catch all."); |
| 5389 | |
| 5390 | llvm::BasicBlock *Match = CGF.createBasicBlock("match"); |
| 5391 | Next = CGF.createBasicBlock("catch.next"); |
| 5392 | llvm::Value *Id = |
| 5393 | CGF.Builder.CreateCall(llvm_eh_typeid_for_i64, |
| 5394 | CGF.Builder.CreateBitCast(SelectorArgs[i+2], |
| 5395 | ObjCTypes.Int8PtrTy)); |
| 5396 | CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id), |
| 5397 | Match, Next); |
| 5398 | |
| 5399 | CGF.EmitBlock(Match); |
| 5400 | } |
| 5401 | |
| 5402 | if (CatchBody) { |
| 5403 | llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end"); |
| 5404 | llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler"); |
| 5405 | |
| 5406 | // Cleanups must call objc_end_catch. |
| 5407 | // |
| 5408 | // FIXME: It seems incorrect for objc_begin_catch to be inside |
| 5409 | // this context, but this matches gcc. |
| 5410 | CGF.PushCleanupBlock(MatchEnd); |
| 5411 | CGF.setInvokeDest(MatchHandler); |
| 5412 | |
| 5413 | llvm::Value *ExcObject = |
| 5414 | CGF.Builder.CreateCall(ObjCTypes.ObjCBeginCatchFn, Exc); |
| 5415 | |
| 5416 | // Bind the catch parameter if it exists. |
| 5417 | if (CatchParam) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5418 | ExcObject = |
| 5419 | CGF.Builder.CreateBitCast(ExcObject, |
| 5420 | CGF.ConvertType(CatchParam->getType())); |
| 5421 | // CatchParam is a ParmVarDecl because of the grammar |
| 5422 | // construction used to handle this, but for codegen purposes |
| 5423 | // we treat this as a local decl. |
| 5424 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
| 5425 | CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5426 | } |
| 5427 | |
| 5428 | CGF.ObjCEHValueStack.push_back(ExcObject); |
| 5429 | CGF.EmitStmt(CatchBody); |
| 5430 | CGF.ObjCEHValueStack.pop_back(); |
| 5431 | |
| 5432 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 5433 | |
| 5434 | CGF.EmitBlock(MatchHandler); |
| 5435 | |
| 5436 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5437 | // We are required to emit this call to satisfy LLVM, even |
| 5438 | // though we don't use the result. |
| 5439 | llvm::SmallVector<llvm::Value*, 8> Args; |
| 5440 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5441 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5442 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 5443 | 0)); |
| 5444 | CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end()); |
| 5445 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5446 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5447 | |
| 5448 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 5449 | |
| 5450 | CGF.EmitBlock(MatchEnd); |
| 5451 | |
| 5452 | // Unfortunately, we also have to generate another EH frame here |
| 5453 | // in case this throws. |
| 5454 | llvm::BasicBlock *MatchEndHandler = |
| 5455 | CGF.createBasicBlock("match.end.handler"); |
| 5456 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
| 5457 | CGF.Builder.CreateInvoke(ObjCTypes.ObjCEndCatchFn, |
| 5458 | Cont, MatchEndHandler, |
| 5459 | Args.begin(), Args.begin()); |
| 5460 | |
| 5461 | CGF.EmitBlock(Cont); |
| 5462 | if (Info.SwitchBlock) |
| 5463 | CGF.EmitBlock(Info.SwitchBlock); |
| 5464 | if (Info.EndBlock) |
| 5465 | CGF.EmitBlock(Info.EndBlock); |
| 5466 | |
| 5467 | CGF.EmitBlock(MatchEndHandler); |
| 5468 | Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5469 | // We are required to emit this call to satisfy LLVM, even |
| 5470 | // though we don't use the result. |
| 5471 | Args.clear(); |
| 5472 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5473 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5474 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 5475 | 0)); |
| 5476 | CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end()); |
| 5477 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5478 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5479 | |
| 5480 | if (Next) |
| 5481 | CGF.EmitBlock(Next); |
| 5482 | } else { |
| 5483 | assert(!Next && "catchup should be last handler."); |
| 5484 | |
| 5485 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5486 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5487 | } |
| 5488 | } |
| 5489 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5490 | // Pop the cleanup entry, the @finally is outside this cleanup |
| 5491 | // scope. |
| 5492 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 5493 | CGF.setInvokeDest(PrevLandingPad); |
| 5494 | |
| 5495 | CGF.EmitBlock(FinallyBlock); |
| 5496 | |
| 5497 | if (isTry) { |
| 5498 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 5499 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 5500 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
| 5501 | } else { |
| 5502 | // Emit 'objc_sync_exit(expr)' as finally's sole statement for |
| 5503 | // @synchronized. |
| 5504 | CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, SyncArg); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5505 | } |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5506 | |
| 5507 | if (Info.SwitchBlock) |
| 5508 | CGF.EmitBlock(Info.SwitchBlock); |
| 5509 | if (Info.EndBlock) |
| 5510 | CGF.EmitBlock(Info.EndBlock); |
| 5511 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5512 | // Branch around the rethrow code. |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5513 | CGF.EmitBranch(FinallyEnd); |
| 5514 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5515 | CGF.EmitBlock(FinallyRethrow); |
| 5516 | CGF.Builder.CreateCall(ObjCTypes.UnwindResumeOrRethrowFn, |
| 5517 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5518 | CGF.Builder.CreateUnreachable(); |
| 5519 | |
| 5520 | CGF.EmitBlock(FinallyEnd); |
| 5521 | } |
| 5522 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5523 | /// EmitThrowStmt - Generate code for a throw statement. |
| 5524 | void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 5525 | const ObjCAtThrowStmt &S) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5526 | llvm::Value *Exception; |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5527 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5528 | Exception = CGF.EmitScalarExpr(ThrowExpr); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5529 | } else { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5530 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
| 5531 | "Unexpected rethrow outside @catch block."); |
| 5532 | Exception = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5533 | } |
| 5534 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5535 | llvm::Value *ExceptionAsObject = |
| 5536 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 5537 | llvm::BasicBlock *InvokeDest = CGF.getInvokeDest(); |
| 5538 | if (InvokeDest) { |
| 5539 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
| 5540 | CGF.Builder.CreateInvoke(ObjCTypes.ExceptionThrowFn, |
| 5541 | Cont, InvokeDest, |
| 5542 | &ExceptionAsObject, &ExceptionAsObject + 1); |
| 5543 | CGF.EmitBlock(Cont); |
| 5544 | } else |
| 5545 | CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject); |
| 5546 | CGF.Builder.CreateUnreachable(); |
| 5547 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5548 | // Clear the insertion point to indicate we are in unreachable code. |
| 5549 | CGF.Builder.ClearInsertionPoint(); |
| 5550 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5551 | |
| 5552 | llvm::Value * |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5553 | CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
| 5554 | bool ForDefinition) { |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5555 | llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()]; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5556 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5557 | // If we don't need a definition, return the entry if found or check |
| 5558 | // if we use an external reference. |
| 5559 | if (!ForDefinition) { |
| 5560 | if (Entry) |
| 5561 | return Entry; |
Daniel Dunbar | 7e075cb | 2009-04-07 06:43:45 +0000 | [diff] [blame] | 5562 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5563 | // If this type (or a super class) has the __objc_exception__ |
| 5564 | // attribute, emit an external reference. |
| 5565 | if (hasObjCExceptionAttribute(ID)) |
| 5566 | return Entry = |
| 5567 | new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5568 | llvm::GlobalValue::ExternalLinkage, |
| 5569 | 0, |
| 5570 | (std::string("OBJC_EHTYPE_$_") + |
| 5571 | ID->getIdentifier()->getName()), |
| 5572 | &CGM.getModule()); |
| 5573 | } |
| 5574 | |
| 5575 | // Otherwise we need to either make a new entry or fill in the |
| 5576 | // initializer. |
| 5577 | assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition"); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5578 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5579 | std::string VTableName = "objc_ehtype_vtable"; |
| 5580 | llvm::GlobalVariable *VTableGV = |
| 5581 | CGM.getModule().getGlobalVariable(VTableName); |
| 5582 | if (!VTableGV) |
| 5583 | VTableGV = new llvm::GlobalVariable(ObjCTypes.Int8PtrTy, false, |
| 5584 | llvm::GlobalValue::ExternalLinkage, |
| 5585 | 0, VTableName, &CGM.getModule()); |
| 5586 | |
| 5587 | llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2); |
| 5588 | |
| 5589 | std::vector<llvm::Constant*> Values(3); |
| 5590 | Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1); |
| 5591 | Values[1] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5592 | Values[2] = GetClassGlobal(ClassName); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5593 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values); |
| 5594 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5595 | if (Entry) { |
| 5596 | Entry->setInitializer(Init); |
| 5597 | } else { |
| 5598 | Entry = new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5599 | llvm::GlobalValue::WeakAnyLinkage, |
| 5600 | Init, |
| 5601 | (std::string("OBJC_EHTYPE_$_") + |
| 5602 | ID->getIdentifier()->getName()), |
| 5603 | &CGM.getModule()); |
| 5604 | } |
| 5605 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 5606 | if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden) |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5607 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5608 | Entry->setAlignment(8); |
| 5609 | |
| 5610 | if (ForDefinition) { |
| 5611 | Entry->setSection("__DATA,__objc_const"); |
| 5612 | Entry->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 5613 | } else { |
| 5614 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 5615 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5616 | |
| 5617 | return Entry; |
| 5618 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5619 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 5620 | /* *** */ |
| 5621 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5622 | CodeGen::CGObjCRuntime * |
| 5623 | CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 5624 | return new CGObjCMac(CGM); |
| 5625 | } |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5626 | |
| 5627 | CodeGen::CGObjCRuntime * |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5628 | CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5629 | return new CGObjCNonFragileABIMac(CGM); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5630 | } |