Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1 | //===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This provides Objective-C code generation targetting the Apple runtime. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CGObjCRuntime.h" |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 15 | |
| 16 | #include "CodeGenModule.h" |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 17 | #include "CodeGenFunction.h" |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 19 | #include "clang/AST/Decl.h" |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclObjC.h" |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 21 | #include "clang/Basic/LangOptions.h" |
| 22 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 23 | #include "llvm/Module.h" |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 24 | #include "llvm/Support/IRBuilder.h" |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetData.h" |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 26 | #include <sstream> |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace clang; |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 29 | using namespace CodeGen; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 30 | |
| 31 | namespace { |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 32 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 33 | typedef std::vector<llvm::Constant*> ConstantVector; |
| 34 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 35 | // FIXME: We should find a nicer way to make the labels for |
| 36 | // metadata, string concatenation is lame. |
| 37 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 38 | /// ObjCTypesHelper - Helper class that encapsulates lazy |
| 39 | /// construction of varies types used during ObjC generation. |
| 40 | class ObjCTypesHelper { |
| 41 | private: |
| 42 | CodeGen::CodeGenModule &CGM; |
| 43 | |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 44 | llvm::Function *MessageSendFn, *MessageSendStretFn; |
| 45 | llvm::Function *MessageSendSuperFn, *MessageSendSuperStretFn; |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 46 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 47 | public: |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 48 | const llvm::Type *ShortTy, *IntTy, *LongTy; |
| 49 | const llvm::Type *Int8PtrTy; |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 50 | |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 51 | /// ObjectPtrTy - LLVM type for object handles (typeof(id)) |
| 52 | const llvm::Type *ObjectPtrTy; |
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; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +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; |
| 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; |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 68 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 69 | /// SymtabTy - LLVM type for struct objc_symtab. |
| 70 | const llvm::StructType *SymtabTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 71 | /// SymtabPtrTy - LLVM type for struct objc_symtab *. |
| 72 | const llvm::Type *SymtabPtrTy; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 73 | /// ModuleTy - LLVM type for struct objc_module. |
| 74 | const llvm::StructType *ModuleTy; |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 75 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 76 | /// ProtocolTy - LLVM type for struct objc_protocol. |
| 77 | const llvm::StructType *ProtocolTy; |
| 78 | /// ProtocolPtrTy - LLVM type for struct objc_protocol *. |
| 79 | const llvm::Type *ProtocolPtrTy; |
| 80 | /// ProtocolExtensionTy - LLVM type for struct |
| 81 | /// objc_protocol_extension. |
| 82 | const llvm::StructType *ProtocolExtensionTy; |
| 83 | /// ProtocolExtensionTy - LLVM type for struct |
| 84 | /// objc_protocol_extension *. |
| 85 | const llvm::Type *ProtocolExtensionPtrTy; |
| 86 | /// MethodDescriptionTy - LLVM type for struct |
| 87 | /// objc_method_description. |
| 88 | const llvm::StructType *MethodDescriptionTy; |
| 89 | /// MethodDescriptionListTy - LLVM type for struct |
| 90 | /// objc_method_description_list. |
| 91 | const llvm::StructType *MethodDescriptionListTy; |
| 92 | /// MethodDescriptionListPtrTy - LLVM type for struct |
| 93 | /// objc_method_description_list *. |
| 94 | const llvm::Type *MethodDescriptionListPtrTy; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 95 | /// PropertyTy - LLVM type for struct objc_property (struct _prop_t |
| 96 | /// in GCC parlance). |
| 97 | const llvm::StructType *PropertyTy; |
| 98 | /// PropertyListTy - LLVM type for struct objc_property_list |
| 99 | /// (_prop_list_t in GCC parlance). |
| 100 | const llvm::StructType *PropertyListTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 101 | /// PropertyListPtrTy - LLVM type for struct objc_property_list*. |
| 102 | const llvm::Type *PropertyListPtrTy; |
| 103 | /// ProtocolListTy - LLVM type for struct objc_property_list. |
| 104 | const llvm::Type *ProtocolListTy; |
| 105 | /// ProtocolListPtrTy - LLVM type for struct objc_property_list*. |
| 106 | const llvm::Type *ProtocolListPtrTy; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 107 | /// CategoryTy - LLVM type for struct objc_category. |
| 108 | const llvm::StructType *CategoryTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 109 | /// ClassTy - LLVM type for struct objc_class. |
| 110 | const llvm::StructType *ClassTy; |
| 111 | /// ClassPtrTy - LLVM type for struct objc_class *. |
| 112 | const llvm::Type *ClassPtrTy; |
| 113 | /// ClassExtensionTy - LLVM type for struct objc_class_ext. |
| 114 | const llvm::StructType *ClassExtensionTy; |
| 115 | /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *. |
| 116 | const llvm::Type *ClassExtensionPtrTy; |
| 117 | /// CacheTy - LLVM type for struct objc_cache. |
| 118 | const llvm::Type *CacheTy; |
| 119 | /// CachePtrTy - LLVM type for struct objc_cache *. |
| 120 | const llvm::Type *CachePtrTy; |
| 121 | // IvarTy - LLVM type for struct objc_ivar. |
| 122 | const llvm::StructType *IvarTy; |
| 123 | /// IvarListTy - LLVM type for struct objc_ivar_list. |
| 124 | const llvm::Type *IvarListTy; |
| 125 | /// IvarListPtrTy - LLVM type for struct objc_ivar_list *. |
| 126 | const llvm::Type *IvarListPtrTy; |
| 127 | // MethodTy - LLVM type for struct objc_method. |
| 128 | const llvm::StructType *MethodTy; |
| 129 | /// MethodListTy - LLVM type for struct objc_method_list. |
| 130 | const llvm::Type *MethodListTy; |
| 131 | /// MethodListPtrTy - LLVM type for struct objc_method_list *. |
| 132 | const llvm::Type *MethodListPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 133 | |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 134 | llvm::Function *EnumerationMutationFn; |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 135 | |
| 136 | /// ExceptionDataTy - LLVM type for struct _objc_exception_data. |
| 137 | const llvm::Type *ExceptionDataTy; |
| 138 | |
| 139 | /// ExceptionThrowFn - LLVM objc_exception_throw function. |
| 140 | llvm::Function *ExceptionThrowFn; |
| 141 | |
| 142 | /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function. |
| 143 | llvm::Function *ExceptionTryEnterFn; |
| 144 | |
| 145 | /// ExceptionTryExitFn - LLVM objc_exception_try_exit function. |
| 146 | llvm::Function *ExceptionTryExitFn; |
| 147 | |
| 148 | /// ExceptionExtractFn - LLVM objc_exception_extract function. |
| 149 | llvm::Function *ExceptionExtractFn; |
| 150 | |
| 151 | /// ExceptionMatchFn - LLVM objc_exception_match function. |
| 152 | llvm::Function *ExceptionMatchFn; |
| 153 | |
| 154 | /// SetJmpFn - LLVM _setjmp function. |
| 155 | llvm::Function *SetJmpFn; |
| 156 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 157 | public: |
| 158 | ObjCTypesHelper(CodeGen::CodeGenModule &cgm); |
| 159 | ~ObjCTypesHelper(); |
| 160 | |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 161 | llvm::Constant *getMessageSendFn(bool IsSuper, bool isStret); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 162 | }; |
| 163 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 164 | class CGObjCMac : public CodeGen::CGObjCRuntime { |
| 165 | private: |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 166 | CodeGen::CodeGenModule &CGM; |
| 167 | ObjCTypesHelper ObjCTypes; |
| 168 | /// ObjCABI - FIXME: Not sure yet. |
| 169 | unsigned ObjCABI; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 170 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 171 | /// LazySymbols - Symbols to generate a lazy reference for. See |
| 172 | /// DefinedSymbols and FinishModule(). |
| 173 | std::set<IdentifierInfo*> LazySymbols; |
| 174 | |
| 175 | /// DefinedSymbols - External symbols which are defined by this |
| 176 | /// module. The symbols in this list and LazySymbols are used to add |
| 177 | /// special linker symbols which ensure that Objective-C modules are |
| 178 | /// linked properly. |
| 179 | std::set<IdentifierInfo*> DefinedSymbols; |
| 180 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 181 | /// ClassNames - uniqued class names. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 182 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 183 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 184 | /// MethodVarNames - uniqued method variable names. |
| 185 | llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames; |
| 186 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 187 | /// MethodVarTypes - uniqued method type signatures. We have to use |
| 188 | /// a StringMap here because have no other unique reference. |
| 189 | llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes; |
| 190 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 191 | /// MethodDefinitions - map of methods which have been defined in |
| 192 | /// this translation unit. |
| 193 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions; |
| 194 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 195 | /// PropertyNames - uniqued method variable names. |
| 196 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames; |
| 197 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 198 | /// ClassReferences - uniqued class references. |
| 199 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences; |
| 200 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 201 | /// SelectorReferences - uniqued selector references. |
| 202 | llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences; |
| 203 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 204 | /// Protocols - Protocols for which an objc_protocol structure has |
| 205 | /// been emitted. Forward declarations are handled by creating an |
| 206 | /// empty structure whose initializer is filled in when/if defined. |
| 207 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols; |
| 208 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 209 | /// DefinedClasses - List of defined classes. |
| 210 | std::vector<llvm::GlobalValue*> DefinedClasses; |
| 211 | |
| 212 | /// DefinedCategories - List of defined categories. |
| 213 | std::vector<llvm::GlobalValue*> DefinedCategories; |
| 214 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 215 | /// UsedGlobals - List of globals to pack into the llvm.used metadata |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 216 | /// to prevent them from being clobbered. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 217 | std::vector<llvm::GlobalVariable*> UsedGlobals; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 218 | |
| 219 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 220 | /// level information. |
| 221 | void EmitImageInfo(); |
| 222 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 223 | /// EmitModuleInfo - Another marker encoding module level |
| 224 | /// information. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 225 | void EmitModuleInfo(); |
| 226 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 227 | /// EmitModuleSymols - Emit module symbols, the list of defined |
| 228 | /// classes and categories. The result has type SymtabPtrTy. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 229 | llvm::Constant *EmitModuleSymbols(); |
| 230 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 231 | /// FinishModule - Write out global data structures at the end of |
| 232 | /// processing a translation unit. |
| 233 | void FinishModule(); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 234 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 235 | /// EmitClassExtension - Generate the class extension structure used |
| 236 | /// to store the weak ivar layout and properties. The return value |
| 237 | /// has type ClassExtensionPtrTy. |
| 238 | llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID); |
| 239 | |
| 240 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 241 | /// for the given class. |
| 242 | llvm::Value *EmitClassRef(llvm::IRBuilder<> &Builder, |
| 243 | const ObjCInterfaceDecl *ID); |
| 244 | |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 245 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 246 | QualType ResultType, |
| 247 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 248 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 249 | QualType Arg0Ty, |
| 250 | bool IsSuper, |
| 251 | const CallArgList &CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 252 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 253 | /// EmitIvarList - Emit the ivar list for the given |
| 254 | /// implementation. If ForClass is true the list of class ivars |
| 255 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 256 | /// interface ivars will be emitted. The return value has type |
| 257 | /// IvarListPtrTy. |
| 258 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID, |
| 259 | bool ForClass, |
| 260 | const llvm::Type *InterfaceTy); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 261 | |
| 262 | /// EmitMetaClass - Emit a forward reference to the class structure |
| 263 | /// for the metaclass of the given interface. The return value has |
| 264 | /// type ClassPtrTy. |
| 265 | llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID); |
| 266 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 267 | /// EmitMetaClass - Emit a class structure for the metaclass of the |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 268 | /// given implementation. The return value has type ClassPtrTy. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 269 | llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID, |
| 270 | llvm::Constant *Protocols, |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 271 | const llvm::Type *InterfaceTy, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 272 | const ConstantVector &Methods); |
| 273 | |
| 274 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
| 275 | |
| 276 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 277 | |
| 278 | /// EmitMethodList - Emit the method list for the given |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 279 | /// implementation. The return value has type MethodListPtrTy. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 280 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 281 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 282 | const ConstantVector &Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 283 | |
| 284 | /// EmitMethodDescList - Emit a method description list for a list of |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 285 | /// method declarations. |
| 286 | /// - TypeName: The name for the type containing the methods. |
| 287 | /// - IsProtocol: True iff these methods are for a protocol. |
| 288 | /// - ClassMethds: True iff these are class methods. |
| 289 | /// - Required: When true, only "required" methods are |
| 290 | /// listed. Similarly, when false only "optional" methods are |
| 291 | /// listed. For classes this should always be true. |
| 292 | /// - begin, end: The method list to output. |
| 293 | /// |
| 294 | /// The return value has type MethodDescriptionListPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 295 | llvm::Constant *EmitMethodDescList(const std::string &Name, |
| 296 | const char *Section, |
| 297 | const ConstantVector &Methods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 298 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 299 | /// EmitPropertyList - Emit the given property list. The return |
| 300 | /// value has type PropertyListPtrTy. |
| 301 | llvm::Constant *EmitPropertyList(const std::string &Name, |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 302 | const Decl *Container, |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 303 | ObjCPropertyDecl * const *begin, |
| 304 | ObjCPropertyDecl * const *end); |
| 305 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 306 | /// EmitProtocolExtension - Generate the protocol extension |
| 307 | /// structure used to store optional instance and class methods, and |
| 308 | /// protocol properties. The return value has type |
| 309 | /// ProtocolExtensionPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 310 | llvm::Constant * |
| 311 | EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 312 | const ConstantVector &OptInstanceMethods, |
| 313 | const ConstantVector &OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 314 | |
| 315 | /// EmitProtocolList - Generate the list of referenced |
| 316 | /// protocols. The return value has type ProtocolListPtrTy. |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 317 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 318 | ObjCProtocolDecl::protocol_iterator begin, |
| 319 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 320 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 321 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 322 | /// for the given selector. |
| 323 | llvm::Value *EmitSelector(llvm::IRBuilder<> &Builder, Selector Sel); |
| 324 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 325 | /// GetProtocolRef - Return a reference to the internal protocol |
| 326 | /// description, creating an empty one if it has not been |
| 327 | /// defined. The return value has type pointer-to ProtocolTy. |
| 328 | llvm::GlobalVariable *GetProtocolRef(const ObjCProtocolDecl *PD); |
| 329 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 330 | /// GetClassName - Return a unique constant for the given selector's |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 331 | /// name. The return value has type char *. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 332 | llvm::Constant *GetClassName(IdentifierInfo *Ident); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 333 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 334 | /// GetMethodVarName - Return a unique constant for the given |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 335 | /// selector's name. The return value has type char *. |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 336 | llvm::Constant *GetMethodVarName(Selector Sel); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 337 | llvm::Constant *GetMethodVarName(IdentifierInfo *Ident); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 338 | llvm::Constant *GetMethodVarName(const std::string &Name); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 339 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 340 | /// GetMethodVarType - Return a unique constant for the given |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 341 | /// selector's name. The return value has type char *. |
| 342 | |
| 343 | // FIXME: This is a horrible name. |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 344 | llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 345 | llvm::Constant *GetMethodVarType(const std::string &Name); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 346 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 347 | /// GetPropertyName - Return a unique constant for the given |
| 348 | /// name. The return value has type char *. |
| 349 | llvm::Constant *GetPropertyName(IdentifierInfo *Ident); |
| 350 | |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 351 | // FIXME: This can be dropped once string functions are unified. |
| 352 | llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 353 | const Decl *Container); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 354 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 355 | /// GetNameForMethod - Return a name for the given method. |
| 356 | /// \param[out] NameOut - The return value. |
| 357 | void GetNameForMethod(const ObjCMethodDecl *OMD, |
| 358 | std::string &NameOut); |
| 359 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 360 | public: |
| 361 | CGObjCMac(CodeGen::CodeGenModule &cgm); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 362 | virtual llvm::Constant *GenerateConstantString(const std::string &String); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 363 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 364 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 365 | QualType ResultType, |
| 366 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 367 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 368 | bool IsClassMessage, |
| 369 | const CallArgList &CallArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 370 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 371 | virtual CodeGen::RValue |
| 372 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 373 | QualType ResultType, |
| 374 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 375 | const ObjCInterfaceDecl *Class, |
| 376 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 377 | bool IsClassMessage, |
| 378 | const CallArgList &CallArgs); |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 379 | |
| 380 | virtual llvm::Value *GetClass(llvm::IRBuilder<> &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 381 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 382 | |
| 383 | virtual llvm::Value *GetSelector(llvm::IRBuilder<> &Builder, Selector Sel); |
| 384 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 385 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 386 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 387 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 388 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 389 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 390 | |
| 391 | virtual llvm::Value *GenerateProtocolRef(llvm::IRBuilder<> &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 392 | const ObjCProtocolDecl *PD); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 393 | |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 394 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 395 | |
| 396 | virtual llvm::Function *ModuleInitFunction(); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 397 | virtual llvm::Function *EnumerationMutationFunction(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 398 | |
| 399 | virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
| 400 | const ObjCAtTryStmt &S); |
| 401 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 402 | const ObjCAtThrowStmt &S); |
| 403 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 404 | }; |
| 405 | } // end anonymous namespace |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 406 | |
| 407 | /* *** Helper Functions *** */ |
| 408 | |
| 409 | /// getConstantGEP() - Help routine to construct simple GEPs. |
| 410 | static llvm::Constant *getConstantGEP(llvm::Constant *C, |
| 411 | unsigned idx0, |
| 412 | unsigned idx1) { |
| 413 | llvm::Value *Idxs[] = { |
| 414 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0), |
| 415 | llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1) |
| 416 | }; |
| 417 | return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2); |
| 418 | } |
| 419 | |
| 420 | /* *** CGObjCMac Public Interface *** */ |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 421 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 422 | CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) |
| 423 | : CGM(cgm), |
| 424 | ObjCTypes(cgm), |
| 425 | ObjCABI(1) |
| 426 | { |
| 427 | // FIXME: How does this get set in GCC? And what does it even mean? |
| 428 | if (ObjCTypes.LongTy != CGM.getTypes().ConvertType(CGM.getContext().IntTy)) |
| 429 | ObjCABI = 2; |
| 430 | |
| 431 | EmitImageInfo(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 434 | /// GetClass - Return a reference to the class for the given interface |
| 435 | /// decl. |
| 436 | llvm::Value *CGObjCMac::GetClass(llvm::IRBuilder<> &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 437 | const ObjCInterfaceDecl *ID) { |
| 438 | return EmitClassRef(Builder, ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | /// GetSelector - Return the pointer to the unique'd string for this selector. |
| 442 | llvm::Value *CGObjCMac::GetSelector(llvm::IRBuilder<> &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 443 | return EmitSelector(Builder, Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 446 | /// Generate a constant CFString object. |
| 447 | /* |
| 448 | struct __builtin_CFString { |
| 449 | const int *isa; // point to __CFConstantStringClassReference |
| 450 | int flags; |
| 451 | const char *str; |
| 452 | long length; |
| 453 | }; |
| 454 | */ |
| 455 | |
| 456 | llvm::Constant *CGObjCMac::GenerateConstantString(const std::string &String) { |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 457 | return CGM.GetAddrOfConstantCFString(String); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | /// Generates a message send where the super is the receiver. This is |
| 461 | /// a message send to self with special delivery semantics indicating |
| 462 | /// which class's method should be called. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 463 | CodeGen::RValue |
| 464 | CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 465 | QualType ResultType, |
| 466 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 467 | const ObjCInterfaceDecl *Class, |
| 468 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 469 | bool IsClassMessage, |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 470 | const CodeGen::CallArgList &CallArgs) { |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 471 | // Create and init a super structure; this is a (receiver, class) |
| 472 | // pair we will pass to objc_msgSendSuper. |
| 473 | llvm::Value *ObjCSuper = |
| 474 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
| 475 | llvm::Value *ReceiverAsObject = |
| 476 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 477 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 478 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 479 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 480 | // If this is a class message the metaclass is passed as the target. |
| 481 | llvm::Value *Target; |
| 482 | if (IsClassMessage) { |
| 483 | llvm::Value *MetaClassPtr = EmitMetaClassRef(Class); |
| 484 | llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1); |
| 485 | llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr); |
| 486 | Target = Super; |
| 487 | } else { |
| 488 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 489 | } |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 490 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext |
| 491 | // and ObjCTypes types. |
| 492 | const llvm::Type *ClassTy = |
| 493 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
| 494 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 495 | CGF.Builder.CreateStore(Target, |
| 496 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
| 497 | |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 498 | return EmitMessageSend(CGF, ResultType, Sel, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 499 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 500 | true, CallArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 501 | } |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 502 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 503 | /// Generate code for a message send expression. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 504 | CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 505 | QualType ResultType, |
| 506 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 507 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 508 | bool IsClassMessage, |
| 509 | const CallArgList &CallArgs) { |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 510 | llvm::Value *Arg0 = |
| 511 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp"); |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 512 | return EmitMessageSend(CGF, ResultType, Sel, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 513 | Arg0, CGF.getContext().getObjCIdType(), |
| 514 | false, CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 518 | QualType ResultType, |
| 519 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 520 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 521 | QualType Arg0Ty, |
| 522 | bool IsSuper, |
| 523 | const CallArgList &CallArgs) { |
| 524 | CallArgList ActualArgs; |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 525 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
| 526 | ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder, |
| 527 | Sel)), |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 528 | CGF.getContext().getObjCSelType())); |
| 529 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 530 | |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 531 | const llvm::FunctionType *FTy = |
| 532 | CGM.getTypes().GetFunctionType(CGCallInfo(ResultType, ActualArgs), |
| 533 | false); |
| 534 | llvm::Constant *Fn = |
| 535 | ObjCTypes.getMessageSendFn(IsSuper, CGM.ReturnTypeUsesSret(ResultType)); |
| 536 | Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy)); |
Daniel Dunbar | 3913f18 | 2008-09-09 23:48:28 +0000 | [diff] [blame] | 537 | return CGF.EmitCall(Fn, ResultType, ActualArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | llvm::Value *CGObjCMac::GenerateProtocolRef(llvm::IRBuilder<> &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 541 | const ObjCProtocolDecl *PD) { |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 542 | // FIXME: I don't understand why gcc generates this, or where it is |
| 543 | // resolved. Investigate. Its also wasteful to look this up over and |
| 544 | // over. |
| 545 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 546 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 547 | return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), |
| 548 | ObjCTypes.ExternalProtocolPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 551 | /* |
| 552 | // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions |
| 553 | struct _objc_protocol { |
| 554 | struct _objc_protocol_extension *isa; |
| 555 | char *protocol_name; |
| 556 | struct _objc_protocol_list *protocol_list; |
| 557 | struct _objc__method_prototype_list *instance_methods; |
| 558 | struct _objc__method_prototype_list *class_methods |
| 559 | }; |
| 560 | |
| 561 | See EmitProtocolExtension(). |
| 562 | */ |
| 563 | void CGObjCMac::GenerateProtocol(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 564 | // FIXME: I don't understand why gcc generates this, or where it is |
| 565 | // resolved. Investigate. Its also wasteful to look this up over and |
| 566 | // over. |
| 567 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 568 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 569 | const char *ProtocolName = PD->getName(); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 570 | |
| 571 | // Construct method lists. |
| 572 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 573 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
| 574 | for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(), |
| 575 | e = PD->instmeth_end(); i != e; ++i) { |
| 576 | ObjCMethodDecl *MD = *i; |
| 577 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 578 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 579 | OptInstanceMethods.push_back(C); |
| 580 | } else { |
| 581 | InstanceMethods.push_back(C); |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(), |
| 586 | e = PD->classmeth_end(); i != e; ++i) { |
| 587 | ObjCMethodDecl *MD = *i; |
| 588 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 589 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 590 | OptClassMethods.push_back(C); |
| 591 | } else { |
| 592 | ClassMethods.push_back(C); |
| 593 | } |
| 594 | } |
| 595 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 596 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 597 | Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 598 | Values[1] = GetClassName(PD->getIdentifier()); |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 599 | Values[2] = |
| 600 | EmitProtocolList(std::string("\01L_OBJC_PROTOCOL_REFS_")+PD->getName(), |
| 601 | PD->protocol_begin(), |
| 602 | PD->protocol_end()); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 603 | Values[3] = |
| 604 | EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_") |
| 605 | + PD->getName(), |
| 606 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 607 | InstanceMethods); |
| 608 | Values[4] = |
| 609 | EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_CLASS_METHODS_") |
| 610 | + PD->getName(), |
| 611 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 612 | ClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 613 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 614 | Values); |
| 615 | |
| 616 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 617 | if (Entry) { |
| 618 | // Already created, just update the initializer |
| 619 | Entry->setInitializer(Init); |
| 620 | } else { |
| 621 | Entry = |
| 622 | new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, |
| 623 | llvm::GlobalValue::InternalLinkage, |
| 624 | Init, |
| 625 | std::string("\01L_OBJC_PROTOCOL_")+ProtocolName, |
| 626 | &CGM.getModule()); |
| 627 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
| 628 | UsedGlobals.push_back(Entry); |
| 629 | // FIXME: Is this necessary? Why only for protocol? |
| 630 | Entry->setAlignment(4); |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | llvm::GlobalVariable *CGObjCMac::GetProtocolRef(const ObjCProtocolDecl *PD) { |
| 635 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 636 | |
| 637 | if (!Entry) { |
| 638 | std::vector<llvm::Constant*> Values(5); |
| 639 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
| 640 | Values[1] = GetClassName(PD->getIdentifier()); |
| 641 | Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 642 | Values[3] = Values[4] = |
| 643 | llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
| 644 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
| 645 | Values); |
| 646 | |
| 647 | Entry = |
| 648 | new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, |
| 649 | llvm::GlobalValue::InternalLinkage, |
| 650 | Init, |
| 651 | std::string("\01L_OBJC_PROTOCOL_")+PD->getName(), |
| 652 | &CGM.getModule()); |
| 653 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
| 654 | UsedGlobals.push_back(Entry); |
| 655 | // FIXME: Is this necessary? Why only for protocol? |
| 656 | Entry->setAlignment(4); |
| 657 | } |
| 658 | |
| 659 | return Entry; |
| 660 | } |
| 661 | |
| 662 | /* |
| 663 | struct _objc_protocol_extension { |
| 664 | uint32_t size; |
| 665 | struct objc_method_description_list *optional_instance_methods; |
| 666 | struct objc_method_description_list *optional_class_methods; |
| 667 | struct objc_property_list *instance_properties; |
| 668 | }; |
| 669 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 670 | llvm::Constant * |
| 671 | CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 672 | const ConstantVector &OptInstanceMethods, |
| 673 | const ConstantVector &OptClassMethods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 674 | uint64_t Size = |
| 675 | CGM.getTargetData().getABITypeSize(ObjCTypes.ProtocolExtensionTy); |
| 676 | std::vector<llvm::Constant*> Values(4); |
| 677 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 678 | Values[1] = |
| 679 | EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_") |
| 680 | + PD->getName(), |
| 681 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 682 | OptInstanceMethods); |
| 683 | Values[2] = |
| 684 | EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_") |
| 685 | + PD->getName(), |
| 686 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 687 | OptClassMethods); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 688 | Values[3] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_PROTO_LIST_") + |
| 689 | PD->getName(), |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 690 | 0, |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 691 | PD->classprop_begin(), |
| 692 | PD->classprop_end()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 693 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 694 | // Return null if no extension bits are used. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 695 | if (Values[1]->isNullValue() && Values[2]->isNullValue() && |
| 696 | Values[3]->isNullValue()) |
| 697 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
| 698 | |
| 699 | llvm::Constant *Init = |
| 700 | llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values); |
| 701 | llvm::GlobalVariable *GV = |
| 702 | new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false, |
| 703 | llvm::GlobalValue::InternalLinkage, |
| 704 | Init, |
| 705 | (std::string("\01L_OBJC_PROTOCOLEXT_") + |
| 706 | PD->getName()), |
| 707 | &CGM.getModule()); |
| 708 | // No special section, but goes in llvm.used |
| 709 | UsedGlobals.push_back(GV); |
| 710 | |
| 711 | return GV; |
| 712 | } |
| 713 | |
| 714 | /* |
| 715 | struct objc_protocol_list { |
| 716 | struct objc_protocol_list *next; |
| 717 | long count; |
| 718 | Protocol *list[]; |
| 719 | }; |
| 720 | */ |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 721 | llvm::Constant * |
| 722 | CGObjCMac::EmitProtocolList(const std::string &Name, |
| 723 | ObjCProtocolDecl::protocol_iterator begin, |
| 724 | ObjCProtocolDecl::protocol_iterator end) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 725 | std::vector<llvm::Constant*> ProtocolRefs; |
| 726 | |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 727 | for (; begin != end; ++begin) |
| 728 | ProtocolRefs.push_back(GetProtocolRef(*begin)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 729 | |
| 730 | // Just return null for empty protocol lists |
| 731 | if (ProtocolRefs.empty()) |
| 732 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 733 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 734 | // This list is null terminated. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 735 | ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy)); |
| 736 | |
| 737 | std::vector<llvm::Constant*> Values(3); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 738 | // This field is only used by the runtime. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 739 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 740 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
| 741 | Values[2] = |
| 742 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy, |
| 743 | ProtocolRefs.size()), |
| 744 | ProtocolRefs); |
| 745 | |
| 746 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 747 | llvm::GlobalVariable *GV = |
| 748 | new llvm::GlobalVariable(Init->getType(), false, |
| 749 | llvm::GlobalValue::InternalLinkage, |
| 750 | Init, |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 751 | Name, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 752 | &CGM.getModule()); |
| 753 | GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip"); |
| 754 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy); |
| 755 | } |
| 756 | |
| 757 | /* |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 758 | struct _objc_property { |
| 759 | const char * const name; |
| 760 | const char * const attributes; |
| 761 | }; |
| 762 | |
| 763 | struct _objc_property_list { |
| 764 | uint32_t entsize; // sizeof (struct _objc_property) |
| 765 | uint32_t prop_count; |
| 766 | struct _objc_property[prop_count]; |
| 767 | }; |
| 768 | */ |
| 769 | llvm::Constant *CGObjCMac::EmitPropertyList(const std::string &Name, |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 770 | const Decl *Container, |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 771 | ObjCPropertyDecl * const *begin, |
| 772 | ObjCPropertyDecl * const *end) { |
| 773 | std::vector<llvm::Constant*> Properties, Prop(2); |
| 774 | for (; begin != end; ++begin) { |
| 775 | const ObjCPropertyDecl *PD = *begin; |
| 776 | Prop[0] = GetPropertyName(PD->getIdentifier()); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 777 | Prop[1] = GetPropertyTypeString(PD, Container); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 778 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, |
| 779 | Prop)); |
| 780 | } |
| 781 | |
| 782 | // Return null for empty list. |
| 783 | if (Properties.empty()) |
| 784 | return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 785 | |
| 786 | unsigned PropertySize = |
| 787 | CGM.getTargetData().getABITypeSize(ObjCTypes.PropertyTy); |
| 788 | std::vector<llvm::Constant*> Values(3); |
| 789 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize); |
| 790 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size()); |
| 791 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy, |
| 792 | Properties.size()); |
| 793 | Values[2] = llvm::ConstantArray::get(AT, Properties); |
| 794 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 795 | |
| 796 | llvm::GlobalVariable *GV = |
| 797 | new llvm::GlobalVariable(Init->getType(), false, |
| 798 | llvm::GlobalValue::InternalLinkage, |
| 799 | Init, |
| 800 | Name, |
| 801 | &CGM.getModule()); |
| 802 | // No special section on property lists? |
| 803 | UsedGlobals.push_back(GV); |
| 804 | return llvm::ConstantExpr::getBitCast(GV, |
| 805 | ObjCTypes.PropertyListPtrTy); |
| 806 | |
| 807 | } |
| 808 | |
| 809 | /* |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 810 | struct objc_method_description_list { |
| 811 | int count; |
| 812 | struct objc_method_description list[]; |
| 813 | }; |
| 814 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 815 | llvm::Constant * |
| 816 | CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 817 | std::vector<llvm::Constant*> Desc(2); |
| 818 | Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 819 | ObjCTypes.SelectorPtrTy); |
| 820 | Desc[1] = GetMethodVarType(MD); |
| 821 | return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy, |
| 822 | Desc); |
| 823 | } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 824 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 825 | llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name, |
| 826 | const char *Section, |
| 827 | const ConstantVector &Methods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 828 | // Return null for empty list. |
| 829 | if (Methods.empty()) |
| 830 | return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
| 831 | |
| 832 | std::vector<llvm::Constant*> Values(2); |
| 833 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 834 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy, |
| 835 | Methods.size()); |
| 836 | Values[1] = llvm::ConstantArray::get(AT, Methods); |
| 837 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 838 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 839 | llvm::GlobalVariable *GV = |
| 840 | new llvm::GlobalVariable(Init->getType(), false, |
| 841 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 842 | Init, Name, &CGM.getModule()); |
| 843 | GV->setSection(Section); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 844 | UsedGlobals.push_back(GV); |
| 845 | return llvm::ConstantExpr::getBitCast(GV, |
| 846 | ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 849 | /* |
| 850 | struct _objc_category { |
| 851 | char *category_name; |
| 852 | char *class_name; |
| 853 | struct _objc_method_list *instance_methods; |
| 854 | struct _objc_method_list *class_methods; |
| 855 | struct _objc_protocol_list *protocols; |
| 856 | uint32_t size; // <rdar://4585769> |
| 857 | struct _objc_property_list *instance_properties; |
| 858 | }; |
| 859 | */ |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 860 | void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 861 | unsigned Size = CGM.getTargetData().getABITypeSize(ObjCTypes.CategoryTy); |
| 862 | |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 863 | // FIXME: This is poor design, the OCD should have a pointer to the |
| 864 | // category decl. Additionally, note that Category can be null for |
| 865 | // the @implementation w/o an @interface case. Sema should just |
| 866 | // create one for us as it does for @implementation so everyone else |
| 867 | // can live life under a clear blue sky. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 868 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 869 | const ObjCCategoryDecl *Category = |
| 870 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 871 | std::string ExtName(std::string(Interface->getName()) + |
| 872 | "_" + |
| 873 | OCD->getName()); |
| 874 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 875 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 876 | for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(), |
| 877 | e = OCD->instmeth_end(); i != e; ++i) { |
| 878 | // Instance methods should always be defined. |
| 879 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 880 | } |
| 881 | for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(), |
| 882 | e = OCD->classmeth_end(); i != e; ++i) { |
| 883 | // Class methods should always be defined. |
| 884 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 885 | } |
| 886 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 887 | std::vector<llvm::Constant*> Values(7); |
| 888 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 889 | Values[1] = GetClassName(Interface->getIdentifier()); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 890 | Values[2] = |
| 891 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") + |
| 892 | ExtName, |
| 893 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 894 | InstanceMethods); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 895 | Values[3] = |
| 896 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName, |
| 897 | "__OBJC,__cat_class_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 898 | ClassMethods); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 899 | if (Category) { |
| 900 | Values[4] = |
| 901 | EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName, |
| 902 | Category->protocol_begin(), |
| 903 | Category->protocol_end()); |
| 904 | } else { |
| 905 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
| 906 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 907 | Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 908 | |
| 909 | // If there is no category @interface then there can be no properties. |
| 910 | if (Category) { |
| 911 | Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName, |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 912 | OCD, |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 913 | Category->classprop_begin(), |
| 914 | Category->classprop_end()); |
| 915 | } else { |
| 916 | Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
| 917 | } |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 918 | |
| 919 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy, |
| 920 | Values); |
| 921 | |
| 922 | llvm::GlobalVariable *GV = |
| 923 | new llvm::GlobalVariable(ObjCTypes.CategoryTy, false, |
| 924 | llvm::GlobalValue::InternalLinkage, |
| 925 | Init, |
| 926 | std::string("\01L_OBJC_CATEGORY_")+ExtName, |
| 927 | &CGM.getModule()); |
| 928 | GV->setSection("__OBJC,__category,regular,no_dead_strip"); |
| 929 | UsedGlobals.push_back(GV); |
| 930 | DefinedCategories.push_back(GV); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 931 | } |
| 932 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 933 | // FIXME: Get from somewhere? |
| 934 | enum ClassFlags { |
| 935 | eClassFlags_Factory = 0x00001, |
| 936 | eClassFlags_Meta = 0x00002, |
| 937 | // <rdr://5142207> |
| 938 | eClassFlags_HasCXXStructors = 0x02000, |
| 939 | eClassFlags_Hidden = 0x20000, |
| 940 | eClassFlags_ABI2_Hidden = 0x00010, |
| 941 | eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634> |
| 942 | }; |
| 943 | |
| 944 | // <rdr://5142207&4705298&4843145> |
| 945 | static bool IsClassHidden(const ObjCInterfaceDecl *ID) { |
| 946 | if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) { |
| 947 | // FIXME: Support -fvisibility |
| 948 | switch (attr->getVisibility()) { |
| 949 | default: |
| 950 | assert(0 && "Unknown visibility"); |
| 951 | return false; |
| 952 | case VisibilityAttr::DefaultVisibility: |
| 953 | case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here? |
| 954 | return false; |
| 955 | case VisibilityAttr::HiddenVisibility: |
| 956 | return true; |
| 957 | } |
| 958 | } else { |
| 959 | return false; // FIXME: Support -fvisibility |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | /* |
| 964 | struct _objc_class { |
| 965 | Class isa; |
| 966 | Class super_class; |
| 967 | const char *name; |
| 968 | long version; |
| 969 | long info; |
| 970 | long instance_size; |
| 971 | struct _objc_ivar_list *ivars; |
| 972 | struct _objc_method_list *methods; |
| 973 | struct _objc_cache *cache; |
| 974 | struct _objc_protocol_list *protocols; |
| 975 | // Objective-C 1.0 extensions (<rdr://4585769>) |
| 976 | const char *ivar_layout; |
| 977 | struct _objc_class_ext *ext; |
| 978 | }; |
| 979 | |
| 980 | See EmitClassExtension(); |
| 981 | */ |
| 982 | void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 983 | DefinedSymbols.insert(ID->getIdentifier()); |
| 984 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 985 | const char *ClassName = ID->getName(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 986 | // FIXME: Gross |
| 987 | ObjCInterfaceDecl *Interface = |
| 988 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 989 | llvm::Constant *Protocols = |
| 990 | EmitProtocolList(std::string("\01L_OBJC_CLASS_PROTOCOLS_") + ID->getName(), |
| 991 | Interface->protocol_begin(), |
| 992 | Interface->protocol_end()); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 993 | const llvm::Type *InterfaceTy = |
| 994 | CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface)); |
| 995 | unsigned Flags = eClassFlags_Factory; |
| 996 | unsigned Size = CGM.getTargetData().getABITypeSize(InterfaceTy); |
| 997 | |
| 998 | // FIXME: Set CXX-structors flag. |
| 999 | if (IsClassHidden(ID->getClassInterface())) |
| 1000 | Flags |= eClassFlags_Hidden; |
| 1001 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1002 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1003 | for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(), |
| 1004 | e = ID->instmeth_end(); i != e; ++i) { |
| 1005 | // Instance methods should always be defined. |
| 1006 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1007 | } |
| 1008 | for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(), |
| 1009 | e = ID->classmeth_end(); i != e; ++i) { |
| 1010 | // Class methods should always be defined. |
| 1011 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1012 | } |
| 1013 | |
| 1014 | for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(), |
| 1015 | e = ID->propimpl_end(); i != e; ++i) { |
| 1016 | ObjCPropertyImplDecl *PID = *i; |
| 1017 | |
| 1018 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 1019 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 1020 | |
| 1021 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 1022 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 1023 | InstanceMethods.push_back(C); |
| 1024 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 1025 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 1026 | InstanceMethods.push_back(C); |
| 1027 | } |
| 1028 | } |
| 1029 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1030 | std::vector<llvm::Constant*> Values(12); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1031 | Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1032 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1033 | // Record a reference to the super class. |
| 1034 | LazySymbols.insert(Super->getIdentifier()); |
| 1035 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1036 | Values[ 1] = |
| 1037 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 1038 | ObjCTypes.ClassPtrTy); |
| 1039 | } else { |
| 1040 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
| 1041 | } |
| 1042 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 1043 | // Version is always 0. |
| 1044 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 1045 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 1046 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
| 1047 | Values[ 6] = EmitIvarList(ID, false, InterfaceTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1048 | Values[ 7] = |
| 1049 | EmitMethodList(std::string("\01L_OBJC_INSTANCE_METHODS_") + ID->getName(), |
| 1050 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1051 | InstanceMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1052 | // cache is always NULL. |
| 1053 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
| 1054 | Values[ 9] = Protocols; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1055 | // FIXME: Set ivar_layout |
| 1056 | Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1057 | Values[11] = EmitClassExtension(ID); |
| 1058 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 1059 | Values); |
| 1060 | |
| 1061 | llvm::GlobalVariable *GV = |
| 1062 | new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 1063 | llvm::GlobalValue::InternalLinkage, |
| 1064 | Init, |
| 1065 | std::string("\01L_OBJC_CLASS_")+ClassName, |
| 1066 | &CGM.getModule()); |
| 1067 | GV->setSection("__OBJC,__class,regular,no_dead_strip"); |
| 1068 | UsedGlobals.push_back(GV); |
| 1069 | // FIXME: Why? |
| 1070 | GV->setAlignment(32); |
| 1071 | DefinedClasses.push_back(GV); |
| 1072 | } |
| 1073 | |
| 1074 | llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID, |
| 1075 | llvm::Constant *Protocols, |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1076 | const llvm::Type *InterfaceTy, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1077 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1078 | const char *ClassName = ID->getName(); |
| 1079 | unsigned Flags = eClassFlags_Meta; |
| 1080 | unsigned Size = CGM.getTargetData().getABITypeSize(ObjCTypes.ClassTy); |
| 1081 | |
| 1082 | if (IsClassHidden(ID->getClassInterface())) |
| 1083 | Flags |= eClassFlags_Hidden; |
| 1084 | |
| 1085 | std::vector<llvm::Constant*> Values(12); |
| 1086 | // The isa for the metaclass is the root of the hierarchy. |
| 1087 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 1088 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 1089 | Root = Super; |
| 1090 | Values[ 0] = |
| 1091 | llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()), |
| 1092 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1093 | // The super class for the metaclass is emitted as the name of the |
| 1094 | // super class. The runtime fixes this up to point to the |
| 1095 | // *metaclass* for the super class. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1096 | if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) { |
| 1097 | Values[ 1] = |
| 1098 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
| 1099 | ObjCTypes.ClassPtrTy); |
| 1100 | } else { |
| 1101 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
| 1102 | } |
| 1103 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 1104 | // Version is always 0. |
| 1105 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 1106 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 1107 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
| 1108 | Values[ 6] = EmitIvarList(ID, true, InterfaceTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1109 | Values[ 7] = |
| 1110 | EmitMethodList(std::string("\01L_OBJC_CLASS_METHODS_") + ID->getName(), |
| 1111 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1112 | Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1113 | // cache is always NULL. |
| 1114 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
| 1115 | Values[ 9] = Protocols; |
| 1116 | // ivar_layout for metaclass is always NULL. |
| 1117 | Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 1118 | // The class extension is always unused for metaclasses. |
| 1119 | Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
| 1120 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
| 1121 | Values); |
| 1122 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1123 | std::string Name("\01L_OBJC_METACLASS_"); |
| 1124 | Name += ClassName; |
| 1125 | |
| 1126 | // Check for a forward reference. |
| 1127 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 1128 | if (GV) { |
| 1129 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 1130 | "Forward metaclass reference has incorrect type."); |
| 1131 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 1132 | GV->setInitializer(Init); |
| 1133 | } else { |
| 1134 | GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 1135 | llvm::GlobalValue::InternalLinkage, |
| 1136 | Init, Name, |
| 1137 | &CGM.getModule()); |
| 1138 | } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1139 | GV->setSection("__OBJC,__meta_class,regular,no_dead_strip"); |
| 1140 | UsedGlobals.push_back(GV); |
| 1141 | // FIXME: Why? |
| 1142 | GV->setAlignment(32); |
| 1143 | |
| 1144 | return GV; |
| 1145 | } |
| 1146 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1147 | llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) { |
| 1148 | std::string Name("\01L_OBJC_METACLASS_"); |
| 1149 | Name += ID->getName(); |
| 1150 | |
| 1151 | // FIXME: Should we look these up somewhere other than the |
| 1152 | // module. Its a bit silly since we only generate these while |
| 1153 | // processing an implementation, so exactly one pointer would work |
| 1154 | // if know when we entered/exitted an implementation block. |
| 1155 | |
| 1156 | // Check for an existing forward reference. |
| 1157 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name)) { |
| 1158 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 1159 | "Forward metaclass reference has incorrect type."); |
| 1160 | return GV; |
| 1161 | } else { |
| 1162 | // Generate as an external reference to keep a consistent |
| 1163 | // module. This will be patched up when we emit the metaclass. |
| 1164 | return new llvm::GlobalVariable(ObjCTypes.ClassTy, false, |
| 1165 | llvm::GlobalValue::ExternalLinkage, |
| 1166 | 0, |
| 1167 | Name, |
| 1168 | &CGM.getModule()); |
| 1169 | } |
| 1170 | } |
| 1171 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1172 | /* |
| 1173 | struct objc_class_ext { |
| 1174 | uint32_t size; |
| 1175 | const char *weak_ivar_layout; |
| 1176 | struct _objc_property_list *properties; |
| 1177 | }; |
| 1178 | */ |
| 1179 | llvm::Constant * |
| 1180 | CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) { |
| 1181 | uint64_t Size = |
| 1182 | CGM.getTargetData().getABITypeSize(ObjCTypes.ClassExtensionTy); |
| 1183 | |
| 1184 | std::vector<llvm::Constant*> Values(3); |
| 1185 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1186 | // FIXME: Output weak_ivar_layout string. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1187 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1188 | Values[2] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + |
| 1189 | ID->getName(), |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1190 | ID, |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1191 | ID->getClassInterface()->classprop_begin(), |
| 1192 | ID->getClassInterface()->classprop_end()); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1193 | |
| 1194 | // Return null if no extension bits are used. |
| 1195 | if (Values[1]->isNullValue() && Values[2]->isNullValue()) |
| 1196 | return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
| 1197 | |
| 1198 | llvm::Constant *Init = |
| 1199 | llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values); |
| 1200 | llvm::GlobalVariable *GV = |
| 1201 | new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false, |
| 1202 | llvm::GlobalValue::InternalLinkage, |
| 1203 | Init, |
| 1204 | (std::string("\01L_OBJC_CLASSEXT_") + |
| 1205 | ID->getName()), |
| 1206 | &CGM.getModule()); |
| 1207 | // No special section, but goes in llvm.used |
| 1208 | UsedGlobals.push_back(GV); |
| 1209 | |
| 1210 | return GV; |
| 1211 | } |
| 1212 | |
| 1213 | /* |
| 1214 | struct objc_ivar { |
| 1215 | char *ivar_name; |
| 1216 | char *ivar_type; |
| 1217 | int ivar_offset; |
| 1218 | }; |
| 1219 | |
| 1220 | struct objc_ivar_list { |
| 1221 | int ivar_count; |
| 1222 | struct objc_ivar list[count]; |
| 1223 | }; |
| 1224 | */ |
| 1225 | llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID, |
| 1226 | bool ForClass, |
| 1227 | const llvm::Type *InterfaceTy) { |
| 1228 | std::vector<llvm::Constant*> Ivars, Ivar(3); |
| 1229 | |
| 1230 | // When emitting the root class GCC emits ivar entries for the |
| 1231 | // actual class structure. It is not clear if we need to follow this |
| 1232 | // behavior; for now lets try and get away with not doing it. If so, |
| 1233 | // the cleanest solution would be to make up an ObjCInterfaceDecl |
| 1234 | // for the class. |
| 1235 | if (ForClass) |
| 1236 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
| 1237 | |
| 1238 | const llvm::StructLayout *Layout = |
| 1239 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy)); |
| 1240 | for (ObjCInterfaceDecl::ivar_iterator |
| 1241 | i = ID->getClassInterface()->ivar_begin(), |
| 1242 | e = ID->getClassInterface()->ivar_end(); i != e; ++i) { |
| 1243 | ObjCIvarDecl *V = *i; |
| 1244 | unsigned Offset = |
| 1245 | Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(V)); |
| 1246 | std::string TypeStr; |
| 1247 | llvm::SmallVector<const RecordType *, 8> EncodingRecordTypes; |
| 1248 | Ivar[0] = GetMethodVarName(V->getIdentifier()); |
| 1249 | CGM.getContext().getObjCEncodingForType(V->getType(), TypeStr, |
| 1250 | EncodingRecordTypes); |
| 1251 | Ivar[1] = GetMethodVarType(TypeStr); |
| 1252 | Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset); |
| 1253 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, |
| 1254 | Ivar)); |
| 1255 | } |
| 1256 | |
| 1257 | // Return null for empty list. |
| 1258 | if (Ivars.empty()) |
| 1259 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
| 1260 | |
| 1261 | std::vector<llvm::Constant*> Values(2); |
| 1262 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
| 1263 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy, |
| 1264 | Ivars.size()); |
| 1265 | Values[1] = llvm::ConstantArray::get(AT, Ivars); |
| 1266 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1267 | |
| 1268 | const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" : |
| 1269 | "\01L_OBJC_INSTANCE_VARIABLES_"); |
| 1270 | llvm::GlobalVariable *GV = |
| 1271 | new llvm::GlobalVariable(Init->getType(), false, |
| 1272 | llvm::GlobalValue::InternalLinkage, |
| 1273 | Init, |
| 1274 | std::string(Prefix) + ID->getName(), |
| 1275 | &CGM.getModule()); |
| 1276 | if (ForClass) { |
| 1277 | GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip"); |
| 1278 | // FIXME: Why is this only here? |
| 1279 | GV->setAlignment(32); |
| 1280 | } else { |
| 1281 | GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip"); |
| 1282 | } |
| 1283 | UsedGlobals.push_back(GV); |
| 1284 | return llvm::ConstantExpr::getBitCast(GV, |
| 1285 | ObjCTypes.IvarListPtrTy); |
| 1286 | } |
| 1287 | |
| 1288 | /* |
| 1289 | struct objc_method { |
| 1290 | SEL method_name; |
| 1291 | char *method_types; |
| 1292 | void *method; |
| 1293 | }; |
| 1294 | |
| 1295 | struct objc_method_list { |
| 1296 | struct objc_method_list *obsolete; |
| 1297 | int count; |
| 1298 | struct objc_method methods_list[count]; |
| 1299 | }; |
| 1300 | */ |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1301 | |
| 1302 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 1303 | /// given method if it has been defined. The result is null if the |
| 1304 | /// method has not been defined. The return value has type MethodPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1305 | llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1306 | // FIXME: Use DenseMap::lookup |
| 1307 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 1308 | if (!Fn) |
| 1309 | return 0; |
| 1310 | |
| 1311 | std::vector<llvm::Constant*> Method(3); |
| 1312 | Method[0] = |
| 1313 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 1314 | ObjCTypes.SelectorPtrTy); |
| 1315 | Method[1] = GetMethodVarType(MD); |
| 1316 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
| 1317 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
| 1318 | } |
| 1319 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1320 | llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name, |
| 1321 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1322 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1323 | // Return null for empty list. |
| 1324 | if (Methods.empty()) |
| 1325 | return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy); |
| 1326 | |
| 1327 | std::vector<llvm::Constant*> Values(3); |
| 1328 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
| 1329 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
| 1330 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
| 1331 | Methods.size()); |
| 1332 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
| 1333 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1334 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1335 | llvm::GlobalVariable *GV = |
| 1336 | new llvm::GlobalVariable(Init->getType(), false, |
| 1337 | llvm::GlobalValue::InternalLinkage, |
| 1338 | Init, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1339 | Name, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1340 | &CGM.getModule()); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1341 | GV->setSection(Section); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1342 | UsedGlobals.push_back(GV); |
| 1343 | return llvm::ConstantExpr::getBitCast(GV, |
| 1344 | ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | llvm::Function *CGObjCMac::GenerateMethod(const ObjCMethodDecl *OMD) { |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1348 | std::string Name; |
| 1349 | GetNameForMethod(OMD, Name); |
| 1350 | |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1351 | const llvm::FunctionType *MethodTy = |
| 1352 | CGM.getTypes().GetFunctionType(CGFunctionInfo(OMD, CGM.getContext())); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1353 | llvm::Function *Method = |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1354 | llvm::Function::Create(MethodTy, |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1355 | llvm::GlobalValue::InternalLinkage, |
| 1356 | Name, |
| 1357 | &CGM.getModule()); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1358 | MethodDefinitions.insert(std::make_pair(OMD, Method)); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1359 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1360 | return Method; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | llvm::Function *CGObjCMac::ModuleInitFunction() { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1364 | // Abuse this interface function as a place to finalize. |
| 1365 | FinishModule(); |
| 1366 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1367 | return NULL; |
| 1368 | } |
| 1369 | |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 1370 | llvm::Function *CGObjCMac::EnumerationMutationFunction() |
| 1371 | { |
| 1372 | return ObjCTypes.EnumerationMutationFn; |
| 1373 | } |
| 1374 | |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1375 | void CGObjCMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
| 1376 | const ObjCAtTryStmt &S) |
| 1377 | { |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1378 | // Allocate exception data. |
| 1379 | llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy, |
| 1380 | "exceptiondata.ptr"); |
| 1381 | |
| 1382 | // Allocate memory for the rethrow pointer. |
| 1383 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy); |
| 1384 | CGF.Builder.CreateStore(llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy), |
| 1385 | RethrowPtr); |
| 1386 | |
| 1387 | // Enter a new try block and call setjmp. |
| 1388 | CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData); |
| 1389 | llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0, |
| 1390 | "jmpbufarray"); |
| 1391 | JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp"); |
| 1392 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn, |
| 1393 | JmpBufPtr, "result"); |
| 1394 | |
| 1395 | |
| 1396 | llvm::BasicBlock *FinallyBlock = llvm::BasicBlock::Create("finally"); |
| 1397 | |
| 1398 | llvm::BasicBlock *TryBlock = llvm::BasicBlock::Create("try"); |
| 1399 | llvm::BasicBlock *ExceptionInTryBlock = |
| 1400 | llvm::BasicBlock::Create("exceptionintry"); |
| 1401 | |
| 1402 | // If setjmp returns 1, there was an exception in the @try block. |
| 1403 | llvm::Value *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); |
| 1404 | llvm::Value *IsZero = CGF.Builder.CreateICmpEQ(SetJmpResult, Zero, "iszero"); |
| 1405 | CGF.Builder.CreateCondBr(IsZero, TryBlock, ExceptionInTryBlock); |
| 1406 | |
| 1407 | // Emit the @try block. |
| 1408 | CGF.EmitBlock(TryBlock); |
| 1409 | CGF.EmitStmt(S.getTryBody()); |
| 1410 | CGF.Builder.CreateBr(FinallyBlock); |
| 1411 | |
| 1412 | // Emit the "exception in @try" block. |
| 1413 | CGF.EmitBlock(ExceptionInTryBlock); |
| 1414 | |
| 1415 | if (const ObjCAtCatchStmt* CatchStmt = S.getCatchStmts()) { |
| 1416 | // Allocate memory for the caught exception and extract it from the |
| 1417 | // exception data. |
| 1418 | llvm::Value *CaughtPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy); |
| 1419 | llvm::Value *Extract = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn, |
| 1420 | ExceptionData); |
| 1421 | CGF.Builder.CreateStore(Extract, CaughtPtr); |
| 1422 | |
| 1423 | // Enter a new exception try block |
| 1424 | // (in case a @catch block throws an exception). |
| 1425 | CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData); |
| 1426 | |
| 1427 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn, |
| 1428 | JmpBufPtr, "result"); |
| 1429 | |
| 1430 | |
| 1431 | llvm::Value *Zero = llvm::Constant::getNullValue(llvm::Type::Int32Ty); |
| 1432 | llvm::Value *IsZero = CGF.Builder.CreateICmpEQ(SetJmpResult, Zero, |
| 1433 | "iszero"); |
| 1434 | |
| 1435 | llvm::BasicBlock *CatchBlock = llvm::BasicBlock::Create("catch"); |
| 1436 | llvm::BasicBlock *ExceptionInCatchBlock = |
| 1437 | llvm::BasicBlock::Create("exceptionincatch"); |
| 1438 | CGF.Builder.CreateCondBr(IsZero, CatchBlock, ExceptionInCatchBlock); |
| 1439 | |
| 1440 | CGF.EmitBlock(CatchBlock); |
| 1441 | |
| 1442 | // Handle catch list |
| 1443 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
| 1444 | llvm::BasicBlock *NextCatchBlock = llvm::BasicBlock::Create("nextcatch"); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame^] | 1445 | llvm::Value *Caught = CGF.Builder.CreateLoad(CaughtPtr, "caught"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1446 | |
| 1447 | QualType T; |
| 1448 | bool MatchesAll = false; |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame^] | 1449 | const DeclStmt *CatchParam = |
| 1450 | cast_or_null<DeclStmt>(CatchStmt->getCatchParamStmt()); |
| 1451 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1452 | // catch(...) always matches. |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame^] | 1453 | if (!CatchParam) |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1454 | MatchesAll = true; |
| 1455 | else { |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame^] | 1456 | QualType PT = cast<ValueDecl>(CatchParam->getDecl())->getType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1457 | T = PT->getAsPointerType()->getPointeeType(); |
| 1458 | |
| 1459 | // catch(id e) always matches. |
| 1460 | if (CGF.getContext().isObjCIdType(T)) |
| 1461 | MatchesAll = true; |
| 1462 | } |
| 1463 | |
Anders Carlsson | 1452f55 | 2008-09-11 08:21:54 +0000 | [diff] [blame] | 1464 | if (MatchesAll) { |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame^] | 1465 | if (CatchParam) { |
| 1466 | CGF.EmitStmt(CatchParam); |
| 1467 | |
| 1468 | const VarDecl *VD = cast<VarDecl>(CatchParam->getDecl()); |
| 1469 | |
| 1470 | llvm::Value *V = CGF.GetAddrOfLocalVar(VD); |
| 1471 | |
| 1472 | CGF.Builder.CreateStore(Caught, V); |
| 1473 | } |
Anders Carlsson | 1452f55 | 2008-09-11 08:21:54 +0000 | [diff] [blame] | 1474 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame^] | 1475 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1476 | CGF.Builder.CreateBr(FinallyBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1477 | CGF.EmitBlock(NextCatchBlock); |
| 1478 | break; |
| 1479 | } |
| 1480 | |
Anders Carlsson | 4b7ff6e | 2008-09-11 06:35:14 +0000 | [diff] [blame] | 1481 | const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1482 | assert(ObjCType && "Catch parameter must have Objective-C type!"); |
| 1483 | |
| 1484 | // Check if the @catch block matches the exception object. |
| 1485 | llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl()); |
| 1486 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1487 | llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn, |
| 1488 | Class, Caught, "match"); |
| 1489 | |
| 1490 | llvm::Value *DidMatch = CGF.Builder.CreateICmpNE(Match, Zero, "iszero"); |
| 1491 | |
| 1492 | llvm::BasicBlock *MatchedBlock = llvm::BasicBlock::Create("matched"); |
| 1493 | |
| 1494 | CGF.Builder.CreateCondBr(DidMatch, MatchedBlock, NextCatchBlock); |
| 1495 | |
| 1496 | // Emit the @catch block. |
| 1497 | CGF.EmitBlock(MatchedBlock); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame^] | 1498 | if (CatchParam) { |
| 1499 | CGF.EmitStmt(CatchParam); |
| 1500 | |
| 1501 | const VarDecl *VD = cast<VarDecl>(CatchParam->getDecl()); |
| 1502 | |
| 1503 | llvm::Value *Tmp = |
| 1504 | CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(VD->getType()), |
| 1505 | "tmp"); |
| 1506 | CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(VD)); |
| 1507 | } |
| 1508 | |
| 1509 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1510 | CGF.Builder.CreateBr(FinallyBlock); |
| 1511 | |
| 1512 | CGF.EmitBlock(NextCatchBlock); |
| 1513 | } |
| 1514 | |
| 1515 | // None of the handlers caught the exception, so store it and rethrow |
| 1516 | // it later. |
| 1517 | llvm::Value *Caught = CGF.Builder.CreateLoad(CaughtPtr, "caught"); |
| 1518 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
| 1519 | CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, |
| 1520 | ExceptionData); |
| 1521 | |
| 1522 | CGF.Builder.CreateBr(FinallyBlock); |
| 1523 | |
| 1524 | CGF.EmitBlock(ExceptionInCatchBlock); |
| 1525 | |
| 1526 | Extract = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn, |
| 1527 | ExceptionData); |
| 1528 | CGF.Builder.CreateStore(Extract, RethrowPtr); |
| 1529 | } |
| 1530 | |
| 1531 | // Emit the @finally block. |
| 1532 | CGF.EmitBlock(FinallyBlock); |
| 1533 | |
| 1534 | llvm::Value *Rethrow = CGF.Builder.CreateLoad(RethrowPtr); |
| 1535 | llvm::Value *ZeroPtr = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy); |
| 1536 | |
| 1537 | llvm::Value *RethrowIsZero = CGF.Builder.CreateICmpEQ(Rethrow, ZeroPtr); |
| 1538 | |
| 1539 | llvm::BasicBlock *TryExitBlock = llvm::BasicBlock::Create("tryexit"); |
| 1540 | llvm::BasicBlock *AfterTryExitBlock = |
| 1541 | llvm::BasicBlock::Create("aftertryexit"); |
| 1542 | |
| 1543 | CGF.Builder.CreateCondBr(RethrowIsZero, TryExitBlock, AfterTryExitBlock); |
| 1544 | CGF.EmitBlock(TryExitBlock); |
| 1545 | CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData); |
| 1546 | CGF.EmitBlock(AfterTryExitBlock); |
| 1547 | |
| 1548 | if (const ObjCAtFinallyStmt* FinallyStmt = S.getFinallyStmt()) |
| 1549 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
| 1550 | |
| 1551 | llvm::Value *RethrowIsNotZero = CGF.Builder.CreateICmpNE(Rethrow, ZeroPtr); |
| 1552 | |
| 1553 | llvm::BasicBlock *RethrowBlock = llvm::BasicBlock::Create("rethrow"); |
| 1554 | llvm::BasicBlock *FinallyEndBlock = llvm::BasicBlock::Create("finallyend"); |
| 1555 | |
| 1556 | // If necessary, rethrow the exception. |
| 1557 | CGF.Builder.CreateCondBr(RethrowIsNotZero, RethrowBlock, FinallyEndBlock); |
| 1558 | CGF.EmitBlock(RethrowBlock); |
| 1559 | CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, Rethrow); |
| 1560 | CGF.EmitBlock(FinallyEndBlock); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1561 | } |
| 1562 | |
| 1563 | void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 1564 | const ObjCAtThrowStmt &S) |
| 1565 | { |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 1566 | llvm::Value *ExceptionAsObject; |
| 1567 | |
| 1568 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
| 1569 | llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr); |
| 1570 | ExceptionAsObject = |
| 1571 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 1572 | } else { |
| 1573 | assert(0 && "FIXME: rethrows not supported!"); |
| 1574 | } |
| 1575 | |
| 1576 | CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 1577 | CGF.Builder.CreateUnreachable(); |
| 1578 | CGF.EmitBlock(llvm::BasicBlock::Create("bb")); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1581 | /* *** Private Interface *** */ |
| 1582 | |
| 1583 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 1584 | /// level information. |
| 1585 | /// |
| 1586 | /// See: <rdr://4810609&4810587&4810587> |
| 1587 | /// struct IMAGE_INFO { |
| 1588 | /// unsigned version; |
| 1589 | /// unsigned flags; |
| 1590 | /// }; |
| 1591 | enum ImageInfoFlags { |
| 1592 | eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies |
| 1593 | eImageInfo_GarbageCollected = (1 << 1), |
| 1594 | eImageInfo_GCOnly = (1 << 2) |
| 1595 | }; |
| 1596 | |
| 1597 | void CGObjCMac::EmitImageInfo() { |
| 1598 | unsigned version = 0; // Version is unused? |
| 1599 | unsigned flags = 0; |
| 1600 | |
| 1601 | // FIXME: Fix and continue? |
| 1602 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 1603 | flags |= eImageInfo_GarbageCollected; |
| 1604 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 1605 | flags |= eImageInfo_GCOnly; |
| 1606 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1607 | // Emitted as int[2]; |
| 1608 | llvm::Constant *values[2] = { |
| 1609 | llvm::ConstantInt::get(llvm::Type::Int32Ty, version), |
| 1610 | llvm::ConstantInt::get(llvm::Type::Int32Ty, flags) |
| 1611 | }; |
| 1612 | llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1613 | llvm::GlobalVariable *GV = |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1614 | new llvm::GlobalVariable(AT, true, |
| 1615 | llvm::GlobalValue::InternalLinkage, |
| 1616 | llvm::ConstantArray::get(AT, values, 2), |
| 1617 | "\01L_OBJC_IMAGE_INFO", |
| 1618 | &CGM.getModule()); |
| 1619 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1620 | if (ObjCABI == 1) { |
| 1621 | GV->setSection("__OBJC, __image_info,regular"); |
| 1622 | } else { |
| 1623 | GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip"); |
| 1624 | } |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1625 | |
| 1626 | UsedGlobals.push_back(GV); |
| 1627 | } |
| 1628 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1629 | |
| 1630 | // struct objc_module { |
| 1631 | // unsigned long version; |
| 1632 | // unsigned long size; |
| 1633 | // const char *name; |
| 1634 | // Symtab symtab; |
| 1635 | // }; |
| 1636 | |
| 1637 | // FIXME: Get from somewhere |
| 1638 | static const int ModuleVersion = 7; |
| 1639 | |
| 1640 | void CGObjCMac::EmitModuleInfo() { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1641 | uint64_t Size = CGM.getTargetData().getABITypeSize(ObjCTypes.ModuleTy); |
| 1642 | |
| 1643 | std::vector<llvm::Constant*> Values(4); |
| 1644 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion); |
| 1645 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1646 | // This used to be the filename, now it is unused. <rdr://4327263> |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1647 | Values[2] = GetClassName(&CGM.getContext().Idents.get("")); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1648 | Values[3] = EmitModuleSymbols(); |
| 1649 | |
| 1650 | llvm::GlobalVariable *GV = |
| 1651 | new llvm::GlobalVariable(ObjCTypes.ModuleTy, false, |
| 1652 | llvm::GlobalValue::InternalLinkage, |
| 1653 | llvm::ConstantStruct::get(ObjCTypes.ModuleTy, |
| 1654 | Values), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1655 | "\01L_OBJC_MODULES", |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1656 | &CGM.getModule()); |
| 1657 | GV->setSection("__OBJC,__module_info,regular,no_dead_strip"); |
| 1658 | UsedGlobals.push_back(GV); |
| 1659 | } |
| 1660 | |
| 1661 | llvm::Constant *CGObjCMac::EmitModuleSymbols() { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1662 | unsigned NumClasses = DefinedClasses.size(); |
| 1663 | unsigned NumCategories = DefinedCategories.size(); |
| 1664 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1665 | // Return null if no symbols were defined. |
| 1666 | if (!NumClasses && !NumCategories) |
| 1667 | return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy); |
| 1668 | |
| 1669 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1670 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 1671 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy); |
| 1672 | Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses); |
| 1673 | Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories); |
| 1674 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1675 | // The runtime expects exactly the list of defined classes followed |
| 1676 | // by the list of defined categories, in a single array. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1677 | std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1678 | for (unsigned i=0; i<NumClasses; i++) |
| 1679 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
| 1680 | ObjCTypes.Int8PtrTy); |
| 1681 | for (unsigned i=0; i<NumCategories; i++) |
| 1682 | Symbols[NumClasses + i] = |
| 1683 | llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
| 1684 | ObjCTypes.Int8PtrTy); |
| 1685 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1686 | Values[4] = |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1687 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1688 | NumClasses + NumCategories), |
| 1689 | Symbols); |
| 1690 | |
| 1691 | llvm::Constant *Init = llvm::ConstantStruct::get(Values); |
| 1692 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1693 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1694 | new llvm::GlobalVariable(Init->getType(), false, |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1695 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1696 | Init, |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1697 | "\01L_OBJC_SYMBOLS", |
| 1698 | &CGM.getModule()); |
| 1699 | GV->setSection("__OBJC,__symbols,regular,no_dead_strip"); |
| 1700 | UsedGlobals.push_back(GV); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1701 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy); |
| 1702 | } |
| 1703 | |
| 1704 | llvm::Value *CGObjCMac::EmitClassRef(llvm::IRBuilder<> &Builder, |
| 1705 | const ObjCInterfaceDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1706 | LazySymbols.insert(ID->getIdentifier()); |
| 1707 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1708 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
| 1709 | |
| 1710 | if (!Entry) { |
| 1711 | llvm::Constant *Casted = |
| 1712 | llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()), |
| 1713 | ObjCTypes.ClassPtrTy); |
| 1714 | Entry = |
| 1715 | new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false, |
| 1716 | llvm::GlobalValue::InternalLinkage, |
| 1717 | Casted, "\01L_OBJC_CLASS_REFERENCES_", |
| 1718 | &CGM.getModule()); |
| 1719 | Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip"); |
| 1720 | UsedGlobals.push_back(Entry); |
| 1721 | } |
| 1722 | |
| 1723 | return Builder.CreateLoad(Entry, false, "tmp"); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1724 | } |
| 1725 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 1726 | llvm::Value *CGObjCMac::EmitSelector(llvm::IRBuilder<> &Builder, Selector Sel) { |
| 1727 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
| 1728 | |
| 1729 | if (!Entry) { |
| 1730 | llvm::Constant *Casted = |
| 1731 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 1732 | ObjCTypes.SelectorPtrTy); |
| 1733 | Entry = |
| 1734 | new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false, |
| 1735 | llvm::GlobalValue::InternalLinkage, |
| 1736 | Casted, "\01L_OBJC_SELECTOR_REFERENCES_", |
| 1737 | &CGM.getModule()); |
| 1738 | Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip"); |
| 1739 | UsedGlobals.push_back(Entry); |
| 1740 | } |
| 1741 | |
| 1742 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 1743 | } |
| 1744 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1745 | llvm::Constant *CGObjCMac::GetClassName(IdentifierInfo *Ident) { |
| 1746 | llvm::GlobalVariable *&Entry = ClassNames[Ident]; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1747 | |
| 1748 | if (!Entry) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1749 | llvm::Constant *C = llvm::ConstantArray::get(Ident->getName()); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1750 | Entry = |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1751 | new llvm::GlobalVariable(C->getType(), false, |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1752 | llvm::GlobalValue::InternalLinkage, |
| 1753 | C, "\01L_OBJC_CLASS_NAME_", |
| 1754 | &CGM.getModule()); |
| 1755 | Entry->setSection("__TEXT,__cstring,cstring_literals"); |
| 1756 | UsedGlobals.push_back(Entry); |
| 1757 | } |
| 1758 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1759 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1760 | } |
| 1761 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 1762 | llvm::Constant *CGObjCMac::GetMethodVarName(Selector Sel) { |
| 1763 | llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; |
| 1764 | |
| 1765 | if (!Entry) { |
| 1766 | llvm::Constant *C = llvm::ConstantArray::get(Sel.getName()); |
| 1767 | Entry = |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1768 | new llvm::GlobalVariable(C->getType(), false, |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 1769 | llvm::GlobalValue::InternalLinkage, |
| 1770 | C, "\01L_OBJC_METH_VAR_NAME_", |
| 1771 | &CGM.getModule()); |
| 1772 | Entry->setSection("__TEXT,__cstring,cstring_literals"); |
| 1773 | UsedGlobals.push_back(Entry); |
| 1774 | } |
| 1775 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1776 | return getConstantGEP(Entry, 0, 0); |
| 1777 | } |
| 1778 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1779 | // FIXME: Merge into a single cstring creation function. |
| 1780 | llvm::Constant *CGObjCMac::GetMethodVarName(IdentifierInfo *ID) { |
| 1781 | return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); |
| 1782 | } |
| 1783 | |
| 1784 | // FIXME: Merge into a single cstring creation function. |
| 1785 | llvm::Constant *CGObjCMac::GetMethodVarName(const std::string &Name) { |
| 1786 | return GetMethodVarName(&CGM.getContext().Idents.get(Name)); |
| 1787 | } |
| 1788 | |
| 1789 | llvm::Constant *CGObjCMac::GetMethodVarType(const std::string &Name) { |
| 1790 | llvm::GlobalVariable *&Entry = MethodVarTypes[Name]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1791 | |
| 1792 | if (!Entry) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1793 | llvm::Constant *C = llvm::ConstantArray::get(Name); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1794 | Entry = |
| 1795 | new llvm::GlobalVariable(C->getType(), false, |
| 1796 | llvm::GlobalValue::InternalLinkage, |
| 1797 | C, "\01L_OBJC_METH_VAR_TYPE_", |
| 1798 | &CGM.getModule()); |
| 1799 | Entry->setSection("__TEXT,__cstring,cstring_literals"); |
| 1800 | UsedGlobals.push_back(Entry); |
| 1801 | } |
| 1802 | |
| 1803 | return getConstantGEP(Entry, 0, 0); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 1804 | } |
| 1805 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1806 | // FIXME: Merge into a single cstring creation function. |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1807 | llvm::Constant *CGObjCMac::GetMethodVarType(const ObjCMethodDecl *D) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1808 | std::string TypeStr; |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1809 | CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D), |
| 1810 | TypeStr); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1811 | return GetMethodVarType(TypeStr); |
| 1812 | } |
| 1813 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1814 | // FIXME: Merge into a single cstring creation function. |
| 1815 | llvm::Constant *CGObjCMac::GetPropertyName(IdentifierInfo *Ident) { |
| 1816 | llvm::GlobalVariable *&Entry = PropertyNames[Ident]; |
| 1817 | |
| 1818 | if (!Entry) { |
| 1819 | llvm::Constant *C = llvm::ConstantArray::get(Ident->getName()); |
| 1820 | Entry = |
| 1821 | new llvm::GlobalVariable(C->getType(), false, |
| 1822 | llvm::GlobalValue::InternalLinkage, |
| 1823 | C, "\01L_OBJC_PROP_NAME_ATTR_", |
| 1824 | &CGM.getModule()); |
| 1825 | Entry->setSection("__TEXT,__cstring,cstring_literals"); |
| 1826 | UsedGlobals.push_back(Entry); |
| 1827 | } |
| 1828 | |
| 1829 | return getConstantGEP(Entry, 0, 0); |
| 1830 | } |
| 1831 | |
| 1832 | // FIXME: Merge into a single cstring creation function. |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1833 | // FIXME: This Decl should be more precise. |
| 1834 | llvm::Constant *CGObjCMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 1835 | const Decl *Container) { |
| 1836 | std::string TypeStr; |
| 1837 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1838 | return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); |
| 1839 | } |
| 1840 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 1841 | void CGObjCMac::GetNameForMethod(const ObjCMethodDecl *D, |
| 1842 | std::string &NameOut) { |
| 1843 | // FIXME: Find the mangling GCC uses. |
| 1844 | std::stringstream s; |
| 1845 | s << (D->isInstance() ? "-" : "+"); |
| 1846 | s << "["; |
| 1847 | s << D->getClassInterface()->getName(); |
| 1848 | s << " "; |
| 1849 | s << D->getSelector().getName(); |
| 1850 | s << "]"; |
| 1851 | NameOut = s.str(); |
| 1852 | } |
| 1853 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1854 | void CGObjCMac::FinishModule() { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1855 | EmitModuleInfo(); |
| 1856 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1857 | std::vector<llvm::Constant*> Used; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1858 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1859 | for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1860 | e = UsedGlobals.end(); i != e; ++i) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1861 | Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1862 | } |
| 1863 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1864 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1865 | llvm::GlobalValue *GV = |
| 1866 | new llvm::GlobalVariable(AT, false, |
| 1867 | llvm::GlobalValue::AppendingLinkage, |
| 1868 | llvm::ConstantArray::get(AT, Used), |
| 1869 | "llvm.used", |
| 1870 | &CGM.getModule()); |
| 1871 | |
| 1872 | GV->setSection("llvm.metadata"); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1873 | |
| 1874 | // Add assembler directives to add lazy undefined symbol references |
| 1875 | // for classes which are referenced but not defined. This is |
| 1876 | // important for correct linker interaction. |
| 1877 | |
| 1878 | // FIXME: Uh, this isn't particularly portable. |
| 1879 | std::stringstream s; |
| 1880 | for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(), |
| 1881 | e = LazySymbols.end(); i != e; ++i) { |
| 1882 | s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n"; |
| 1883 | } |
| 1884 | for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(), |
| 1885 | e = DefinedSymbols.end(); i != e; ++i) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1886 | s << "\t.objc_class_name_" << (*i)->getName() << "=0\n" |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1887 | << "\t.globl .objc_class_name_" << (*i)->getName() << "\n"; |
| 1888 | } |
| 1889 | CGM.getModule().appendModuleInlineAsm(s.str()); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 1890 | } |
| 1891 | |
| 1892 | /* *** */ |
| 1893 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1894 | ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) |
Daniel Dunbar | 3e9df99 | 2008-08-23 18:37:06 +0000 | [diff] [blame] | 1895 | : CGM(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1896 | { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1897 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 1898 | ASTContext &Ctx = CGM.getContext(); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1899 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1900 | ShortTy = Types.ConvertType(Ctx.ShortTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1901 | IntTy = Types.ConvertType(Ctx.IntTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1902 | LongTy = Types.ConvertType(Ctx.LongTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1903 | Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 1904 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1905 | ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType()); |
| 1906 | SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1907 | |
| 1908 | // FIXME: It would be nice to unify this with the opaque type, so |
| 1909 | // that the IR comes out a bit cleaner. |
| 1910 | const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); |
| 1911 | ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 1912 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1913 | MethodDescriptionTy = |
| 1914 | llvm::StructType::get(SelectorPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1915 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1916 | NULL); |
| 1917 | CGM.getModule().addTypeName("struct._objc_method_description", |
| 1918 | MethodDescriptionTy); |
| 1919 | |
| 1920 | MethodDescriptionListTy = |
| 1921 | llvm::StructType::get(IntTy, |
| 1922 | llvm::ArrayType::get(MethodDescriptionTy, 0), |
| 1923 | NULL); |
| 1924 | CGM.getModule().addTypeName("struct._objc_method_description_list", |
| 1925 | MethodDescriptionListTy); |
| 1926 | MethodDescriptionListPtrTy = |
| 1927 | llvm::PointerType::getUnqual(MethodDescriptionListTy); |
| 1928 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1929 | PropertyTy = llvm::StructType::get(Int8PtrTy, |
| 1930 | Int8PtrTy, |
| 1931 | NULL); |
| 1932 | CGM.getModule().addTypeName("struct._objc_property", |
| 1933 | PropertyTy); |
| 1934 | |
| 1935 | PropertyListTy = llvm::StructType::get(IntTy, |
| 1936 | IntTy, |
| 1937 | llvm::ArrayType::get(PropertyTy, 0), |
| 1938 | NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1939 | CGM.getModule().addTypeName("struct._objc_property_list", |
| 1940 | PropertyListTy); |
| 1941 | PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy); |
| 1942 | |
| 1943 | // Protocol description structures |
| 1944 | |
| 1945 | ProtocolExtensionTy = |
| 1946 | llvm::StructType::get(Types.ConvertType(Ctx.IntTy), |
| 1947 | llvm::PointerType::getUnqual(MethodDescriptionListTy), |
| 1948 | llvm::PointerType::getUnqual(MethodDescriptionListTy), |
| 1949 | PropertyListPtrTy, |
| 1950 | NULL); |
| 1951 | CGM.getModule().addTypeName("struct._objc_protocol_extension", |
| 1952 | ProtocolExtensionTy); |
| 1953 | ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy); |
| 1954 | |
| 1955 | // Handle recursive construction of Protocl and ProtocolList types |
| 1956 | |
| 1957 | llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(); |
| 1958 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); |
| 1959 | |
| 1960 | T = llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 1961 | LongTy, |
| 1962 | llvm::ArrayType::get(ProtocolTyHolder, 0), |
| 1963 | NULL); |
| 1964 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T); |
| 1965 | |
| 1966 | T = llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolExtensionTy), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1967 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1968 | llvm::PointerType::getUnqual(ProtocolListTyHolder), |
| 1969 | MethodDescriptionListPtrTy, |
| 1970 | MethodDescriptionListPtrTy, |
| 1971 | NULL); |
| 1972 | cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T); |
| 1973 | |
| 1974 | ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get()); |
| 1975 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 1976 | ProtocolListTy); |
| 1977 | ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy); |
| 1978 | |
| 1979 | ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get()); |
| 1980 | CGM.getModule().addTypeName("struct.__objc_protocol", ProtocolTy); |
| 1981 | ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1982 | |
| 1983 | // Class description structures |
| 1984 | |
| 1985 | IvarTy = llvm::StructType::get(Int8PtrTy, |
| 1986 | Int8PtrTy, |
| 1987 | IntTy, |
| 1988 | NULL); |
| 1989 | CGM.getModule().addTypeName("struct._objc_ivar", IvarTy); |
| 1990 | |
| 1991 | IvarListTy = llvm::OpaqueType::get(); |
| 1992 | CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy); |
| 1993 | IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy); |
| 1994 | |
| 1995 | MethodTy = llvm::StructType::get(SelectorPtrTy, |
| 1996 | Int8PtrTy, |
| 1997 | Int8PtrTy, |
| 1998 | NULL); |
| 1999 | CGM.getModule().addTypeName("struct._objc_method", MethodTy); |
| 2000 | |
| 2001 | MethodListTy = llvm::OpaqueType::get(); |
| 2002 | CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy); |
| 2003 | MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy); |
| 2004 | |
| 2005 | CacheTy = llvm::OpaqueType::get(); |
| 2006 | CGM.getModule().addTypeName("struct._objc_cache", CacheTy); |
| 2007 | CachePtrTy = llvm::PointerType::getUnqual(CacheTy); |
| 2008 | |
| 2009 | ClassExtensionTy = |
| 2010 | llvm::StructType::get(IntTy, |
| 2011 | Int8PtrTy, |
| 2012 | PropertyListPtrTy, |
| 2013 | NULL); |
| 2014 | CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy); |
| 2015 | ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy); |
| 2016 | |
| 2017 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); |
| 2018 | |
| 2019 | T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), |
| 2020 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 2021 | Int8PtrTy, |
| 2022 | LongTy, |
| 2023 | LongTy, |
| 2024 | LongTy, |
| 2025 | IvarListPtrTy, |
| 2026 | MethodListPtrTy, |
| 2027 | CachePtrTy, |
| 2028 | ProtocolListPtrTy, |
| 2029 | Int8PtrTy, |
| 2030 | ClassExtensionPtrTy, |
| 2031 | NULL); |
| 2032 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T); |
| 2033 | |
| 2034 | ClassTy = cast<llvm::StructType>(ClassTyHolder.get()); |
| 2035 | CGM.getModule().addTypeName("struct._objc_class", ClassTy); |
| 2036 | ClassPtrTy = llvm::PointerType::getUnqual(ClassTy); |
| 2037 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2038 | CategoryTy = llvm::StructType::get(Int8PtrTy, |
| 2039 | Int8PtrTy, |
| 2040 | MethodListPtrTy, |
| 2041 | MethodListPtrTy, |
| 2042 | ProtocolListPtrTy, |
| 2043 | IntTy, |
| 2044 | PropertyListPtrTy, |
| 2045 | NULL); |
| 2046 | CGM.getModule().addTypeName("struct._objc_category", CategoryTy); |
| 2047 | |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 2048 | // I'm not sure I like this. The implicit coordination is a bit |
| 2049 | // gross. We should solve this in a reasonable fashion because this |
| 2050 | // is a pretty common task (match some runtime data structure with |
| 2051 | // an LLVM data structure). |
| 2052 | |
| 2053 | // FIXME: This is leaked. |
| 2054 | // FIXME: Merge with rewriter code? |
| 2055 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 2056 | SourceLocation(), |
Ted Kremenek | df042e6 | 2008-09-05 01:34:33 +0000 | [diff] [blame] | 2057 | &Ctx.Idents.get("_objc_super")); |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 2058 | FieldDecl *FieldDecls[2]; |
| 2059 | FieldDecls[0] = FieldDecl::Create(Ctx, SourceLocation(), 0, |
| 2060 | Ctx.getObjCIdType()); |
| 2061 | FieldDecls[1] = FieldDecl::Create(Ctx, SourceLocation(), 0, |
| 2062 | Ctx.getObjCClassType()); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2063 | RD->defineBody(Ctx, FieldDecls, 2); |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 2064 | |
| 2065 | SuperCTy = Ctx.getTagDeclType(RD); |
| 2066 | SuperPtrCTy = Ctx.getPointerType(SuperCTy); |
| 2067 | |
| 2068 | SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy)); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 2069 | SuperPtrTy = llvm::PointerType::getUnqual(SuperTy); |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 2070 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2071 | // Global metadata structures |
| 2072 | |
| 2073 | SymtabTy = llvm::StructType::get(LongTy, |
| 2074 | SelectorPtrTy, |
| 2075 | ShortTy, |
| 2076 | ShortTy, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2077 | llvm::ArrayType::get(Int8PtrTy, 0), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2078 | NULL); |
| 2079 | CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy); |
| 2080 | SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy); |
| 2081 | |
| 2082 | ModuleTy = |
| 2083 | llvm::StructType::get(LongTy, |
| 2084 | LongTy, |
| 2085 | Int8PtrTy, |
| 2086 | SymtabPtrTy, |
| 2087 | NULL); |
| 2088 | CGM.getModule().addTypeName("struct._objc_module", ModuleTy); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 2089 | |
| 2090 | // Message send functions |
| 2091 | |
| 2092 | std::vector<const llvm::Type*> Params; |
| 2093 | Params.push_back(ObjectPtrTy); |
| 2094 | Params.push_back(SelectorPtrTy); |
| 2095 | MessageSendFn = llvm::Function::Create(llvm::FunctionType::get(ObjectPtrTy, |
| 2096 | Params, |
| 2097 | true), |
| 2098 | llvm::Function::ExternalLinkage, |
| 2099 | "objc_msgSend", |
| 2100 | &CGM.getModule()); |
| 2101 | |
| 2102 | Params.clear(); |
| 2103 | Params.push_back(Int8PtrTy); |
| 2104 | Params.push_back(ObjectPtrTy); |
| 2105 | Params.push_back(SelectorPtrTy); |
| 2106 | MessageSendStretFn = |
| 2107 | llvm::Function::Create(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 2108 | Params, |
| 2109 | true), |
| 2110 | llvm::Function::ExternalLinkage, |
| 2111 | "objc_msgSend_stret", |
| 2112 | &CGM.getModule()); |
| 2113 | |
| 2114 | Params.clear(); |
| 2115 | Params.push_back(SuperPtrTy); |
| 2116 | Params.push_back(SelectorPtrTy); |
| 2117 | MessageSendSuperFn = |
| 2118 | llvm::Function::Create(llvm::FunctionType::get(ObjectPtrTy, |
| 2119 | Params, |
| 2120 | true), |
| 2121 | llvm::Function::ExternalLinkage, |
| 2122 | "objc_msgSendSuper", |
| 2123 | &CGM.getModule()); |
| 2124 | |
| 2125 | Params.clear(); |
| 2126 | Params.push_back(Int8PtrTy); |
| 2127 | Params.push_back(SuperPtrTy); |
| 2128 | Params.push_back(SelectorPtrTy); |
| 2129 | MessageSendSuperStretFn = |
| 2130 | llvm::Function::Create(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 2131 | Params, |
| 2132 | true), |
| 2133 | llvm::Function::ExternalLinkage, |
| 2134 | "objc_msgSendSuper_stret", |
| 2135 | &CGM.getModule()); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 2136 | |
| 2137 | // Enumeration mutation. |
| 2138 | |
| 2139 | Params.clear(); |
| 2140 | Params.push_back(ObjectPtrTy); |
| 2141 | EnumerationMutationFn = |
| 2142 | llvm::Function::Create(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 2143 | Params, |
| 2144 | false), |
| 2145 | llvm::Function::ExternalLinkage, |
| 2146 | "objc_enumerationMutation", |
| 2147 | &CGM.getModule()); |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 2148 | |
| 2149 | // FIXME: This is the size of the setjmp buffer and should be |
| 2150 | // target specific. 18 is what's used on 32-bit X86. |
| 2151 | uint64_t SetJmpBufferSize = 18; |
| 2152 | |
| 2153 | // Exceptions |
| 2154 | const llvm::Type *StackPtrTy = |
| 2155 | llvm::PointerType::getUnqual(llvm::ArrayType::get(llvm::Type::Int8Ty, 4)); |
| 2156 | |
| 2157 | ExceptionDataTy = |
| 2158 | llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty, |
| 2159 | SetJmpBufferSize), |
| 2160 | StackPtrTy, NULL); |
| 2161 | CGM.getModule().addTypeName("struct._objc_exception_data", |
| 2162 | ExceptionDataTy); |
| 2163 | |
| 2164 | Params.clear(); |
| 2165 | Params.push_back(ObjectPtrTy); |
| 2166 | ExceptionThrowFn = |
| 2167 | llvm::Function::Create(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 2168 | Params, |
| 2169 | false), |
| 2170 | llvm::Function::ExternalLinkage, |
| 2171 | "objc_exception_throw", |
| 2172 | &CGM.getModule()); |
| 2173 | |
| 2174 | Params.clear(); |
| 2175 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
| 2176 | ExceptionTryEnterFn = |
| 2177 | llvm::Function::Create(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 2178 | Params, |
| 2179 | false), |
| 2180 | llvm::Function::ExternalLinkage, |
| 2181 | "objc_exception_try_enter", |
| 2182 | &CGM.getModule()); |
| 2183 | ExceptionTryExitFn = |
| 2184 | llvm::Function::Create(llvm::FunctionType::get(llvm::Type::VoidTy, |
| 2185 | Params, |
| 2186 | false), |
| 2187 | llvm::Function::ExternalLinkage, |
| 2188 | "objc_exception_try_exit", |
| 2189 | &CGM.getModule()); |
| 2190 | ExceptionExtractFn = |
| 2191 | llvm::Function::Create(llvm::FunctionType::get(ObjectPtrTy, |
| 2192 | Params, |
| 2193 | false), |
| 2194 | llvm::Function::ExternalLinkage, |
| 2195 | "objc_exception_extract", |
| 2196 | &CGM.getModule()); |
| 2197 | |
| 2198 | Params.clear(); |
| 2199 | Params.push_back(ClassPtrTy); |
| 2200 | Params.push_back(ObjectPtrTy); |
| 2201 | ExceptionMatchFn = |
| 2202 | llvm::Function::Create(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 2203 | Params, |
| 2204 | false), |
| 2205 | llvm::Function::ExternalLinkage, |
| 2206 | "objc_exception_match", |
| 2207 | &CGM.getModule()); |
| 2208 | |
| 2209 | Params.clear(); |
| 2210 | Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty)); |
| 2211 | SetJmpFn = |
| 2212 | llvm::Function::Create(llvm::FunctionType::get(llvm::Type::Int32Ty, |
| 2213 | Params, |
| 2214 | false), |
| 2215 | llvm::Function::ExternalLinkage, |
| 2216 | "_setjmp", |
| 2217 | &CGM.getModule()); |
| 2218 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 2219 | } |
| 2220 | |
| 2221 | ObjCTypesHelper::~ObjCTypesHelper() { |
| 2222 | } |
| 2223 | |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 2224 | llvm::Constant *ObjCTypesHelper::getMessageSendFn(bool IsSuper, bool IsStret) { |
Daniel Dunbar | 3913f18 | 2008-09-09 23:48:28 +0000 | [diff] [blame] | 2225 | if (IsStret) { |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 2226 | return IsSuper ? MessageSendSuperStretFn : MessageSendStretFn; |
Daniel Dunbar | 662174c8 | 2008-08-29 17:28:43 +0000 | [diff] [blame] | 2227 | } else { // FIXME: floating point? |
Daniel Dunbar | 62d5c1b | 2008-09-10 07:00:50 +0000 | [diff] [blame] | 2228 | return IsSuper ? MessageSendSuperFn : MessageSendFn; |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2229 | } |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 2230 | } |
| 2231 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 2232 | /* *** */ |
| 2233 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2234 | CodeGen::CGObjCRuntime * |
| 2235 | CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2236 | return new CGObjCMac(CGM); |
| 2237 | } |