Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1 | //===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This provides Objective-C code generation targetting the Apple runtime. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CGObjCRuntime.h" |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 15 | |
| 16 | #include "CodeGenModule.h" |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 17 | #include "CodeGenFunction.h" |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 19 | #include "clang/AST/Decl.h" |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclObjC.h" |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 21 | #include "clang/Basic/LangOptions.h" |
| 22 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 23 | #include "llvm/Intrinsics.h" |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 24 | #include "llvm/Module.h" |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/DenseSet.h" |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetData.h" |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 27 | #include <sstream> |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace clang; |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 30 | using namespace CodeGen; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 31 | |
| 32 | namespace { |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 33 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 34 | typedef std::vector<llvm::Constant*> ConstantVector; |
| 35 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 36 | // FIXME: We should find a nicer way to make the labels for |
| 37 | // metadata, string concatenation is lame. |
| 38 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 39 | class ObjCCommonTypesHelper { |
| 40 | protected: |
| 41 | CodeGen::CodeGenModule &CGM; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 42 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 43 | public: |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 44 | const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 45 | const llvm::Type *Int8PtrTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 46 | |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 47 | /// ObjectPtrTy - LLVM type for object handles (typeof(id)) |
| 48 | const llvm::Type *ObjectPtrTy; |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 49 | |
| 50 | /// PtrObjectPtrTy - LLVM type for id * |
| 51 | const llvm::Type *PtrObjectPtrTy; |
| 52 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 53 | /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL)) |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 54 | const llvm::Type *SelectorPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 55 | /// ProtocolPtrTy - LLVM type for external protocol handles |
| 56 | /// (typeof(Protocol)) |
| 57 | const llvm::Type *ExternalProtocolPtrTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 58 | |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 59 | // SuperCTy - clang type for struct objc_super. |
| 60 | QualType SuperCTy; |
| 61 | // SuperPtrCTy - clang type for struct objc_super *. |
| 62 | QualType SuperPtrCTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 63 | |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 64 | /// SuperTy - LLVM type for struct objc_super. |
| 65 | const llvm::StructType *SuperTy; |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 66 | /// SuperPtrTy - LLVM type for struct objc_super *. |
| 67 | const llvm::Type *SuperPtrTy; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 68 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 69 | /// PropertyTy - LLVM type for struct objc_property (struct _prop_t |
| 70 | /// in GCC parlance). |
| 71 | const llvm::StructType *PropertyTy; |
| 72 | |
| 73 | /// PropertyListTy - LLVM type for struct objc_property_list |
| 74 | /// (_prop_list_t in GCC parlance). |
| 75 | const llvm::StructType *PropertyListTy; |
| 76 | /// PropertyListPtrTy - LLVM type for struct objc_property_list*. |
| 77 | const llvm::Type *PropertyListPtrTy; |
| 78 | |
| 79 | // MethodTy - LLVM type for struct objc_method. |
| 80 | const llvm::StructType *MethodTy; |
| 81 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 82 | /// CacheTy - LLVM type for struct objc_cache. |
| 83 | const llvm::Type *CacheTy; |
| 84 | /// CachePtrTy - LLVM type for struct objc_cache *. |
| 85 | const llvm::Type *CachePtrTy; |
| 86 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 87 | llvm::Constant *GetPropertyFn, *SetPropertyFn; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 88 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 89 | llvm::Constant *EnumerationMutationFn; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 90 | |
| 91 | /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 92 | llvm::Constant *GcReadWeakFn; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 93 | |
| 94 | /// GcAssignWeakFn -- LLVM objc_assign_weak function. |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 95 | llvm::Constant *getGcAssignWeakFn() { |
| 96 | // id objc_assign_weak (id, id *) |
| 97 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 98 | Args.push_back(ObjectPtrTy->getPointerTo()); |
| 99 | llvm::FunctionType *FTy = |
| 100 | llvm::FunctionType::get(ObjectPtrTy, Args, false); |
| 101 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak"); |
| 102 | } |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 103 | |
| 104 | /// GcAssignGlobalFn -- LLVM objc_assign_global function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 105 | llvm::Constant *GcAssignGlobalFn; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 106 | |
| 107 | /// GcAssignIvarFn -- LLVM objc_assign_ivar function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 108 | llvm::Constant *GcAssignIvarFn; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 109 | |
| 110 | /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 111 | llvm::Constant *GcAssignStrongCastFn; |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 112 | |
| 113 | /// ExceptionThrowFn - LLVM objc_exception_throw function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 114 | llvm::Constant *ExceptionThrowFn; |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 115 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 116 | /// SyncEnterFn - LLVM object_sync_enter function. |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 117 | llvm::Constant *getSyncEnterFn() { |
| 118 | // void objc_sync_enter (id) |
| 119 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 120 | llvm::FunctionType *FTy = |
| 121 | llvm::FunctionType::get(llvm::Type::VoidTy, Args, false); |
| 122 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter"); |
| 123 | } |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 124 | |
| 125 | /// SyncExitFn - LLVM object_sync_exit function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 126 | llvm::Constant *SyncExitFn; |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 127 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 128 | ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm); |
| 129 | ~ObjCCommonTypesHelper(){} |
| 130 | }; |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 131 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 132 | /// ObjCTypesHelper - Helper class that encapsulates lazy |
| 133 | /// construction of varies types used during ObjC generation. |
| 134 | class ObjCTypesHelper : public ObjCCommonTypesHelper { |
| 135 | private: |
| 136 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 137 | llvm::Constant *MessageSendFn, *MessageSendStretFn, *MessageSendFpretFn; |
| 138 | llvm::Constant *MessageSendSuperFn, *MessageSendSuperStretFn, |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 139 | *MessageSendSuperFpretFn; |
| 140 | |
| 141 | public: |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 142 | /// SymtabTy - LLVM type for struct objc_symtab. |
| 143 | const llvm::StructType *SymtabTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 144 | /// SymtabPtrTy - LLVM type for struct objc_symtab *. |
| 145 | const llvm::Type *SymtabPtrTy; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 146 | /// ModuleTy - LLVM type for struct objc_module. |
| 147 | const llvm::StructType *ModuleTy; |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 148 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 149 | /// ProtocolTy - LLVM type for struct objc_protocol. |
| 150 | const llvm::StructType *ProtocolTy; |
| 151 | /// ProtocolPtrTy - LLVM type for struct objc_protocol *. |
| 152 | const llvm::Type *ProtocolPtrTy; |
| 153 | /// ProtocolExtensionTy - LLVM type for struct |
| 154 | /// objc_protocol_extension. |
| 155 | const llvm::StructType *ProtocolExtensionTy; |
| 156 | /// ProtocolExtensionTy - LLVM type for struct |
| 157 | /// objc_protocol_extension *. |
| 158 | const llvm::Type *ProtocolExtensionPtrTy; |
| 159 | /// MethodDescriptionTy - LLVM type for struct |
| 160 | /// objc_method_description. |
| 161 | const llvm::StructType *MethodDescriptionTy; |
| 162 | /// MethodDescriptionListTy - LLVM type for struct |
| 163 | /// objc_method_description_list. |
| 164 | const llvm::StructType *MethodDescriptionListTy; |
| 165 | /// MethodDescriptionListPtrTy - LLVM type for struct |
| 166 | /// objc_method_description_list *. |
| 167 | const llvm::Type *MethodDescriptionListPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 168 | /// ProtocolListTy - LLVM type for struct objc_property_list. |
| 169 | const llvm::Type *ProtocolListTy; |
| 170 | /// ProtocolListPtrTy - LLVM type for struct objc_property_list*. |
| 171 | const llvm::Type *ProtocolListPtrTy; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 172 | /// CategoryTy - LLVM type for struct objc_category. |
| 173 | const llvm::StructType *CategoryTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 174 | /// ClassTy - LLVM type for struct objc_class. |
| 175 | const llvm::StructType *ClassTy; |
| 176 | /// ClassPtrTy - LLVM type for struct objc_class *. |
| 177 | const llvm::Type *ClassPtrTy; |
| 178 | /// ClassExtensionTy - LLVM type for struct objc_class_ext. |
| 179 | const llvm::StructType *ClassExtensionTy; |
| 180 | /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *. |
| 181 | const llvm::Type *ClassExtensionPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 182 | // IvarTy - LLVM type for struct objc_ivar. |
| 183 | const llvm::StructType *IvarTy; |
| 184 | /// IvarListTy - LLVM type for struct objc_ivar_list. |
| 185 | const llvm::Type *IvarListTy; |
| 186 | /// IvarListPtrTy - LLVM type for struct objc_ivar_list *. |
| 187 | const llvm::Type *IvarListPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 188 | /// MethodListTy - LLVM type for struct objc_method_list. |
| 189 | const llvm::Type *MethodListTy; |
| 190 | /// MethodListPtrTy - LLVM type for struct objc_method_list *. |
| 191 | const llvm::Type *MethodListPtrTy; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 192 | |
| 193 | /// ExceptionDataTy - LLVM type for struct _objc_exception_data. |
| 194 | const llvm::Type *ExceptionDataTy; |
| 195 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 196 | /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 197 | llvm::Constant *ExceptionTryEnterFn; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 198 | |
| 199 | /// ExceptionTryExitFn - LLVM objc_exception_try_exit function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 200 | llvm::Constant *ExceptionTryExitFn; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 201 | |
| 202 | /// ExceptionExtractFn - LLVM objc_exception_extract function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 203 | llvm::Constant *ExceptionExtractFn; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 204 | |
| 205 | /// ExceptionMatchFn - LLVM objc_exception_match function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 206 | llvm::Constant *ExceptionMatchFn; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 207 | |
| 208 | /// SetJmpFn - LLVM _setjmp function. |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 209 | llvm::Constant *SetJmpFn; |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 210 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 211 | public: |
| 212 | ObjCTypesHelper(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 213 | ~ObjCTypesHelper() {} |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 214 | |
| 215 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 216 | llvm::Constant *getSendFn(bool IsSuper) { |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 217 | return IsSuper ? MessageSendSuperFn : MessageSendFn; |
| 218 | } |
| 219 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 220 | llvm::Constant *getSendStretFn(bool IsSuper) { |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 221 | return IsSuper ? MessageSendSuperStretFn : MessageSendStretFn; |
| 222 | } |
| 223 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 224 | llvm::Constant *getSendFpretFn(bool IsSuper) { |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 225 | return IsSuper ? MessageSendSuperFpretFn : MessageSendFpretFn; |
| 226 | } |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 227 | }; |
| 228 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 229 | /// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 230 | /// modern abi |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 231 | class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 232 | public: |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 233 | llvm::Constant *MessageSendFixupFn, *MessageSendFpretFixupFn, |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 234 | *MessageSendStretFixupFn, *MessageSendIdFixupFn, |
| 235 | *MessageSendIdStretFixupFn, *MessageSendSuper2FixupFn, |
| 236 | *MessageSendSuper2StretFixupFn; |
| 237 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 238 | // MethodListnfABITy - LLVM for struct _method_list_t |
| 239 | const llvm::StructType *MethodListnfABITy; |
| 240 | |
| 241 | // MethodListnfABIPtrTy - LLVM for struct _method_list_t* |
| 242 | const llvm::Type *MethodListnfABIPtrTy; |
| 243 | |
| 244 | // ProtocolnfABITy = LLVM for struct _protocol_t |
| 245 | const llvm::StructType *ProtocolnfABITy; |
| 246 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 247 | // ProtocolnfABIPtrTy = LLVM for struct _protocol_t* |
| 248 | const llvm::Type *ProtocolnfABIPtrTy; |
| 249 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 250 | // ProtocolListnfABITy - LLVM for struct _objc_protocol_list |
| 251 | const llvm::StructType *ProtocolListnfABITy; |
| 252 | |
| 253 | // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list* |
| 254 | const llvm::Type *ProtocolListnfABIPtrTy; |
| 255 | |
| 256 | // ClassnfABITy - LLVM for struct _class_t |
| 257 | const llvm::StructType *ClassnfABITy; |
| 258 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 259 | // ClassnfABIPtrTy - LLVM for struct _class_t* |
| 260 | const llvm::Type *ClassnfABIPtrTy; |
| 261 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 262 | // IvarnfABITy - LLVM for struct _ivar_t |
| 263 | const llvm::StructType *IvarnfABITy; |
| 264 | |
| 265 | // IvarListnfABITy - LLVM for struct _ivar_list_t |
| 266 | const llvm::StructType *IvarListnfABITy; |
| 267 | |
| 268 | // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t* |
| 269 | const llvm::Type *IvarListnfABIPtrTy; |
| 270 | |
| 271 | // ClassRonfABITy - LLVM for struct _class_ro_t |
| 272 | const llvm::StructType *ClassRonfABITy; |
| 273 | |
| 274 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 275 | const llvm::Type *ImpnfABITy; |
| 276 | |
| 277 | // CategorynfABITy - LLVM for struct _category_t |
| 278 | const llvm::StructType *CategorynfABITy; |
| 279 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 280 | // New types for nonfragile abi messaging. |
| 281 | |
| 282 | // MessageRefTy - LLVM for: |
| 283 | // struct _message_ref_t { |
| 284 | // IMP messenger; |
| 285 | // SEL name; |
| 286 | // }; |
| 287 | const llvm::StructType *MessageRefTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 288 | // MessageRefCTy - clang type for struct _message_ref_t |
| 289 | QualType MessageRefCTy; |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 290 | |
| 291 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
| 292 | const llvm::Type *MessageRefPtrTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 293 | // MessageRefCPtrTy - clang type for struct _message_ref_t* |
| 294 | QualType MessageRefCPtrTy; |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 295 | |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 296 | // MessengerTy - Type of the messenger (shown as IMP above) |
| 297 | const llvm::FunctionType *MessengerTy; |
| 298 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 299 | // SuperMessageRefTy - LLVM for: |
| 300 | // struct _super_message_ref_t { |
| 301 | // SUPER_IMP messenger; |
| 302 | // SEL name; |
| 303 | // }; |
| 304 | const llvm::StructType *SuperMessageRefTy; |
| 305 | |
| 306 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
| 307 | const llvm::Type *SuperMessageRefPtrTy; |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 308 | |
| 309 | /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C |
| 310 | /// exception personality function. |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 311 | llvm::Value *getEHPersonalityPtr() { |
| 312 | llvm::Constant *Personality = |
| 313 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 314 | std::vector<const llvm::Type*>(), |
| 315 | true), |
| 316 | "__objc_personality_v0"); |
| 317 | return llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy); |
| 318 | } |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 319 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 320 | llvm::Constant *UnwindResumeOrRethrowFn, *ObjCBeginCatchFn, *ObjCEndCatchFn; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 321 | |
| 322 | const llvm::StructType *EHTypeTy; |
| 323 | const llvm::Type *EHTypePtrTy; |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 324 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 325 | ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm); |
| 326 | ~ObjCNonFragileABITypesHelper(){} |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 327 | }; |
| 328 | |
| 329 | class CGObjCCommonMac : public CodeGen::CGObjCRuntime { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 330 | public: |
| 331 | // FIXME - accessibility |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 332 | class GC_IVAR { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 333 | public: |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 334 | unsigned int ivar_bytepos; |
| 335 | unsigned int ivar_size; |
| 336 | GC_IVAR() : ivar_bytepos(0), ivar_size(0) {} |
| 337 | }; |
| 338 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 339 | class SKIP_SCAN { |
| 340 | public: |
| 341 | unsigned int skip; |
| 342 | unsigned int scan; |
| 343 | SKIP_SCAN() : skip(0), scan(0) {} |
| 344 | }; |
| 345 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 346 | protected: |
| 347 | CodeGen::CodeGenModule &CGM; |
| 348 | // FIXME! May not be needing this after all. |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 349 | unsigned ObjCABI; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 350 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 351 | // gc ivar layout bitmap calculation helper caches. |
| 352 | llvm::SmallVector<GC_IVAR, 16> SkipIvars; |
| 353 | llvm::SmallVector<GC_IVAR, 16> IvarsInfo; |
| 354 | llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 355 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 356 | /// LazySymbols - Symbols to generate a lazy reference for. See |
| 357 | /// DefinedSymbols and FinishModule(). |
| 358 | std::set<IdentifierInfo*> LazySymbols; |
| 359 | |
| 360 | /// DefinedSymbols - External symbols which are defined by this |
| 361 | /// module. The symbols in this list and LazySymbols are used to add |
| 362 | /// special linker symbols which ensure that Objective-C modules are |
| 363 | /// linked properly. |
| 364 | std::set<IdentifierInfo*> DefinedSymbols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 365 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 366 | /// ClassNames - uniqued class names. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 367 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 368 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 369 | /// MethodVarNames - uniqued method variable names. |
| 370 | llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 371 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 372 | /// MethodVarTypes - uniqued method type signatures. We have to use |
| 373 | /// a StringMap here because have no other unique reference. |
| 374 | llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 375 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 376 | /// MethodDefinitions - map of methods which have been defined in |
| 377 | /// this translation unit. |
| 378 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 379 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 380 | /// PropertyNames - uniqued method variable names. |
| 381 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 382 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 383 | /// ClassReferences - uniqued class references. |
| 384 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 385 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 386 | /// SelectorReferences - uniqued selector references. |
| 387 | llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 388 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 389 | /// Protocols - Protocols for which an objc_protocol structure has |
| 390 | /// been emitted. Forward declarations are handled by creating an |
| 391 | /// empty structure whose initializer is filled in when/if defined. |
| 392 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 393 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 394 | /// DefinedProtocols - Protocols which have actually been |
| 395 | /// defined. We should not need this, see FIXME in GenerateProtocol. |
| 396 | llvm::DenseSet<IdentifierInfo*> DefinedProtocols; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 397 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 398 | /// DefinedClasses - List of defined classes. |
| 399 | std::vector<llvm::GlobalValue*> DefinedClasses; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 400 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 401 | /// DefinedCategories - List of defined categories. |
| 402 | std::vector<llvm::GlobalValue*> DefinedCategories; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 403 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 404 | /// UsedGlobals - List of globals to pack into the llvm.used metadata |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 405 | /// to prevent them from being clobbered. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 406 | std::vector<llvm::GlobalVariable*> UsedGlobals; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 407 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 408 | /// GetNameForMethod - Return a name for the given method. |
| 409 | /// \param[out] NameOut - The return value. |
| 410 | void GetNameForMethod(const ObjCMethodDecl *OMD, |
| 411 | const ObjCContainerDecl *CD, |
| 412 | std::string &NameOut); |
| 413 | |
| 414 | /// GetMethodVarName - Return a unique constant for the given |
| 415 | /// selector's name. The return value has type char *. |
| 416 | llvm::Constant *GetMethodVarName(Selector Sel); |
| 417 | llvm::Constant *GetMethodVarName(IdentifierInfo *Ident); |
| 418 | llvm::Constant *GetMethodVarName(const std::string &Name); |
| 419 | |
| 420 | /// GetMethodVarType - Return a unique constant for the given |
| 421 | /// selector's name. The return value has type char *. |
| 422 | |
| 423 | // FIXME: This is a horrible name. |
| 424 | llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 425 | llvm::Constant *GetMethodVarType(FieldDecl *D); |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 426 | |
| 427 | /// GetPropertyName - Return a unique constant for the given |
| 428 | /// name. The return value has type char *. |
| 429 | llvm::Constant *GetPropertyName(IdentifierInfo *Ident); |
| 430 | |
| 431 | // FIXME: This can be dropped once string functions are unified. |
| 432 | llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 433 | const Decl *Container); |
| 434 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 435 | /// GetClassName - Return a unique constant for the given selector's |
| 436 | /// name. The return value has type char *. |
| 437 | llvm::Constant *GetClassName(IdentifierInfo *Ident); |
| 438 | |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 439 | /// GetInterfaceDeclStructLayout - Get layout for ivars of given |
| 440 | /// interface declaration. |
| 441 | const llvm::StructLayout *GetInterfaceDeclStructLayout( |
| 442 | const ObjCInterfaceDecl *ID) const; |
| 443 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 444 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 445 | /// implementation for the __strong or __weak case. |
| 446 | /// |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 447 | llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI, |
| 448 | bool ForStrongLayout); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 449 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 450 | void BuildAggrIvarLayout(const ObjCInterfaceDecl *OI, |
| 451 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 452 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 453 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 454 | unsigned int BytePos, bool ForStrongLayout, |
| 455 | int &Index, int &SkIndex, bool &HasUnion); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 456 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 457 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 458 | /// ivar layout bitmap. |
| 459 | llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident, |
| 460 | const ObjCCommonTypesHelper &ObjCTypes); |
| 461 | |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 462 | const RecordDecl *GetFirstIvarInRecord(const ObjCInterfaceDecl *OID, |
| 463 | RecordDecl::field_iterator &FIV, |
| 464 | RecordDecl::field_iterator &PIV); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 465 | /// EmitPropertyList - Emit the given property list. The return |
| 466 | /// value has type PropertyListPtrTy. |
| 467 | llvm::Constant *EmitPropertyList(const std::string &Name, |
| 468 | const Decl *Container, |
| 469 | const ObjCContainerDecl *OCD, |
| 470 | const ObjCCommonTypesHelper &ObjCTypes); |
| 471 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 472 | /// GetProtocolRef - Return a reference to the internal protocol |
| 473 | /// description, creating an empty one if it has not been |
| 474 | /// defined. The return value has type ProtocolPtrTy. |
| 475 | llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD); |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 476 | |
| 477 | /// GetIvarBaseOffset - returns ivars byte offset. |
| 478 | uint64_t GetIvarBaseOffset(const llvm::StructLayout *Layout, |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 479 | const FieldDecl *Field); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 480 | |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 481 | /// GetFieldBaseOffset - return's field byte offset. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 482 | uint64_t GetFieldBaseOffset(const ObjCInterfaceDecl *OI, |
| 483 | const llvm::StructLayout *Layout, |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 484 | const FieldDecl *Field); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 485 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 486 | /// CreateMetadataVar - Create a global variable with internal |
| 487 | /// linkage for use by the Objective-C runtime. |
| 488 | /// |
| 489 | /// This is a convenience wrapper which not only creates the |
| 490 | /// variable, but also sets the section and alignment and adds the |
| 491 | /// global to the UsedGlobals list. |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 492 | /// |
| 493 | /// \param Name - The variable name. |
| 494 | /// \param Init - The variable initializer; this is also used to |
| 495 | /// define the type of the variable. |
| 496 | /// \param Section - The section the variable should go into, or 0. |
| 497 | /// \param Align - The alignment for the variable, or 0. |
| 498 | /// \param AddToUsed - Whether the variable should be added to |
Daniel Dunbar | c158306 | 2009-04-14 17:42:51 +0000 | [diff] [blame] | 499 | /// "llvm.used". |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 500 | llvm::GlobalVariable *CreateMetadataVar(const std::string &Name, |
| 501 | llvm::Constant *Init, |
| 502 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 503 | unsigned Align, |
| 504 | bool AddToUsed); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 505 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 506 | public: |
| 507 | CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm) |
| 508 | { } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 509 | |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 510 | virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *SL); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 511 | |
| 512 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
| 513 | const ObjCContainerDecl *CD=0); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 514 | |
| 515 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); |
| 516 | |
| 517 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 518 | /// declaration, emitting it if necessary. The return value has type |
| 519 | /// ProtocolPtrTy. |
| 520 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0; |
| 521 | |
| 522 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 523 | /// object for the given declaration, emitting it if needed. These |
| 524 | /// forward references will be filled in with empty bodies if no |
| 525 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 526 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 527 | }; |
| 528 | |
| 529 | class CGObjCMac : public CGObjCCommonMac { |
| 530 | private: |
| 531 | ObjCTypesHelper ObjCTypes; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 532 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 533 | /// level information. |
| 534 | void EmitImageInfo(); |
| 535 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 536 | /// EmitModuleInfo - Another marker encoding module level |
| 537 | /// information. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 538 | void EmitModuleInfo(); |
| 539 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 540 | /// EmitModuleSymols - Emit module symbols, the list of defined |
| 541 | /// classes and categories. The result has type SymtabPtrTy. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 542 | llvm::Constant *EmitModuleSymbols(); |
| 543 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 544 | /// FinishModule - Write out global data structures at the end of |
| 545 | /// processing a translation unit. |
| 546 | void FinishModule(); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 547 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 548 | /// EmitClassExtension - Generate the class extension structure used |
| 549 | /// to store the weak ivar layout and properties. The return value |
| 550 | /// has type ClassExtensionPtrTy. |
| 551 | llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID); |
| 552 | |
| 553 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 554 | /// for the given class. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 555 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 556 | const ObjCInterfaceDecl *ID); |
| 557 | |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 558 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 559 | QualType ResultType, |
| 560 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 561 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 562 | QualType Arg0Ty, |
| 563 | bool IsSuper, |
| 564 | const CallArgList &CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 565 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 566 | /// EmitIvarList - Emit the ivar list for the given |
| 567 | /// implementation. If ForClass is true the list of class ivars |
| 568 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 569 | /// interface ivars will be emitted. The return value has type |
| 570 | /// IvarListPtrTy. |
| 571 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 572 | bool ForClass); |
| 573 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 574 | /// EmitMetaClass - Emit a forward reference to the class structure |
| 575 | /// for the metaclass of the given interface. The return value has |
| 576 | /// type ClassPtrTy. |
| 577 | llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID); |
| 578 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 579 | /// EmitMetaClass - Emit a class structure for the metaclass of the |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 580 | /// given implementation. The return value has type ClassPtrTy. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 581 | llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID, |
| 582 | llvm::Constant *Protocols, |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 583 | const llvm::Type *InterfaceTy, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 584 | const ConstantVector &Methods); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 585 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 586 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 587 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 588 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 589 | |
| 590 | /// EmitMethodList - Emit the method list for the given |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 591 | /// implementation. The return value has type MethodListPtrTy. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 592 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 593 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 594 | const ConstantVector &Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 595 | |
| 596 | /// EmitMethodDescList - Emit a method description list for a list of |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 597 | /// method declarations. |
| 598 | /// - TypeName: The name for the type containing the methods. |
| 599 | /// - IsProtocol: True iff these methods are for a protocol. |
| 600 | /// - ClassMethds: True iff these are class methods. |
| 601 | /// - Required: When true, only "required" methods are |
| 602 | /// listed. Similarly, when false only "optional" methods are |
| 603 | /// listed. For classes this should always be true. |
| 604 | /// - begin, end: The method list to output. |
| 605 | /// |
| 606 | /// The return value has type MethodDescriptionListPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 607 | llvm::Constant *EmitMethodDescList(const std::string &Name, |
| 608 | const char *Section, |
| 609 | const ConstantVector &Methods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 610 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 611 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 612 | /// declaration, emitting it if necessary. The return value has type |
| 613 | /// ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 614 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 615 | |
| 616 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 617 | /// object for the given declaration, emitting it if needed. These |
| 618 | /// forward references will be filled in with empty bodies if no |
| 619 | /// definition is seen. The return value has type ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 620 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 621 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 622 | /// EmitProtocolExtension - Generate the protocol extension |
| 623 | /// structure used to store optional instance and class methods, and |
| 624 | /// protocol properties. The return value has type |
| 625 | /// ProtocolExtensionPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 626 | llvm::Constant * |
| 627 | EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 628 | const ConstantVector &OptInstanceMethods, |
| 629 | const ConstantVector &OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 630 | |
| 631 | /// EmitProtocolList - Generate the list of referenced |
| 632 | /// protocols. The return value has type ProtocolListPtrTy. |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 633 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 634 | ObjCProtocolDecl::protocol_iterator begin, |
| 635 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 636 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 637 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 638 | /// for the given selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 639 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 640 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 641 | public: |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 642 | CGObjCMac(CodeGen::CodeGenModule &cgm); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 643 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 644 | virtual llvm::Function *ModuleInitFunction(); |
| 645 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 646 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 647 | QualType ResultType, |
| 648 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 649 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 650 | bool IsClassMessage, |
| 651 | const CallArgList &CallArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 652 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 653 | virtual CodeGen::RValue |
| 654 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 655 | QualType ResultType, |
| 656 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 657 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 658 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 659 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 660 | bool IsClassMessage, |
| 661 | const CallArgList &CallArgs); |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 662 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 663 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 664 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 665 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 666 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 667 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 668 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 669 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 670 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 671 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 672 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 673 | const ObjCProtocolDecl *PD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 674 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 675 | virtual llvm::Constant *GetPropertyGetFunction(); |
| 676 | virtual llvm::Constant *GetPropertySetFunction(); |
| 677 | virtual llvm::Constant *EnumerationMutationFunction(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 678 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 679 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 680 | const Stmt &S); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 681 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 682 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 683 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 684 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 685 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 686 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 687 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 688 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 689 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 690 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 691 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 692 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 693 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 694 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 695 | QualType ObjectTy, |
| 696 | llvm::Value *BaseValue, |
| 697 | const ObjCIvarDecl *Ivar, |
| 698 | const FieldDecl *Field, |
| 699 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 700 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
| 701 | ObjCInterfaceDecl *Interface, |
| 702 | const ObjCIvarDecl *Ivar); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 703 | }; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 704 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 705 | class CGObjCNonFragileABIMac : public CGObjCCommonMac { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 706 | private: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 707 | ObjCNonFragileABITypesHelper ObjCTypes; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 708 | llvm::GlobalVariable* ObjCEmptyCacheVar; |
| 709 | llvm::GlobalVariable* ObjCEmptyVtableVar; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 710 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 711 | /// SuperClassReferences - uniqued super class references. |
| 712 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences; |
| 713 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 714 | /// MetaClassReferences - uniqued meta class references. |
| 715 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 716 | |
| 717 | /// EHTypeReferences - uniqued class ehtype references. |
| 718 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 719 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 720 | /// FinishNonFragileABIModule - Write out global data structures at the end of |
| 721 | /// processing a translation unit. |
| 722 | void FinishNonFragileABIModule(); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 723 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 724 | llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags, |
| 725 | unsigned InstanceStart, |
| 726 | unsigned InstanceSize, |
| 727 | const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 728 | llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName, |
| 729 | llvm::Constant *IsAGV, |
| 730 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 731 | llvm::Constant *ClassRoGV, |
| 732 | bool HiddenVisibility); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 733 | |
| 734 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
| 735 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 736 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
| 737 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 738 | /// EmitMethodList - Emit the method list for the given |
| 739 | /// implementation. The return value has type MethodListnfABITy. |
| 740 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 741 | const char *Section, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 742 | const ConstantVector &Methods); |
| 743 | /// EmitIvarList - Emit the ivar list for the given |
| 744 | /// implementation. If ForClass is true the list of class ivars |
| 745 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 746 | /// interface ivars will be emitted. The return value has type |
| 747 | /// IvarListnfABIPtrTy. |
| 748 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 749 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 750 | llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 751 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 752 | unsigned long int offset); |
| 753 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 754 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 755 | /// declaration, emitting it if necessary. The return value has type |
| 756 | /// ProtocolPtrTy. |
| 757 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
| 758 | |
| 759 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 760 | /// object for the given declaration, emitting it if needed. These |
| 761 | /// forward references will be filled in with empty bodies if no |
| 762 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 763 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
| 764 | |
| 765 | /// EmitProtocolList - Generate the list of referenced |
| 766 | /// protocols. The return value has type ProtocolListPtrTy. |
| 767 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 768 | ObjCProtocolDecl::protocol_iterator begin, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 769 | ObjCProtocolDecl::protocol_iterator end); |
| 770 | |
| 771 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 772 | QualType ResultType, |
| 773 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 774 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 775 | QualType Arg0Ty, |
| 776 | bool IsSuper, |
| 777 | const CallArgList &CallArgs); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 778 | |
| 779 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 780 | /// class of the given name. |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 781 | llvm::GlobalVariable *GetClassGlobal(const std::string &Name); |
| 782 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 783 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 784 | /// for the given class reference. |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 785 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 786 | const ObjCInterfaceDecl *ID); |
| 787 | |
| 788 | /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 789 | /// for the given super class reference. |
| 790 | llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder, |
| 791 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 792 | |
| 793 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 794 | /// meta-data |
| 795 | llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder, |
| 796 | const ObjCInterfaceDecl *ID); |
| 797 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 798 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 799 | /// the given ivar. |
| 800 | /// |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame^] | 801 | llvm::GlobalVariable * ObjCIvarOffsetVariable( |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 802 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 803 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 804 | |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 805 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 806 | /// for the given selector. |
| 807 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 808 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 809 | /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 810 | /// interface. The return value has type EHTypePtrTy. |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 811 | llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
| 812 | bool ForDefinition); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 813 | |
| 814 | const char *getMetaclassSymbolPrefix() const { |
| 815 | return "OBJC_METACLASS_$_"; |
| 816 | } |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 817 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 818 | const char *getClassSymbolPrefix() const { |
| 819 | return "OBJC_CLASS_$_"; |
| 820 | } |
| 821 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 822 | public: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 823 | CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 824 | // FIXME. All stubs for now! |
| 825 | virtual llvm::Function *ModuleInitFunction(); |
| 826 | |
| 827 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
| 828 | QualType ResultType, |
| 829 | Selector Sel, |
| 830 | llvm::Value *Receiver, |
| 831 | bool IsClassMessage, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 832 | const CallArgList &CallArgs); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 833 | |
| 834 | virtual CodeGen::RValue |
| 835 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 836 | QualType ResultType, |
| 837 | Selector Sel, |
| 838 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 839 | bool isCategoryImpl, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 840 | llvm::Value *Receiver, |
| 841 | bool IsClassMessage, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 842 | const CallArgList &CallArgs); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 843 | |
| 844 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 845 | const ObjCInterfaceDecl *ID); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 846 | |
| 847 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel) |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 848 | { return EmitSelector(Builder, Sel); } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 849 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 850 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 851 | |
| 852 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 853 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 854 | const ObjCProtocolDecl *PD); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 855 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 856 | virtual llvm::Constant *GetPropertyGetFunction() { |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 857 | return ObjCTypes.GetPropertyFn; |
| 858 | } |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 859 | virtual llvm::Constant *GetPropertySetFunction() { |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 860 | return ObjCTypes.SetPropertyFn; |
| 861 | } |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 862 | virtual llvm::Constant *EnumerationMutationFunction() { |
Daniel Dunbar | 28ed084 | 2009-02-16 18:48:45 +0000 | [diff] [blame] | 863 | return ObjCTypes.EnumerationMutationFn; |
| 864 | } |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 865 | |
| 866 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 867 | const Stmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 868 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 869 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 870 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 871 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 872 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 873 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 874 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 875 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 876 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 877 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 878 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 879 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 880 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 881 | QualType ObjectTy, |
| 882 | llvm::Value *BaseValue, |
| 883 | const ObjCIvarDecl *Ivar, |
| 884 | const FieldDecl *Field, |
| 885 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 886 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
| 887 | ObjCInterfaceDecl *Interface, |
| 888 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 889 | }; |
| 890 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 891 | } // end anonymous namespace |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 892 | |
| 893 | /* *** Helper Functions *** */ |
| 894 | |
| 895 | /// getConstantGEP() - Help routine to construct simple GEPs. |
| 896 | static llvm::Constant *getConstantGEP(llvm::Constant *C, |
| 897 | unsigned idx0, |
| 898 | unsigned idx1) { |
| 899 | llvm::Value *Idxs[] = { |
| 900 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0), |
| 901 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1) |
| 902 | }; |
| 903 | return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2); |
| 904 | } |
| 905 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 906 | /// hasObjCExceptionAttribute - Return true if this class or any super |
| 907 | /// class has the __objc_exception__ attribute. |
| 908 | static bool hasObjCExceptionAttribute(const ObjCInterfaceDecl *OID) { |
Daniel Dunbar | b11fa0d | 2009-04-13 21:08:27 +0000 | [diff] [blame] | 909 | if (OID->hasAttr<ObjCExceptionAttr>()) |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 910 | return true; |
| 911 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
| 912 | return hasObjCExceptionAttribute(Super); |
| 913 | return false; |
| 914 | } |
| 915 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 916 | /* *** CGObjCMac Public Interface *** */ |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 917 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 918 | CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm), |
| 919 | ObjCTypes(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 920 | { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 921 | ObjCABI = 1; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 922 | EmitImageInfo(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 923 | } |
| 924 | |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 925 | /// GetClass - Return a reference to the class for the given interface |
| 926 | /// decl. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 927 | llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 928 | const ObjCInterfaceDecl *ID) { |
| 929 | return EmitClassRef(Builder, ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | /// GetSelector - Return the pointer to the unique'd string for this selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 933 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 934 | return EmitSelector(Builder, Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 935 | } |
| 936 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 937 | /// Generate a constant CFString object. |
| 938 | /* |
| 939 | struct __builtin_CFString { |
| 940 | const int *isa; // point to __CFConstantStringClassReference |
| 941 | int flags; |
| 942 | const char *str; |
| 943 | long length; |
| 944 | }; |
| 945 | */ |
| 946 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 947 | llvm::Constant *CGObjCCommonMac::GenerateConstantString( |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 948 | const ObjCStringLiteral *SL) { |
Steve Naroff | 8d4141f | 2009-04-01 13:55:36 +0000 | [diff] [blame] | 949 | return CGM.GetAddrOfConstantCFString(SL->getString()); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | /// Generates a message send where the super is the receiver. This is |
| 953 | /// a message send to self with special delivery semantics indicating |
| 954 | /// which class's method should be called. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 955 | CodeGen::RValue |
| 956 | CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 957 | QualType ResultType, |
| 958 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 959 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 960 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 961 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 962 | bool IsClassMessage, |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 963 | const CodeGen::CallArgList &CallArgs) { |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 964 | // Create and init a super structure; this is a (receiver, class) |
| 965 | // pair we will pass to objc_msgSendSuper. |
| 966 | llvm::Value *ObjCSuper = |
| 967 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 968 | llvm::Value *ReceiverAsObject = |
| 969 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 970 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 971 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 972 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 973 | // If this is a class message the metaclass is passed as the target. |
| 974 | llvm::Value *Target; |
| 975 | if (IsClassMessage) { |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 976 | if (isCategoryImpl) { |
| 977 | // Message sent to 'super' in a class method defined in a category |
| 978 | // implementation requires an odd treatment. |
| 979 | // If we are in a class method, we must retrieve the |
| 980 | // _metaclass_ for the current class, pointed at by |
| 981 | // the class's "isa" pointer. The following assumes that |
| 982 | // isa" is the first ivar in a class (which it must be). |
| 983 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 984 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 985 | Target = CGF.Builder.CreateLoad(Target); |
| 986 | } |
| 987 | else { |
| 988 | llvm::Value *MetaClassPtr = EmitMetaClassRef(Class); |
| 989 | llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1); |
| 990 | llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr); |
| 991 | Target = Super; |
| 992 | } |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 993 | } else { |
| 994 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 995 | } |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 996 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext |
| 997 | // and ObjCTypes types. |
| 998 | const llvm::Type *ClassTy = |
| 999 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1000 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1001 | CGF.Builder.CreateStore(Target, |
| 1002 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
| 1003 | |
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 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 1006 | true, CallArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1007 | } |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1008 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1009 | /// Generate code for a message send expression. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1010 | CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1011 | QualType ResultType, |
| 1012 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1013 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1014 | bool IsClassMessage, |
| 1015 | const CallArgList &CallArgs) { |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1016 | llvm::Value *Arg0 = |
| 1017 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp"); |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1018 | return EmitMessageSend(CGF, ResultType, Sel, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1019 | Arg0, CGF.getContext().getObjCIdType(), |
| 1020 | false, CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1024 | QualType ResultType, |
| 1025 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1026 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1027 | QualType Arg0Ty, |
| 1028 | bool IsSuper, |
| 1029 | const CallArgList &CallArgs) { |
| 1030 | CallArgList ActualArgs; |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 1031 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
| 1032 | ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder, |
| 1033 | Sel)), |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1034 | CGF.getContext().getObjCSelType())); |
| 1035 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1036 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1037 | CodeGenTypes &Types = CGM.getTypes(); |
| 1038 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs); |
| 1039 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1040 | |
| 1041 | llvm::Constant *Fn; |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1042 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1043 | Fn = ObjCTypes.getSendStretFn(IsSuper); |
| 1044 | } else if (ResultType->isFloatingType()) { |
| 1045 | // FIXME: Sadly, this is wrong. This actually depends on the |
| 1046 | // architecture. This happens to be right for x86-32 though. |
| 1047 | Fn = ObjCTypes.getSendFpretFn(IsSuper); |
| 1048 | } else { |
| 1049 | Fn = ObjCTypes.getSendFn(IsSuper); |
| 1050 | } |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 1051 | Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy)); |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1052 | return CGF.EmitCall(FnInfo, Fn, ActualArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1055 | llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1056 | const ObjCProtocolDecl *PD) { |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1057 | // FIXME: I don't understand why gcc generates this, or where it is |
| 1058 | // resolved. Investigate. Its also wasteful to look this up over and |
| 1059 | // over. |
| 1060 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1061 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1062 | return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), |
| 1063 | ObjCTypes.ExternalProtocolPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1066 | void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1067 | // FIXME: We shouldn't need this, the protocol decl should contain |
| 1068 | // enough information to tell us whether this was a declaration or a |
| 1069 | // definition. |
| 1070 | DefinedProtocols.insert(PD->getIdentifier()); |
| 1071 | |
| 1072 | // If we have generated a forward reference to this protocol, emit |
| 1073 | // it now. Otherwise do nothing, the protocol objects are lazily |
| 1074 | // emitted. |
| 1075 | if (Protocols.count(PD->getIdentifier())) |
| 1076 | GetOrEmitProtocol(PD); |
| 1077 | } |
| 1078 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1079 | llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1080 | if (DefinedProtocols.count(PD->getIdentifier())) |
| 1081 | return GetOrEmitProtocol(PD); |
| 1082 | return GetOrEmitProtocolRef(PD); |
| 1083 | } |
| 1084 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1085 | /* |
| 1086 | // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions |
| 1087 | struct _objc_protocol { |
| 1088 | struct _objc_protocol_extension *isa; |
| 1089 | char *protocol_name; |
| 1090 | struct _objc_protocol_list *protocol_list; |
| 1091 | struct _objc__method_prototype_list *instance_methods; |
| 1092 | struct _objc__method_prototype_list *class_methods |
| 1093 | }; |
| 1094 | |
| 1095 | See EmitProtocolExtension(). |
| 1096 | */ |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1097 | llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) { |
| 1098 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1099 | |
| 1100 | // Early exit if a defining object has already been generated. |
| 1101 | if (Entry && Entry->hasInitializer()) |
| 1102 | return Entry; |
| 1103 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1104 | // FIXME: I don't understand why gcc generates this, or where it is |
| 1105 | // resolved. Investigate. Its also wasteful to look this up over and |
| 1106 | // over. |
| 1107 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1108 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1109 | const char *ProtocolName = PD->getNameAsCString(); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1110 | |
| 1111 | // Construct method lists. |
| 1112 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1113 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1114 | for (ObjCProtocolDecl::instmeth_iterator |
| 1115 | i = PD->instmeth_begin(CGM.getContext()), |
| 1116 | e = PD->instmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1117 | ObjCMethodDecl *MD = *i; |
| 1118 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1119 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1120 | OptInstanceMethods.push_back(C); |
| 1121 | } else { |
| 1122 | InstanceMethods.push_back(C); |
| 1123 | } |
| 1124 | } |
| 1125 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1126 | for (ObjCProtocolDecl::classmeth_iterator |
| 1127 | i = PD->classmeth_begin(CGM.getContext()), |
| 1128 | e = PD->classmeth_end(CGM.getContext()); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1129 | ObjCMethodDecl *MD = *i; |
| 1130 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1131 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1132 | OptClassMethods.push_back(C); |
| 1133 | } else { |
| 1134 | ClassMethods.push_back(C); |
| 1135 | } |
| 1136 | } |
| 1137 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1138 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1139 | Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1140 | Values[1] = GetClassName(PD->getIdentifier()); |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1141 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1142 | EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1143 | PD->protocol_begin(), |
| 1144 | PD->protocol_end()); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1145 | Values[3] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1146 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" |
| 1147 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1148 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1149 | InstanceMethods); |
| 1150 | Values[4] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1151 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" |
| 1152 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1153 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1154 | ClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1155 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 1156 | Values); |
| 1157 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1158 | if (Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1159 | // Already created, fix the linkage and update the initializer. |
| 1160 | Entry->setLinkage(llvm::GlobalValue::InternalLinkage); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1161 | Entry->setInitializer(Init); |
| 1162 | } else { |
| 1163 | Entry = |
| 1164 | new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, |
| 1165 | llvm::GlobalValue::InternalLinkage, |
| 1166 | Init, |
| 1167 | std::string("\01L_OBJC_PROTOCOL_")+ProtocolName, |
| 1168 | &CGM.getModule()); |
| 1169 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1170 | Entry->setAlignment(4); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1171 | UsedGlobals.push_back(Entry); |
| 1172 | // FIXME: Is this necessary? Why only for protocol? |
| 1173 | Entry->setAlignment(4); |
| 1174 | } |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1175 | |
| 1176 | return Entry; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1179 | llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1180 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1181 | |
| 1182 | if (!Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1183 | // We use the initializer as a marker of whether this is a forward |
| 1184 | // reference or not. At module finalization we add the empty |
| 1185 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1186 | Entry = |
| 1187 | new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1188 | llvm::GlobalValue::ExternalLinkage, |
| 1189 | 0, |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1190 | "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1191 | &CGM.getModule()); |
| 1192 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1193 | Entry->setAlignment(4); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1194 | UsedGlobals.push_back(Entry); |
| 1195 | // FIXME: Is this necessary? Why only for protocol? |
| 1196 | Entry->setAlignment(4); |
| 1197 | } |
| 1198 | |
| 1199 | return Entry; |
| 1200 | } |
| 1201 | |
| 1202 | /* |
| 1203 | struct _objc_protocol_extension { |
| 1204 | uint32_t size; |
| 1205 | struct objc_method_description_list *optional_instance_methods; |
| 1206 | struct objc_method_description_list *optional_class_methods; |
| 1207 | struct objc_property_list *instance_properties; |
| 1208 | }; |
| 1209 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1210 | llvm::Constant * |
| 1211 | CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 1212 | const ConstantVector &OptInstanceMethods, |
| 1213 | const ConstantVector &OptClassMethods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1214 | uint64_t Size = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1215 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1216 | std::vector<llvm::Constant*> Values(4); |
| 1217 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1218 | Values[1] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1219 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_" |
| 1220 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1221 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1222 | OptInstanceMethods); |
| 1223 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1224 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" |
| 1225 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1226 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1227 | OptClassMethods); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1228 | Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + |
| 1229 | PD->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1230 | 0, PD, ObjCTypes); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1231 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1232 | // Return null if no extension bits are used. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1233 | if (Values[1]->isNullValue() && Values[2]->isNullValue() && |
| 1234 | Values[3]->isNullValue()) |
| 1235 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
| 1236 | |
| 1237 | llvm::Constant *Init = |
| 1238 | llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1239 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1240 | // No special section, but goes in llvm.used |
| 1241 | return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(), |
| 1242 | Init, |
| 1243 | 0, 0, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | /* |
| 1247 | struct objc_protocol_list { |
| 1248 | struct objc_protocol_list *next; |
| 1249 | long count; |
| 1250 | Protocol *list[]; |
| 1251 | }; |
| 1252 | */ |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1253 | llvm::Constant * |
| 1254 | CGObjCMac::EmitProtocolList(const std::string &Name, |
| 1255 | ObjCProtocolDecl::protocol_iterator begin, |
| 1256 | ObjCProtocolDecl::protocol_iterator end) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1257 | std::vector<llvm::Constant*> ProtocolRefs; |
| 1258 | |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1259 | for (; begin != end; ++begin) |
| 1260 | ProtocolRefs.push_back(GetProtocolRef(*begin)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1261 | |
| 1262 | // Just return null for empty protocol lists |
| 1263 | if (ProtocolRefs.empty()) |
| 1264 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1265 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1266 | // This list is null terminated. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1267 | ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy)); |
| 1268 | |
| 1269 | std::vector<llvm::Constant*> Values(3); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1270 | // This field is only used by the runtime. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1271 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1272 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
| 1273 | Values[2] = |
| 1274 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy, |
| 1275 | ProtocolRefs.size()), |
| 1276 | ProtocolRefs); |
| 1277 | |
| 1278 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1279 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1280 | CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1281 | 4, false); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1282 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy); |
| 1283 | } |
| 1284 | |
| 1285 | /* |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1286 | struct _objc_property { |
| 1287 | const char * const name; |
| 1288 | const char * const attributes; |
| 1289 | }; |
| 1290 | |
| 1291 | struct _objc_property_list { |
| 1292 | uint32_t entsize; // sizeof (struct _objc_property) |
| 1293 | uint32_t prop_count; |
| 1294 | struct _objc_property[prop_count]; |
| 1295 | }; |
| 1296 | */ |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1297 | llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name, |
| 1298 | const Decl *Container, |
| 1299 | const ObjCContainerDecl *OCD, |
| 1300 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1301 | std::vector<llvm::Constant*> Properties, Prop(2); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1302 | for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(CGM.getContext()), |
| 1303 | E = OCD->prop_end(CGM.getContext()); I != E; ++I) { |
Steve Naroff | 93983f8 | 2009-01-11 12:47:58 +0000 | [diff] [blame] | 1304 | const ObjCPropertyDecl *PD = *I; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1305 | Prop[0] = GetPropertyName(PD->getIdentifier()); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1306 | Prop[1] = GetPropertyTypeString(PD, Container); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1307 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, |
| 1308 | Prop)); |
| 1309 | } |
| 1310 | |
| 1311 | // Return null for empty list. |
| 1312 | if (Properties.empty()) |
| 1313 | return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 1314 | |
| 1315 | unsigned PropertySize = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1316 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1317 | std::vector<llvm::Constant*> Values(3); |
| 1318 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize); |
| 1319 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size()); |
| 1320 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy, |
| 1321 | Properties.size()); |
| 1322 | Values[2] = llvm::ConstantArray::get(AT, Properties); |
| 1323 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1324 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1325 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1326 | CreateMetadataVar(Name, Init, |
| 1327 | (ObjCABI == 2) ? "__DATA, __objc_const" : |
| 1328 | "__OBJC,__property,regular,no_dead_strip", |
| 1329 | (ObjCABI == 2) ? 8 : 4, |
| 1330 | true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1331 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | /* |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1335 | struct objc_method_description_list { |
| 1336 | int count; |
| 1337 | struct objc_method_description list[]; |
| 1338 | }; |
| 1339 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1340 | llvm::Constant * |
| 1341 | CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 1342 | std::vector<llvm::Constant*> Desc(2); |
| 1343 | Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 1344 | ObjCTypes.SelectorPtrTy); |
| 1345 | Desc[1] = GetMethodVarType(MD); |
| 1346 | return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy, |
| 1347 | Desc); |
| 1348 | } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1349 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1350 | llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name, |
| 1351 | const char *Section, |
| 1352 | const ConstantVector &Methods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1353 | // Return null for empty list. |
| 1354 | if (Methods.empty()) |
| 1355 | return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
| 1356 | |
| 1357 | std::vector<llvm::Constant*> Values(2); |
| 1358 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 1359 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy, |
| 1360 | Methods.size()); |
| 1361 | Values[1] = llvm::ConstantArray::get(AT, Methods); |
| 1362 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1363 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1364 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1365 | return llvm::ConstantExpr::getBitCast(GV, |
| 1366 | ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1369 | /* |
| 1370 | struct _objc_category { |
| 1371 | char *category_name; |
| 1372 | char *class_name; |
| 1373 | struct _objc_method_list *instance_methods; |
| 1374 | struct _objc_method_list *class_methods; |
| 1375 | struct _objc_protocol_list *protocols; |
| 1376 | uint32_t size; // <rdar://4585769> |
| 1377 | struct _objc_property_list *instance_properties; |
| 1378 | }; |
| 1379 | */ |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1380 | void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1381 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1382 | |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1383 | // FIXME: This is poor design, the OCD should have a pointer to the |
| 1384 | // category decl. Additionally, note that Category can be null for |
| 1385 | // the @implementation w/o an @interface case. Sema should just |
| 1386 | // create one for us as it does for @implementation so everyone else |
| 1387 | // can live life under a clear blue sky. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1388 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1389 | const ObjCCategoryDecl *Category = |
| 1390 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1391 | std::string ExtName(Interface->getNameAsString() + "_" + |
| 1392 | OCD->getNameAsString()); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1393 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1394 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1395 | for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(), |
| 1396 | e = OCD->instmeth_end(); i != e; ++i) { |
| 1397 | // Instance methods should always be defined. |
| 1398 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1399 | } |
| 1400 | for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(), |
| 1401 | e = OCD->classmeth_end(); i != e; ++i) { |
| 1402 | // Class methods should always be defined. |
| 1403 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1404 | } |
| 1405 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1406 | std::vector<llvm::Constant*> Values(7); |
| 1407 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 1408 | Values[1] = GetClassName(Interface->getIdentifier()); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1409 | Values[2] = |
| 1410 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") + |
| 1411 | ExtName, |
| 1412 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1413 | InstanceMethods); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1414 | Values[3] = |
| 1415 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName, |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1416 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1417 | ClassMethods); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1418 | if (Category) { |
| 1419 | Values[4] = |
| 1420 | EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName, |
| 1421 | Category->protocol_begin(), |
| 1422 | Category->protocol_end()); |
| 1423 | } else { |
| 1424 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 1425 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1426 | Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1427 | |
| 1428 | // If there is no category @interface then there can be no properties. |
| 1429 | if (Category) { |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1430 | Values[6] = EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName, |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1431 | OCD, Category, ObjCTypes); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1432 | } else { |
| 1433 | Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 1434 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1435 | |
| 1436 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy, |
| 1437 | Values); |
| 1438 | |
| 1439 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1440 | CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init, |
| 1441 | "__OBJC,__category,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1442 | 4, true); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1443 | DefinedCategories.push_back(GV); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1446 | // FIXME: Get from somewhere? |
| 1447 | enum ClassFlags { |
| 1448 | eClassFlags_Factory = 0x00001, |
| 1449 | eClassFlags_Meta = 0x00002, |
| 1450 | // <rdr://5142207> |
| 1451 | eClassFlags_HasCXXStructors = 0x02000, |
| 1452 | eClassFlags_Hidden = 0x20000, |
| 1453 | eClassFlags_ABI2_Hidden = 0x00010, |
| 1454 | eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634> |
| 1455 | }; |
| 1456 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1457 | /* |
| 1458 | struct _objc_class { |
| 1459 | Class isa; |
| 1460 | Class super_class; |
| 1461 | const char *name; |
| 1462 | long version; |
| 1463 | long info; |
| 1464 | long instance_size; |
| 1465 | struct _objc_ivar_list *ivars; |
| 1466 | struct _objc_method_list *methods; |
| 1467 | struct _objc_cache *cache; |
| 1468 | struct _objc_protocol_list *protocols; |
| 1469 | // Objective-C 1.0 extensions (<rdr://4585769>) |
| 1470 | const char *ivar_layout; |
| 1471 | struct _objc_class_ext *ext; |
| 1472 | }; |
| 1473 | |
| 1474 | See EmitClassExtension(); |
| 1475 | */ |
| 1476 | void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1477 | DefinedSymbols.insert(ID->getIdentifier()); |
| 1478 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1479 | std::string ClassName = ID->getNameAsString(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1480 | // FIXME: Gross |
| 1481 | ObjCInterfaceDecl *Interface = |
| 1482 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1483 | llvm::Constant *Protocols = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1484 | EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1485 | Interface->protocol_begin(), |
| 1486 | Interface->protocol_end()); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1487 | const llvm::Type *InterfaceTy = |
Chris Lattner | 03d9f34 | 2009-04-01 06:23:52 +0000 | [diff] [blame] | 1488 | CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1489 | unsigned Flags = eClassFlags_Factory; |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1490 | unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1491 | |
| 1492 | // FIXME: Set CXX-structors flag. |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 1493 | if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1494 | Flags |= eClassFlags_Hidden; |
| 1495 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1496 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1497 | for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(), |
| 1498 | e = ID->instmeth_end(); i != e; ++i) { |
| 1499 | // Instance methods should always be defined. |
| 1500 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1501 | } |
| 1502 | for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(), |
| 1503 | e = ID->classmeth_end(); i != e; ++i) { |
| 1504 | // Class methods should always be defined. |
| 1505 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1506 | } |
| 1507 | |
| 1508 | for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(), |
| 1509 | e = ID->propimpl_end(); i != e; ++i) { |
| 1510 | ObjCPropertyImplDecl *PID = *i; |
| 1511 | |
| 1512 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 1513 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 1514 | |
| 1515 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 1516 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 1517 | InstanceMethods.push_back(C); |
| 1518 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 1519 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 1520 | InstanceMethods.push_back(C); |
| 1521 | } |
| 1522 | } |
| 1523 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1524 | std::vector<llvm::Constant*> Values(12); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1525 | Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1526 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1527 | // Record a reference to the super class. |
| 1528 | LazySymbols.insert(Super->getIdentifier()); |
| 1529 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1530 | Values[ 1] = |
| 1531 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 1532 | ObjCTypes.ClassPtrTy); |
| 1533 | } else { |
| 1534 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
| 1535 | } |
| 1536 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 1537 | // Version is always 0. |
| 1538 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 1539 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 1540 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1541 | Values[ 6] = EmitIvarList(ID, false); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1542 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1543 | EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1544 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1545 | InstanceMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1546 | // cache is always NULL. |
| 1547 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
| 1548 | Values[ 9] = Protocols; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1549 | // FIXME: Set ivar_layout |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 1550 | // Values[10] = BuildIvarLayout(ID, true); |
| 1551 | Values[10] = GetIvarLayoutName(0, ObjCTypes); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1552 | Values[11] = EmitClassExtension(ID); |
| 1553 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 1554 | Values); |
| 1555 | |
| 1556 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1557 | CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init, |
| 1558 | "__OBJC,__class,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1559 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1560 | DefinedClasses.push_back(GV); |
| 1561 | } |
| 1562 | |
| 1563 | llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID, |
| 1564 | llvm::Constant *Protocols, |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1565 | const llvm::Type *InterfaceTy, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1566 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1567 | unsigned Flags = eClassFlags_Meta; |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1568 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1569 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 1570 | if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1571 | Flags |= eClassFlags_Hidden; |
| 1572 | |
| 1573 | std::vector<llvm::Constant*> Values(12); |
| 1574 | // The isa for the metaclass is the root of the hierarchy. |
| 1575 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 1576 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 1577 | Root = Super; |
| 1578 | Values[ 0] = |
| 1579 | llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()), |
| 1580 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1581 | // The super class for the metaclass is emitted as the name of the |
| 1582 | // super class. The runtime fixes this up to point to the |
| 1583 | // *metaclass* for the super class. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1584 | if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) { |
| 1585 | Values[ 1] = |
| 1586 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 1587 | ObjCTypes.ClassPtrTy); |
| 1588 | } else { |
| 1589 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
| 1590 | } |
| 1591 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 1592 | // Version is always 0. |
| 1593 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 1594 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 1595 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1596 | Values[ 6] = EmitIvarList(ID, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1597 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1598 | EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1599 | "__OBJC,__cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1600 | Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1601 | // cache is always NULL. |
| 1602 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
| 1603 | Values[ 9] = Protocols; |
| 1604 | // ivar_layout for metaclass is always NULL. |
| 1605 | Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 1606 | // The class extension is always unused for metaclasses. |
| 1607 | Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
| 1608 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 1609 | Values); |
| 1610 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1611 | std::string Name("\01L_OBJC_METACLASS_"); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1612 | Name += ID->getNameAsCString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1613 | |
| 1614 | // Check for a forward reference. |
| 1615 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 1616 | if (GV) { |
| 1617 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 1618 | "Forward metaclass reference has incorrect type."); |
| 1619 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 1620 | GV->setInitializer(Init); |
| 1621 | } else { |
| 1622 | GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 1623 | llvm::GlobalValue::InternalLinkage, |
| 1624 | Init, Name, |
| 1625 | &CGM.getModule()); |
| 1626 | } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1627 | GV->setSection("__OBJC,__meta_class,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1628 | GV->setAlignment(4); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1629 | UsedGlobals.push_back(GV); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1630 | |
| 1631 | return GV; |
| 1632 | } |
| 1633 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1634 | llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1635 | std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1636 | |
| 1637 | // FIXME: Should we look these up somewhere other than the |
| 1638 | // module. Its a bit silly since we only generate these while |
| 1639 | // processing an implementation, so exactly one pointer would work |
| 1640 | // if know when we entered/exitted an implementation block. |
| 1641 | |
| 1642 | // Check for an existing forward reference. |
Fariborz Jahanian | b0d2794 | 2009-01-07 20:11:22 +0000 | [diff] [blame] | 1643 | // Previously, metaclass with internal linkage may have been defined. |
| 1644 | // pass 'true' as 2nd argument so it is returned. |
| 1645 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) { |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1646 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 1647 | "Forward metaclass reference has incorrect type."); |
| 1648 | return GV; |
| 1649 | } else { |
| 1650 | // Generate as an external reference to keep a consistent |
| 1651 | // module. This will be patched up when we emit the metaclass. |
| 1652 | return new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 1653 | llvm::GlobalValue::ExternalLinkage, |
| 1654 | 0, |
| 1655 | Name, |
| 1656 | &CGM.getModule()); |
| 1657 | } |
| 1658 | } |
| 1659 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1660 | /* |
| 1661 | struct objc_class_ext { |
| 1662 | uint32_t size; |
| 1663 | const char *weak_ivar_layout; |
| 1664 | struct _objc_property_list *properties; |
| 1665 | }; |
| 1666 | */ |
| 1667 | llvm::Constant * |
| 1668 | CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) { |
| 1669 | uint64_t Size = |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 1670 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1671 | |
| 1672 | std::vector<llvm::Constant*> Values(3); |
| 1673 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1674 | // FIXME: Output weak_ivar_layout string. |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 1675 | // Values[1] = BuildIvarLayout(ID, false); |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 1676 | Values[1] = GetIvarLayoutName(0, ObjCTypes); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1677 | Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1678 | ID, ID->getClassInterface(), ObjCTypes); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1679 | |
| 1680 | // Return null if no extension bits are used. |
| 1681 | if (Values[1]->isNullValue() && Values[2]->isNullValue()) |
| 1682 | return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
| 1683 | |
| 1684 | llvm::Constant *Init = |
| 1685 | llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1686 | return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1687 | Init, "__OBJC,__class_ext,regular,no_dead_strip", |
| 1688 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
Fariborz Jahanian | f54b194 | 2009-01-17 19:36:33 +0000 | [diff] [blame] | 1691 | /// countInheritedIvars - count number of ivars in class and its super class(s) |
| 1692 | /// |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1693 | static int countInheritedIvars(const ObjCInterfaceDecl *OI, |
| 1694 | ASTContext &Context) { |
Fariborz Jahanian | f54b194 | 2009-01-17 19:36:33 +0000 | [diff] [blame] | 1695 | int count = 0; |
| 1696 | if (!OI) |
| 1697 | return 0; |
| 1698 | const ObjCInterfaceDecl *SuperClass = OI->getSuperClass(); |
| 1699 | if (SuperClass) |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1700 | count += countInheritedIvars(SuperClass, Context); |
Fariborz Jahanian | f54b194 | 2009-01-17 19:36:33 +0000 | [diff] [blame] | 1701 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 1702 | E = OI->ivar_end(); I != E; ++I) |
| 1703 | ++count; |
Fariborz Jahanian | 1819188 | 2009-03-31 18:11:23 +0000 | [diff] [blame] | 1704 | // look into properties. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1705 | for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(Context), |
| 1706 | E = OI->prop_end(Context); I != E; ++I) { |
Fariborz Jahanian | 1819188 | 2009-03-31 18:11:23 +0000 | [diff] [blame] | 1707 | if ((*I)->getPropertyIvarDecl()) |
| 1708 | ++count; |
| 1709 | } |
Fariborz Jahanian | f54b194 | 2009-01-17 19:36:33 +0000 | [diff] [blame] | 1710 | return count; |
| 1711 | } |
| 1712 | |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 1713 | /// getInterfaceDeclForIvar - Get the interface declaration node where |
| 1714 | /// this ivar is declared in. |
| 1715 | /// FIXME. Ideally, this info should be in the ivar node. But currently |
| 1716 | /// it is not and prevailing wisdom is that ASTs should not have more |
| 1717 | /// info than is absolutely needed, even though this info reflects the |
| 1718 | /// source language. |
| 1719 | /// |
| 1720 | static const ObjCInterfaceDecl *getInterfaceDeclForIvar( |
| 1721 | const ObjCInterfaceDecl *OI, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1722 | const ObjCIvarDecl *IVD, |
| 1723 | ASTContext &Context) { |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 1724 | if (!OI) |
| 1725 | return 0; |
| 1726 | assert(isa<ObjCInterfaceDecl>(OI) && "OI is not an interface"); |
| 1727 | for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), |
| 1728 | E = OI->ivar_end(); I != E; ++I) |
| 1729 | if ((*I)->getIdentifier() == IVD->getIdentifier()) |
| 1730 | return OI; |
Fariborz Jahanian | 5a4b453 | 2009-03-31 17:00:52 +0000 | [diff] [blame] | 1731 | // look into properties. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1732 | for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(Context), |
| 1733 | E = OI->prop_end(Context); I != E; ++I) { |
Fariborz Jahanian | 5a4b453 | 2009-03-31 17:00:52 +0000 | [diff] [blame] | 1734 | ObjCPropertyDecl *PDecl = (*I); |
| 1735 | if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl()) |
| 1736 | if (IV->getIdentifier() == IVD->getIdentifier()) |
| 1737 | return OI; |
| 1738 | } |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1739 | return getInterfaceDeclForIvar(OI->getSuperClass(), IVD, Context); |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1742 | /* |
| 1743 | struct objc_ivar { |
| 1744 | char *ivar_name; |
| 1745 | char *ivar_type; |
| 1746 | int ivar_offset; |
| 1747 | }; |
| 1748 | |
| 1749 | struct objc_ivar_list { |
| 1750 | int ivar_count; |
| 1751 | struct objc_ivar list[count]; |
| 1752 | }; |
| 1753 | */ |
| 1754 | llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1755 | bool ForClass) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1756 | std::vector<llvm::Constant*> Ivars, Ivar(3); |
| 1757 | |
| 1758 | // When emitting the root class GCC emits ivar entries for the |
| 1759 | // actual class structure. It is not clear if we need to follow this |
| 1760 | // behavior; for now lets try and get away with not doing it. If so, |
| 1761 | // the cleanest solution would be to make up an ObjCInterfaceDecl |
| 1762 | // for the class. |
| 1763 | if (ForClass) |
| 1764 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1765 | |
| 1766 | ObjCInterfaceDecl *OID = |
| 1767 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 1768 | const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OID); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1769 | |
| 1770 | RecordDecl::field_iterator ifield, pfield; |
| 1771 | const RecordDecl *RD = GetFirstIvarInRecord(OID, ifield, pfield); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1772 | for (RecordDecl::field_iterator e = RD->field_end(CGM.getContext()); |
| 1773 | ifield != e; ++ifield) { |
Fariborz Jahanian | f54b194 | 2009-01-17 19:36:33 +0000 | [diff] [blame] | 1774 | FieldDecl *Field = *ifield; |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 1775 | uint64_t Offset = GetIvarBaseOffset(Layout, Field); |
Fariborz Jahanian | f54b194 | 2009-01-17 19:36:33 +0000 | [diff] [blame] | 1776 | if (Field->getIdentifier()) |
| 1777 | Ivar[0] = GetMethodVarName(Field->getIdentifier()); |
| 1778 | else |
| 1779 | Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 1780 | Ivar[1] = GetMethodVarType(Field); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1781 | Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset); |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 1782 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
| 1785 | // Return null for empty list. |
| 1786 | if (Ivars.empty()) |
| 1787 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
| 1788 | |
| 1789 | std::vector<llvm::Constant*> Values(2); |
| 1790 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 1791 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy, |
| 1792 | Ivars.size()); |
| 1793 | Values[1] = llvm::ConstantArray::get(AT, Ivars); |
| 1794 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1795 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1796 | llvm::GlobalVariable *GV; |
| 1797 | if (ForClass) |
| 1798 | GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(), |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1799 | Init, "__OBJC,__class_vars,regular,no_dead_strip", |
| 1800 | 4, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1801 | else |
| 1802 | GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" |
| 1803 | + ID->getNameAsString(), |
| 1804 | Init, "__OBJC,__instance_vars,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1805 | 4, true); |
| 1806 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
| 1809 | /* |
| 1810 | struct objc_method { |
| 1811 | SEL method_name; |
| 1812 | char *method_types; |
| 1813 | void *method; |
| 1814 | }; |
| 1815 | |
| 1816 | struct objc_method_list { |
| 1817 | struct objc_method_list *obsolete; |
| 1818 | int count; |
| 1819 | struct objc_method methods_list[count]; |
| 1820 | }; |
| 1821 | */ |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1822 | |
| 1823 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 1824 | /// given method if it has been defined. The result is null if the |
| 1825 | /// method has not been defined. The return value has type MethodPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1826 | llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1827 | // FIXME: Use DenseMap::lookup |
| 1828 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 1829 | if (!Fn) |
| 1830 | return 0; |
| 1831 | |
| 1832 | std::vector<llvm::Constant*> Method(3); |
| 1833 | Method[0] = |
| 1834 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 1835 | ObjCTypes.SelectorPtrTy); |
| 1836 | Method[1] = GetMethodVarType(MD); |
| 1837 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 1838 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 1839 | } |
| 1840 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1841 | llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name, |
| 1842 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1843 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1844 | // Return null for empty list. |
| 1845 | if (Methods.empty()) |
| 1846 | return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy); |
| 1847 | |
| 1848 | std::vector<llvm::Constant*> Values(3); |
| 1849 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 1850 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 1851 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 1852 | Methods.size()); |
| 1853 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 1854 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1855 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1856 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1857 | return llvm::ConstantExpr::getBitCast(GV, |
| 1858 | ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1859 | } |
| 1860 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1861 | llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD, |
Daniel Dunbar | bb36d33 | 2009-02-02 21:43:58 +0000 | [diff] [blame] | 1862 | const ObjCContainerDecl *CD) { |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1863 | std::string Name; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 1864 | GetNameForMethod(OMD, CD, Name); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1865 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1866 | CodeGenTypes &Types = CGM.getTypes(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1867 | const llvm::FunctionType *MethodTy = |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1868 | Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic()); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1869 | llvm::Function *Method = |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1870 | llvm::Function::Create(MethodTy, |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1871 | llvm::GlobalValue::InternalLinkage, |
| 1872 | Name, |
| 1873 | &CGM.getModule()); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1874 | MethodDefinitions.insert(std::make_pair(OMD, Method)); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1875 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1876 | return Method; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 1879 | uint64_t CGObjCCommonMac::GetIvarBaseOffset(const llvm::StructLayout *Layout, |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 1880 | const FieldDecl *Field) { |
Fariborz Jahanian | a6681ae | 2009-03-09 20:44:22 +0000 | [diff] [blame] | 1881 | if (!Field->isBitField()) |
| 1882 | return Layout->getElementOffset( |
| 1883 | CGM.getTypes().getLLVMFieldNo(Field)); |
| 1884 | // FIXME. Must be a better way of getting a bitfield base offset. |
| 1885 | uint64_t offset = CGM.getTypes().getLLVMFieldNo(Field); |
| 1886 | const llvm::Type *Ty = CGM.getTypes().ConvertTypeForMemRecursive(Field->getType()); |
| 1887 | uint64_t size = CGM.getTypes().getTargetData().getTypePaddedSizeInBits(Ty); |
| 1888 | offset = (offset*size)/8; |
| 1889 | return offset; |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 1890 | } |
| 1891 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 1892 | /// GetFieldBaseOffset - return's field byt offset. |
| 1893 | uint64_t CGObjCCommonMac::GetFieldBaseOffset(const ObjCInterfaceDecl *OI, |
| 1894 | const llvm::StructLayout *Layout, |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 1895 | const FieldDecl *Field) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 1896 | const ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field); |
Chris Lattner | cd0ee14 | 2009-03-31 08:33:16 +0000 | [diff] [blame] | 1897 | const FieldDecl *FD = OI->lookupFieldDeclForIvar(CGM.getContext(), Ivar); |
| 1898 | return GetIvarBaseOffset(Layout, FD); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 1901 | llvm::GlobalVariable * |
| 1902 | CGObjCCommonMac::CreateMetadataVar(const std::string &Name, |
| 1903 | llvm::Constant *Init, |
| 1904 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 1905 | unsigned Align, |
| 1906 | bool AddToUsed) { |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 1907 | const llvm::Type *Ty = Init->getType(); |
| 1908 | llvm::GlobalVariable *GV = |
| 1909 | new llvm::GlobalVariable(Ty, false, |
| 1910 | llvm::GlobalValue::InternalLinkage, |
| 1911 | Init, |
| 1912 | Name, |
| 1913 | &CGM.getModule()); |
| 1914 | if (Section) |
| 1915 | GV->setSection(Section); |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 1916 | if (Align) |
| 1917 | GV->setAlignment(Align); |
| 1918 | if (AddToUsed) |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 1919 | UsedGlobals.push_back(GV); |
| 1920 | return GV; |
| 1921 | } |
| 1922 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1923 | llvm::Function *CGObjCMac::ModuleInitFunction() { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1924 | // Abuse this interface function as a place to finalize. |
| 1925 | FinishModule(); |
| 1926 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1927 | return NULL; |
| 1928 | } |
| 1929 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1930 | llvm::Constant *CGObjCMac::GetPropertyGetFunction() { |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 1931 | return ObjCTypes.GetPropertyFn; |
| 1932 | } |
| 1933 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1934 | llvm::Constant *CGObjCMac::GetPropertySetFunction() { |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 1935 | return ObjCTypes.SetPropertyFn; |
| 1936 | } |
| 1937 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1938 | llvm::Constant *CGObjCMac::EnumerationMutationFunction() { |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 1939 | return ObjCTypes.EnumerationMutationFn; |
| 1940 | } |
| 1941 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1942 | /* |
| 1943 | |
| 1944 | Objective-C setjmp-longjmp (sjlj) Exception Handling |
| 1945 | -- |
| 1946 | |
| 1947 | The basic framework for a @try-catch-finally is as follows: |
| 1948 | { |
| 1949 | objc_exception_data d; |
| 1950 | id _rethrow = null; |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 1951 | bool _call_try_exit = true; |
| 1952 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1953 | objc_exception_try_enter(&d); |
| 1954 | if (!setjmp(d.jmp_buf)) { |
| 1955 | ... try body ... |
| 1956 | } else { |
| 1957 | // exception path |
| 1958 | id _caught = objc_exception_extract(&d); |
| 1959 | |
| 1960 | // enter new try scope for handlers |
| 1961 | if (!setjmp(d.jmp_buf)) { |
| 1962 | ... match exception and execute catch blocks ... |
| 1963 | |
| 1964 | // fell off end, rethrow. |
| 1965 | _rethrow = _caught; |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1966 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1967 | } else { |
| 1968 | // exception in catch block |
| 1969 | _rethrow = objc_exception_extract(&d); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 1970 | _call_try_exit = false; |
| 1971 | ... jump-through-finally to finally_rethrow ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1972 | } |
| 1973 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1974 | ... jump-through-finally to finally_end ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1975 | |
| 1976 | finally: |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 1977 | if (_call_try_exit) |
| 1978 | objc_exception_try_exit(&d); |
| 1979 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1980 | ... finally block .... |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1981 | ... dispatch to finally destination ... |
| 1982 | |
| 1983 | finally_rethrow: |
| 1984 | objc_exception_throw(_rethrow); |
| 1985 | |
| 1986 | finally_end: |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
| 1989 | This framework differs slightly from the one gcc uses, in that gcc |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 1990 | uses _rethrow to determine if objc_exception_try_exit should be called |
| 1991 | and if the object should be rethrown. This breaks in the face of |
| 1992 | throwing nil and introduces unnecessary branches. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 1993 | |
| 1994 | We specialize this framework for a few particular circumstances: |
| 1995 | |
| 1996 | - If there are no catch blocks, then we avoid emitting the second |
| 1997 | exception handling context. |
| 1998 | |
| 1999 | - If there is a catch-all catch block (i.e. @catch(...) or @catch(id |
| 2000 | e)) we avoid emitting the code to rethrow an uncaught exception. |
| 2001 | |
| 2002 | - FIXME: If there is no @finally block we can do a few more |
| 2003 | simplifications. |
| 2004 | |
| 2005 | Rethrows and Jumps-Through-Finally |
| 2006 | -- |
| 2007 | |
| 2008 | Support for implicit rethrows and jumping through the finally block is |
| 2009 | handled by storing the current exception-handling context in |
| 2010 | ObjCEHStack. |
| 2011 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2012 | In order to implement proper @finally semantics, we support one basic |
| 2013 | mechanism for jumping through the finally block to an arbitrary |
| 2014 | destination. Constructs which generate exits from a @try or @catch |
| 2015 | block use this mechanism to implement the proper semantics by chaining |
| 2016 | jumps, as necessary. |
| 2017 | |
| 2018 | This mechanism works like the one used for indirect goto: we |
| 2019 | arbitrarily assign an ID to each destination and store the ID for the |
| 2020 | destination in a variable prior to entering the finally block. At the |
| 2021 | end of the finally block we simply create a switch to the proper |
| 2022 | destination. |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2023 | |
| 2024 | Code gen for @synchronized(expr) stmt; |
| 2025 | Effectively generating code for: |
| 2026 | objc_sync_enter(expr); |
| 2027 | @try stmt @finally { objc_sync_exit(expr); } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2028 | */ |
| 2029 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2030 | void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 2031 | const Stmt &S) { |
| 2032 | bool isTry = isa<ObjCAtTryStmt>(S); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2033 | // Create various blocks we refer to for handling @finally. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2034 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2035 | llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit"); |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2036 | llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit"); |
| 2037 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
| 2038 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2039 | |
| 2040 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 2041 | // evaluation of the expression must occur before we enter the |
| 2042 | // @synchronized. We can safely avoid a temp here because jumps into |
| 2043 | // @synchronized are illegal & this will dominate uses. |
| 2044 | llvm::Value *SyncArg = 0; |
| 2045 | if (!isTry) { |
| 2046 | SyncArg = |
| 2047 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 2048 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 2049 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2050 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2051 | |
| 2052 | // Push an EH context entry, used for handling rethrows and jumps |
| 2053 | // through finally. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2054 | CGF.PushCleanupBlock(FinallyBlock); |
| 2055 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2056 | CGF.ObjCEHValueStack.push_back(0); |
| 2057 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2058 | // Allocate memory for the exception data and rethrow pointer. |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2059 | llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy, |
| 2060 | "exceptiondata.ptr"); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2061 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy, |
| 2062 | "_rethrow"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2063 | llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty, |
| 2064 | "_call_try_exit"); |
| 2065 | CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr); |
| 2066 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2067 | // Enter a new try block and call setjmp. |
| 2068 | CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData); |
| 2069 | llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0, |
| 2070 | "jmpbufarray"); |
| 2071 | JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp"); |
| 2072 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn, |
| 2073 | JmpBufPtr, "result"); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2074 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2075 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 2076 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2077 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2078 | TryHandler, TryBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2079 | |
| 2080 | // Emit the @try block. |
| 2081 | CGF.EmitBlock(TryBlock); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2082 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 2083 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2084 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2085 | |
| 2086 | // Emit the "exception in @try" block. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2087 | CGF.EmitBlock(TryHandler); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2088 | |
| 2089 | // Retrieve the exception object. We may emit multiple blocks but |
| 2090 | // nothing can cross this so the value is already in SSA form. |
| 2091 | llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn, |
| 2092 | ExceptionData, |
| 2093 | "caught"); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2094 | CGF.ObjCEHValueStack.back() = Caught; |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2095 | if (!isTry) |
| 2096 | { |
| 2097 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2098 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2099 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2100 | } |
| 2101 | else if (const ObjCAtCatchStmt* CatchStmt = |
| 2102 | cast<ObjCAtTryStmt>(S).getCatchStmts()) |
| 2103 | { |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2104 | // Enter a new exception try block (in case a @catch block throws |
| 2105 | // an exception). |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2106 | CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2107 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2108 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn, |
| 2109 | JmpBufPtr, "result"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2110 | llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2111 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2112 | llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch"); |
| 2113 | llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler"); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2114 | CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2115 | |
| 2116 | CGF.EmitBlock(CatchBlock); |
| 2117 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2118 | // Handle catch list. As a special case we check if everything is |
| 2119 | // matched and avoid generating code for falling off the end if |
| 2120 | // so. |
| 2121 | bool AllMatched = false; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2122 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2123 | llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2124 | |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2125 | const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl(); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2126 | const PointerType *PT = 0; |
| 2127 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2128 | // catch(...) always matches. |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2129 | if (!CatchParam) { |
| 2130 | AllMatched = true; |
| 2131 | } else { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2132 | PT = CatchParam->getType()->getAsPointerType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2133 | |
Daniel Dunbar | 97f61d1 | 2008-09-27 22:21:14 +0000 | [diff] [blame] | 2134 | // catch(id e) always matches. |
| 2135 | // FIXME: For the time being we also match id<X>; this should |
| 2136 | // be rejected by Sema instead. |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 2137 | if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) || |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2138 | CatchParam->getType()->isObjCQualifiedIdType()) |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2139 | AllMatched = true; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2140 | } |
| 2141 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2142 | if (AllMatched) { |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2143 | if (CatchParam) { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2144 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2145 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2146 | CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2147 | } |
Anders Carlsson | 1452f55 | 2008-09-11 08:21:54 +0000 | [diff] [blame] | 2148 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2149 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2150 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2151 | break; |
| 2152 | } |
| 2153 | |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2154 | assert(PT && "Unexpected non-pointer type in @catch"); |
| 2155 | QualType T = PT->getPointeeType(); |
Anders Carlsson | 4b7ff6e | 2008-09-11 06:35:14 +0000 | [diff] [blame] | 2156 | const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2157 | assert(ObjCType && "Catch parameter must have Objective-C type!"); |
| 2158 | |
| 2159 | // Check if the @catch block matches the exception object. |
| 2160 | llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl()); |
| 2161 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2162 | llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn, |
| 2163 | Class, Caught, "match"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2164 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2165 | llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2166 | |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2167 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2168 | MatchedBlock, NextCatchBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2169 | |
| 2170 | // Emit the @catch block. |
| 2171 | CGF.EmitBlock(MatchedBlock); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2172 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2173 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2174 | |
| 2175 | llvm::Value *Tmp = |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2176 | CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()), |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2177 | "tmp"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2178 | CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2179 | |
| 2180 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2181 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2182 | |
| 2183 | CGF.EmitBlock(NextCatchBlock); |
| 2184 | } |
| 2185 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2186 | if (!AllMatched) { |
| 2187 | // None of the handlers caught the exception, so store it to be |
| 2188 | // rethrown at the end of the @finally block. |
| 2189 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2190 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2191 | } |
| 2192 | |
| 2193 | // Emit the exception handler for the @catch blocks. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2194 | CGF.EmitBlock(CatchHandler); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2195 | CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn, |
| 2196 | ExceptionData), |
| 2197 | RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2198 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2199 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2200 | } else { |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2201 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2202 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2203 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2204 | } |
| 2205 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2206 | // Pop the exception-handling stack entry. It is important to do |
| 2207 | // this now, because the code in the @finally block is not in this |
| 2208 | // context. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2209 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 2210 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2211 | CGF.ObjCEHValueStack.pop_back(); |
| 2212 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2213 | // Emit the @finally block. |
| 2214 | CGF.EmitBlock(FinallyBlock); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2215 | llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp"); |
| 2216 | |
| 2217 | CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit); |
| 2218 | |
| 2219 | CGF.EmitBlock(FinallyExit); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2220 | CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2221 | |
| 2222 | CGF.EmitBlock(FinallyNoExit); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2223 | if (isTry) { |
| 2224 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 2225 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 2226 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2227 | } else { |
| 2228 | // Emit objc_sync_exit(expr); as finally's sole statement for |
| 2229 | // @synchronized. |
| 2230 | CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, SyncArg); |
Fariborz Jahanian | f2878e5 | 2008-11-21 19:21:53 +0000 | [diff] [blame] | 2231 | } |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2232 | |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2233 | // Emit the switch block |
| 2234 | if (Info.SwitchBlock) |
| 2235 | CGF.EmitBlock(Info.SwitchBlock); |
| 2236 | if (Info.EndBlock) |
| 2237 | CGF.EmitBlock(Info.EndBlock); |
| 2238 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2239 | CGF.EmitBlock(FinallyRethrow); |
| 2240 | CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, |
| 2241 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2242 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2243 | |
| 2244 | CGF.EmitBlock(FinallyEnd); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2245 | } |
| 2246 | |
| 2247 | void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2248 | const ObjCAtThrowStmt &S) { |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2249 | llvm::Value *ExceptionAsObject; |
| 2250 | |
| 2251 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
| 2252 | llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr); |
| 2253 | ExceptionAsObject = |
| 2254 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 2255 | } else { |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2256 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2257 | "Unexpected rethrow outside @catch block."); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2258 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2259 | } |
| 2260 | |
| 2261 | CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2262 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2263 | |
| 2264 | // Clear the insertion point to indicate we are in unreachable code. |
| 2265 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2268 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2269 | /// object: objc_read_weak (id *src) |
| 2270 | /// |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2271 | llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2272 | llvm::Value *AddrWeakObj) |
| 2273 | { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2274 | const llvm::Type* DestTy = |
| 2275 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2276 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2277 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn, |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2278 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2279 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2280 | return read_weak; |
| 2281 | } |
| 2282 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2283 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 2284 | /// objc_assign_weak (id src, id *dst) |
| 2285 | /// |
| 2286 | void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 2287 | llvm::Value *src, llvm::Value *dst) |
| 2288 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2289 | const llvm::Type * SrcTy = src->getType(); |
| 2290 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2291 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2292 | assert(Size <= 8 && "does not support size > 8"); |
| 2293 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2294 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2295 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2296 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2297 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2298 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 2299 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2300 | src, dst, "weakassign"); |
| 2301 | return; |
| 2302 | } |
| 2303 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2304 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 2305 | /// objc_assign_global (id src, id *dst) |
| 2306 | /// |
| 2307 | void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 2308 | llvm::Value *src, llvm::Value *dst) |
| 2309 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2310 | const llvm::Type * SrcTy = src->getType(); |
| 2311 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2312 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2313 | assert(Size <= 8 && "does not support size > 8"); |
| 2314 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2315 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2316 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2317 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2318 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2319 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2320 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn, |
| 2321 | src, dst, "globalassign"); |
| 2322 | return; |
| 2323 | } |
| 2324 | |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2325 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 2326 | /// objc_assign_ivar (id src, id *dst) |
| 2327 | /// |
| 2328 | void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 2329 | llvm::Value *src, llvm::Value *dst) |
| 2330 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2331 | const llvm::Type * SrcTy = src->getType(); |
| 2332 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2333 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2334 | assert(Size <= 8 && "does not support size > 8"); |
| 2335 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2336 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2337 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2338 | } |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2339 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2340 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
| 2341 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn, |
| 2342 | src, dst, "assignivar"); |
| 2343 | return; |
| 2344 | } |
| 2345 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2346 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 2347 | /// objc_assign_strongCast (id src, id *dst) |
| 2348 | /// |
| 2349 | void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 2350 | llvm::Value *src, llvm::Value *dst) |
| 2351 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2352 | const llvm::Type * SrcTy = src->getType(); |
| 2353 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 2354 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 2355 | assert(Size <= 8 && "does not support size > 8"); |
| 2356 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 2357 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2358 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2359 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2360 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2361 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2362 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn, |
| 2363 | src, dst, "weakassign"); |
| 2364 | return; |
| 2365 | } |
| 2366 | |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2367 | /// EmitObjCValueForIvar - Code Gen for ivar reference. |
| 2368 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2369 | LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 2370 | QualType ObjectTy, |
| 2371 | llvm::Value *BaseValue, |
| 2372 | const ObjCIvarDecl *Ivar, |
| 2373 | const FieldDecl *Field, |
| 2374 | unsigned CVRQualifiers) { |
| 2375 | if (Ivar->isBitField()) |
| 2376 | return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field), |
| 2377 | CVRQualifiers); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2378 | // TODO: Add a special case for isa (index 0) |
| 2379 | unsigned Index = CGM.getTypes().getLLVMFieldNo(Field); |
| 2380 | llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp"); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2381 | LValue LV = LValue::MakeAddr(V, |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 2382 | Ivar->getType().getCVRQualifiers()|CVRQualifiers, |
| 2383 | CGM.getContext().getObjCGCAttrKind(Ivar->getType())); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2384 | LValue::SetObjCIvar(LV, true); |
| 2385 | return LV; |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2386 | } |
| 2387 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2388 | llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
| 2389 | ObjCInterfaceDecl *Interface, |
| 2390 | const ObjCIvarDecl *Ivar) { |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 2391 | const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(Interface); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2392 | FieldDecl *Field = Interface->lookupFieldDeclForIvar(CGM.getContext(), Ivar); |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 2393 | uint64_t Offset = GetIvarBaseOffset(Layout, Field); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2394 | return llvm::ConstantInt::get( |
| 2395 | CGM.getTypes().ConvertType(CGM.getContext().LongTy), |
| 2396 | Offset); |
| 2397 | } |
| 2398 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2399 | /* *** Private Interface *** */ |
| 2400 | |
| 2401 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 2402 | /// level information. |
| 2403 | /// |
| 2404 | /// See: <rdr://4810609&4810587&4810587> |
| 2405 | /// struct IMAGE_INFO { |
| 2406 | /// unsigned version; |
| 2407 | /// unsigned flags; |
| 2408 | /// }; |
| 2409 | enum ImageInfoFlags { |
| 2410 | eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies |
| 2411 | eImageInfo_GarbageCollected = (1 << 1), |
| 2412 | eImageInfo_GCOnly = (1 << 2) |
| 2413 | }; |
| 2414 | |
| 2415 | void CGObjCMac::EmitImageInfo() { |
| 2416 | unsigned version = 0; // Version is unused? |
| 2417 | unsigned flags = 0; |
| 2418 | |
| 2419 | // FIXME: Fix and continue? |
| 2420 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 2421 | flags |= eImageInfo_GarbageCollected; |
| 2422 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 2423 | flags |= eImageInfo_GCOnly; |
| 2424 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2425 | // Emitted as int[2]; |
| 2426 | llvm::Constant *values[2] = { |
| 2427 | llvm::ConstantInt::get(llvm::Type::Int32Ty, version), |
| 2428 | llvm::ConstantInt::get(llvm::Type::Int32Ty, flags) |
| 2429 | }; |
| 2430 | llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2431 | |
| 2432 | const char *Section; |
| 2433 | if (ObjCABI == 1) |
| 2434 | Section = "__OBJC, __image_info,regular"; |
| 2435 | else |
| 2436 | Section = "__DATA, __objc_imageinfo, regular, no_dead_strip"; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2437 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2438 | CreateMetadataVar("\01L_OBJC_IMAGE_INFO", |
| 2439 | llvm::ConstantArray::get(AT, values, 2), |
| 2440 | Section, |
| 2441 | 0, |
| 2442 | true); |
| 2443 | GV->setConstant(true); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2444 | } |
| 2445 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2446 | |
| 2447 | // struct objc_module { |
| 2448 | // unsigned long version; |
| 2449 | // unsigned long size; |
| 2450 | // const char *name; |
| 2451 | // Symtab symtab; |
| 2452 | // }; |
| 2453 | |
| 2454 | // FIXME: Get from somewhere |
| 2455 | static const int ModuleVersion = 7; |
| 2456 | |
| 2457 | void CGObjCMac::EmitModuleInfo() { |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 2458 | uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2459 | |
| 2460 | std::vector<llvm::Constant*> Values(4); |
| 2461 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion); |
| 2462 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2463 | // This used to be the filename, now it is unused. <rdr://4327263> |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2464 | Values[2] = GetClassName(&CGM.getContext().Idents.get("")); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2465 | Values[3] = EmitModuleSymbols(); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2466 | CreateMetadataVar("\01L_OBJC_MODULES", |
| 2467 | llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values), |
| 2468 | "__OBJC,__module_info,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2469 | 4, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2470 | } |
| 2471 | |
| 2472 | llvm::Constant *CGObjCMac::EmitModuleSymbols() { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2473 | unsigned NumClasses = DefinedClasses.size(); |
| 2474 | unsigned NumCategories = DefinedCategories.size(); |
| 2475 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2476 | // Return null if no symbols were defined. |
| 2477 | if (!NumClasses && !NumCategories) |
| 2478 | return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy); |
| 2479 | |
| 2480 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2481 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2482 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy); |
| 2483 | Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses); |
| 2484 | Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories); |
| 2485 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2486 | // The runtime expects exactly the list of defined classes followed |
| 2487 | // by the list of defined categories, in a single array. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2488 | std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2489 | for (unsigned i=0; i<NumClasses; i++) |
| 2490 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
| 2491 | ObjCTypes.Int8PtrTy); |
| 2492 | for (unsigned i=0; i<NumCategories; i++) |
| 2493 | Symbols[NumClasses + i] = |
| 2494 | llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
| 2495 | ObjCTypes.Int8PtrTy); |
| 2496 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2497 | Values[4] = |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2498 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2499 | NumClasses + NumCategories), |
| 2500 | Symbols); |
| 2501 | |
| 2502 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 2503 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2504 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2505 | CreateMetadataVar("\01L_OBJC_SYMBOLS", Init, |
| 2506 | "__OBJC,__symbols,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2507 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2508 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy); |
| 2509 | } |
| 2510 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2511 | llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2512 | const ObjCInterfaceDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2513 | LazySymbols.insert(ID->getIdentifier()); |
| 2514 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2515 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 2516 | |
| 2517 | if (!Entry) { |
| 2518 | llvm::Constant *Casted = |
| 2519 | llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()), |
| 2520 | ObjCTypes.ClassPtrTy); |
| 2521 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2522 | CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted, |
| 2523 | "__OBJC,__cls_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2524 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2525 | } |
| 2526 | |
| 2527 | return Builder.CreateLoad(Entry, false, "tmp"); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2528 | } |
| 2529 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2530 | llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2531 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 2532 | |
| 2533 | if (!Entry) { |
| 2534 | llvm::Constant *Casted = |
| 2535 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 2536 | ObjCTypes.SelectorPtrTy); |
| 2537 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2538 | CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted, |
| 2539 | "__OBJC,__message_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2540 | 4, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2541 | } |
| 2542 | |
| 2543 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 2544 | } |
| 2545 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 2546 | llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2547 | llvm::GlobalVariable *&Entry = ClassNames[Ident]; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2548 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2549 | if (!Entry) |
| 2550 | Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
| 2551 | llvm::ConstantArray::get(Ident->getName()), |
| 2552 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 2553 | 1, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2554 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2555 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2556 | } |
| 2557 | |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 2558 | /// GetInterfaceDeclStructLayout - Get layout for ivars of given |
| 2559 | /// interface declaration. |
| 2560 | const llvm::StructLayout *CGObjCCommonMac::GetInterfaceDeclStructLayout( |
| 2561 | const ObjCInterfaceDecl *OID) const { |
| 2562 | const llvm::Type *InterfaceTy = |
| 2563 | CGM.getTypes().ConvertType( |
| 2564 | CGM.getContext().getObjCInterfaceType( |
| 2565 | const_cast<ObjCInterfaceDecl*>(OID))); |
| 2566 | const llvm::StructLayout *Layout = |
| 2567 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy)); |
| 2568 | return Layout; |
| 2569 | } |
| 2570 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 2571 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 2572 | /// ivar layout bitmap. |
| 2573 | llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident, |
| 2574 | const ObjCCommonTypesHelper &ObjCTypes) { |
| 2575 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 2576 | } |
| 2577 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2578 | void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCInterfaceDecl *OI, |
| 2579 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2580 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2581 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2582 | unsigned int BytePos, bool ForStrongLayout, |
| 2583 | int &Index, int &SkIndex, bool &HasUnion) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2584 | bool IsUnion = (RD && RD->isUnion()); |
| 2585 | uint64_t MaxUnionIvarSize = 0; |
| 2586 | uint64_t MaxSkippedUnionIvarSize = 0; |
| 2587 | FieldDecl *MaxField = 0; |
| 2588 | FieldDecl *MaxSkippedField = 0; |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2589 | unsigned base = 0; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2590 | if (RecFields.empty()) |
| 2591 | return; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2592 | if (IsUnion) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2593 | base = BytePos + GetFieldBaseOffset(OI, Layout, RecFields[0]); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2594 | unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0); |
| 2595 | unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth(); |
| 2596 | |
| 2597 | llvm::SmallVector<FieldDecl*, 16> TmpRecFields; |
| 2598 | |
| 2599 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2600 | FieldDecl *Field = RecFields[i]; |
| 2601 | // Skip over unnamed or bitfields |
| 2602 | if (!Field->getIdentifier() || Field->isBitField()) |
| 2603 | continue; |
| 2604 | QualType FQT = Field->getType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2605 | if (FQT->isRecordType() || FQT->isUnionType()) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2606 | if (FQT->isUnionType()) |
| 2607 | HasUnion = true; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2608 | else |
| 2609 | assert(FQT->isRecordType() && |
| 2610 | "only union/record is supported for ivar layout bitmap"); |
| 2611 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2612 | const RecordType *RT = FQT->getAsRecordType(); |
| 2613 | const RecordDecl *RD = RT->getDecl(); |
| 2614 | // FIXME - Find a more efficiant way of passing records down. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2615 | TmpRecFields.append(RD->field_begin(CGM.getContext()), |
| 2616 | RD->field_end(CGM.getContext())); |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2617 | // FIXME - Is Layout correct? |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2618 | BuildAggrIvarLayout(OI, Layout, RD, TmpRecFields, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2619 | BytePos + GetFieldBaseOffset(OI, Layout, Field), |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2620 | ForStrongLayout, Index, SkIndex, |
| 2621 | HasUnion); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2622 | TmpRecFields.clear(); |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2623 | continue; |
| 2624 | } |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2625 | |
| 2626 | if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2627 | const ConstantArrayType *CArray = |
| 2628 | dyn_cast_or_null<ConstantArrayType>(Array); |
| 2629 | assert(CArray && "only array with know element size is supported"); |
| 2630 | FQT = CArray->getElementType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2631 | while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 2632 | const ConstantArrayType *CArray = |
| 2633 | dyn_cast_or_null<ConstantArrayType>(Array); |
| 2634 | FQT = CArray->getElementType(); |
| 2635 | } |
| 2636 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2637 | assert(!FQT->isUnionType() && |
| 2638 | "layout for array of unions not supported"); |
| 2639 | if (FQT->isRecordType()) { |
| 2640 | uint64_t ElCount = CArray->getSize().getZExtValue(); |
| 2641 | int OldIndex = Index; |
| 2642 | int OldSkIndex = SkIndex; |
| 2643 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2644 | // FIXME - Use a common routine with the above! |
| 2645 | const RecordType *RT = FQT->getAsRecordType(); |
| 2646 | const RecordDecl *RD = RT->getDecl(); |
| 2647 | // FIXME - Find a more efficiant way of passing records down. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2648 | TmpRecFields.append(RD->field_begin(CGM.getContext()), |
| 2649 | RD->field_end(CGM.getContext())); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2650 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2651 | BuildAggrIvarLayout(OI, Layout, RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2652 | TmpRecFields, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2653 | BytePos + GetFieldBaseOffset(OI, Layout, Field), |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2654 | ForStrongLayout, Index, SkIndex, |
| 2655 | HasUnion); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2656 | TmpRecFields.clear(); |
| 2657 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2658 | // Replicate layout information for each array element. Note that |
| 2659 | // one element is already done. |
| 2660 | uint64_t ElIx = 1; |
| 2661 | for (int FirstIndex = Index, FirstSkIndex = SkIndex; |
| 2662 | ElIx < ElCount; ElIx++) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2663 | uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2664 | for (int i = OldIndex+1; i <= FirstIndex; ++i) |
| 2665 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2666 | GC_IVAR gcivar; |
| 2667 | gcivar.ivar_bytepos = IvarsInfo[i].ivar_bytepos + Size*ElIx; |
| 2668 | gcivar.ivar_size = IvarsInfo[i].ivar_size; |
| 2669 | IvarsInfo.push_back(gcivar); ++Index; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2670 | } |
| 2671 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2672 | for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2673 | GC_IVAR skivar; |
| 2674 | skivar.ivar_bytepos = SkipIvars[i].ivar_bytepos + Size*ElIx; |
| 2675 | skivar.ivar_size = SkipIvars[i].ivar_size; |
| 2676 | SkipIvars.push_back(skivar); ++SkIndex; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2677 | } |
| 2678 | } |
| 2679 | continue; |
| 2680 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2681 | } |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2682 | // At this point, we are done with Record/Union and array there of. |
| 2683 | // For other arrays we are down to its element type. |
| 2684 | QualType::GCAttrTypes GCAttr = QualType::GCNone; |
| 2685 | do { |
| 2686 | if (FQT.isObjCGCStrong() || FQT.isObjCGCWeak()) { |
| 2687 | GCAttr = FQT.isObjCGCStrong() ? QualType::Strong : QualType::Weak; |
| 2688 | break; |
| 2689 | } |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2690 | else if (CGM.getContext().isObjCObjectPointerType(FQT)) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2691 | GCAttr = QualType::Strong; |
| 2692 | break; |
| 2693 | } |
| 2694 | else if (const PointerType *PT = FQT->getAsPointerType()) { |
| 2695 | FQT = PT->getPointeeType(); |
| 2696 | } |
| 2697 | else { |
| 2698 | break; |
| 2699 | } |
| 2700 | } while (true); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2701 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2702 | if ((ForStrongLayout && GCAttr == QualType::Strong) |
| 2703 | || (!ForStrongLayout && GCAttr == QualType::Weak)) { |
| 2704 | if (IsUnion) |
| 2705 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2706 | uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType()) |
| 2707 | / WordSizeInBits; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2708 | if (UnionIvarSize > MaxUnionIvarSize) |
| 2709 | { |
| 2710 | MaxUnionIvarSize = UnionIvarSize; |
| 2711 | MaxField = Field; |
| 2712 | } |
| 2713 | } |
| 2714 | else |
| 2715 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2716 | GC_IVAR gcivar; |
| 2717 | gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field); |
| 2718 | gcivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) / |
| 2719 | WordSizeInBits; |
| 2720 | IvarsInfo.push_back(gcivar); ++Index; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2721 | } |
| 2722 | } |
| 2723 | else if ((ForStrongLayout && |
| 2724 | (GCAttr == QualType::GCNone || GCAttr == QualType::Weak)) |
| 2725 | || (!ForStrongLayout && GCAttr != QualType::Weak)) { |
| 2726 | if (IsUnion) |
| 2727 | { |
| 2728 | uint64_t UnionIvarSize = CGM.getContext().getTypeSize(Field->getType()); |
| 2729 | if (UnionIvarSize > MaxSkippedUnionIvarSize) |
| 2730 | { |
| 2731 | MaxSkippedUnionIvarSize = UnionIvarSize; |
| 2732 | MaxSkippedField = Field; |
| 2733 | } |
| 2734 | } |
| 2735 | else |
| 2736 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2737 | GC_IVAR skivar; |
| 2738 | skivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, Field); |
| 2739 | skivar.ivar_size = CGM.getContext().getTypeSize(Field->getType()) / |
| 2740 | WordSizeInBits; |
| 2741 | SkipIvars.push_back(skivar); ++SkIndex; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2742 | } |
| 2743 | } |
| 2744 | } |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2745 | if (MaxField) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2746 | GC_IVAR gcivar; |
| 2747 | gcivar.ivar_bytepos = BytePos + GetFieldBaseOffset(OI, Layout, MaxField); |
| 2748 | gcivar.ivar_size = MaxUnionIvarSize; |
| 2749 | IvarsInfo.push_back(gcivar); ++Index; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 2750 | } |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2751 | |
| 2752 | if (MaxSkippedField) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2753 | GC_IVAR skivar; |
| 2754 | skivar.ivar_bytepos = BytePos + |
| 2755 | GetFieldBaseOffset(OI, Layout, MaxSkippedField); |
| 2756 | skivar.ivar_size = MaxSkippedUnionIvarSize; |
| 2757 | SkipIvars.push_back(skivar); ++SkIndex; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2758 | } |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2759 | } |
| 2760 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2761 | static int |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2762 | IvarBytePosCompare(const void *a, const void *b) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2763 | { |
| 2764 | unsigned int sa = ((CGObjCCommonMac::GC_IVAR *)a)->ivar_bytepos; |
| 2765 | unsigned int sb = ((CGObjCCommonMac::GC_IVAR *)b)->ivar_bytepos; |
| 2766 | |
| 2767 | if (sa < sb) |
| 2768 | return -1; |
| 2769 | if (sa > sb) |
| 2770 | return 1; |
| 2771 | return 0; |
| 2772 | } |
| 2773 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2774 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 2775 | /// implementation for the __strong or __weak case. |
| 2776 | /// The layout map displays which words in ivar list must be skipped |
| 2777 | /// and which must be scanned by GC (see below). String is built of bytes. |
| 2778 | /// Each byte is divided up in two nibbles (4-bit each). Left nibble is count |
| 2779 | /// of words to skip and right nibble is count of words to scan. So, each |
| 2780 | /// nibble represents up to 15 workds to skip or scan. Skipping the rest is |
| 2781 | /// represented by a 0x00 byte which also ends the string. |
| 2782 | /// 1. when ForStrongLayout is true, following ivars are scanned: |
| 2783 | /// - id, Class |
| 2784 | /// - object * |
| 2785 | /// - __strong anything |
| 2786 | /// |
| 2787 | /// 2. When ForStrongLayout is false, following ivars are scanned: |
| 2788 | /// - __weak anything |
| 2789 | /// |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 2790 | llvm::Constant *CGObjCCommonMac::BuildIvarLayout( |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2791 | const ObjCImplementationDecl *OMD, |
| 2792 | bool ForStrongLayout) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2793 | int Index = -1; |
| 2794 | int SkIndex = -1; |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2795 | bool hasUnion = false; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2796 | int SkipScan; |
| 2797 | unsigned int WordsToScan, WordsToSkip; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2798 | const llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 2799 | if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC) |
| 2800 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2801 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2802 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2803 | const ObjCInterfaceDecl *OI = OMD->getClassInterface(); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2804 | CGM.getContext().CollectObjCIvars(OI, RecFields); |
| 2805 | if (RecFields.empty()) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2806 | return llvm::Constant::getNullValue(PtrTy); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2807 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2808 | SkipIvars.clear(); |
| 2809 | IvarsInfo.clear(); |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 2810 | |
| 2811 | const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OI); |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 2812 | BuildAggrIvarLayout(OI, Layout, 0, RecFields, 0, ForStrongLayout, |
| 2813 | Index, SkIndex, hasUnion); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2814 | if (Index == -1) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2815 | return llvm::Constant::getNullValue(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2816 | |
| 2817 | // Sort on byte position in case we encounterred a union nested in |
| 2818 | // the ivar list. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2819 | if (hasUnion && !IvarsInfo.empty()) |
| 2820 | qsort(&IvarsInfo[0], Index+1, sizeof(GC_IVAR), IvarBytePosCompare); |
| 2821 | if (hasUnion && !SkipIvars.empty()) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2822 | qsort(&SkipIvars[0], Index+1, sizeof(GC_IVAR), IvarBytePosCompare); |
| 2823 | |
| 2824 | // Build the string of skip/scan nibbles |
| 2825 | SkipScan = -1; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2826 | SkipScanIvars.clear(); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2827 | unsigned int WordSize = |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2828 | CGM.getTypes().getTargetData().getTypePaddedSize(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2829 | if (IvarsInfo[0].ivar_bytepos == 0) { |
| 2830 | WordsToSkip = 0; |
| 2831 | WordsToScan = IvarsInfo[0].ivar_size; |
| 2832 | } |
| 2833 | else { |
| 2834 | WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize; |
| 2835 | WordsToScan = IvarsInfo[0].ivar_size; |
| 2836 | } |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2837 | for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2838 | { |
| 2839 | unsigned int TailPrevGCObjC = |
| 2840 | IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize; |
| 2841 | if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) |
| 2842 | { |
| 2843 | // consecutive 'scanned' object pointers. |
| 2844 | WordsToScan += IvarsInfo[i].ivar_size; |
| 2845 | } |
| 2846 | else |
| 2847 | { |
| 2848 | // Skip over 'gc'able object pointer which lay over each other. |
| 2849 | if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos) |
| 2850 | continue; |
| 2851 | // Must skip over 1 or more words. We save current skip/scan values |
| 2852 | // and start a new pair. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2853 | SKIP_SCAN SkScan; |
| 2854 | SkScan.skip = WordsToSkip; |
| 2855 | SkScan.scan = WordsToScan; |
| 2856 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
| 2857 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2858 | // Skip the hole. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2859 | SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize; |
| 2860 | SkScan.scan = 0; |
| 2861 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2862 | WordsToSkip = 0; |
| 2863 | WordsToScan = IvarsInfo[i].ivar_size; |
| 2864 | } |
| 2865 | } |
| 2866 | if (WordsToScan > 0) |
| 2867 | { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2868 | SKIP_SCAN SkScan; |
| 2869 | SkScan.skip = WordsToSkip; |
| 2870 | SkScan.scan = WordsToScan; |
| 2871 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2872 | } |
| 2873 | |
| 2874 | bool BytesSkipped = false; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2875 | if (!SkipIvars.empty()) |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2876 | { |
| 2877 | int LastByteSkipped = |
| 2878 | SkipIvars[SkIndex].ivar_bytepos + SkipIvars[SkIndex].ivar_size; |
| 2879 | int LastByteScanned = |
| 2880 | IvarsInfo[Index].ivar_bytepos + IvarsInfo[Index].ivar_size * WordSize; |
| 2881 | BytesSkipped = (LastByteSkipped > LastByteScanned); |
| 2882 | // Compute number of bytes to skip at the tail end of the last ivar scanned. |
| 2883 | if (BytesSkipped) |
| 2884 | { |
| 2885 | unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2886 | SKIP_SCAN SkScan; |
| 2887 | SkScan.skip = TotalWords - (LastByteScanned/WordSize); |
| 2888 | SkScan.scan = 0; |
| 2889 | SkipScanIvars.push_back(SkScan); ++SkipScan; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2890 | } |
| 2891 | } |
| 2892 | // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced |
| 2893 | // as 0xMN. |
| 2894 | for (int i = 0; i <= SkipScan; i++) |
| 2895 | { |
| 2896 | if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0 |
| 2897 | && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) { |
| 2898 | // 0xM0 followed by 0x0N detected. |
| 2899 | SkipScanIvars[i].scan = SkipScanIvars[i+1].scan; |
| 2900 | for (int j = i+1; j < SkipScan; j++) |
| 2901 | SkipScanIvars[j] = SkipScanIvars[j+1]; |
| 2902 | --SkipScan; |
| 2903 | } |
| 2904 | } |
| 2905 | |
| 2906 | // Generate the string. |
| 2907 | std::string BitMap; |
| 2908 | for (int i = 0; i <= SkipScan; i++) |
| 2909 | { |
| 2910 | unsigned char byte; |
| 2911 | unsigned int skip_small = SkipScanIvars[i].skip % 0xf; |
| 2912 | unsigned int scan_small = SkipScanIvars[i].scan % 0xf; |
| 2913 | unsigned int skip_big = SkipScanIvars[i].skip / 0xf; |
| 2914 | unsigned int scan_big = SkipScanIvars[i].scan / 0xf; |
| 2915 | |
| 2916 | if (skip_small > 0 || skip_big > 0) |
| 2917 | BytesSkipped = true; |
| 2918 | // first skip big. |
| 2919 | for (unsigned int ix = 0; ix < skip_big; ix++) |
| 2920 | BitMap += (unsigned char)(0xf0); |
| 2921 | |
| 2922 | // next (skip small, scan) |
| 2923 | if (skip_small) |
| 2924 | { |
| 2925 | byte = skip_small << 4; |
| 2926 | if (scan_big > 0) |
| 2927 | { |
| 2928 | byte |= 0xf; |
| 2929 | --scan_big; |
| 2930 | } |
| 2931 | else if (scan_small) |
| 2932 | { |
| 2933 | byte |= scan_small; |
| 2934 | scan_small = 0; |
| 2935 | } |
| 2936 | BitMap += byte; |
| 2937 | } |
| 2938 | // next scan big |
| 2939 | for (unsigned int ix = 0; ix < scan_big; ix++) |
| 2940 | BitMap += (unsigned char)(0x0f); |
| 2941 | // last scan small |
| 2942 | if (scan_small) |
| 2943 | { |
| 2944 | byte = scan_small; |
| 2945 | BitMap += byte; |
| 2946 | } |
| 2947 | } |
| 2948 | // null terminate string. |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2949 | unsigned char zero = 0; |
| 2950 | BitMap += zero; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 2951 | // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as |
| 2952 | // final layout. |
| 2953 | if (ForStrongLayout && !BytesSkipped) |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2954 | return llvm::Constant::getNullValue(PtrTy); |
| 2955 | llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
| 2956 | llvm::ConstantArray::get(BitMap.c_str()), |
| 2957 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 2958 | 1, true); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2959 | // FIXME. Need a commomand-line option for this eventually. |
| 2960 | if (ForStrongLayout) |
| 2961 | printf("\nstrong ivar layout: "); |
| 2962 | else |
| 2963 | printf("\nweak ivar layout: "); |
| 2964 | const unsigned char *s = (unsigned char*)BitMap.c_str(); |
| 2965 | for (unsigned i = 0; i < BitMap.size(); i++) |
Fariborz Jahanian | dbf15cb | 2009-03-26 19:10:36 +0000 | [diff] [blame] | 2966 | if (!(s[i] & 0xf0)) |
| 2967 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 2968 | else |
| 2969 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 2970 | printf("\n"); |
| 2971 | |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 2972 | return getConstantGEP(Entry, 0, 0); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 2973 | } |
| 2974 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 2975 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2976 | llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; |
| 2977 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2978 | // FIXME: Avoid std::string copying. |
| 2979 | if (!Entry) |
| 2980 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_", |
| 2981 | llvm::ConstantArray::get(Sel.getAsString()), |
| 2982 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 2983 | 1, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2984 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2985 | return getConstantGEP(Entry, 0, 0); |
| 2986 | } |
| 2987 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2988 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 2989 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2990 | return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); |
| 2991 | } |
| 2992 | |
| 2993 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 2994 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2995 | return GetMethodVarName(&CGM.getContext().Idents.get(Name)); |
| 2996 | } |
| 2997 | |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 2998 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(FieldDecl *Field) { |
| 2999 | std::string TypeStr; |
| 3000 | CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); |
| 3001 | |
| 3002 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3003 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3004 | if (!Entry) |
| 3005 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
| 3006 | llvm::ConstantArray::get(TypeStr), |
| 3007 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3008 | 1, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3009 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3010 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3011 | } |
| 3012 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3013 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3014 | std::string TypeStr; |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3015 | CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D), |
| 3016 | TypeStr); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3017 | |
| 3018 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
| 3019 | |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3020 | if (!Entry) |
| 3021 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
| 3022 | llvm::ConstantArray::get(TypeStr), |
| 3023 | "__TEXT,__cstring,cstring_literals", |
| 3024 | 1, true); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3025 | |
| 3026 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3027 | } |
| 3028 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3029 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3030 | llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3031 | llvm::GlobalVariable *&Entry = PropertyNames[Ident]; |
| 3032 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3033 | if (!Entry) |
| 3034 | Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_", |
| 3035 | llvm::ConstantArray::get(Ident->getName()), |
| 3036 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3037 | 1, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3038 | |
| 3039 | return getConstantGEP(Entry, 0, 0); |
| 3040 | } |
| 3041 | |
| 3042 | // FIXME: Merge into a single cstring creation function. |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3043 | // FIXME: This Decl should be more precise. |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3044 | llvm::Constant * |
| 3045 | CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 3046 | const Decl *Container) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3047 | std::string TypeStr; |
| 3048 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3049 | return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); |
| 3050 | } |
| 3051 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3052 | void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D, |
| 3053 | const ObjCContainerDecl *CD, |
| 3054 | std::string &NameOut) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 3055 | NameOut = '\01'; |
| 3056 | NameOut += (D->isInstanceMethod() ? '-' : '+'); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3057 | NameOut += '['; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 3058 | assert (CD && "Missing container decl in GetNameForMethod"); |
| 3059 | NameOut += CD->getNameAsString(); |
Fariborz Jahanian | 1e9aef3 | 2009-04-16 18:34:20 +0000 | [diff] [blame] | 3060 | if (const ObjCCategoryImplDecl *CID = |
| 3061 | dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) { |
| 3062 | NameOut += '('; |
| 3063 | NameOut += CID->getNameAsString(); |
| 3064 | NameOut+= ')'; |
| 3065 | } |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3066 | NameOut += ' '; |
| 3067 | NameOut += D->getSelector().getAsString(); |
| 3068 | NameOut += ']'; |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3069 | } |
| 3070 | |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 3071 | /// GetFirstIvarInRecord - This routine returns the record for the |
| 3072 | /// implementation of the fiven class OID. It also returns field |
| 3073 | /// corresponding to the first ivar in the class in FIV. It also |
| 3074 | /// returns the one before the first ivar. |
| 3075 | /// |
| 3076 | const RecordDecl *CGObjCCommonMac::GetFirstIvarInRecord( |
| 3077 | const ObjCInterfaceDecl *OID, |
| 3078 | RecordDecl::field_iterator &FIV, |
| 3079 | RecordDecl::field_iterator &PIV) { |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3080 | int countSuperClassIvars = countInheritedIvars(OID->getSuperClass(), |
| 3081 | CGM.getContext()); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 3082 | const RecordDecl *RD = CGM.getContext().addRecordToClass(OID); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3083 | RecordDecl::field_iterator ifield = RD->field_begin(CGM.getContext()); |
| 3084 | RecordDecl::field_iterator pfield = RD->field_end(CGM.getContext()); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 3085 | while (countSuperClassIvars-- > 0) { |
| 3086 | pfield = ifield; |
| 3087 | ++ifield; |
| 3088 | } |
| 3089 | FIV = ifield; |
| 3090 | PIV = pfield; |
| 3091 | return RD; |
| 3092 | } |
| 3093 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3094 | void CGObjCMac::FinishModule() { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3095 | EmitModuleInfo(); |
| 3096 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3097 | // Emit the dummy bodies for any protocols which were referenced but |
| 3098 | // never defined. |
| 3099 | for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator |
| 3100 | i = Protocols.begin(), e = Protocols.end(); i != e; ++i) { |
| 3101 | if (i->second->hasInitializer()) |
| 3102 | continue; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3103 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3104 | std::vector<llvm::Constant*> Values(5); |
| 3105 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
| 3106 | Values[1] = GetClassName(i->first); |
| 3107 | Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 3108 | Values[3] = Values[4] = |
| 3109 | llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
| 3110 | i->second->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 3111 | i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 3112 | Values)); |
| 3113 | } |
| 3114 | |
| 3115 | std::vector<llvm::Constant*> Used; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3116 | for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3117 | e = UsedGlobals.end(); i != e; ++i) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3118 | Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3119 | } |
| 3120 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3121 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3122 | llvm::GlobalValue *GV = |
| 3123 | new llvm::GlobalVariable(AT, false, |
| 3124 | llvm::GlobalValue::AppendingLinkage, |
| 3125 | llvm::ConstantArray::get(AT, Used), |
| 3126 | "llvm.used", |
| 3127 | &CGM.getModule()); |
| 3128 | |
| 3129 | GV->setSection("llvm.metadata"); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3130 | |
| 3131 | // Add assembler directives to add lazy undefined symbol references |
| 3132 | // for classes which are referenced but not defined. This is |
| 3133 | // important for correct linker interaction. |
| 3134 | |
| 3135 | // FIXME: Uh, this isn't particularly portable. |
| 3136 | std::stringstream s; |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 3137 | |
| 3138 | if (!CGM.getModule().getModuleInlineAsm().empty()) |
| 3139 | s << "\n"; |
| 3140 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3141 | for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(), |
| 3142 | e = LazySymbols.end(); i != e; ++i) { |
| 3143 | s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n"; |
| 3144 | } |
| 3145 | for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(), |
| 3146 | e = DefinedSymbols.end(); i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3147 | s << "\t.objc_class_name_" << (*i)->getName() << "=0\n" |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3148 | << "\t.globl .objc_class_name_" << (*i)->getName() << "\n"; |
| 3149 | } |
Anders Carlsson | 565c99f | 2008-12-10 02:21:04 +0000 | [diff] [blame] | 3150 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3151 | CGM.getModule().appendModuleInlineAsm(s.str()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3152 | } |
| 3153 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3154 | CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3155 | : CGObjCCommonMac(cgm), |
| 3156 | ObjCTypes(cgm) |
| 3157 | { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3158 | ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3159 | ObjCABI = 2; |
| 3160 | } |
| 3161 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3162 | /* *** */ |
| 3163 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3164 | ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm) |
| 3165 | : CGM(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3166 | { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3167 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3168 | ASTContext &Ctx = CGM.getContext(); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3169 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3170 | ShortTy = Types.ConvertType(Ctx.ShortTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3171 | IntTy = Types.ConvertType(Ctx.IntTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3172 | LongTy = Types.ConvertType(Ctx.LongTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3173 | LongLongTy = Types.ConvertType(Ctx.LongLongTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3174 | Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 3175 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3176 | ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType()); |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 3177 | PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3178 | SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3179 | |
| 3180 | // FIXME: It would be nice to unify this with the opaque type, so |
| 3181 | // that the IR comes out a bit cleaner. |
| 3182 | const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); |
| 3183 | ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3184 | |
| 3185 | // I'm not sure I like this. The implicit coordination is a bit |
| 3186 | // gross. We should solve this in a reasonable fashion because this |
| 3187 | // is a pretty common task (match some runtime data structure with |
| 3188 | // an LLVM data structure). |
| 3189 | |
| 3190 | // FIXME: This is leaked. |
| 3191 | // FIXME: Merge with rewriter code? |
| 3192 | |
| 3193 | // struct _objc_super { |
| 3194 | // id self; |
| 3195 | // Class cls; |
| 3196 | // } |
| 3197 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 3198 | SourceLocation(), |
| 3199 | &Ctx.Idents.get("_objc_super")); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3200 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3201 | Ctx.getObjCIdType(), 0, false)); |
| 3202 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3203 | Ctx.getObjCClassType(), 0, false)); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3204 | RD->completeDefinition(Ctx); |
| 3205 | |
| 3206 | SuperCTy = Ctx.getTagDeclType(RD); |
| 3207 | SuperPtrCTy = Ctx.getPointerType(SuperCTy); |
| 3208 | |
| 3209 | SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy)); |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3210 | SuperPtrTy = llvm::PointerType::getUnqual(SuperTy); |
| 3211 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3212 | // struct _prop_t { |
| 3213 | // char *name; |
| 3214 | // char *attributes; |
| 3215 | // } |
| 3216 | PropertyTy = llvm::StructType::get(Int8PtrTy, |
| 3217 | Int8PtrTy, |
| 3218 | NULL); |
| 3219 | CGM.getModule().addTypeName("struct._prop_t", |
| 3220 | PropertyTy); |
| 3221 | |
| 3222 | // struct _prop_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3223 | // uint32_t entsize; // sizeof(struct _prop_t) |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3224 | // uint32_t count_of_properties; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3225 | // struct _prop_t prop_list[count_of_properties]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3226 | // } |
| 3227 | PropertyListTy = llvm::StructType::get(IntTy, |
| 3228 | IntTy, |
| 3229 | llvm::ArrayType::get(PropertyTy, 0), |
| 3230 | NULL); |
| 3231 | CGM.getModule().addTypeName("struct._prop_list_t", |
| 3232 | PropertyListTy); |
| 3233 | // struct _prop_list_t * |
| 3234 | PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy); |
| 3235 | |
| 3236 | // struct _objc_method { |
| 3237 | // SEL _cmd; |
| 3238 | // char *method_type; |
| 3239 | // char *_imp; |
| 3240 | // } |
| 3241 | MethodTy = llvm::StructType::get(SelectorPtrTy, |
| 3242 | Int8PtrTy, |
| 3243 | Int8PtrTy, |
| 3244 | NULL); |
| 3245 | CGM.getModule().addTypeName("struct._objc_method", MethodTy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3246 | |
| 3247 | // struct _objc_cache * |
| 3248 | CacheTy = llvm::OpaqueType::get(); |
| 3249 | CGM.getModule().addTypeName("struct._objc_cache", CacheTy); |
| 3250 | CachePtrTy = llvm::PointerType::getUnqual(CacheTy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3251 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3252 | // Property manipulation functions. |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3253 | |
| 3254 | QualType IdType = Ctx.getObjCIdType(); |
| 3255 | QualType SelType = Ctx.getObjCSelType(); |
| 3256 | llvm::SmallVector<QualType,16> Params; |
| 3257 | const llvm::FunctionType *FTy; |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3258 | |
| 3259 | // id objc_getProperty (id, SEL, ptrdiff_t, bool) |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3260 | Params.push_back(IdType); |
| 3261 | Params.push_back(SelType); |
| 3262 | Params.push_back(Ctx.LongTy); |
| 3263 | Params.push_back(Ctx.BoolTy); |
| 3264 | FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), |
| 3265 | false); |
| 3266 | GetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_getProperty"); |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3267 | |
| 3268 | // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool) |
| 3269 | Params.clear(); |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3270 | Params.push_back(IdType); |
| 3271 | Params.push_back(SelType); |
| 3272 | Params.push_back(Ctx.LongTy); |
| 3273 | Params.push_back(IdType); |
| 3274 | Params.push_back(Ctx.BoolTy); |
| 3275 | Params.push_back(Ctx.BoolTy); |
| 3276 | FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
| 3277 | SetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_setProperty"); |
| 3278 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3279 | // Enumeration mutation. |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3280 | |
| 3281 | // void objc_enumerationMutation (id) |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3282 | Params.clear(); |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3283 | Params.push_back(IdType); |
| 3284 | FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
| 3285 | EnumerationMutationFn = CGM.CreateRuntimeFunction(FTy, |
| 3286 | "objc_enumerationMutation"); |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3287 | |
| 3288 | // gc's API |
| 3289 | // id objc_read_weak (id *) |
| 3290 | Params.clear(); |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3291 | Params.push_back(Ctx.getPointerType(IdType)); |
| 3292 | FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false); |
| 3293 | GcReadWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_read_weak"); |
| 3294 | |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 3295 | // id objc_assign_global (id, id *) |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3296 | Params.clear(); |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3297 | Params.push_back(IdType); |
| 3298 | Params.push_back(Ctx.getPointerType(IdType)); |
| 3299 | |
| 3300 | FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false); |
Daniel Dunbar | 34c94a2 | 2009-02-04 00:44:42 +0000 | [diff] [blame] | 3301 | GcAssignGlobalFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_global"); |
| 3302 | GcAssignIvarFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar"); |
| 3303 | GcAssignStrongCastFn = |
| 3304 | CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast"); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 3305 | |
| 3306 | // void objc_exception_throw(id) |
| 3307 | Params.clear(); |
| 3308 | Params.push_back(IdType); |
| 3309 | |
| 3310 | FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 3311 | ExceptionThrowFn = |
| 3312 | CGM.CreateRuntimeFunction(FTy, "objc_exception_throw"); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3313 | |
| 3314 | // synchronized APIs |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3315 | // void objc_sync_exit (id) |
| 3316 | Params.clear(); |
| 3317 | Params.push_back(IdType); |
| 3318 | |
| 3319 | FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 3320 | SyncExitFn = CGM.CreateRuntimeFunction(FTy, "objc_sync_exit"); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3321 | } |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3322 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3323 | ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) |
| 3324 | : ObjCCommonTypesHelper(cgm) |
| 3325 | { |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3326 | // struct _objc_method_description { |
| 3327 | // SEL name; |
| 3328 | // char *types; |
| 3329 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3330 | MethodDescriptionTy = |
| 3331 | llvm::StructType::get(SelectorPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3332 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3333 | NULL); |
| 3334 | CGM.getModule().addTypeName("struct._objc_method_description", |
| 3335 | MethodDescriptionTy); |
| 3336 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3337 | // struct _objc_method_description_list { |
| 3338 | // int count; |
| 3339 | // struct _objc_method_description[1]; |
| 3340 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3341 | MethodDescriptionListTy = |
| 3342 | llvm::StructType::get(IntTy, |
| 3343 | llvm::ArrayType::get(MethodDescriptionTy, 0), |
| 3344 | NULL); |
| 3345 | CGM.getModule().addTypeName("struct._objc_method_description_list", |
| 3346 | MethodDescriptionListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3347 | |
| 3348 | // struct _objc_method_description_list * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3349 | MethodDescriptionListPtrTy = |
| 3350 | llvm::PointerType::getUnqual(MethodDescriptionListTy); |
| 3351 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3352 | // Protocol description structures |
| 3353 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3354 | // struct _objc_protocol_extension { |
| 3355 | // uint32_t size; // sizeof(struct _objc_protocol_extension) |
| 3356 | // struct _objc_method_description_list *optional_instance_methods; |
| 3357 | // struct _objc_method_description_list *optional_class_methods; |
| 3358 | // struct _objc_property_list *instance_properties; |
| 3359 | // } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3360 | ProtocolExtensionTy = |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3361 | llvm::StructType::get(IntTy, |
| 3362 | MethodDescriptionListPtrTy, |
| 3363 | MethodDescriptionListPtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3364 | PropertyListPtrTy, |
| 3365 | NULL); |
| 3366 | CGM.getModule().addTypeName("struct._objc_protocol_extension", |
| 3367 | ProtocolExtensionTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3368 | |
| 3369 | // struct _objc_protocol_extension * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3370 | ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy); |
| 3371 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3372 | // Handle recursive construction of Protocol and ProtocolList types |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3373 | |
| 3374 | llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(); |
| 3375 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 3376 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3377 | const llvm::Type *T = |
| 3378 | llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 3379 | LongTy, |
| 3380 | llvm::ArrayType::get(ProtocolTyHolder, 0), |
| 3381 | NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3382 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T); |
| 3383 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3384 | // struct _objc_protocol { |
| 3385 | // struct _objc_protocol_extension *isa; |
| 3386 | // char *protocol_name; |
| 3387 | // struct _objc_protocol **_objc_protocol_list; |
| 3388 | // struct _objc_method_description_list *instance_methods; |
| 3389 | // struct _objc_method_description_list *class_methods; |
| 3390 | // } |
| 3391 | T = llvm::StructType::get(ProtocolExtensionPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3392 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3393 | llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 3394 | MethodDescriptionListPtrTy, |
| 3395 | MethodDescriptionListPtrTy, |
| 3396 | NULL); |
| 3397 | cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T); |
| 3398 | |
| 3399 | ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get()); |
| 3400 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 3401 | ProtocolListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3402 | // struct _objc_protocol_list * |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3403 | ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy); |
| 3404 | |
| 3405 | ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get()); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3406 | CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3407 | ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3408 | |
| 3409 | // Class description structures |
| 3410 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3411 | // struct _objc_ivar { |
| 3412 | // char *ivar_name; |
| 3413 | // char *ivar_type; |
| 3414 | // int ivar_offset; |
| 3415 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3416 | IvarTy = llvm::StructType::get(Int8PtrTy, |
| 3417 | Int8PtrTy, |
| 3418 | IntTy, |
| 3419 | NULL); |
| 3420 | CGM.getModule().addTypeName("struct._objc_ivar", IvarTy); |
| 3421 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3422 | // struct _objc_ivar_list * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3423 | IvarListTy = llvm::OpaqueType::get(); |
| 3424 | CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy); |
| 3425 | IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy); |
| 3426 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3427 | // struct _objc_method_list * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3428 | MethodListTy = llvm::OpaqueType::get(); |
| 3429 | CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy); |
| 3430 | MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy); |
| 3431 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3432 | // struct _objc_class_extension * |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3433 | ClassExtensionTy = |
| 3434 | llvm::StructType::get(IntTy, |
| 3435 | Int8PtrTy, |
| 3436 | PropertyListPtrTy, |
| 3437 | NULL); |
| 3438 | CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy); |
| 3439 | ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy); |
| 3440 | |
| 3441 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 3442 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3443 | // struct _objc_class { |
| 3444 | // Class isa; |
| 3445 | // Class super_class; |
| 3446 | // char *name; |
| 3447 | // long version; |
| 3448 | // long info; |
| 3449 | // long instance_size; |
| 3450 | // struct _objc_ivar_list *ivars; |
| 3451 | // struct _objc_method_list *methods; |
| 3452 | // struct _objc_cache *cache; |
| 3453 | // struct _objc_protocol_list *protocols; |
| 3454 | // char *ivar_layout; |
| 3455 | // struct _objc_class_ext *ext; |
| 3456 | // }; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3457 | T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 3458 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 3459 | Int8PtrTy, |
| 3460 | LongTy, |
| 3461 | LongTy, |
| 3462 | LongTy, |
| 3463 | IvarListPtrTy, |
| 3464 | MethodListPtrTy, |
| 3465 | CachePtrTy, |
| 3466 | ProtocolListPtrTy, |
| 3467 | Int8PtrTy, |
| 3468 | ClassExtensionPtrTy, |
| 3469 | NULL); |
| 3470 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T); |
| 3471 | |
| 3472 | ClassTy = cast<llvm::StructType>(ClassTyHolder.get()); |
| 3473 | CGM.getModule().addTypeName("struct._objc_class", ClassTy); |
| 3474 | ClassPtrTy = llvm::PointerType::getUnqual(ClassTy); |
| 3475 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3476 | // struct _objc_category { |
| 3477 | // char *category_name; |
| 3478 | // char *class_name; |
| 3479 | // struct _objc_method_list *instance_method; |
| 3480 | // struct _objc_method_list *class_method; |
| 3481 | // uint32_t size; // sizeof(struct _objc_category) |
| 3482 | // struct _objc_property_list *instance_properties;// category's @property |
| 3483 | // } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3484 | CategoryTy = llvm::StructType::get(Int8PtrTy, |
| 3485 | Int8PtrTy, |
| 3486 | MethodListPtrTy, |
| 3487 | MethodListPtrTy, |
| 3488 | ProtocolListPtrTy, |
| 3489 | IntTy, |
| 3490 | PropertyListPtrTy, |
| 3491 | NULL); |
| 3492 | CGM.getModule().addTypeName("struct._objc_category", CategoryTy); |
| 3493 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3494 | // Global metadata structures |
| 3495 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3496 | // struct _objc_symtab { |
| 3497 | // long sel_ref_cnt; |
| 3498 | // SEL *refs; |
| 3499 | // short cls_def_cnt; |
| 3500 | // short cat_def_cnt; |
| 3501 | // char *defs[cls_def_cnt + cat_def_cnt]; |
| 3502 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3503 | SymtabTy = llvm::StructType::get(LongTy, |
| 3504 | SelectorPtrTy, |
| 3505 | ShortTy, |
| 3506 | ShortTy, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3507 | llvm::ArrayType::get(Int8PtrTy, 0), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3508 | NULL); |
| 3509 | CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy); |
| 3510 | SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy); |
| 3511 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3512 | // struct _objc_module { |
| 3513 | // long version; |
| 3514 | // long size; // sizeof(struct _objc_module) |
| 3515 | // char *name; |
| 3516 | // struct _objc_symtab* symtab; |
| 3517 | // } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3518 | ModuleTy = |
| 3519 | llvm::StructType::get(LongTy, |
| 3520 | LongTy, |
| 3521 | Int8PtrTy, |
| 3522 | SymtabPtrTy, |
| 3523 | NULL); |
| 3524 | CGM.getModule().addTypeName("struct._objc_module", ModuleTy); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3525 | |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 3526 | // Message send functions. |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3527 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3528 | // id objc_msgSend (id, SEL, ...) |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3529 | std::vector<const llvm::Type*> Params; |
| 3530 | Params.push_back(ObjectPtrTy); |
| 3531 | Params.push_back(SelectorPtrTy); |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3532 | MessageSendFn = |
| 3533 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3534 | Params, |
| 3535 | true), |
| 3536 | "objc_msgSend"); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3537 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3538 | // id objc_msgSend_stret (id, SEL, ...) |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3539 | Params.clear(); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3540 | Params.push_back(ObjectPtrTy); |
| 3541 | Params.push_back(SelectorPtrTy); |
| 3542 | MessageSendStretFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3543 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3544 | Params, |
| 3545 | true), |
| 3546 | "objc_msgSend_stret"); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 3547 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3548 | // |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 3549 | Params.clear(); |
| 3550 | Params.push_back(ObjectPtrTy); |
| 3551 | Params.push_back(SelectorPtrTy); |
| 3552 | // FIXME: This should be long double on x86_64? |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3553 | // [double | long double] objc_msgSend_fpret(id self, SEL op, ...) |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 3554 | MessageSendFpretFn = |
| 3555 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy, |
| 3556 | Params, |
| 3557 | true), |
| 3558 | "objc_msgSend_fpret"); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3559 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3560 | // id objc_msgSendSuper(struct objc_super *super, SEL op, ...) |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3561 | Params.clear(); |
| 3562 | Params.push_back(SuperPtrTy); |
| 3563 | Params.push_back(SelectorPtrTy); |
| 3564 | MessageSendSuperFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3565 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3566 | Params, |
| 3567 | true), |
| 3568 | "objc_msgSendSuper"); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3569 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3570 | // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super, |
| 3571 | // SEL op, ...) |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3572 | Params.clear(); |
| 3573 | Params.push_back(Int8PtrTy); |
| 3574 | Params.push_back(SuperPtrTy); |
| 3575 | Params.push_back(SelectorPtrTy); |
| 3576 | MessageSendSuperStretFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3577 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3578 | Params, |
| 3579 | true), |
| 3580 | "objc_msgSendSuper_stret"); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 3581 | |
| 3582 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 3583 | MessageSendSuperFpretFn = MessageSendSuperFn; |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 3584 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3585 | // FIXME: This is the size of the setjmp buffer and should be |
| 3586 | // target specific. 18 is what's used on 32-bit X86. |
| 3587 | uint64_t SetJmpBufferSize = 18; |
| 3588 | |
| 3589 | // Exceptions |
| 3590 | const llvm::Type *StackPtrTy = |
Daniel Dunbar | 1000491 | 2008-09-27 06:32:25 +0000 | [diff] [blame] | 3591 | llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3592 | |
| 3593 | ExceptionDataTy = |
| 3594 | llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty, |
| 3595 | SetJmpBufferSize), |
| 3596 | StackPtrTy, NULL); |
| 3597 | CGM.getModule().addTypeName("struct._objc_exception_data", |
| 3598 | ExceptionDataTy); |
| 3599 | |
| 3600 | Params.clear(); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3601 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
| 3602 | ExceptionTryEnterFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3603 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3604 | Params, |
| 3605 | false), |
| 3606 | "objc_exception_try_enter"); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3607 | ExceptionTryExitFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3608 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3609 | Params, |
| 3610 | false), |
| 3611 | "objc_exception_try_exit"); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3612 | ExceptionExtractFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3613 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3614 | Params, |
| 3615 | false), |
| 3616 | "objc_exception_extract"); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3617 | |
| 3618 | Params.clear(); |
| 3619 | Params.push_back(ClassPtrTy); |
| 3620 | Params.push_back(ObjectPtrTy); |
| 3621 | ExceptionMatchFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3622 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 3623 | Params, |
| 3624 | false), |
| 3625 | "objc_exception_match"); |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 3626 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3627 | Params.clear(); |
| 3628 | Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty)); |
| 3629 | SetJmpFn = |
Daniel Dunbar | ad2dc71 | 2008-10-01 01:06:06 +0000 | [diff] [blame] | 3630 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 3631 | Params, |
| 3632 | false), |
| 3633 | "_setjmp"); |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 3634 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3635 | } |
| 3636 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3637 | ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3638 | : ObjCCommonTypesHelper(cgm) |
| 3639 | { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3640 | // struct _method_list_t { |
| 3641 | // uint32_t entsize; // sizeof(struct _objc_method) |
| 3642 | // uint32_t method_count; |
| 3643 | // struct _objc_method method_list[method_count]; |
| 3644 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3645 | MethodListnfABITy = llvm::StructType::get(IntTy, |
| 3646 | IntTy, |
| 3647 | llvm::ArrayType::get(MethodTy, 0), |
| 3648 | NULL); |
| 3649 | CGM.getModule().addTypeName("struct.__method_list_t", |
| 3650 | MethodListnfABITy); |
| 3651 | // struct method_list_t * |
| 3652 | MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3653 | |
| 3654 | // struct _protocol_t { |
| 3655 | // id isa; // NULL |
| 3656 | // const char * const protocol_name; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3657 | // const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3658 | // const struct method_list_t * const instance_methods; |
| 3659 | // const struct method_list_t * const class_methods; |
| 3660 | // const struct method_list_t *optionalInstanceMethods; |
| 3661 | // const struct method_list_t *optionalClassMethods; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3662 | // const struct _prop_list_t * properties; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3663 | // const uint32_t size; // sizeof(struct _protocol_t) |
| 3664 | // const uint32_t flags; // = 0 |
| 3665 | // } |
| 3666 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3667 | // Holder for struct _protocol_list_t * |
| 3668 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 3669 | |
| 3670 | ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy, |
| 3671 | Int8PtrTy, |
| 3672 | llvm::PointerType::getUnqual( |
| 3673 | ProtocolListTyHolder), |
| 3674 | MethodListnfABIPtrTy, |
| 3675 | MethodListnfABIPtrTy, |
| 3676 | MethodListnfABIPtrTy, |
| 3677 | MethodListnfABIPtrTy, |
| 3678 | PropertyListPtrTy, |
| 3679 | IntTy, |
| 3680 | IntTy, |
| 3681 | NULL); |
| 3682 | CGM.getModule().addTypeName("struct._protocol_t", |
| 3683 | ProtocolnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3684 | |
| 3685 | // struct _protocol_t* |
| 3686 | ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3687 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 3688 | // struct _protocol_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3689 | // long protocol_count; // Note, this is 32/64 bit |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3690 | // struct _protocol_t *[protocol_count]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3691 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3692 | ProtocolListnfABITy = llvm::StructType::get(LongTy, |
| 3693 | llvm::ArrayType::get( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3694 | ProtocolnfABIPtrTy, 0), |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3695 | NULL); |
| 3696 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 3697 | ProtocolListnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3698 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo( |
| 3699 | ProtocolListnfABITy); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3700 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3701 | // struct _objc_protocol_list* |
| 3702 | ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3703 | |
| 3704 | // struct _ivar_t { |
| 3705 | // unsigned long int *offset; // pointer to ivar offset location |
| 3706 | // char *name; |
| 3707 | // char *type; |
| 3708 | // uint32_t alignment; |
| 3709 | // uint32_t size; |
| 3710 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3711 | IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy), |
| 3712 | Int8PtrTy, |
| 3713 | Int8PtrTy, |
| 3714 | IntTy, |
| 3715 | IntTy, |
| 3716 | NULL); |
| 3717 | CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy); |
| 3718 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3719 | // struct _ivar_list_t { |
| 3720 | // uint32 entsize; // sizeof(struct _ivar_t) |
| 3721 | // uint32 count; |
| 3722 | // struct _iver_t list[count]; |
| 3723 | // } |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3724 | IvarListnfABITy = llvm::StructType::get(IntTy, |
| 3725 | IntTy, |
| 3726 | llvm::ArrayType::get( |
| 3727 | IvarnfABITy, 0), |
| 3728 | NULL); |
| 3729 | CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy); |
| 3730 | |
| 3731 | IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy); |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3732 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3733 | // struct _class_ro_t { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3734 | // uint32_t const flags; |
| 3735 | // uint32_t const instanceStart; |
| 3736 | // uint32_t const instanceSize; |
| 3737 | // uint32_t const reserved; // only when building for 64bit targets |
| 3738 | // const uint8_t * const ivarLayout; |
| 3739 | // const char *const name; |
| 3740 | // const struct _method_list_t * const baseMethods; |
| 3741 | // const struct _objc_protocol_list *const baseProtocols; |
| 3742 | // const struct _ivar_list_t *const ivars; |
| 3743 | // const uint8_t * const weakIvarLayout; |
| 3744 | // const struct _prop_list_t * const properties; |
| 3745 | // } |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3746 | |
| 3747 | // FIXME. Add 'reserved' field in 64bit abi mode! |
| 3748 | ClassRonfABITy = llvm::StructType::get(IntTy, |
| 3749 | IntTy, |
| 3750 | IntTy, |
| 3751 | Int8PtrTy, |
| 3752 | Int8PtrTy, |
| 3753 | MethodListnfABIPtrTy, |
| 3754 | ProtocolListnfABIPtrTy, |
| 3755 | IvarListnfABIPtrTy, |
| 3756 | Int8PtrTy, |
| 3757 | PropertyListPtrTy, |
| 3758 | NULL); |
| 3759 | CGM.getModule().addTypeName("struct._class_ro_t", |
| 3760 | ClassRonfABITy); |
| 3761 | |
| 3762 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 3763 | std::vector<const llvm::Type*> Params; |
| 3764 | Params.push_back(ObjectPtrTy); |
| 3765 | Params.push_back(SelectorPtrTy); |
| 3766 | ImpnfABITy = llvm::PointerType::getUnqual( |
| 3767 | llvm::FunctionType::get(ObjectPtrTy, Params, false)); |
| 3768 | |
| 3769 | // struct _class_t { |
| 3770 | // struct _class_t *isa; |
| 3771 | // struct _class_t * const superclass; |
| 3772 | // void *cache; |
| 3773 | // IMP *vtable; |
| 3774 | // struct class_ro_t *ro; |
| 3775 | // } |
| 3776 | |
| 3777 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 3778 | ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 3779 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 3780 | CachePtrTy, |
| 3781 | llvm::PointerType::getUnqual(ImpnfABITy), |
| 3782 | llvm::PointerType::getUnqual( |
| 3783 | ClassRonfABITy), |
| 3784 | NULL); |
| 3785 | CGM.getModule().addTypeName("struct._class_t", ClassnfABITy); |
| 3786 | |
| 3787 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo( |
| 3788 | ClassnfABITy); |
| 3789 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3790 | // LLVM for struct _class_t * |
| 3791 | ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy); |
| 3792 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3793 | // struct _category_t { |
| 3794 | // const char * const name; |
| 3795 | // struct _class_t *const cls; |
| 3796 | // const struct _method_list_t * const instance_methods; |
| 3797 | // const struct _method_list_t * const class_methods; |
| 3798 | // const struct _protocol_list_t * const protocols; |
| 3799 | // const struct _prop_list_t * const properties; |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3800 | // } |
| 3801 | CategorynfABITy = llvm::StructType::get(Int8PtrTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3802 | ClassnfABIPtrTy, |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 3803 | MethodListnfABIPtrTy, |
| 3804 | MethodListnfABIPtrTy, |
| 3805 | ProtocolListnfABIPtrTy, |
| 3806 | PropertyListPtrTy, |
| 3807 | NULL); |
| 3808 | CGM.getModule().addTypeName("struct._category_t", CategorynfABITy); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3809 | |
| 3810 | // New types for nonfragile abi messaging. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3811 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3812 | ASTContext &Ctx = CGM.getContext(); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3813 | |
| 3814 | // MessageRefTy - LLVM for: |
| 3815 | // struct _message_ref_t { |
| 3816 | // IMP messenger; |
| 3817 | // SEL name; |
| 3818 | // }; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3819 | |
| 3820 | // First the clang type for struct _message_ref_t |
| 3821 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 3822 | SourceLocation(), |
| 3823 | &Ctx.Idents.get("_message_ref_t")); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3824 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3825 | Ctx.VoidPtrTy, 0, false)); |
| 3826 | RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
| 3827 | Ctx.getObjCSelType(), 0, false)); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3828 | RD->completeDefinition(Ctx); |
| 3829 | |
| 3830 | MessageRefCTy = Ctx.getTagDeclType(RD); |
| 3831 | MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy); |
| 3832 | MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy)); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3833 | |
| 3834 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
| 3835 | MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy); |
| 3836 | |
| 3837 | // SuperMessageRefTy - LLVM for: |
| 3838 | // struct _super_message_ref_t { |
| 3839 | // SUPER_IMP messenger; |
| 3840 | // SEL name; |
| 3841 | // }; |
| 3842 | SuperMessageRefTy = llvm::StructType::get(ImpnfABITy, |
| 3843 | SelectorPtrTy, |
| 3844 | NULL); |
| 3845 | CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy); |
| 3846 | |
| 3847 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
| 3848 | SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy); |
| 3849 | |
| 3850 | // id objc_msgSend_fixup (id, struct message_ref_t*, ...) |
| 3851 | Params.clear(); |
| 3852 | Params.push_back(ObjectPtrTy); |
| 3853 | Params.push_back(MessageRefPtrTy); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 3854 | MessengerTy = llvm::FunctionType::get(ObjectPtrTy, |
| 3855 | Params, |
| 3856 | true); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3857 | MessageSendFixupFn = |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 3858 | CGM.CreateRuntimeFunction(MessengerTy, |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3859 | "objc_msgSend_fixup"); |
| 3860 | |
| 3861 | // id objc_msgSend_fpret_fixup (id, struct message_ref_t*, ...) |
| 3862 | MessageSendFpretFixupFn = |
| 3863 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3864 | Params, |
| 3865 | true), |
| 3866 | "objc_msgSend_fpret_fixup"); |
| 3867 | |
| 3868 | // id objc_msgSend_stret_fixup (id, struct message_ref_t*, ...) |
| 3869 | MessageSendStretFixupFn = |
| 3870 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3871 | Params, |
| 3872 | true), |
| 3873 | "objc_msgSend_stret_fixup"); |
| 3874 | |
| 3875 | // id objc_msgSendId_fixup (id, struct message_ref_t*, ...) |
| 3876 | MessageSendIdFixupFn = |
| 3877 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3878 | Params, |
| 3879 | true), |
| 3880 | "objc_msgSendId_fixup"); |
| 3881 | |
| 3882 | |
| 3883 | // id objc_msgSendId_stret_fixup (id, struct message_ref_t*, ...) |
| 3884 | MessageSendIdStretFixupFn = |
| 3885 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3886 | Params, |
| 3887 | true), |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 3888 | "objc_msgSendId_stret_fixup"); |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 3889 | |
| 3890 | // id objc_msgSendSuper2_fixup (struct objc_super *, |
| 3891 | // struct _super_message_ref_t*, ...) |
| 3892 | Params.clear(); |
| 3893 | Params.push_back(SuperPtrTy); |
| 3894 | Params.push_back(SuperMessageRefPtrTy); |
| 3895 | MessageSendSuper2FixupFn = |
| 3896 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3897 | Params, |
| 3898 | true), |
| 3899 | "objc_msgSendSuper2_fixup"); |
| 3900 | |
| 3901 | |
| 3902 | // id objc_msgSendSuper2_stret_fixup (struct objc_super *, |
| 3903 | // struct _super_message_ref_t*, ...) |
| 3904 | MessageSendSuper2StretFixupFn = |
| 3905 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 3906 | Params, |
| 3907 | true), |
| 3908 | "objc_msgSendSuper2_stret_fixup"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 3909 | |
| 3910 | Params.clear(); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 3911 | Params.push_back(Int8PtrTy); |
| 3912 | UnwindResumeOrRethrowFn = |
| 3913 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3914 | Params, |
| 3915 | false), |
| 3916 | "_Unwind_Resume_or_Rethrow"); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 3917 | ObjCBeginCatchFn = |
| 3918 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy, |
| 3919 | Params, |
| 3920 | false), |
| 3921 | "objc_begin_catch"); |
| 3922 | |
| 3923 | Params.clear(); |
| 3924 | ObjCEndCatchFn = |
| 3925 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 3926 | Params, |
| 3927 | false), |
| 3928 | "objc_end_catch"); |
| 3929 | |
| 3930 | // struct objc_typeinfo { |
| 3931 | // const void** vtable; // objc_ehtype_vtable + 2 |
| 3932 | // const char* name; // c++ typeinfo string |
| 3933 | // Class cls; |
| 3934 | // }; |
| 3935 | EHTypeTy = llvm::StructType::get(llvm::PointerType::getUnqual(Int8PtrTy), |
| 3936 | Int8PtrTy, |
| 3937 | ClassnfABIPtrTy, |
| 3938 | NULL); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 3939 | CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 3940 | EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3941 | } |
| 3942 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3943 | llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() { |
| 3944 | FinishNonFragileABIModule(); |
| 3945 | |
| 3946 | return NULL; |
| 3947 | } |
| 3948 | |
| 3949 | void CGObjCNonFragileABIMac::FinishNonFragileABIModule() { |
| 3950 | // nonfragile abi has no module definition. |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 3951 | |
| 3952 | // Build list of all implemented classe addresses in array |
| 3953 | // L_OBJC_LABEL_CLASS_$. |
| 3954 | // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$ |
| 3955 | // list of 'nonlazy' implementations (defined as those with a +load{} |
| 3956 | // method!!). |
| 3957 | unsigned NumClasses = DefinedClasses.size(); |
| 3958 | if (NumClasses) { |
| 3959 | std::vector<llvm::Constant*> Symbols(NumClasses); |
| 3960 | for (unsigned i=0; i<NumClasses; i++) |
| 3961 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
| 3962 | ObjCTypes.Int8PtrTy); |
| 3963 | llvm::Constant* Init = |
| 3964 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 3965 | NumClasses), |
| 3966 | Symbols); |
| 3967 | |
| 3968 | llvm::GlobalVariable *GV = |
| 3969 | new llvm::GlobalVariable(Init->getType(), false, |
| 3970 | llvm::GlobalValue::InternalLinkage, |
| 3971 | Init, |
| 3972 | "\01L_OBJC_LABEL_CLASS_$", |
| 3973 | &CGM.getModule()); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 3974 | GV->setAlignment(8); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 3975 | GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip"); |
| 3976 | UsedGlobals.push_back(GV); |
| 3977 | } |
| 3978 | |
| 3979 | // Build list of all implemented category addresses in array |
| 3980 | // L_OBJC_LABEL_CATEGORY_$. |
| 3981 | // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$ |
| 3982 | // list of 'nonlazy' category implementations (defined as those with a +load{} |
| 3983 | // method!!). |
| 3984 | unsigned NumCategory = DefinedCategories.size(); |
| 3985 | if (NumCategory) { |
| 3986 | std::vector<llvm::Constant*> Symbols(NumCategory); |
| 3987 | for (unsigned i=0; i<NumCategory; i++) |
| 3988 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
| 3989 | ObjCTypes.Int8PtrTy); |
| 3990 | llvm::Constant* Init = |
| 3991 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
| 3992 | NumCategory), |
| 3993 | Symbols); |
| 3994 | |
| 3995 | llvm::GlobalVariable *GV = |
| 3996 | new llvm::GlobalVariable(Init->getType(), false, |
| 3997 | llvm::GlobalValue::InternalLinkage, |
| 3998 | Init, |
| 3999 | "\01L_OBJC_LABEL_CATEGORY_$", |
| 4000 | &CGM.getModule()); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 4001 | GV->setAlignment(8); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4002 | GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip"); |
| 4003 | UsedGlobals.push_back(GV); |
| 4004 | } |
| 4005 | |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4006 | // static int L_OBJC_IMAGE_INFO[2] = { 0, flags }; |
| 4007 | // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0 |
| 4008 | std::vector<llvm::Constant*> Values(2); |
| 4009 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0); |
Fariborz Jahanian | 067986e | 2009-02-24 21:08:09 +0000 | [diff] [blame] | 4010 | unsigned int flags = 0; |
Fariborz Jahanian | 66a5c2c | 2009-02-24 23:34:44 +0000 | [diff] [blame] | 4011 | // FIXME: Fix and continue? |
| 4012 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 4013 | flags |= eImageInfo_GarbageCollected; |
| 4014 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 4015 | flags |= eImageInfo_GCOnly; |
Fariborz Jahanian | 067986e | 2009-02-24 21:08:09 +0000 | [diff] [blame] | 4016 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4017 | llvm::Constant* Init = llvm::ConstantArray::get( |
| 4018 | llvm::ArrayType::get(ObjCTypes.IntTy, 2), |
| 4019 | Values); |
| 4020 | llvm::GlobalVariable *IMGV = |
| 4021 | new llvm::GlobalVariable(Init->getType(), false, |
| 4022 | llvm::GlobalValue::InternalLinkage, |
| 4023 | Init, |
| 4024 | "\01L_OBJC_IMAGE_INFO", |
| 4025 | &CGM.getModule()); |
| 4026 | IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip"); |
| 4027 | UsedGlobals.push_back(IMGV); |
| 4028 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4029 | std::vector<llvm::Constant*> Used; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4030 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4031 | for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), |
| 4032 | e = UsedGlobals.end(); i != e; ++i) { |
| 4033 | Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); |
| 4034 | } |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4035 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4036 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); |
| 4037 | llvm::GlobalValue *GV = |
| 4038 | new llvm::GlobalVariable(AT, false, |
| 4039 | llvm::GlobalValue::AppendingLinkage, |
| 4040 | llvm::ConstantArray::get(AT, Used), |
| 4041 | "llvm.used", |
| 4042 | &CGM.getModule()); |
| 4043 | |
| 4044 | GV->setSection("llvm.metadata"); |
| 4045 | |
| 4046 | } |
| 4047 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4048 | // Metadata flags |
| 4049 | enum MetaDataDlags { |
| 4050 | CLS = 0x0, |
| 4051 | CLS_META = 0x1, |
| 4052 | CLS_ROOT = 0x2, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4053 | OBJC2_CLS_HIDDEN = 0x10, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4054 | CLS_EXCEPTION = 0x20 |
| 4055 | }; |
| 4056 | /// BuildClassRoTInitializer - generate meta-data for: |
| 4057 | /// struct _class_ro_t { |
| 4058 | /// uint32_t const flags; |
| 4059 | /// uint32_t const instanceStart; |
| 4060 | /// uint32_t const instanceSize; |
| 4061 | /// uint32_t const reserved; // only when building for 64bit targets |
| 4062 | /// const uint8_t * const ivarLayout; |
| 4063 | /// const char *const name; |
| 4064 | /// const struct _method_list_t * const baseMethods; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4065 | /// const struct _protocol_list_t *const baseProtocols; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4066 | /// const struct _ivar_list_t *const ivars; |
| 4067 | /// const uint8_t * const weakIvarLayout; |
| 4068 | /// const struct _prop_list_t * const properties; |
| 4069 | /// } |
| 4070 | /// |
| 4071 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer( |
| 4072 | unsigned flags, |
| 4073 | unsigned InstanceStart, |
| 4074 | unsigned InstanceSize, |
| 4075 | const ObjCImplementationDecl *ID) { |
| 4076 | std::string ClassName = ID->getNameAsString(); |
| 4077 | std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets! |
| 4078 | Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
| 4079 | Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart); |
| 4080 | Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize); |
| 4081 | // FIXME. For 64bit targets add 0 here. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4082 | // FIXME. ivarLayout is currently null! |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 4083 | Values[ 3] = GetIvarLayoutName(0, ObjCTypes); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4084 | Values[ 4] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4085 | // const struct _method_list_t * const baseMethods; |
| 4086 | std::vector<llvm::Constant*> Methods; |
| 4087 | std::string MethodListName("\01l_OBJC_$_"); |
| 4088 | if (flags & CLS_META) { |
| 4089 | MethodListName += "CLASS_METHODS_" + ID->getNameAsString(); |
| 4090 | for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(), |
| 4091 | e = ID->classmeth_end(); i != e; ++i) { |
| 4092 | // Class methods should always be defined. |
| 4093 | Methods.push_back(GetMethodConstant(*i)); |
| 4094 | } |
| 4095 | } else { |
| 4096 | MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString(); |
| 4097 | for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(), |
| 4098 | e = ID->instmeth_end(); i != e; ++i) { |
| 4099 | // Instance methods should always be defined. |
| 4100 | Methods.push_back(GetMethodConstant(*i)); |
| 4101 | } |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4102 | for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(), |
| 4103 | e = ID->propimpl_end(); i != e; ++i) { |
| 4104 | ObjCPropertyImplDecl *PID = *i; |
| 4105 | |
| 4106 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){ |
| 4107 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 4108 | |
| 4109 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 4110 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4111 | Methods.push_back(C); |
| 4112 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 4113 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4114 | Methods.push_back(C); |
| 4115 | } |
| 4116 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4117 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4118 | Values[ 5] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4119 | "__DATA, __objc_const", Methods); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4120 | |
| 4121 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4122 | assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer"); |
| 4123 | Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_" |
| 4124 | + OID->getNameAsString(), |
| 4125 | OID->protocol_begin(), |
| 4126 | OID->protocol_end()); |
| 4127 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4128 | if (flags & CLS_META) |
| 4129 | Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
| 4130 | else |
| 4131 | Values[ 7] = EmitIvarList(ID); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4132 | // FIXME. weakIvarLayout is currently null. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 4133 | Values[ 8] = GetIvarLayoutName(0, ObjCTypes); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4134 | if (flags & CLS_META) |
| 4135 | Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 4136 | else |
| 4137 | Values[ 9] = |
| 4138 | EmitPropertyList( |
| 4139 | "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
| 4140 | ID, ID->getClassInterface(), ObjCTypes); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4141 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy, |
| 4142 | Values); |
| 4143 | llvm::GlobalVariable *CLASS_RO_GV = |
| 4144 | new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false, |
| 4145 | llvm::GlobalValue::InternalLinkage, |
| 4146 | Init, |
| 4147 | (flags & CLS_META) ? |
| 4148 | std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName : |
| 4149 | std::string("\01l_OBJC_CLASS_RO_$_")+ClassName, |
| 4150 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4151 | CLASS_RO_GV->setAlignment( |
| 4152 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4153 | CLASS_RO_GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4154 | return CLASS_RO_GV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4155 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4156 | } |
| 4157 | |
| 4158 | /// BuildClassMetaData - This routine defines that to-level meta-data |
| 4159 | /// for the given ClassName for: |
| 4160 | /// struct _class_t { |
| 4161 | /// struct _class_t *isa; |
| 4162 | /// struct _class_t * const superclass; |
| 4163 | /// void *cache; |
| 4164 | /// IMP *vtable; |
| 4165 | /// struct class_ro_t *ro; |
| 4166 | /// } |
| 4167 | /// |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4168 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData( |
| 4169 | std::string &ClassName, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4170 | llvm::Constant *IsAGV, |
| 4171 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4172 | llvm::Constant *ClassRoGV, |
| 4173 | bool HiddenVisibility) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4174 | std::vector<llvm::Constant*> Values(5); |
| 4175 | Values[0] = IsAGV; |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4176 | Values[1] = SuperClassGV |
| 4177 | ? SuperClassGV |
| 4178 | : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4179 | Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar |
| 4180 | Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar |
| 4181 | Values[4] = ClassRoGV; // &CLASS_RO_GV |
| 4182 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy, |
| 4183 | Values); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4184 | llvm::GlobalVariable *GV = GetClassGlobal(ClassName); |
| 4185 | GV->setInitializer(Init); |
Fariborz Jahanian | dd0db2a | 2009-01-31 01:07:39 +0000 | [diff] [blame] | 4186 | GV->setSection("__DATA, __objc_data"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4187 | GV->setAlignment( |
| 4188 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy)); |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4189 | if (HiddenVisibility) |
| 4190 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4191 | return GV; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4192 | } |
| 4193 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4194 | void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) { |
| 4195 | std::string ClassName = ID->getNameAsString(); |
| 4196 | if (!ObjCEmptyCacheVar) { |
| 4197 | ObjCEmptyCacheVar = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4198 | ObjCTypes.CacheTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4199 | false, |
| 4200 | llvm::GlobalValue::ExternalLinkage, |
| 4201 | 0, |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4202 | "_objc_empty_cache", |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4203 | &CGM.getModule()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4204 | |
| 4205 | ObjCEmptyVtableVar = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4206 | ObjCTypes.ImpnfABITy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4207 | false, |
| 4208 | llvm::GlobalValue::ExternalLinkage, |
| 4209 | 0, |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4210 | "_objc_empty_vtable", |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4211 | &CGM.getModule()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4212 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4213 | assert(ID->getClassInterface() && |
| 4214 | "CGObjCNonFragileABIMac::GenerateClass - class is 0"); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4215 | uint32_t InstanceStart = |
| 4216 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy); |
| 4217 | uint32_t InstanceSize = InstanceStart; |
| 4218 | uint32_t flags = CLS_META; |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4219 | std::string ObjCMetaClassName(getMetaclassSymbolPrefix()); |
| 4220 | std::string ObjCClassName(getClassSymbolPrefix()); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4221 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4222 | llvm::GlobalVariable *SuperClassGV, *IsAGV; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4223 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4224 | bool classIsHidden = |
| 4225 | CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4226 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4227 | flags |= OBJC2_CLS_HIDDEN; |
| 4228 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4229 | // class is root |
| 4230 | flags |= CLS_ROOT; |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4231 | SuperClassGV = GetClassGlobal(ObjCClassName + ClassName); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4232 | IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4233 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4234 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4235 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 4236 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 4237 | Root = Super; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4238 | IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString()); |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4239 | // work on super class metadata symbol. |
| 4240 | std::string SuperClassName = |
| 4241 | ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4242 | SuperClassGV = GetClassGlobal(SuperClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4243 | } |
| 4244 | llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags, |
| 4245 | InstanceStart, |
| 4246 | InstanceSize,ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4247 | std::string TClassName = ObjCMetaClassName + ClassName; |
| 4248 | llvm::GlobalVariable *MetaTClass = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4249 | BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV, |
| 4250 | classIsHidden); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4251 | |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4252 | // Metadata for the class |
| 4253 | flags = CLS; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4254 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4255 | flags |= OBJC2_CLS_HIDDEN; |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4256 | |
| 4257 | if (hasObjCExceptionAttribute(ID->getClassInterface())) |
| 4258 | flags |= CLS_EXCEPTION; |
| 4259 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4260 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4261 | flags |= CLS_ROOT; |
| 4262 | SuperClassGV = 0; |
| 4263 | } |
| 4264 | else { |
| 4265 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4266 | std::string RootClassName = |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4267 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4268 | SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4269 | } |
Fariborz Jahanian | ebf9ed3 | 2009-03-20 20:48:19 +0000 | [diff] [blame] | 4270 | // FIXME: Gross |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4271 | InstanceStart = InstanceSize = 0; |
| 4272 | if (ObjCInterfaceDecl *OID = |
| 4273 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface())) { |
| 4274 | // FIXME. Share this with the one in EmitIvarList. |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 4275 | const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OID); |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4276 | |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 4277 | RecordDecl::field_iterator firstField, lastField; |
| 4278 | const RecordDecl *RD = GetFirstIvarInRecord(OID, firstField, lastField); |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4279 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4280 | for (RecordDecl::field_iterator e = RD->field_end(CGM.getContext()), |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4281 | ifield = firstField; ifield != e; ++ifield) |
| 4282 | lastField = ifield; |
| 4283 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4284 | if (lastField != RD->field_end(CGM.getContext())) { |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4285 | FieldDecl *Field = *lastField; |
| 4286 | const llvm::Type *FieldTy = |
| 4287 | CGM.getTypes().ConvertTypeForMem(Field->getType()); |
| 4288 | unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy); |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 4289 | InstanceSize = GetIvarBaseOffset(Layout, Field) + Size; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4290 | if (firstField == RD->field_end(CGM.getContext())) |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4291 | InstanceStart = InstanceSize; |
Fariborz Jahanian | 11894a4 | 2009-03-07 19:43:20 +0000 | [diff] [blame] | 4292 | else { |
| 4293 | Field = *firstField; |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 4294 | InstanceStart = GetIvarBaseOffset(Layout, Field); |
Fariborz Jahanian | 11894a4 | 2009-03-07 19:43:20 +0000 | [diff] [blame] | 4295 | } |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4296 | } |
| 4297 | } |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4298 | CLASS_RO_GV = BuildClassRoTInitializer(flags, |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4299 | InstanceStart, |
| 4300 | InstanceSize, |
| 4301 | ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4302 | |
| 4303 | TClassName = ObjCClassName + ClassName; |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4304 | llvm::GlobalVariable *ClassMD = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4305 | BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV, |
| 4306 | classIsHidden); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4307 | DefinedClasses.push_back(ClassMD); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4308 | |
| 4309 | // Force the definition of the EHType if necessary. |
| 4310 | if (flags & CLS_EXCEPTION) |
| 4311 | GetInterfaceEHType(ID->getClassInterface(), true); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4312 | } |
| 4313 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4314 | /// GenerateProtocolRef - This routine is called to generate code for |
| 4315 | /// a protocol reference expression; as in: |
| 4316 | /// @code |
| 4317 | /// @protocol(Proto1); |
| 4318 | /// @endcode |
| 4319 | /// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1 |
| 4320 | /// which will hold address of the protocol meta-data. |
| 4321 | /// |
| 4322 | llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder, |
| 4323 | const ObjCProtocolDecl *PD) { |
| 4324 | |
Fariborz Jahanian | 960cd06 | 2009-04-10 18:47:34 +0000 | [diff] [blame] | 4325 | // This routine is called for @protocol only. So, we must build definition |
| 4326 | // of protocol's meta-data (not a reference to it!) |
| 4327 | // |
| 4328 | llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD), |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4329 | ObjCTypes.ExternalProtocolPtrTy); |
| 4330 | |
| 4331 | std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_"); |
| 4332 | ProtocolName += PD->getNameAsCString(); |
| 4333 | |
| 4334 | llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName); |
| 4335 | if (PTGV) |
| 4336 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 4337 | PTGV = new llvm::GlobalVariable( |
| 4338 | Init->getType(), false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4339 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4340 | Init, |
| 4341 | ProtocolName, |
| 4342 | &CGM.getModule()); |
| 4343 | PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip"); |
| 4344 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4345 | UsedGlobals.push_back(PTGV); |
| 4346 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 4347 | } |
| 4348 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4349 | /// GenerateCategory - Build metadata for a category implementation. |
| 4350 | /// struct _category_t { |
| 4351 | /// const char * const name; |
| 4352 | /// struct _class_t *const cls; |
| 4353 | /// const struct _method_list_t * const instance_methods; |
| 4354 | /// const struct _method_list_t * const class_methods; |
| 4355 | /// const struct _protocol_list_t * const protocols; |
| 4356 | /// const struct _prop_list_t * const properties; |
| 4357 | /// } |
| 4358 | /// |
| 4359 | void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) |
| 4360 | { |
| 4361 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4362 | const char *Prefix = "\01l_OBJC_$_CATEGORY_"; |
| 4363 | std::string ExtCatName(Prefix + Interface->getNameAsString()+ |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4364 | "_$_" + OCD->getNameAsString()); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4365 | std::string ExtClassName(getClassSymbolPrefix() + |
| 4366 | Interface->getNameAsString()); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4367 | |
| 4368 | std::vector<llvm::Constant*> Values(6); |
| 4369 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 4370 | // meta-class entry symbol |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4371 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4372 | Values[1] = ClassGV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4373 | std::vector<llvm::Constant*> Methods; |
| 4374 | std::string MethodListName(Prefix); |
| 4375 | MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() + |
| 4376 | "_$_" + OCD->getNameAsString(); |
| 4377 | |
| 4378 | for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(), |
| 4379 | e = OCD->instmeth_end(); i != e; ++i) { |
| 4380 | // Instance methods should always be defined. |
| 4381 | Methods.push_back(GetMethodConstant(*i)); |
| 4382 | } |
| 4383 | |
| 4384 | Values[2] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4385 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4386 | Methods); |
| 4387 | |
| 4388 | MethodListName = Prefix; |
| 4389 | MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" + |
| 4390 | OCD->getNameAsString(); |
| 4391 | Methods.clear(); |
| 4392 | for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(), |
| 4393 | e = OCD->classmeth_end(); i != e; ++i) { |
| 4394 | // Class methods should always be defined. |
| 4395 | Methods.push_back(GetMethodConstant(*i)); |
| 4396 | } |
| 4397 | |
| 4398 | Values[3] = EmitMethodList(MethodListName, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4399 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4400 | Methods); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4401 | const ObjCCategoryDecl *Category = |
| 4402 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 4403 | if (Category) { |
| 4404 | std::string ExtName(Interface->getNameAsString() + "_$_" + |
| 4405 | OCD->getNameAsString()); |
| 4406 | Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_" |
| 4407 | + Interface->getNameAsString() + "_$_" |
| 4408 | + Category->getNameAsString(), |
| 4409 | Category->protocol_begin(), |
| 4410 | Category->protocol_end()); |
| 4411 | Values[5] = |
| 4412 | EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName, |
| 4413 | OCD, Category, ObjCTypes); |
| 4414 | } |
| 4415 | else { |
| 4416 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 4417 | Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 4418 | } |
| 4419 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4420 | llvm::Constant *Init = |
| 4421 | llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy, |
| 4422 | Values); |
| 4423 | llvm::GlobalVariable *GCATV |
| 4424 | = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy, |
| 4425 | false, |
| 4426 | llvm::GlobalValue::InternalLinkage, |
| 4427 | Init, |
| 4428 | ExtCatName, |
| 4429 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4430 | GCATV->setAlignment( |
| 4431 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4432 | GCATV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4433 | UsedGlobals.push_back(GCATV); |
| 4434 | DefinedCategories.push_back(GCATV); |
| 4435 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4436 | |
| 4437 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 4438 | /// given method if it has been defined. The result is null if the |
| 4439 | /// method has not been defined. The return value has type MethodPtrTy. |
| 4440 | llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant( |
| 4441 | const ObjCMethodDecl *MD) { |
| 4442 | // FIXME: Use DenseMap::lookup |
| 4443 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 4444 | if (!Fn) |
| 4445 | return 0; |
| 4446 | |
| 4447 | std::vector<llvm::Constant*> Method(3); |
| 4448 | Method[0] = |
| 4449 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 4450 | ObjCTypes.SelectorPtrTy); |
| 4451 | Method[1] = GetMethodVarType(MD); |
| 4452 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 4453 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 4454 | } |
| 4455 | |
| 4456 | /// EmitMethodList - Build meta-data for method declarations |
| 4457 | /// struct _method_list_t { |
| 4458 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 4459 | /// uint32_t method_count; |
| 4460 | /// struct _objc_method method_list[method_count]; |
| 4461 | /// } |
| 4462 | /// |
| 4463 | llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList( |
| 4464 | const std::string &Name, |
| 4465 | const char *Section, |
| 4466 | const ConstantVector &Methods) { |
| 4467 | // Return null for empty list. |
| 4468 | if (Methods.empty()) |
| 4469 | return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy); |
| 4470 | |
| 4471 | std::vector<llvm::Constant*> Values(3); |
| 4472 | // sizeof(struct _objc_method) |
| 4473 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy); |
| 4474 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4475 | // method_count |
| 4476 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 4477 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 4478 | Methods.size()); |
| 4479 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 4480 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4481 | |
| 4482 | llvm::GlobalVariable *GV = |
| 4483 | new llvm::GlobalVariable(Init->getType(), false, |
| 4484 | llvm::GlobalValue::InternalLinkage, |
| 4485 | Init, |
| 4486 | Name, |
| 4487 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4488 | GV->setAlignment( |
| 4489 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4490 | GV->setSection(Section); |
| 4491 | UsedGlobals.push_back(GV); |
| 4492 | return llvm::ConstantExpr::getBitCast(GV, |
| 4493 | ObjCTypes.MethodListnfABIPtrTy); |
| 4494 | } |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4495 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4496 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 4497 | /// the given ivar. |
| 4498 | /// |
| 4499 | llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable( |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 4500 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4501 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame^] | 4502 | std::string Name = "OBJC_IVAR_$_" + |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4503 | getInterfaceDeclForIvar(ID, Ivar, CGM.getContext())->getNameAsString() + |
| 4504 | '.' + Ivar->getNameAsString(); |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4505 | llvm::GlobalVariable *IvarOffsetGV = |
| 4506 | CGM.getModule().getGlobalVariable(Name); |
| 4507 | if (!IvarOffsetGV) |
| 4508 | IvarOffsetGV = |
| 4509 | new llvm::GlobalVariable(ObjCTypes.LongTy, |
| 4510 | false, |
| 4511 | llvm::GlobalValue::ExternalLinkage, |
| 4512 | 0, |
| 4513 | Name, |
| 4514 | &CGM.getModule()); |
| 4515 | return IvarOffsetGV; |
| 4516 | } |
| 4517 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4518 | llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar( |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4519 | const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4520 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4521 | unsigned long int Offset) { |
| 4522 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4523 | assert(ID && "EmitIvarOffsetVar - null interface decl."); |
Daniel Dunbar | 7595fb1 | 2009-04-15 19:52:32 +0000 | [diff] [blame] | 4524 | std::string ExternalName("OBJC_IVAR_$_" + ID->getNameAsString() + '.' |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4525 | + Ivar->getNameAsString()); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4526 | llvm::Constant *Init = llvm::ConstantInt::get(ObjCTypes.LongTy, Offset); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4527 | llvm::GlobalVariable *IvarOffsetGV = |
| 4528 | CGM.getModule().getGlobalVariable(ExternalName); |
Fariborz Jahanian | 77c9fd2 | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 4529 | if (IvarOffsetGV) |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4530 | // ivar offset symbol already built due to user code referencing it. |
| 4531 | IvarOffsetGV->setInitializer(Init); |
Fariborz Jahanian | 77c9fd2 | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 4532 | else |
| 4533 | IvarOffsetGV = |
| 4534 | new llvm::GlobalVariable(Init->getType(), |
| 4535 | false, |
| 4536 | llvm::GlobalValue::ExternalLinkage, |
| 4537 | Init, |
| 4538 | ExternalName, |
| 4539 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4540 | IvarOffsetGV->setAlignment( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4541 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy)); |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4542 | // @private and @package have hidden visibility. |
| 4543 | bool globalVisibility = (Ivar->getAccessControl() == ObjCIvarDecl::Public || |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4544 | Ivar->getAccessControl() == ObjCIvarDecl::Protected); |
| 4545 | if (!globalVisibility || CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden) |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4546 | IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4547 | else |
Fariborz Jahanian | 77c9fd2 | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 4548 | IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4549 | IvarOffsetGV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4550 | return IvarOffsetGV; |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4551 | } |
| 4552 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4553 | /// EmitIvarList - Emit the ivar list for the given |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 4554 | /// implementation. The return value has type |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4555 | /// IvarListnfABIPtrTy. |
| 4556 | /// struct _ivar_t { |
| 4557 | /// unsigned long int *offset; // pointer to ivar offset location |
| 4558 | /// char *name; |
| 4559 | /// char *type; |
| 4560 | /// uint32_t alignment; |
| 4561 | /// uint32_t size; |
| 4562 | /// } |
| 4563 | /// struct _ivar_list_t { |
| 4564 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 4565 | /// uint32 count; |
| 4566 | /// struct _iver_t list[count]; |
| 4567 | /// } |
| 4568 | /// |
| 4569 | llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList( |
| 4570 | const ObjCImplementationDecl *ID) { |
| 4571 | |
| 4572 | std::vector<llvm::Constant*> Ivars, Ivar(5); |
| 4573 | |
| 4574 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4575 | assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface"); |
| 4576 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4577 | // FIXME. Consolidate this with similar code in GenerateClass. |
Fariborz Jahanian | 21e6f17 | 2009-03-11 21:42:00 +0000 | [diff] [blame] | 4578 | const llvm::StructLayout *Layout = GetInterfaceDeclStructLayout(OID); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 4579 | |
| 4580 | RecordDecl::field_iterator i,p; |
| 4581 | const RecordDecl *RD = GetFirstIvarInRecord(OID, i,p); |
Fariborz Jahanian | 1819188 | 2009-03-31 18:11:23 +0000 | [diff] [blame] | 4582 | // collect declared and synthesized ivars in a small vector. |
| 4583 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
| 4584 | for (ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(), |
| 4585 | E = OID->ivar_end(); I != E; ++I) |
| 4586 | OIvars.push_back(*I); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4587 | for (ObjCInterfaceDecl::prop_iterator I = OID->prop_begin(CGM.getContext()), |
| 4588 | E = OID->prop_end(CGM.getContext()); I != E; ++I) |
Fariborz Jahanian | 1819188 | 2009-03-31 18:11:23 +0000 | [diff] [blame] | 4589 | if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl()) |
| 4590 | OIvars.push_back(IV); |
Fariborz Jahanian | 99eee36 | 2009-04-01 19:37:34 +0000 | [diff] [blame] | 4591 | |
Fariborz Jahanian | 1819188 | 2009-03-31 18:11:23 +0000 | [diff] [blame] | 4592 | unsigned iv = 0; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4593 | for (RecordDecl::field_iterator e = RD->field_end(CGM.getContext()); |
| 4594 | i != e; ++i) { |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4595 | FieldDecl *Field = *i; |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 4596 | uint64_t offset = GetIvarBaseOffset(Layout, Field); |
Fariborz Jahanian | 1819188 | 2009-03-31 18:11:23 +0000 | [diff] [blame] | 4597 | Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), OIvars[iv++], offset); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4598 | if (Field->getIdentifier()) |
| 4599 | Ivar[1] = GetMethodVarName(Field->getIdentifier()); |
| 4600 | else |
| 4601 | Ivar[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 4602 | Ivar[2] = GetMethodVarType(Field); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4603 | const llvm::Type *FieldTy = |
| 4604 | CGM.getTypes().ConvertTypeForMem(Field->getType()); |
| 4605 | unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy); |
| 4606 | unsigned Align = CGM.getContext().getPreferredTypeAlign( |
| 4607 | Field->getType().getTypePtr()) >> 3; |
| 4608 | Align = llvm::Log2_32(Align); |
| 4609 | Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align); |
Fariborz Jahanian | 07236ba | 2009-01-27 22:27:56 +0000 | [diff] [blame] | 4610 | // NOTE. Size of a bitfield does not match gcc's, because of the way |
| 4611 | // bitfields are treated special in each. But I am told that 'size' |
| 4612 | // for bitfield ivars is ignored by the runtime so it does not matter. |
| 4613 | // (even if it matters, some day, there is enough info. to get the bitfield |
| 4614 | // right! |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4615 | Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4616 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar)); |
| 4617 | } |
| 4618 | // Return null for empty list. |
| 4619 | if (Ivars.empty()) |
| 4620 | return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
| 4621 | std::vector<llvm::Constant*> Values(3); |
| 4622 | unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy); |
| 4623 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4624 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 4625 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy, |
| 4626 | Ivars.size()); |
| 4627 | Values[2] = llvm::ConstantArray::get(AT, Ivars); |
| 4628 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4629 | const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_"; |
| 4630 | llvm::GlobalVariable *GV = |
| 4631 | new llvm::GlobalVariable(Init->getType(), false, |
| 4632 | llvm::GlobalValue::InternalLinkage, |
| 4633 | Init, |
| 4634 | Prefix + OID->getNameAsString(), |
| 4635 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4636 | GV->setAlignment( |
| 4637 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4638 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4639 | |
| 4640 | UsedGlobals.push_back(GV); |
| 4641 | return llvm::ConstantExpr::getBitCast(GV, |
| 4642 | ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4643 | } |
| 4644 | |
| 4645 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef( |
| 4646 | const ObjCProtocolDecl *PD) { |
| 4647 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4648 | |
| 4649 | if (!Entry) { |
| 4650 | // We use the initializer as a marker of whether this is a forward |
| 4651 | // reference or not. At module finalization we add the empty |
| 4652 | // contents for protocols which were referenced but never defined. |
| 4653 | Entry = |
| 4654 | new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false, |
| 4655 | llvm::GlobalValue::ExternalLinkage, |
| 4656 | 0, |
| 4657 | "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(), |
| 4658 | &CGM.getModule()); |
| 4659 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 4660 | UsedGlobals.push_back(Entry); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4661 | } |
| 4662 | |
| 4663 | return Entry; |
| 4664 | } |
| 4665 | |
| 4666 | /// GetOrEmitProtocol - Generate the protocol meta-data: |
| 4667 | /// @code |
| 4668 | /// struct _protocol_t { |
| 4669 | /// id isa; // NULL |
| 4670 | /// const char * const protocol_name; |
| 4671 | /// const struct _protocol_list_t * protocol_list; // super protocols |
| 4672 | /// const struct method_list_t * const instance_methods; |
| 4673 | /// const struct method_list_t * const class_methods; |
| 4674 | /// const struct method_list_t *optionalInstanceMethods; |
| 4675 | /// const struct method_list_t *optionalClassMethods; |
| 4676 | /// const struct _prop_list_t * properties; |
| 4677 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 4678 | /// const uint32_t flags; // = 0 |
| 4679 | /// } |
| 4680 | /// @endcode |
| 4681 | /// |
| 4682 | |
| 4683 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol( |
| 4684 | const ObjCProtocolDecl *PD) { |
| 4685 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 4686 | |
| 4687 | // Early exit if a defining object has already been generated. |
| 4688 | if (Entry && Entry->hasInitializer()) |
| 4689 | return Entry; |
| 4690 | |
| 4691 | const char *ProtocolName = PD->getNameAsCString(); |
| 4692 | |
| 4693 | // Construct method lists. |
| 4694 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 4695 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4696 | for (ObjCProtocolDecl::instmeth_iterator |
| 4697 | i = PD->instmeth_begin(CGM.getContext()), |
| 4698 | e = PD->instmeth_end(CGM.getContext()); |
| 4699 | i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4700 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4701 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4702 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4703 | OptInstanceMethods.push_back(C); |
| 4704 | } else { |
| 4705 | InstanceMethods.push_back(C); |
| 4706 | } |
| 4707 | } |
| 4708 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4709 | for (ObjCProtocolDecl::classmeth_iterator |
| 4710 | i = PD->classmeth_begin(CGM.getContext()), |
| 4711 | e = PD->classmeth_end(CGM.getContext()); |
| 4712 | i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4713 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4714 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4715 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4716 | OptClassMethods.push_back(C); |
| 4717 | } else { |
| 4718 | ClassMethods.push_back(C); |
| 4719 | } |
| 4720 | } |
| 4721 | |
| 4722 | std::vector<llvm::Constant*> Values(10); |
| 4723 | // isa is NULL |
| 4724 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy); |
| 4725 | Values[1] = GetClassName(PD->getIdentifier()); |
| 4726 | Values[2] = EmitProtocolList( |
| 4727 | "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(), |
| 4728 | PD->protocol_begin(), |
| 4729 | PD->protocol_end()); |
| 4730 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4731 | Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4732 | + PD->getNameAsString(), |
| 4733 | "__DATA, __objc_const", |
| 4734 | InstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4735 | Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4736 | + PD->getNameAsString(), |
| 4737 | "__DATA, __objc_const", |
| 4738 | ClassMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4739 | Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4740 | + PD->getNameAsString(), |
| 4741 | "__DATA, __objc_const", |
| 4742 | OptInstanceMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4743 | Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4744 | + PD->getNameAsString(), |
| 4745 | "__DATA, __objc_const", |
| 4746 | OptClassMethods); |
| 4747 | Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(), |
| 4748 | 0, PD, ObjCTypes); |
| 4749 | uint32_t Size = |
| 4750 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy); |
| 4751 | Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4752 | Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy); |
| 4753 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy, |
| 4754 | Values); |
| 4755 | |
| 4756 | if (Entry) { |
| 4757 | // Already created, fix the linkage and update the initializer. |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4758 | Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4759 | Entry->setInitializer(Init); |
| 4760 | } else { |
| 4761 | Entry = |
| 4762 | new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4763 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4764 | Init, |
| 4765 | std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName, |
| 4766 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4767 | Entry->setAlignment( |
| 4768 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4769 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4770 | } |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4771 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4772 | |
| 4773 | // Use this protocol meta-data to build protocol list table in section |
| 4774 | // __DATA, __objc_protolist |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4775 | llvm::GlobalVariable *PTGV = new llvm::GlobalVariable( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4776 | ObjCTypes.ProtocolnfABIPtrTy, false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4777 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4778 | Entry, |
| 4779 | std::string("\01l_OBJC_LABEL_PROTOCOL_$_") |
| 4780 | +ProtocolName, |
| 4781 | &CGM.getModule()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4782 | PTGV->setAlignment( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4783 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy)); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 4784 | PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip"); |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4785 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 4786 | UsedGlobals.push_back(PTGV); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4787 | return Entry; |
| 4788 | } |
| 4789 | |
| 4790 | /// EmitProtocolList - Generate protocol list meta-data: |
| 4791 | /// @code |
| 4792 | /// struct _protocol_list_t { |
| 4793 | /// long protocol_count; // Note, this is 32/64 bit |
| 4794 | /// struct _protocol_t[protocol_count]; |
| 4795 | /// } |
| 4796 | /// @endcode |
| 4797 | /// |
| 4798 | llvm::Constant * |
| 4799 | CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name, |
| 4800 | ObjCProtocolDecl::protocol_iterator begin, |
| 4801 | ObjCProtocolDecl::protocol_iterator end) { |
| 4802 | std::vector<llvm::Constant*> ProtocolRefs; |
| 4803 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4804 | // Just return null for empty protocol lists |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4805 | if (begin == end) |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4806 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 4807 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4808 | // FIXME: We shouldn't need to do this lookup here, should we? |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4809 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true); |
| 4810 | if (GV) |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4811 | return llvm::ConstantExpr::getBitCast(GV, |
| 4812 | ObjCTypes.ProtocolListnfABIPtrTy); |
| 4813 | |
| 4814 | for (; begin != end; ++begin) |
| 4815 | ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented??? |
| 4816 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4817 | // This list is null terminated. |
| 4818 | ProtocolRefs.push_back(llvm::Constant::getNullValue( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4819 | ObjCTypes.ProtocolnfABIPtrTy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4820 | |
| 4821 | std::vector<llvm::Constant*> Values(2); |
| 4822 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
| 4823 | Values[1] = |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4824 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4825 | ProtocolRefs.size()), |
| 4826 | ProtocolRefs); |
| 4827 | |
| 4828 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4829 | GV = new llvm::GlobalVariable(Init->getType(), false, |
| 4830 | llvm::GlobalValue::InternalLinkage, |
| 4831 | Init, |
| 4832 | Name, |
| 4833 | &CGM.getModule()); |
| 4834 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4835 | GV->setAlignment( |
| 4836 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4837 | UsedGlobals.push_back(GV); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4838 | return llvm::ConstantExpr::getBitCast(GV, |
| 4839 | ObjCTypes.ProtocolListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4840 | } |
| 4841 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4842 | /// GetMethodDescriptionConstant - This routine build following meta-data: |
| 4843 | /// struct _objc_method { |
| 4844 | /// SEL _cmd; |
| 4845 | /// char *method_type; |
| 4846 | /// char *_imp; |
| 4847 | /// } |
| 4848 | |
| 4849 | llvm::Constant * |
| 4850 | CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 4851 | std::vector<llvm::Constant*> Desc(3); |
| 4852 | Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 4853 | ObjCTypes.SelectorPtrTy); |
| 4854 | Desc[1] = GetMethodVarType(MD); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4855 | // Protocol methods have no implementation. So, this entry is always NULL. |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4856 | Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 4857 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc); |
| 4858 | } |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4859 | |
| 4860 | /// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference. |
| 4861 | /// This code gen. amounts to generating code for: |
| 4862 | /// @code |
| 4863 | /// (type *)((char *)base + _OBJC_IVAR_$_.ivar; |
| 4864 | /// @encode |
| 4865 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4866 | LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4867 | CodeGen::CodeGenFunction &CGF, |
| 4868 | QualType ObjectTy, |
| 4869 | llvm::Value *BaseValue, |
| 4870 | const ObjCIvarDecl *Ivar, |
| 4871 | const FieldDecl *Field, |
| 4872 | unsigned CVRQualifiers) { |
| 4873 | assert(ObjectTy->isObjCInterfaceType() && |
| 4874 | "CGObjCNonFragileABIMac::EmitObjCValueForIvar"); |
Fariborz Jahanian | 01a0c36 | 2009-02-12 18:51:23 +0000 | [diff] [blame] | 4875 | ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl(); |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame^] | 4876 | llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4877 | |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4878 | // (char *) BaseValue |
Chris Lattner | 51123fe | 2009-04-17 17:46:19 +0000 | [diff] [blame] | 4879 | llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4880 | llvm::Value *Offset = CGF.Builder.CreateLoad(IvarOffsetGV); |
| 4881 | // (char*)BaseValue + Offset_symbol |
| 4882 | V = CGF.Builder.CreateGEP(V, Offset, "add.ptr"); |
| 4883 | // (type *)((char*)BaseValue + Offset_symbol) |
| 4884 | const llvm::Type *IvarTy = |
Chris Lattner | 51123fe | 2009-04-17 17:46:19 +0000 | [diff] [blame] | 4885 | CGM.getTypes().ConvertTypeForMem(Ivar->getType()); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4886 | llvm::Type *ptrIvarTy = llvm::PointerType::getUnqual(IvarTy); |
| 4887 | V = CGF.Builder.CreateBitCast(V, ptrIvarTy); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4888 | |
Fariborz Jahanian | a6681ae | 2009-03-09 20:44:22 +0000 | [diff] [blame] | 4889 | if (Ivar->isBitField()) { |
Chris Lattner | 51123fe | 2009-04-17 17:46:19 +0000 | [diff] [blame] | 4890 | QualType FieldTy = Field->getType(); |
Fariborz Jahanian | a6681ae | 2009-03-09 20:44:22 +0000 | [diff] [blame] | 4891 | CodeGenTypes::BitFieldInfo bitFieldInfo = |
| 4892 | CGM.getTypes().getBitFieldInfo(Field); |
| 4893 | return LValue::MakeBitfield(V, bitFieldInfo.Begin, bitFieldInfo.Size, |
Chris Lattner | 51123fe | 2009-04-17 17:46:19 +0000 | [diff] [blame] | 4894 | FieldTy->isSignedIntegerType(), |
| 4895 | FieldTy.getCVRQualifiers()|CVRQualifiers); |
Fariborz Jahanian | a6681ae | 2009-03-09 20:44:22 +0000 | [diff] [blame] | 4896 | } |
| 4897 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4898 | LValue LV = LValue::MakeAddr(V, |
Fariborz Jahanian | a223cca | 2009-02-19 23:36:06 +0000 | [diff] [blame] | 4899 | Ivar->getType().getCVRQualifiers()|CVRQualifiers, |
| 4900 | CGM.getContext().getObjCGCAttrKind(Ivar->getType())); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4901 | LValue::SetObjCIvar(LV, true); |
| 4902 | return LV; |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4903 | } |
| 4904 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4905 | llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset( |
| 4906 | CodeGen::CodeGenFunction &CGF, |
| 4907 | ObjCInterfaceDecl *Interface, |
| 4908 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame^] | 4909 | return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar), |
| 4910 | false, "ivar"); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4911 | } |
| 4912 | |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 4913 | CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend( |
| 4914 | CodeGen::CodeGenFunction &CGF, |
| 4915 | QualType ResultType, |
| 4916 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4917 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 4918 | QualType Arg0Ty, |
| 4919 | bool IsSuper, |
| 4920 | const CallArgList &CallArgs) { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4921 | // FIXME. Even though IsSuper is passes. This function doese not |
| 4922 | // handle calls to 'super' receivers. |
| 4923 | CodeGenTypes &Types = CGM.getTypes(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4924 | llvm::Value *Arg0 = Receiver; |
| 4925 | if (!IsSuper) |
| 4926 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4927 | |
| 4928 | // Find the message function name. |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 4929 | // FIXME. This is too much work to get the ABI-specific result type |
| 4930 | // needed to find the message name. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4931 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, |
| 4932 | llvm::SmallVector<QualType, 16>()); |
| 4933 | llvm::Constant *Fn; |
| 4934 | std::string Name("\01l_"); |
| 4935 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4936 | #if 0 |
| 4937 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4938 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
| 4939 | Fn = ObjCTypes.MessageSendIdStretFixupFn; |
| 4940 | // FIXME. Is there a better way of getting these names. |
| 4941 | // They are available in RuntimeFunctions vector pair. |
| 4942 | Name += "objc_msgSendId_stret_fixup"; |
| 4943 | } |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4944 | else |
| 4945 | #endif |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4946 | if (IsSuper) { |
| 4947 | Fn = ObjCTypes.MessageSendSuper2StretFixupFn; |
| 4948 | Name += "objc_msgSendSuper2_stret_fixup"; |
| 4949 | } |
| 4950 | else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4951 | { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4952 | Fn = ObjCTypes.MessageSendStretFixupFn; |
| 4953 | Name += "objc_msgSend_stret_fixup"; |
| 4954 | } |
| 4955 | } |
Fariborz Jahanian | 1a6b368 | 2009-02-05 19:35:43 +0000 | [diff] [blame] | 4956 | else if (ResultType->isFloatingType() && |
| 4957 | // Selection of frret API only happens in 32bit nonfragile ABI. |
| 4958 | CGM.getTargetData().getTypePaddedSize(ObjCTypes.LongTy) == 4) { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4959 | Fn = ObjCTypes.MessageSendFpretFixupFn; |
| 4960 | Name += "objc_msgSend_fpret_fixup"; |
| 4961 | } |
| 4962 | else { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4963 | #if 0 |
| 4964 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4965 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
| 4966 | Fn = ObjCTypes.MessageSendIdFixupFn; |
| 4967 | Name += "objc_msgSendId_fixup"; |
| 4968 | } |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4969 | else |
| 4970 | #endif |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 4971 | if (IsSuper) { |
| 4972 | Fn = ObjCTypes.MessageSendSuper2FixupFn; |
| 4973 | Name += "objc_msgSendSuper2_fixup"; |
| 4974 | } |
| 4975 | else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 4976 | { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4977 | Fn = ObjCTypes.MessageSendFixupFn; |
| 4978 | Name += "objc_msgSend_fixup"; |
| 4979 | } |
| 4980 | } |
| 4981 | Name += '_'; |
| 4982 | std::string SelName(Sel.getAsString()); |
| 4983 | // Replace all ':' in selector name with '_' ouch! |
| 4984 | for(unsigned i = 0; i < SelName.size(); i++) |
| 4985 | if (SelName[i] == ':') |
| 4986 | SelName[i] = '_'; |
| 4987 | Name += SelName; |
| 4988 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 4989 | if (!GV) { |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 4990 | // Build message ref table entry. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4991 | std::vector<llvm::Constant*> Values(2); |
| 4992 | Values[0] = Fn; |
| 4993 | Values[1] = GetMethodVarName(Sel); |
| 4994 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 4995 | GV = new llvm::GlobalVariable(Init->getType(), false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4996 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4997 | Init, |
| 4998 | Name, |
| 4999 | &CGM.getModule()); |
| 5000 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | f59c1a6 | 2009-04-15 19:04:46 +0000 | [diff] [blame] | 5001 | GV->setAlignment(16); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5002 | GV->setSection("__DATA, __objc_msgrefs, coalesced"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5003 | } |
| 5004 | llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5005 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5006 | CallArgList ActualArgs; |
| 5007 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
| 5008 | ActualArgs.push_back(std::make_pair(RValue::get(Arg1), |
| 5009 | ObjCTypes.MessageRefCPtrTy)); |
| 5010 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5011 | const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs); |
| 5012 | llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0); |
| 5013 | Callee = CGF.Builder.CreateLoad(Callee); |
Fariborz Jahanian | 3ab75bd | 2009-02-14 21:25:36 +0000 | [diff] [blame] | 5014 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5015 | Callee = CGF.Builder.CreateBitCast(Callee, |
| 5016 | llvm::PointerType::getUnqual(FTy)); |
| 5017 | return CGF.EmitCall(FnInfo1, Callee, ActualArgs); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5018 | } |
| 5019 | |
| 5020 | /// Generate code for a message send expression in the nonfragile abi. |
| 5021 | CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend( |
| 5022 | CodeGen::CodeGenFunction &CGF, |
| 5023 | QualType ResultType, |
| 5024 | Selector Sel, |
| 5025 | llvm::Value *Receiver, |
| 5026 | bool IsClassMessage, |
| 5027 | const CallArgList &CallArgs) { |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5028 | return EmitMessageSend(CGF, ResultType, Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5029 | Receiver, CGF.getContext().getObjCIdType(), |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5030 | false, CallArgs); |
| 5031 | } |
| 5032 | |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5033 | llvm::GlobalVariable * |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5034 | CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5035 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5036 | |
Daniel Dunbar | dfff230 | 2009-03-02 05:18:14 +0000 | [diff] [blame] | 5037 | if (!GV) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5038 | GV = new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, |
| 5039 | llvm::GlobalValue::ExternalLinkage, |
| 5040 | 0, Name, &CGM.getModule()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5041 | } |
| 5042 | |
| 5043 | return GV; |
| 5044 | } |
| 5045 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5046 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5047 | const ObjCInterfaceDecl *ID) { |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5048 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 5049 | |
| 5050 | if (!Entry) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5051 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5052 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5053 | Entry = |
| 5054 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5055 | llvm::GlobalValue::InternalLinkage, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5056 | ClassGV, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5057 | "\01L_OBJC_CLASSLIST_REFERENCES_$_", |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5058 | &CGM.getModule()); |
| 5059 | Entry->setAlignment( |
| 5060 | CGM.getTargetData().getPrefTypeAlignment( |
| 5061 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5062 | Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip"); |
| 5063 | UsedGlobals.push_back(Entry); |
| 5064 | } |
| 5065 | |
| 5066 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5067 | } |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5068 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5069 | llvm::Value * |
| 5070 | CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder, |
| 5071 | const ObjCInterfaceDecl *ID) { |
| 5072 | llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()]; |
| 5073 | |
| 5074 | if (!Entry) { |
| 5075 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
| 5076 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
| 5077 | Entry = |
| 5078 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5079 | llvm::GlobalValue::InternalLinkage, |
| 5080 | ClassGV, |
| 5081 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_", |
| 5082 | &CGM.getModule()); |
| 5083 | Entry->setAlignment( |
| 5084 | CGM.getTargetData().getPrefTypeAlignment( |
| 5085 | ObjCTypes.ClassnfABIPtrTy)); |
| 5086 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5087 | UsedGlobals.push_back(Entry); |
| 5088 | } |
| 5089 | |
| 5090 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5091 | } |
| 5092 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5093 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 5094 | /// meta-data |
| 5095 | /// |
| 5096 | llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder, |
| 5097 | const ObjCInterfaceDecl *ID) { |
| 5098 | llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()]; |
| 5099 | if (Entry) |
| 5100 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5101 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5102 | std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5103 | llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5104 | Entry = |
| 5105 | new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, |
| 5106 | llvm::GlobalValue::InternalLinkage, |
| 5107 | MetaClassGV, |
| 5108 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_", |
| 5109 | &CGM.getModule()); |
| 5110 | Entry->setAlignment( |
| 5111 | CGM.getTargetData().getPrefTypeAlignment( |
| 5112 | ObjCTypes.ClassnfABIPtrTy)); |
| 5113 | |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5114 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5115 | UsedGlobals.push_back(Entry); |
| 5116 | |
| 5117 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5118 | } |
| 5119 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5120 | /// GetClass - Return a reference to the class for the given interface |
| 5121 | /// decl. |
| 5122 | llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder, |
| 5123 | const ObjCInterfaceDecl *ID) { |
| 5124 | return EmitClassRef(Builder, ID); |
| 5125 | } |
| 5126 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5127 | /// Generates a message send where the super is the receiver. This is |
| 5128 | /// a message send to self with special delivery semantics indicating |
| 5129 | /// which class's method should be called. |
| 5130 | CodeGen::RValue |
| 5131 | CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 5132 | QualType ResultType, |
| 5133 | Selector Sel, |
| 5134 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5135 | bool isCategoryImpl, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5136 | llvm::Value *Receiver, |
| 5137 | bool IsClassMessage, |
| 5138 | const CodeGen::CallArgList &CallArgs) { |
| 5139 | // ... |
| 5140 | // Create and init a super structure; this is a (receiver, class) |
| 5141 | // pair we will pass to objc_msgSendSuper. |
| 5142 | llvm::Value *ObjCSuper = |
| 5143 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 5144 | |
| 5145 | llvm::Value *ReceiverAsObject = |
| 5146 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 5147 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 5148 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
| 5149 | |
| 5150 | // 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] | 5151 | llvm::Value *Target; |
| 5152 | if (IsClassMessage) { |
| 5153 | if (isCategoryImpl) { |
| 5154 | // Message sent to "super' in a class method defined in |
| 5155 | // a category implementation. |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5156 | Target = EmitClassRef(CGF.Builder, Class); |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5157 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 5158 | Target = CGF.Builder.CreateLoad(Target); |
| 5159 | } |
| 5160 | else |
| 5161 | Target = EmitMetaClassRef(CGF.Builder, Class); |
| 5162 | } |
| 5163 | else |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5164 | Target = EmitSuperClassRef(CGF.Builder, Class); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5165 | |
| 5166 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext |
| 5167 | // and ObjCTypes types. |
| 5168 | const llvm::Type *ClassTy = |
| 5169 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
| 5170 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
| 5171 | CGF.Builder.CreateStore(Target, |
| 5172 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
| 5173 | |
| 5174 | return EmitMessageSend(CGF, ResultType, Sel, |
| 5175 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 5176 | true, CallArgs); |
| 5177 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5178 | |
| 5179 | llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder, |
| 5180 | Selector Sel) { |
| 5181 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 5182 | |
| 5183 | if (!Entry) { |
| 5184 | llvm::Constant *Casted = |
| 5185 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 5186 | ObjCTypes.SelectorPtrTy); |
| 5187 | Entry = |
| 5188 | new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false, |
| 5189 | llvm::GlobalValue::InternalLinkage, |
| 5190 | Casted, "\01L_OBJC_SELECTOR_REFERENCES_", |
| 5191 | &CGM.getModule()); |
| 5192 | Entry->setSection("__DATA,__objc_selrefs,literal_pointers,no_dead_strip"); |
| 5193 | UsedGlobals.push_back(Entry); |
| 5194 | } |
| 5195 | |
| 5196 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5197 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5198 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 5199 | /// objc_assign_ivar (id src, id *dst) |
| 5200 | /// |
| 5201 | void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 5202 | llvm::Value *src, llvm::Value *dst) |
| 5203 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5204 | const llvm::Type * SrcTy = src->getType(); |
| 5205 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5206 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5207 | assert(Size <= 8 && "does not support size > 8"); |
| 5208 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5209 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5210 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5211 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5212 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5213 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
| 5214 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn, |
| 5215 | src, dst, "assignivar"); |
| 5216 | return; |
| 5217 | } |
| 5218 | |
| 5219 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 5220 | /// objc_assign_strongCast (id src, id *dst) |
| 5221 | /// |
| 5222 | void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign( |
| 5223 | CodeGen::CodeGenFunction &CGF, |
| 5224 | llvm::Value *src, llvm::Value *dst) |
| 5225 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5226 | const llvm::Type * SrcTy = src->getType(); |
| 5227 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5228 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5229 | assert(Size <= 8 && "does not support size > 8"); |
| 5230 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5231 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5232 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5233 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5234 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5235 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
| 5236 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn, |
| 5237 | src, dst, "weakassign"); |
| 5238 | return; |
| 5239 | } |
| 5240 | |
| 5241 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
| 5242 | /// object: objc_read_weak (id *src) |
| 5243 | /// |
| 5244 | llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead( |
| 5245 | CodeGen::CodeGenFunction &CGF, |
| 5246 | llvm::Value *AddrWeakObj) |
| 5247 | { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5248 | const llvm::Type* DestTy = |
| 5249 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5250 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
| 5251 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn, |
| 5252 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5253 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5254 | return read_weak; |
| 5255 | } |
| 5256 | |
| 5257 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 5258 | /// objc_assign_weak (id src, id *dst) |
| 5259 | /// |
| 5260 | void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 5261 | llvm::Value *src, llvm::Value *dst) |
| 5262 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5263 | const llvm::Type * SrcTy = src->getType(); |
| 5264 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5265 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5266 | assert(Size <= 8 && "does not support size > 8"); |
| 5267 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5268 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5269 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5270 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5271 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5272 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 5273 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5274 | src, dst, "weakassign"); |
| 5275 | return; |
| 5276 | } |
| 5277 | |
| 5278 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 5279 | /// objc_assign_global (id src, id *dst) |
| 5280 | /// |
| 5281 | void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 5282 | llvm::Value *src, llvm::Value *dst) |
| 5283 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5284 | const llvm::Type * SrcTy = src->getType(); |
| 5285 | if (!isa<llvm::PointerType>(SrcTy)) { |
| 5286 | unsigned Size = CGM.getTargetData().getTypePaddedSize(SrcTy); |
| 5287 | assert(Size <= 8 && "does not support size > 8"); |
| 5288 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5289 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5290 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5291 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5292 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5293 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
| 5294 | CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn, |
| 5295 | src, dst, "globalassign"); |
| 5296 | return; |
| 5297 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5298 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5299 | void |
| 5300 | CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 5301 | const Stmt &S) { |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5302 | bool isTry = isa<ObjCAtTryStmt>(S); |
| 5303 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 5304 | llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5305 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5306 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5307 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5308 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
| 5309 | |
| 5310 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 5311 | // evaluation of the expression must occur before we enter the |
| 5312 | // @synchronized. We can safely avoid a temp here because jumps into |
| 5313 | // @synchronized are illegal & this will dominate uses. |
| 5314 | llvm::Value *SyncArg = 0; |
| 5315 | if (!isTry) { |
| 5316 | SyncArg = |
| 5317 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 5318 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5319 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5320 | } |
| 5321 | |
| 5322 | // Push an EH context entry, used for handling rethrows and jumps |
| 5323 | // through finally. |
| 5324 | CGF.PushCleanupBlock(FinallyBlock); |
| 5325 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5326 | CGF.setInvokeDest(TryHandler); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5327 | |
| 5328 | CGF.EmitBlock(TryBlock); |
| 5329 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 5330 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
| 5331 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 5332 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5333 | // Emit the exception handler. |
| 5334 | |
| 5335 | CGF.EmitBlock(TryHandler); |
| 5336 | |
| 5337 | llvm::Value *llvm_eh_exception = |
| 5338 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception); |
| 5339 | llvm::Value *llvm_eh_selector_i64 = |
| 5340 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64); |
| 5341 | llvm::Value *llvm_eh_typeid_for_i64 = |
| 5342 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64); |
| 5343 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5344 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow"); |
| 5345 | |
| 5346 | llvm::SmallVector<llvm::Value*, 8> SelectorArgs; |
| 5347 | SelectorArgs.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5348 | SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5349 | |
| 5350 | // Construct the lists of (type, catch body) to handle. |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5351 | llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5352 | bool HasCatchAll = false; |
| 5353 | if (isTry) { |
| 5354 | if (const ObjCAtCatchStmt* CatchStmt = |
| 5355 | cast<ObjCAtTryStmt>(S).getCatchStmts()) { |
| 5356 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5357 | const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl(); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5358 | Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody())); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5359 | |
| 5360 | // catch(...) always matches. |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5361 | if (!CatchDecl) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5362 | // Use i8* null here to signal this is a catch all, not a cleanup. |
| 5363 | llvm::Value *Null = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 5364 | SelectorArgs.push_back(Null); |
| 5365 | HasCatchAll = true; |
| 5366 | break; |
| 5367 | } |
| 5368 | |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5369 | if (CGF.getContext().isObjCIdType(CatchDecl->getType()) || |
| 5370 | CatchDecl->getType()->isObjCQualifiedIdType()) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5371 | llvm::Value *IDEHType = |
| 5372 | CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id"); |
| 5373 | if (!IDEHType) |
| 5374 | IDEHType = |
| 5375 | new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5376 | llvm::GlobalValue::ExternalLinkage, |
| 5377 | 0, "OBJC_EHTYPE_id", &CGM.getModule()); |
| 5378 | SelectorArgs.push_back(IDEHType); |
| 5379 | HasCatchAll = true; |
| 5380 | break; |
| 5381 | } |
| 5382 | |
| 5383 | // All other types should be Objective-C interface pointer types. |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5384 | const PointerType *PT = CatchDecl->getType()->getAsPointerType(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5385 | assert(PT && "Invalid @catch type."); |
| 5386 | const ObjCInterfaceType *IT = |
| 5387 | PT->getPointeeType()->getAsObjCInterfaceType(); |
| 5388 | assert(IT && "Invalid @catch type."); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5389 | llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5390 | SelectorArgs.push_back(EHType); |
| 5391 | } |
| 5392 | } |
| 5393 | } |
| 5394 | |
| 5395 | // We use a cleanup unless there was already a catch all. |
| 5396 | if (!HasCatchAll) { |
| 5397 | SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0)); |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5398 | Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5399 | } |
| 5400 | |
| 5401 | llvm::Value *Selector = |
| 5402 | CGF.Builder.CreateCall(llvm_eh_selector_i64, |
| 5403 | SelectorArgs.begin(), SelectorArgs.end(), |
| 5404 | "selector"); |
| 5405 | for (unsigned i = 0, e = Handlers.size(); i != e; ++i) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5406 | const ParmVarDecl *CatchParam = Handlers[i].first; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5407 | const Stmt *CatchBody = Handlers[i].second; |
| 5408 | |
| 5409 | llvm::BasicBlock *Next = 0; |
| 5410 | |
| 5411 | // The last handler always matches. |
| 5412 | if (i + 1 != e) { |
| 5413 | assert(CatchParam && "Only last handler can be a catch all."); |
| 5414 | |
| 5415 | llvm::BasicBlock *Match = CGF.createBasicBlock("match"); |
| 5416 | Next = CGF.createBasicBlock("catch.next"); |
| 5417 | llvm::Value *Id = |
| 5418 | CGF.Builder.CreateCall(llvm_eh_typeid_for_i64, |
| 5419 | CGF.Builder.CreateBitCast(SelectorArgs[i+2], |
| 5420 | ObjCTypes.Int8PtrTy)); |
| 5421 | CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id), |
| 5422 | Match, Next); |
| 5423 | |
| 5424 | CGF.EmitBlock(Match); |
| 5425 | } |
| 5426 | |
| 5427 | if (CatchBody) { |
| 5428 | llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end"); |
| 5429 | llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler"); |
| 5430 | |
| 5431 | // Cleanups must call objc_end_catch. |
| 5432 | // |
| 5433 | // FIXME: It seems incorrect for objc_begin_catch to be inside |
| 5434 | // this context, but this matches gcc. |
| 5435 | CGF.PushCleanupBlock(MatchEnd); |
| 5436 | CGF.setInvokeDest(MatchHandler); |
| 5437 | |
| 5438 | llvm::Value *ExcObject = |
| 5439 | CGF.Builder.CreateCall(ObjCTypes.ObjCBeginCatchFn, Exc); |
| 5440 | |
| 5441 | // Bind the catch parameter if it exists. |
| 5442 | if (CatchParam) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5443 | ExcObject = |
| 5444 | CGF.Builder.CreateBitCast(ExcObject, |
| 5445 | CGF.ConvertType(CatchParam->getType())); |
| 5446 | // CatchParam is a ParmVarDecl because of the grammar |
| 5447 | // construction used to handle this, but for codegen purposes |
| 5448 | // we treat this as a local decl. |
| 5449 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
| 5450 | CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5451 | } |
| 5452 | |
| 5453 | CGF.ObjCEHValueStack.push_back(ExcObject); |
| 5454 | CGF.EmitStmt(CatchBody); |
| 5455 | CGF.ObjCEHValueStack.pop_back(); |
| 5456 | |
| 5457 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 5458 | |
| 5459 | CGF.EmitBlock(MatchHandler); |
| 5460 | |
| 5461 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5462 | // We are required to emit this call to satisfy LLVM, even |
| 5463 | // though we don't use the result. |
| 5464 | llvm::SmallVector<llvm::Value*, 8> Args; |
| 5465 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5466 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5467 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 5468 | 0)); |
| 5469 | CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end()); |
| 5470 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5471 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5472 | |
| 5473 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 5474 | |
| 5475 | CGF.EmitBlock(MatchEnd); |
| 5476 | |
| 5477 | // Unfortunately, we also have to generate another EH frame here |
| 5478 | // in case this throws. |
| 5479 | llvm::BasicBlock *MatchEndHandler = |
| 5480 | CGF.createBasicBlock("match.end.handler"); |
| 5481 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
| 5482 | CGF.Builder.CreateInvoke(ObjCTypes.ObjCEndCatchFn, |
| 5483 | Cont, MatchEndHandler, |
| 5484 | Args.begin(), Args.begin()); |
| 5485 | |
| 5486 | CGF.EmitBlock(Cont); |
| 5487 | if (Info.SwitchBlock) |
| 5488 | CGF.EmitBlock(Info.SwitchBlock); |
| 5489 | if (Info.EndBlock) |
| 5490 | CGF.EmitBlock(Info.EndBlock); |
| 5491 | |
| 5492 | CGF.EmitBlock(MatchEndHandler); |
| 5493 | Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5494 | // We are required to emit this call to satisfy LLVM, even |
| 5495 | // though we don't use the result. |
| 5496 | Args.clear(); |
| 5497 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5498 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5499 | Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 5500 | 0)); |
| 5501 | CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end()); |
| 5502 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5503 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5504 | |
| 5505 | if (Next) |
| 5506 | CGF.EmitBlock(Next); |
| 5507 | } else { |
| 5508 | assert(!Next && "catchup should be last handler."); |
| 5509 | |
| 5510 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5511 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5512 | } |
| 5513 | } |
| 5514 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5515 | // Pop the cleanup entry, the @finally is outside this cleanup |
| 5516 | // scope. |
| 5517 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 5518 | CGF.setInvokeDest(PrevLandingPad); |
| 5519 | |
| 5520 | CGF.EmitBlock(FinallyBlock); |
| 5521 | |
| 5522 | if (isTry) { |
| 5523 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 5524 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 5525 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
| 5526 | } else { |
| 5527 | // Emit 'objc_sync_exit(expr)' as finally's sole statement for |
| 5528 | // @synchronized. |
| 5529 | CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, SyncArg); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5530 | } |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5531 | |
| 5532 | if (Info.SwitchBlock) |
| 5533 | CGF.EmitBlock(Info.SwitchBlock); |
| 5534 | if (Info.EndBlock) |
| 5535 | CGF.EmitBlock(Info.EndBlock); |
| 5536 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5537 | // Branch around the rethrow code. |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5538 | CGF.EmitBranch(FinallyEnd); |
| 5539 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5540 | CGF.EmitBlock(FinallyRethrow); |
| 5541 | CGF.Builder.CreateCall(ObjCTypes.UnwindResumeOrRethrowFn, |
| 5542 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5543 | CGF.Builder.CreateUnreachable(); |
| 5544 | |
| 5545 | CGF.EmitBlock(FinallyEnd); |
| 5546 | } |
| 5547 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5548 | /// EmitThrowStmt - Generate code for a throw statement. |
| 5549 | void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 5550 | const ObjCAtThrowStmt &S) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5551 | llvm::Value *Exception; |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5552 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5553 | Exception = CGF.EmitScalarExpr(ThrowExpr); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5554 | } else { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5555 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
| 5556 | "Unexpected rethrow outside @catch block."); |
| 5557 | Exception = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5558 | } |
| 5559 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5560 | llvm::Value *ExceptionAsObject = |
| 5561 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 5562 | llvm::BasicBlock *InvokeDest = CGF.getInvokeDest(); |
| 5563 | if (InvokeDest) { |
| 5564 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
| 5565 | CGF.Builder.CreateInvoke(ObjCTypes.ExceptionThrowFn, |
| 5566 | Cont, InvokeDest, |
| 5567 | &ExceptionAsObject, &ExceptionAsObject + 1); |
| 5568 | CGF.EmitBlock(Cont); |
| 5569 | } else |
| 5570 | CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject); |
| 5571 | CGF.Builder.CreateUnreachable(); |
| 5572 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5573 | // Clear the insertion point to indicate we are in unreachable code. |
| 5574 | CGF.Builder.ClearInsertionPoint(); |
| 5575 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5576 | |
| 5577 | llvm::Value * |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5578 | CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
| 5579 | bool ForDefinition) { |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5580 | llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()]; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5581 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5582 | // If we don't need a definition, return the entry if found or check |
| 5583 | // if we use an external reference. |
| 5584 | if (!ForDefinition) { |
| 5585 | if (Entry) |
| 5586 | return Entry; |
Daniel Dunbar | 7e075cb | 2009-04-07 06:43:45 +0000 | [diff] [blame] | 5587 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5588 | // If this type (or a super class) has the __objc_exception__ |
| 5589 | // attribute, emit an external reference. |
| 5590 | if (hasObjCExceptionAttribute(ID)) |
| 5591 | return Entry = |
| 5592 | new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5593 | llvm::GlobalValue::ExternalLinkage, |
| 5594 | 0, |
| 5595 | (std::string("OBJC_EHTYPE_$_") + |
| 5596 | ID->getIdentifier()->getName()), |
| 5597 | &CGM.getModule()); |
| 5598 | } |
| 5599 | |
| 5600 | // Otherwise we need to either make a new entry or fill in the |
| 5601 | // initializer. |
| 5602 | assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition"); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5603 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5604 | std::string VTableName = "objc_ehtype_vtable"; |
| 5605 | llvm::GlobalVariable *VTableGV = |
| 5606 | CGM.getModule().getGlobalVariable(VTableName); |
| 5607 | if (!VTableGV) |
| 5608 | VTableGV = new llvm::GlobalVariable(ObjCTypes.Int8PtrTy, false, |
| 5609 | llvm::GlobalValue::ExternalLinkage, |
| 5610 | 0, VTableName, &CGM.getModule()); |
| 5611 | |
| 5612 | llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2); |
| 5613 | |
| 5614 | std::vector<llvm::Constant*> Values(3); |
| 5615 | Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1); |
| 5616 | Values[1] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5617 | Values[2] = GetClassGlobal(ClassName); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5618 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values); |
| 5619 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5620 | if (Entry) { |
| 5621 | Entry->setInitializer(Init); |
| 5622 | } else { |
| 5623 | Entry = new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false, |
| 5624 | llvm::GlobalValue::WeakAnyLinkage, |
| 5625 | Init, |
| 5626 | (std::string("OBJC_EHTYPE_$_") + |
| 5627 | ID->getIdentifier()->getName()), |
| 5628 | &CGM.getModule()); |
| 5629 | } |
| 5630 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 5631 | if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden) |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5632 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5633 | Entry->setAlignment(8); |
| 5634 | |
| 5635 | if (ForDefinition) { |
| 5636 | Entry->setSection("__DATA,__objc_const"); |
| 5637 | Entry->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 5638 | } else { |
| 5639 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 5640 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5641 | |
| 5642 | return Entry; |
| 5643 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5644 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 5645 | /* *** */ |
| 5646 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5647 | CodeGen::CGObjCRuntime * |
| 5648 | CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 5649 | return new CGObjCMac(CGM); |
| 5650 | } |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5651 | |
| 5652 | CodeGen::CGObjCRuntime * |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5653 | CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5654 | return new CGObjCNonFragileABIMac(CGM); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5655 | } |