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