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