blob: 69a0c4b06e87e2f4566ecfc42c9815e31a8ee118 [file] [log] [blame]
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001//===------- 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 Dunbarf77ac862008-08-11 21:35:06 +000015
16#include "CodeGenModule.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000017#include "CodeGenFunction.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000019#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000020#include "clang/AST/DeclObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000021#include "clang/Basic/LangOptions.h"
22
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000023#include "llvm/Module.h"
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000024#include "llvm/Support/IRBuilder.h"
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000025#include "llvm/Target/TargetData.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000026#include <sstream>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000027
28using namespace clang;
29
30namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000031
Daniel Dunbarae226fa2008-08-27 02:31:56 +000032 typedef std::vector<llvm::Constant*> ConstantVector;
33
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000034 // FIXME: We should find a nicer way to make the labels for
35 // metadata, string concatenation is lame.
36
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000037/// ObjCTypesHelper - Helper class that encapsulates lazy
38/// construction of varies types used during ObjC generation.
39class ObjCTypesHelper {
40private:
41 CodeGen::CodeGenModule &CGM;
42
Daniel Dunbar14c80b72008-08-23 09:25:55 +000043 llvm::Function *MessageSendFn, *MessageSendStretFn;
44 llvm::Function *MessageSendSuperFn, *MessageSendSuperStretFn;
Daniel Dunbar259d93d2008-08-12 03:39:23 +000045
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000046public:
Daniel Dunbar27f9d772008-08-21 04:36:09 +000047 const llvm::Type *ShortTy, *IntTy, *LongTy;
48 const llvm::Type *Int8PtrTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +000049
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000050 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
51 const llvm::Type *ObjectPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000052 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000053 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000054 /// ProtocolPtrTy - LLVM type for external protocol handles
55 /// (typeof(Protocol))
56 const llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000057
Daniel Dunbare8b470d2008-08-23 04:28:29 +000058 /// SuperTy - LLVM type for struct objc_super.
59 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +000060 /// SuperPtrTy - LLVM type for struct objc_super *.
61 const llvm::Type *SuperPtrTy;
Daniel Dunbare8b470d2008-08-23 04:28:29 +000062
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000063 /// SymtabTy - LLVM type for struct objc_symtab.
64 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +000065 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
66 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000067 /// ModuleTy - LLVM type for struct objc_module.
68 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +000069
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000070 /// ProtocolTy - LLVM type for struct objc_protocol.
71 const llvm::StructType *ProtocolTy;
72 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
73 const llvm::Type *ProtocolPtrTy;
74 /// ProtocolExtensionTy - LLVM type for struct
75 /// objc_protocol_extension.
76 const llvm::StructType *ProtocolExtensionTy;
77 /// ProtocolExtensionTy - LLVM type for struct
78 /// objc_protocol_extension *.
79 const llvm::Type *ProtocolExtensionPtrTy;
80 /// MethodDescriptionTy - LLVM type for struct
81 /// objc_method_description.
82 const llvm::StructType *MethodDescriptionTy;
83 /// MethodDescriptionListTy - LLVM type for struct
84 /// objc_method_description_list.
85 const llvm::StructType *MethodDescriptionListTy;
86 /// MethodDescriptionListPtrTy - LLVM type for struct
87 /// objc_method_description_list *.
88 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +000089 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
90 /// in GCC parlance).
91 const llvm::StructType *PropertyTy;
92 /// PropertyListTy - LLVM type for struct objc_property_list
93 /// (_prop_list_t in GCC parlance).
94 const llvm::StructType *PropertyListTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000095 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
96 const llvm::Type *PropertyListPtrTy;
97 /// ProtocolListTy - LLVM type for struct objc_property_list.
98 const llvm::Type *ProtocolListTy;
99 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
100 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000101 /// CategoryTy - LLVM type for struct objc_category.
102 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000103 /// ClassTy - LLVM type for struct objc_class.
104 const llvm::StructType *ClassTy;
105 /// ClassPtrTy - LLVM type for struct objc_class *.
106 const llvm::Type *ClassPtrTy;
107 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
108 const llvm::StructType *ClassExtensionTy;
109 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
110 const llvm::Type *ClassExtensionPtrTy;
111 /// CacheTy - LLVM type for struct objc_cache.
112 const llvm::Type *CacheTy;
113 /// CachePtrTy - LLVM type for struct objc_cache *.
114 const llvm::Type *CachePtrTy;
115 // IvarTy - LLVM type for struct objc_ivar.
116 const llvm::StructType *IvarTy;
117 /// IvarListTy - LLVM type for struct objc_ivar_list.
118 const llvm::Type *IvarListTy;
119 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
120 const llvm::Type *IvarListPtrTy;
121 // MethodTy - LLVM type for struct objc_method.
122 const llvm::StructType *MethodTy;
123 /// MethodListTy - LLVM type for struct objc_method_list.
124 const llvm::Type *MethodListTy;
125 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
126 const llvm::Type *MethodListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000127
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000128public:
129 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
130 ~ObjCTypesHelper();
131
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000132 llvm::Value *getMessageSendFn(bool IsSuper, const llvm::Type *ReturnTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000133};
134
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000135class CGObjCMac : public CodeGen::CGObjCRuntime {
136private:
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000137 CodeGen::CodeGenModule &CGM;
138 ObjCTypesHelper ObjCTypes;
139 /// ObjCABI - FIXME: Not sure yet.
140 unsigned ObjCABI;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000141
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000142 /// LazySymbols - Symbols to generate a lazy reference for. See
143 /// DefinedSymbols and FinishModule().
144 std::set<IdentifierInfo*> LazySymbols;
145
146 /// DefinedSymbols - External symbols which are defined by this
147 /// module. The symbols in this list and LazySymbols are used to add
148 /// special linker symbols which ensure that Objective-C modules are
149 /// linked properly.
150 std::set<IdentifierInfo*> DefinedSymbols;
151
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000152 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000153 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000154
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000155 /// MethodVarNames - uniqued method variable names.
156 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
157
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000158 /// MethodVarTypes - uniqued method type signatures. We have to use
159 /// a StringMap here because have no other unique reference.
160 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
161
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000162 /// MethodDefinitions - map of methods which have been defined in
163 /// this translation unit.
164 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
165
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000166 /// PropertyNames - uniqued method variable names.
167 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
168
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000169 /// ClassReferences - uniqued class references.
170 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
171
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000172 /// SelectorReferences - uniqued selector references.
173 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
174
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000175 /// Protocols - Protocols for which an objc_protocol structure has
176 /// been emitted. Forward declarations are handled by creating an
177 /// empty structure whose initializer is filled in when/if defined.
178 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
179
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000180 /// DefinedClasses - List of defined classes.
181 std::vector<llvm::GlobalValue*> DefinedClasses;
182
183 /// DefinedCategories - List of defined categories.
184 std::vector<llvm::GlobalValue*> DefinedCategories;
185
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000186 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000187 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000188 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000189
190 /// EmitImageInfo - Emit the image info marker used to encode some module
191 /// level information.
192 void EmitImageInfo();
193
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000194 /// EmitModuleInfo - Another marker encoding module level
195 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000196 void EmitModuleInfo();
197
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000198 /// EmitModuleSymols - Emit module symbols, the list of defined
199 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000200 llvm::Constant *EmitModuleSymbols();
201
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000202 /// FinishModule - Write out global data structures at the end of
203 /// processing a translation unit.
204 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000205
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000206 /// EmitClassExtension - Generate the class extension structure used
207 /// to store the weak ivar layout and properties. The return value
208 /// has type ClassExtensionPtrTy.
209 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
210
211 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
212 /// for the given class.
213 llvm::Value *EmitClassRef(llvm::IRBuilder<> &Builder,
214 const ObjCInterfaceDecl *ID);
215
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000216 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
217 const ObjCMessageExpr *E,
218 llvm::Value *Arg0,
219 bool IsSuper);
220
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000221 /// EmitIvarList - Emit the ivar list for the given
222 /// implementation. If ForClass is true the list of class ivars
223 /// (i.e. metaclass ivars) is emitted, otherwise the list of
224 /// interface ivars will be emitted. The return value has type
225 /// IvarListPtrTy.
226 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
227 bool ForClass,
228 const llvm::Type *InterfaceTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000229
230 /// EmitMetaClass - Emit a forward reference to the class structure
231 /// for the metaclass of the given interface. The return value has
232 /// type ClassPtrTy.
233 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
234
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000235 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000236 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000237 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
238 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000239 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000240 const ConstantVector &Methods);
241
242 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
243
244 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000245
246 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000247 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000248 llvm::Constant *EmitMethodList(const std::string &Name,
249 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000250 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000251
252 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000253 /// method declarations.
254 /// - TypeName: The name for the type containing the methods.
255 /// - IsProtocol: True iff these methods are for a protocol.
256 /// - ClassMethds: True iff these are class methods.
257 /// - Required: When true, only "required" methods are
258 /// listed. Similarly, when false only "optional" methods are
259 /// listed. For classes this should always be true.
260 /// - begin, end: The method list to output.
261 ///
262 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000263 llvm::Constant *EmitMethodDescList(const std::string &Name,
264 const char *Section,
265 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000266
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000267 /// EmitPropertyList - Emit the given property list. The return
268 /// value has type PropertyListPtrTy.
269 llvm::Constant *EmitPropertyList(const std::string &Name,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +0000270 const Decl *Container,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000271 ObjCPropertyDecl * const *begin,
272 ObjCPropertyDecl * const *end);
273
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000274 /// EmitProtocolExtension - Generate the protocol extension
275 /// structure used to store optional instance and class methods, and
276 /// protocol properties. The return value has type
277 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000278 llvm::Constant *
279 EmitProtocolExtension(const ObjCProtocolDecl *PD,
280 const ConstantVector &OptInstanceMethods,
281 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000282
283 /// EmitProtocolList - Generate the list of referenced
284 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc933702008-08-21 21:57:41 +0000285 llvm::Constant *EmitProtocolList(const std::string &Name,
286 ObjCProtocolDecl::protocol_iterator begin,
287 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000288
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000289 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
290 /// for the given selector.
291 llvm::Value *EmitSelector(llvm::IRBuilder<> &Builder, Selector Sel);
292
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000293 /// GetProtocolRef - Return a reference to the internal protocol
294 /// description, creating an empty one if it has not been
295 /// defined. The return value has type pointer-to ProtocolTy.
296 llvm::GlobalVariable *GetProtocolRef(const ObjCProtocolDecl *PD);
297
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000298 /// GetClassName - Return a unique constant for the given selector's
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000299 /// name. The return value has type char *.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000300 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000301
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000302 /// GetMethodVarName - Return a unique constant for the given
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000303 /// selector's name. The return value has type char *.
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000304 llvm::Constant *GetMethodVarName(Selector Sel);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000305 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000306 llvm::Constant *GetMethodVarName(const std::string &Name);
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000307
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000308 /// GetMethodVarType - Return a unique constant for the given
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000309 /// selector's name. The return value has type char *.
310
311 // FIXME: This is a horrible name.
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000312 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000313 llvm::Constant *GetMethodVarType(const std::string &Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000314
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000315 /// GetPropertyName - Return a unique constant for the given
316 /// name. The return value has type char *.
317 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
318
Daniel Dunbarc56f34a2008-08-28 04:38:10 +0000319 // FIXME: This can be dropped once string functions are unified.
320 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
321 const Decl *Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000322
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000323 /// GetNameForMethod - Return a name for the given method.
324 /// \param[out] NameOut - The return value.
325 void GetNameForMethod(const ObjCMethodDecl *OMD,
326 std::string &NameOut);
327
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000328public:
329 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000330 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000331
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000332 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
333 const ObjCMessageExpr *E,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000334 llvm::Value *Receiver,
335 bool IsClassMessage);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000336
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000337 virtual CodeGen::RValue
338 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
339 const ObjCMessageExpr *E,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000340 const ObjCInterfaceDecl *Class,
341 llvm::Value *Receiver,
342 bool IsClassMessage);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000343
344 virtual llvm::Value *GetClass(llvm::IRBuilder<> &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000345 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000346
347 virtual llvm::Value *GetSelector(llvm::IRBuilder<> &Builder, Selector Sel);
348
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000349 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000350
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000351 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000352
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000353 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000354
355 virtual llvm::Value *GenerateProtocolRef(llvm::IRBuilder<> &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000356 const ObjCProtocolDecl *PD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000357
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000358 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000359
360 virtual llvm::Function *ModuleInitFunction();
361};
362} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000363
364/* *** Helper Functions *** */
365
366/// getConstantGEP() - Help routine to construct simple GEPs.
367static llvm::Constant *getConstantGEP(llvm::Constant *C,
368 unsigned idx0,
369 unsigned idx1) {
370 llvm::Value *Idxs[] = {
371 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
372 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
373 };
374 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
375}
376
377/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000378
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000379CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm)
380 : CGM(cgm),
381 ObjCTypes(cgm),
382 ObjCABI(1)
383{
384 // FIXME: How does this get set in GCC? And what does it even mean?
385 if (ObjCTypes.LongTy != CGM.getTypes().ConvertType(CGM.getContext().IntTy))
386 ObjCABI = 2;
387
388 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000389}
390
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000391/// GetClass - Return a reference to the class for the given interface
392/// decl.
393llvm::Value *CGObjCMac::GetClass(llvm::IRBuilder<> &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000394 const ObjCInterfaceDecl *ID) {
395 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000396}
397
398/// GetSelector - Return the pointer to the unique'd string for this selector.
399llvm::Value *CGObjCMac::GetSelector(llvm::IRBuilder<> &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000400 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000401}
402
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000403/// Generate a constant CFString object.
404/*
405 struct __builtin_CFString {
406 const int *isa; // point to __CFConstantStringClassReference
407 int flags;
408 const char *str;
409 long length;
410 };
411*/
412
413llvm::Constant *CGObjCMac::GenerateConstantString(const std::string &String) {
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000414 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000415}
416
417/// Generates a message send where the super is the receiver. This is
418/// a message send to self with special delivery semantics indicating
419/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000420CodeGen::RValue
421CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
422 const ObjCMessageExpr *E,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000423 const ObjCInterfaceDecl *Class,
424 llvm::Value *Receiver,
425 bool IsClassMessage) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000426 // Create and init a super structure; this is a (receiver, class)
427 // pair we will pass to objc_msgSendSuper.
428 llvm::Value *ObjCSuper =
429 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
430 llvm::Value *ReceiverAsObject =
431 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
432 CGF.Builder.CreateStore(ReceiverAsObject,
433 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000434
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000435 // If this is a class message the metaclass is passed as the target.
436 llvm::Value *Target;
437 if (IsClassMessage) {
438 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
439 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
440 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
441 Target = Super;
442 } else {
443 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
444 }
445 CGF.Builder.CreateStore(Target,
446 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
447
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000448 return EmitMessageSend(CGF, E, ObjCSuper, true);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000449}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000450
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000451/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000452CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
453 const ObjCMessageExpr *E,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000454 llvm::Value *Receiver,
455 bool IsClassMessage) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000456 llvm::Value *Arg0 =
457 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
458 return EmitMessageSend(CGF, E, Arg0, false);
459}
460
461CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
462 const ObjCMessageExpr *E,
463 llvm::Value *Arg0,
464 bool IsSuper) {
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000465 llvm::Value *Args[2];
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000466 Args[0] = Arg0;
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000467 Args[1] = EmitSelector(CGF.Builder, E->getSelector());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000468
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000469 // FIXME: This is a hack, we are implicitly coordinating with
470 // EmitCallExprExt, which will move the return type to the first
471 // parameter and set the structure return flag. See
472 // getMessageSendFn().
473
474
475 const llvm::Type *ReturnTy = CGM.getTypes().ConvertType(E->getType());
476 return CGF.EmitCallExprExt(ObjCTypes.getMessageSendFn(IsSuper, ReturnTy),
477 E->getType(),
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000478 E->arg_begin(),
479 E->arg_end(),
480 Args, 2);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000481}
482
483llvm::Value *CGObjCMac::GenerateProtocolRef(llvm::IRBuilder<> &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000484 const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000485 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
486 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000487}
488
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000489/*
490 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
491 struct _objc_protocol {
492 struct _objc_protocol_extension *isa;
493 char *protocol_name;
494 struct _objc_protocol_list *protocol_list;
495 struct _objc__method_prototype_list *instance_methods;
496 struct _objc__method_prototype_list *class_methods
497 };
498
499 See EmitProtocolExtension().
500*/
501void CGObjCMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000502 // FIXME: I don't understand why gcc generates this, or where it is
503 // resolved. Investigate. Its also wasteful to look this up over and
504 // over.
505 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
506
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000507 const char *ProtocolName = PD->getName();
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000508
509 // Construct method lists.
510 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
511 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
512 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
513 e = PD->instmeth_end(); i != e; ++i) {
514 ObjCMethodDecl *MD = *i;
515 llvm::Constant *C = GetMethodDescriptionConstant(MD);
516 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
517 OptInstanceMethods.push_back(C);
518 } else {
519 InstanceMethods.push_back(C);
520 }
521 }
522
523 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
524 e = PD->classmeth_end(); i != e; ++i) {
525 ObjCMethodDecl *MD = *i;
526 llvm::Constant *C = GetMethodDescriptionConstant(MD);
527 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
528 OptClassMethods.push_back(C);
529 } else {
530 ClassMethods.push_back(C);
531 }
532 }
533
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000534 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000535 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000536 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +0000537 Values[2] =
538 EmitProtocolList(std::string("\01L_OBJC_PROTOCOL_REFS_")+PD->getName(),
539 PD->protocol_begin(),
540 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000541 Values[3] =
542 EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_")
543 + PD->getName(),
544 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
545 InstanceMethods);
546 Values[4] =
547 EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_CLASS_METHODS_")
548 + PD->getName(),
549 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
550 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000551 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
552 Values);
553
554 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
555 if (Entry) {
556 // Already created, just update the initializer
557 Entry->setInitializer(Init);
558 } else {
559 Entry =
560 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
561 llvm::GlobalValue::InternalLinkage,
562 Init,
563 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
564 &CGM.getModule());
565 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
566 UsedGlobals.push_back(Entry);
567 // FIXME: Is this necessary? Why only for protocol?
568 Entry->setAlignment(4);
569 }
570}
571
572llvm::GlobalVariable *CGObjCMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
573 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
574
575 if (!Entry) {
576 std::vector<llvm::Constant*> Values(5);
577 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
578 Values[1] = GetClassName(PD->getIdentifier());
579 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
580 Values[3] = Values[4] =
581 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
582 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
583 Values);
584
585 Entry =
586 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
587 llvm::GlobalValue::InternalLinkage,
588 Init,
589 std::string("\01L_OBJC_PROTOCOL_")+PD->getName(),
590 &CGM.getModule());
591 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
592 UsedGlobals.push_back(Entry);
593 // FIXME: Is this necessary? Why only for protocol?
594 Entry->setAlignment(4);
595 }
596
597 return Entry;
598}
599
600/*
601 struct _objc_protocol_extension {
602 uint32_t size;
603 struct objc_method_description_list *optional_instance_methods;
604 struct objc_method_description_list *optional_class_methods;
605 struct objc_property_list *instance_properties;
606 };
607*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000608llvm::Constant *
609CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
610 const ConstantVector &OptInstanceMethods,
611 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000612 uint64_t Size =
613 CGM.getTargetData().getABITypeSize(ObjCTypes.ProtocolExtensionTy);
614 std::vector<llvm::Constant*> Values(4);
615 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000616 Values[1] =
617 EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_")
618 + PD->getName(),
619 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
620 OptInstanceMethods);
621 Values[2] =
622 EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_")
623 + PD->getName(),
624 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
625 OptClassMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000626 Values[3] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_PROTO_LIST_") +
627 PD->getName(),
Daniel Dunbarc56f34a2008-08-28 04:38:10 +0000628 0,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000629 PD->classprop_begin(),
630 PD->classprop_end());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000631
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000632 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000633 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
634 Values[3]->isNullValue())
635 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
636
637 llvm::Constant *Init =
638 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
639 llvm::GlobalVariable *GV =
640 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
641 llvm::GlobalValue::InternalLinkage,
642 Init,
643 (std::string("\01L_OBJC_PROTOCOLEXT_") +
644 PD->getName()),
645 &CGM.getModule());
646 // No special section, but goes in llvm.used
647 UsedGlobals.push_back(GV);
648
649 return GV;
650}
651
652/*
653 struct objc_protocol_list {
654 struct objc_protocol_list *next;
655 long count;
656 Protocol *list[];
657 };
658*/
Daniel Dunbardbc933702008-08-21 21:57:41 +0000659llvm::Constant *
660CGObjCMac::EmitProtocolList(const std::string &Name,
661 ObjCProtocolDecl::protocol_iterator begin,
662 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000663 std::vector<llvm::Constant*> ProtocolRefs;
664
Daniel Dunbardbc933702008-08-21 21:57:41 +0000665 for (; begin != end; ++begin)
666 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000667
668 // Just return null for empty protocol lists
669 if (ProtocolRefs.empty())
670 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
671
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000672 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000673 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
674
675 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000676 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000677 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
678 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
679 Values[2] =
680 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
681 ProtocolRefs.size()),
682 ProtocolRefs);
683
684 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
685 llvm::GlobalVariable *GV =
686 new llvm::GlobalVariable(Init->getType(), false,
687 llvm::GlobalValue::InternalLinkage,
688 Init,
Daniel Dunbardbc933702008-08-21 21:57:41 +0000689 Name,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000690 &CGM.getModule());
691 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
692 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
693}
694
695/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000696 struct _objc_property {
697 const char * const name;
698 const char * const attributes;
699 };
700
701 struct _objc_property_list {
702 uint32_t entsize; // sizeof (struct _objc_property)
703 uint32_t prop_count;
704 struct _objc_property[prop_count];
705 };
706*/
707llvm::Constant *CGObjCMac::EmitPropertyList(const std::string &Name,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +0000708 const Decl *Container,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000709 ObjCPropertyDecl * const *begin,
710 ObjCPropertyDecl * const *end) {
711 std::vector<llvm::Constant*> Properties, Prop(2);
712 for (; begin != end; ++begin) {
713 const ObjCPropertyDecl *PD = *begin;
714 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +0000715 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000716 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
717 Prop));
718 }
719
720 // Return null for empty list.
721 if (Properties.empty())
722 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
723
724 unsigned PropertySize =
725 CGM.getTargetData().getABITypeSize(ObjCTypes.PropertyTy);
726 std::vector<llvm::Constant*> Values(3);
727 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
728 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
729 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
730 Properties.size());
731 Values[2] = llvm::ConstantArray::get(AT, Properties);
732 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
733
734 llvm::GlobalVariable *GV =
735 new llvm::GlobalVariable(Init->getType(), false,
736 llvm::GlobalValue::InternalLinkage,
737 Init,
738 Name,
739 &CGM.getModule());
740 // No special section on property lists?
741 UsedGlobals.push_back(GV);
742 return llvm::ConstantExpr::getBitCast(GV,
743 ObjCTypes.PropertyListPtrTy);
744
745}
746
747/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000748 struct objc_method_description_list {
749 int count;
750 struct objc_method_description list[];
751 };
752*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000753llvm::Constant *
754CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
755 std::vector<llvm::Constant*> Desc(2);
756 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
757 ObjCTypes.SelectorPtrTy);
758 Desc[1] = GetMethodVarType(MD);
759 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
760 Desc);
761}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000762
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000763llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
764 const char *Section,
765 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000766 // Return null for empty list.
767 if (Methods.empty())
768 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
769
770 std::vector<llvm::Constant*> Values(2);
771 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
772 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
773 Methods.size());
774 Values[1] = llvm::ConstantArray::get(AT, Methods);
775 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
776
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000777 llvm::GlobalVariable *GV =
778 new llvm::GlobalVariable(Init->getType(), false,
779 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000780 Init, Name, &CGM.getModule());
781 GV->setSection(Section);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000782 UsedGlobals.push_back(GV);
783 return llvm::ConstantExpr::getBitCast(GV,
784 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000785}
786
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000787/*
788 struct _objc_category {
789 char *category_name;
790 char *class_name;
791 struct _objc_method_list *instance_methods;
792 struct _objc_method_list *class_methods;
793 struct _objc_protocol_list *protocols;
794 uint32_t size; // <rdar://4585769>
795 struct _objc_property_list *instance_properties;
796 };
797 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000798void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000799 unsigned Size = CGM.getTargetData().getABITypeSize(ObjCTypes.CategoryTy);
800
Daniel Dunbar86e2f402008-08-26 23:03:11 +0000801 // FIXME: This is poor design, the OCD should have a pointer to the
802 // category decl. Additionally, note that Category can be null for
803 // the @implementation w/o an @interface case. Sema should just
804 // create one for us as it does for @implementation so everyone else
805 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000806 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +0000807 const ObjCCategoryDecl *Category =
808 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000809 std::string ExtName(std::string(Interface->getName()) +
810 "_" +
811 OCD->getName());
812
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000813 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
814 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
815 e = OCD->instmeth_end(); i != e; ++i) {
816 // Instance methods should always be defined.
817 InstanceMethods.push_back(GetMethodConstant(*i));
818 }
819 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
820 e = OCD->classmeth_end(); i != e; ++i) {
821 // Class methods should always be defined.
822 ClassMethods.push_back(GetMethodConstant(*i));
823 }
824
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000825 std::vector<llvm::Constant*> Values(7);
826 Values[0] = GetClassName(OCD->getIdentifier());
827 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000828 Values[2] =
829 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
830 ExtName,
831 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000832 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000833 Values[3] =
834 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
835 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000836 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000837 if (Category) {
838 Values[4] =
839 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
840 Category->protocol_begin(),
841 Category->protocol_end());
842 } else {
843 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
844 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000845 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +0000846
847 // If there is no category @interface then there can be no properties.
848 if (Category) {
849 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +0000850 OCD,
Daniel Dunbar86e2f402008-08-26 23:03:11 +0000851 Category->classprop_begin(),
852 Category->classprop_end());
853 } else {
854 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
855 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000856
857 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
858 Values);
859
860 llvm::GlobalVariable *GV =
861 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
862 llvm::GlobalValue::InternalLinkage,
863 Init,
864 std::string("\01L_OBJC_CATEGORY_")+ExtName,
865 &CGM.getModule());
866 GV->setSection("__OBJC,__category,regular,no_dead_strip");
867 UsedGlobals.push_back(GV);
868 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000869}
870
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000871// FIXME: Get from somewhere?
872enum ClassFlags {
873 eClassFlags_Factory = 0x00001,
874 eClassFlags_Meta = 0x00002,
875 // <rdr://5142207>
876 eClassFlags_HasCXXStructors = 0x02000,
877 eClassFlags_Hidden = 0x20000,
878 eClassFlags_ABI2_Hidden = 0x00010,
879 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
880};
881
882// <rdr://5142207&4705298&4843145>
883static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
884 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
885 // FIXME: Support -fvisibility
886 switch (attr->getVisibility()) {
887 default:
888 assert(0 && "Unknown visibility");
889 return false;
890 case VisibilityAttr::DefaultVisibility:
891 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
892 return false;
893 case VisibilityAttr::HiddenVisibility:
894 return true;
895 }
896 } else {
897 return false; // FIXME: Support -fvisibility
898 }
899}
900
901/*
902 struct _objc_class {
903 Class isa;
904 Class super_class;
905 const char *name;
906 long version;
907 long info;
908 long instance_size;
909 struct _objc_ivar_list *ivars;
910 struct _objc_method_list *methods;
911 struct _objc_cache *cache;
912 struct _objc_protocol_list *protocols;
913 // Objective-C 1.0 extensions (<rdr://4585769>)
914 const char *ivar_layout;
915 struct _objc_class_ext *ext;
916 };
917
918 See EmitClassExtension();
919 */
920void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000921 DefinedSymbols.insert(ID->getIdentifier());
922
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000923 const char *ClassName = ID->getName();
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000924 // FIXME: Gross
925 ObjCInterfaceDecl *Interface =
926 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +0000927 llvm::Constant *Protocols =
928 EmitProtocolList(std::string("\01L_OBJC_CLASS_PROTOCOLS_") + ID->getName(),
929 Interface->protocol_begin(),
930 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000931 const llvm::Type *InterfaceTy =
932 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
933 unsigned Flags = eClassFlags_Factory;
934 unsigned Size = CGM.getTargetData().getABITypeSize(InterfaceTy);
935
936 // FIXME: Set CXX-structors flag.
937 if (IsClassHidden(ID->getClassInterface()))
938 Flags |= eClassFlags_Hidden;
939
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000940 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
941 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
942 e = ID->instmeth_end(); i != e; ++i) {
943 // Instance methods should always be defined.
944 InstanceMethods.push_back(GetMethodConstant(*i));
945 }
946 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
947 e = ID->classmeth_end(); i != e; ++i) {
948 // Class methods should always be defined.
949 ClassMethods.push_back(GetMethodConstant(*i));
950 }
951
952 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
953 e = ID->propimpl_end(); i != e; ++i) {
954 ObjCPropertyImplDecl *PID = *i;
955
956 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
957 ObjCPropertyDecl *PD = PID->getPropertyDecl();
958
959 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
960 if (llvm::Constant *C = GetMethodConstant(MD))
961 InstanceMethods.push_back(C);
962 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
963 if (llvm::Constant *C = GetMethodConstant(MD))
964 InstanceMethods.push_back(C);
965 }
966 }
967
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000968 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000969 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000970 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000971 // Record a reference to the super class.
972 LazySymbols.insert(Super->getIdentifier());
973
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000974 Values[ 1] =
975 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
976 ObjCTypes.ClassPtrTy);
977 } else {
978 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
979 }
980 Values[ 2] = GetClassName(ID->getIdentifier());
981 // Version is always 0.
982 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
983 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
984 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
985 Values[ 6] = EmitIvarList(ID, false, InterfaceTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000986 Values[ 7] =
987 EmitMethodList(std::string("\01L_OBJC_INSTANCE_METHODS_") + ID->getName(),
988 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000989 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000990 // cache is always NULL.
991 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
992 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000993 // FIXME: Set ivar_layout
994 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000995 Values[11] = EmitClassExtension(ID);
996 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
997 Values);
998
999 llvm::GlobalVariable *GV =
1000 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1001 llvm::GlobalValue::InternalLinkage,
1002 Init,
1003 std::string("\01L_OBJC_CLASS_")+ClassName,
1004 &CGM.getModule());
1005 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1006 UsedGlobals.push_back(GV);
1007 // FIXME: Why?
1008 GV->setAlignment(32);
1009 DefinedClasses.push_back(GV);
1010}
1011
1012llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1013 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001014 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001015 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001016 const char *ClassName = ID->getName();
1017 unsigned Flags = eClassFlags_Meta;
1018 unsigned Size = CGM.getTargetData().getABITypeSize(ObjCTypes.ClassTy);
1019
1020 if (IsClassHidden(ID->getClassInterface()))
1021 Flags |= eClassFlags_Hidden;
1022
1023 std::vector<llvm::Constant*> Values(12);
1024 // The isa for the metaclass is the root of the hierarchy.
1025 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1026 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1027 Root = Super;
1028 Values[ 0] =
1029 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1030 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001031 // The super class for the metaclass is emitted as the name of the
1032 // super class. The runtime fixes this up to point to the
1033 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001034 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1035 Values[ 1] =
1036 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1037 ObjCTypes.ClassPtrTy);
1038 } else {
1039 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1040 }
1041 Values[ 2] = GetClassName(ID->getIdentifier());
1042 // Version is always 0.
1043 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1044 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1045 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
1046 Values[ 6] = EmitIvarList(ID, true, InterfaceTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001047 Values[ 7] =
1048 EmitMethodList(std::string("\01L_OBJC_CLASS_METHODS_") + ID->getName(),
1049 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001050 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001051 // cache is always NULL.
1052 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1053 Values[ 9] = Protocols;
1054 // ivar_layout for metaclass is always NULL.
1055 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1056 // The class extension is always unused for metaclasses.
1057 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1058 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1059 Values);
1060
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001061 std::string Name("\01L_OBJC_METACLASS_");
1062 Name += ClassName;
1063
1064 // Check for a forward reference.
1065 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1066 if (GV) {
1067 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1068 "Forward metaclass reference has incorrect type.");
1069 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1070 GV->setInitializer(Init);
1071 } else {
1072 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1073 llvm::GlobalValue::InternalLinkage,
1074 Init, Name,
1075 &CGM.getModule());
1076 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001077 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1078 UsedGlobals.push_back(GV);
1079 // FIXME: Why?
1080 GV->setAlignment(32);
1081
1082 return GV;
1083}
1084
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001085llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
1086 std::string Name("\01L_OBJC_METACLASS_");
1087 Name += ID->getName();
1088
1089 // FIXME: Should we look these up somewhere other than the
1090 // module. Its a bit silly since we only generate these while
1091 // processing an implementation, so exactly one pointer would work
1092 // if know when we entered/exitted an implementation block.
1093
1094 // Check for an existing forward reference.
1095 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name)) {
1096 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1097 "Forward metaclass reference has incorrect type.");
1098 return GV;
1099 } else {
1100 // Generate as an external reference to keep a consistent
1101 // module. This will be patched up when we emit the metaclass.
1102 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1103 llvm::GlobalValue::ExternalLinkage,
1104 0,
1105 Name,
1106 &CGM.getModule());
1107 }
1108}
1109
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001110/*
1111 struct objc_class_ext {
1112 uint32_t size;
1113 const char *weak_ivar_layout;
1114 struct _objc_property_list *properties;
1115 };
1116*/
1117llvm::Constant *
1118CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1119 uint64_t Size =
1120 CGM.getTargetData().getABITypeSize(ObjCTypes.ClassExtensionTy);
1121
1122 std::vector<llvm::Constant*> Values(3);
1123 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001124 // FIXME: Output weak_ivar_layout string.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001125 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001126 Values[2] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") +
1127 ID->getName(),
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001128 ID,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001129 ID->getClassInterface()->classprop_begin(),
1130 ID->getClassInterface()->classprop_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001131
1132 // Return null if no extension bits are used.
1133 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1134 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1135
1136 llvm::Constant *Init =
1137 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1138 llvm::GlobalVariable *GV =
1139 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1140 llvm::GlobalValue::InternalLinkage,
1141 Init,
1142 (std::string("\01L_OBJC_CLASSEXT_") +
1143 ID->getName()),
1144 &CGM.getModule());
1145 // No special section, but goes in llvm.used
1146 UsedGlobals.push_back(GV);
1147
1148 return GV;
1149}
1150
1151/*
1152 struct objc_ivar {
1153 char *ivar_name;
1154 char *ivar_type;
1155 int ivar_offset;
1156 };
1157
1158 struct objc_ivar_list {
1159 int ivar_count;
1160 struct objc_ivar list[count];
1161 };
1162 */
1163llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
1164 bool ForClass,
1165 const llvm::Type *InterfaceTy) {
1166 std::vector<llvm::Constant*> Ivars, Ivar(3);
1167
1168 // When emitting the root class GCC emits ivar entries for the
1169 // actual class structure. It is not clear if we need to follow this
1170 // behavior; for now lets try and get away with not doing it. If so,
1171 // the cleanest solution would be to make up an ObjCInterfaceDecl
1172 // for the class.
1173 if (ForClass)
1174 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1175
1176 const llvm::StructLayout *Layout =
1177 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
1178 for (ObjCInterfaceDecl::ivar_iterator
1179 i = ID->getClassInterface()->ivar_begin(),
1180 e = ID->getClassInterface()->ivar_end(); i != e; ++i) {
1181 ObjCIvarDecl *V = *i;
1182 unsigned Offset =
1183 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(V));
1184 std::string TypeStr;
1185 llvm::SmallVector<const RecordType *, 8> EncodingRecordTypes;
1186 Ivar[0] = GetMethodVarName(V->getIdentifier());
1187 CGM.getContext().getObjCEncodingForType(V->getType(), TypeStr,
1188 EncodingRecordTypes);
1189 Ivar[1] = GetMethodVarType(TypeStr);
1190 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
1191 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy,
1192 Ivar));
1193 }
1194
1195 // Return null for empty list.
1196 if (Ivars.empty())
1197 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1198
1199 std::vector<llvm::Constant*> Values(2);
1200 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1201 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1202 Ivars.size());
1203 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1204 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1205
1206 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1207 "\01L_OBJC_INSTANCE_VARIABLES_");
1208 llvm::GlobalVariable *GV =
1209 new llvm::GlobalVariable(Init->getType(), false,
1210 llvm::GlobalValue::InternalLinkage,
1211 Init,
1212 std::string(Prefix) + ID->getName(),
1213 &CGM.getModule());
1214 if (ForClass) {
1215 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1216 // FIXME: Why is this only here?
1217 GV->setAlignment(32);
1218 } else {
1219 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1220 }
1221 UsedGlobals.push_back(GV);
1222 return llvm::ConstantExpr::getBitCast(GV,
1223 ObjCTypes.IvarListPtrTy);
1224}
1225
1226/*
1227 struct objc_method {
1228 SEL method_name;
1229 char *method_types;
1230 void *method;
1231 };
1232
1233 struct objc_method_list {
1234 struct objc_method_list *obsolete;
1235 int count;
1236 struct objc_method methods_list[count];
1237 };
1238*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001239
1240/// GetMethodConstant - Return a struct objc_method constant for the
1241/// given method if it has been defined. The result is null if the
1242/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001243llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001244 // FIXME: Use DenseMap::lookup
1245 llvm::Function *Fn = MethodDefinitions[MD];
1246 if (!Fn)
1247 return 0;
1248
1249 std::vector<llvm::Constant*> Method(3);
1250 Method[0] =
1251 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1252 ObjCTypes.SelectorPtrTy);
1253 Method[1] = GetMethodVarType(MD);
1254 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1255 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1256}
1257
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001258llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1259 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001260 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001261 // Return null for empty list.
1262 if (Methods.empty())
1263 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1264
1265 std::vector<llvm::Constant*> Values(3);
1266 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1267 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1268 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1269 Methods.size());
1270 Values[2] = llvm::ConstantArray::get(AT, Methods);
1271 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1272
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001273 llvm::GlobalVariable *GV =
1274 new llvm::GlobalVariable(Init->getType(), false,
1275 llvm::GlobalValue::InternalLinkage,
1276 Init,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001277 Name,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001278 &CGM.getModule());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001279 GV->setSection(Section);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001280 UsedGlobals.push_back(GV);
1281 return llvm::ConstantExpr::getBitCast(GV,
1282 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001283}
1284
1285llvm::Function *CGObjCMac::GenerateMethod(const ObjCMethodDecl *OMD) {
1286 const llvm::Type *ReturnTy =
1287 CGM.getTypes().ConvertReturnType(OMD->getResultType());
1288 const llvm::Type *SelfTy =
1289 CGM.getTypes().ConvertType(OMD->getSelfDecl()->getType());
1290
1291 std::vector<const llvm::Type*> ArgTys;
1292 ArgTys.reserve(1 + 2 + OMD->param_size());
1293
1294 // FIXME: This is not something we should have to be dealing with
1295 // here.
1296 bool useStructRet =
1297 CodeGen::CodeGenFunction::hasAggregateLLVMType(OMD->getResultType());
1298 if (useStructRet) {
1299 ArgTys.push_back(llvm::PointerType::getUnqual(ReturnTy));
1300 ReturnTy = llvm::Type::VoidTy;
1301 }
1302
1303 // Implicit arguments
1304 ArgTys.push_back(SelfTy);
1305 ArgTys.push_back(ObjCTypes.SelectorPtrTy);
1306
1307 for (ObjCMethodDecl::param_const_iterator
1308 i = OMD->param_begin(), e = OMD->param_end();
1309 i != e; ++i) {
1310 const llvm::Type *Ty = CGM.getTypes().ConvertType((*i)->getType());
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001311 if (Ty->isSingleValueType()) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001312 ArgTys.push_back(Ty);
1313 } else {
1314 ArgTys.push_back(llvm::PointerType::getUnqual(Ty));
1315 }
1316 }
1317
1318 std::string Name;
1319 GetNameForMethod(OMD, Name);
1320
1321 llvm::Function *Method =
1322 llvm::Function::Create(llvm::FunctionType::get(ReturnTy,
1323 ArgTys,
1324 OMD->isVariadic()),
1325 llvm::GlobalValue::InternalLinkage,
1326 Name,
1327 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001328 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001329
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001330 unsigned Offset = 3; // Return plus self and selector implicit args.
1331 if (useStructRet) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001332 Method->addParamAttr(1, llvm::ParamAttr::StructRet);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001333 ++Offset;
1334 }
1335
1336 // FIXME: This is horrible, we need to be reusing the machinery in
1337 // CodeGenModule.cpp (SetFunctionAttributes).
1338 for (ObjCMethodDecl::param_const_iterator
1339 i = OMD->param_begin(), e = OMD->param_end();
1340 i != e; ++i, ++Offset) {
1341 const llvm::Type *Ty = CGM.getTypes().ConvertType((*i)->getType());
1342 if (!Ty->isSingleValueType())
1343 Method->addParamAttr(Offset, llvm::ParamAttr::ByVal);
1344 }
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001345
1346 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001347}
1348
1349llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001350 // Abuse this interface function as a place to finalize.
1351 FinishModule();
1352
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001353 return NULL;
1354}
1355
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001356/* *** Private Interface *** */
1357
1358/// EmitImageInfo - Emit the image info marker used to encode some module
1359/// level information.
1360///
1361/// See: <rdr://4810609&4810587&4810587>
1362/// struct IMAGE_INFO {
1363/// unsigned version;
1364/// unsigned flags;
1365/// };
1366enum ImageInfoFlags {
1367 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
1368 eImageInfo_GarbageCollected = (1 << 1),
1369 eImageInfo_GCOnly = (1 << 2)
1370};
1371
1372void CGObjCMac::EmitImageInfo() {
1373 unsigned version = 0; // Version is unused?
1374 unsigned flags = 0;
1375
1376 // FIXME: Fix and continue?
1377 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
1378 flags |= eImageInfo_GarbageCollected;
1379 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
1380 flags |= eImageInfo_GCOnly;
1381
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001382 // Emitted as int[2];
1383 llvm::Constant *values[2] = {
1384 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
1385 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
1386 };
1387 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001388 llvm::GlobalVariable *GV =
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001389 new llvm::GlobalVariable(AT, true,
1390 llvm::GlobalValue::InternalLinkage,
1391 llvm::ConstantArray::get(AT, values, 2),
1392 "\01L_OBJC_IMAGE_INFO",
1393 &CGM.getModule());
1394
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001395 if (ObjCABI == 1) {
1396 GV->setSection("__OBJC, __image_info,regular");
1397 } else {
1398 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
1399 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001400
1401 UsedGlobals.push_back(GV);
1402}
1403
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001404
1405// struct objc_module {
1406// unsigned long version;
1407// unsigned long size;
1408// const char *name;
1409// Symtab symtab;
1410// };
1411
1412// FIXME: Get from somewhere
1413static const int ModuleVersion = 7;
1414
1415void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001416 uint64_t Size = CGM.getTargetData().getABITypeSize(ObjCTypes.ModuleTy);
1417
1418 std::vector<llvm::Constant*> Values(4);
1419 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
1420 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001421 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001422 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001423 Values[3] = EmitModuleSymbols();
1424
1425 llvm::GlobalVariable *GV =
1426 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
1427 llvm::GlobalValue::InternalLinkage,
1428 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
1429 Values),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001430 "\01L_OBJC_MODULES",
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001431 &CGM.getModule());
1432 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
1433 UsedGlobals.push_back(GV);
1434}
1435
1436llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001437 unsigned NumClasses = DefinedClasses.size();
1438 unsigned NumCategories = DefinedCategories.size();
1439
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001440 // Return null if no symbols were defined.
1441 if (!NumClasses && !NumCategories)
1442 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
1443
1444 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001445 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1446 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
1447 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
1448 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
1449
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001450 // The runtime expects exactly the list of defined classes followed
1451 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001452 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001453 for (unsigned i=0; i<NumClasses; i++)
1454 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
1455 ObjCTypes.Int8PtrTy);
1456 for (unsigned i=0; i<NumCategories; i++)
1457 Symbols[NumClasses + i] =
1458 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
1459 ObjCTypes.Int8PtrTy);
1460
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001461 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001462 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001463 NumClasses + NumCategories),
1464 Symbols);
1465
1466 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1467
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001468 llvm::GlobalVariable *GV =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001469 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001470 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001471 Init,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001472 "\01L_OBJC_SYMBOLS",
1473 &CGM.getModule());
1474 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
1475 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001476 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
1477}
1478
1479llvm::Value *CGObjCMac::EmitClassRef(llvm::IRBuilder<> &Builder,
1480 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001481 LazySymbols.insert(ID->getIdentifier());
1482
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001483 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
1484
1485 if (!Entry) {
1486 llvm::Constant *Casted =
1487 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
1488 ObjCTypes.ClassPtrTy);
1489 Entry =
1490 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
1491 llvm::GlobalValue::InternalLinkage,
1492 Casted, "\01L_OBJC_CLASS_REFERENCES_",
1493 &CGM.getModule());
1494 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
1495 UsedGlobals.push_back(Entry);
1496 }
1497
1498 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001499}
1500
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001501llvm::Value *CGObjCMac::EmitSelector(llvm::IRBuilder<> &Builder, Selector Sel) {
1502 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
1503
1504 if (!Entry) {
1505 llvm::Constant *Casted =
1506 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
1507 ObjCTypes.SelectorPtrTy);
1508 Entry =
1509 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
1510 llvm::GlobalValue::InternalLinkage,
1511 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
1512 &CGM.getModule());
1513 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
1514 UsedGlobals.push_back(Entry);
1515 }
1516
1517 return Builder.CreateLoad(Entry, false, "tmp");
1518}
1519
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001520llvm::Constant *CGObjCMac::GetClassName(IdentifierInfo *Ident) {
1521 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001522
1523 if (!Entry) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001524 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001525 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001526 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001527 llvm::GlobalValue::InternalLinkage,
1528 C, "\01L_OBJC_CLASS_NAME_",
1529 &CGM.getModule());
1530 Entry->setSection("__TEXT,__cstring,cstring_literals");
1531 UsedGlobals.push_back(Entry);
1532 }
1533
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001534 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001535}
1536
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001537llvm::Constant *CGObjCMac::GetMethodVarName(Selector Sel) {
1538 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
1539
1540 if (!Entry) {
1541 llvm::Constant *C = llvm::ConstantArray::get(Sel.getName());
1542 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001543 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001544 llvm::GlobalValue::InternalLinkage,
1545 C, "\01L_OBJC_METH_VAR_NAME_",
1546 &CGM.getModule());
1547 Entry->setSection("__TEXT,__cstring,cstring_literals");
1548 UsedGlobals.push_back(Entry);
1549 }
1550
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001551 return getConstantGEP(Entry, 0, 0);
1552}
1553
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001554// FIXME: Merge into a single cstring creation function.
1555llvm::Constant *CGObjCMac::GetMethodVarName(IdentifierInfo *ID) {
1556 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
1557}
1558
1559// FIXME: Merge into a single cstring creation function.
1560llvm::Constant *CGObjCMac::GetMethodVarName(const std::string &Name) {
1561 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
1562}
1563
1564llvm::Constant *CGObjCMac::GetMethodVarType(const std::string &Name) {
1565 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001566
1567 if (!Entry) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001568 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001569 Entry =
1570 new llvm::GlobalVariable(C->getType(), false,
1571 llvm::GlobalValue::InternalLinkage,
1572 C, "\01L_OBJC_METH_VAR_TYPE_",
1573 &CGM.getModule());
1574 Entry->setSection("__TEXT,__cstring,cstring_literals");
1575 UsedGlobals.push_back(Entry);
1576 }
1577
1578 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001579}
1580
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001581// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001582llvm::Constant *CGObjCMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001583 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001584 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
1585 TypeStr);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001586 return GetMethodVarType(TypeStr);
1587}
1588
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001589// FIXME: Merge into a single cstring creation function.
1590llvm::Constant *CGObjCMac::GetPropertyName(IdentifierInfo *Ident) {
1591 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
1592
1593 if (!Entry) {
1594 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
1595 Entry =
1596 new llvm::GlobalVariable(C->getType(), false,
1597 llvm::GlobalValue::InternalLinkage,
1598 C, "\01L_OBJC_PROP_NAME_ATTR_",
1599 &CGM.getModule());
1600 Entry->setSection("__TEXT,__cstring,cstring_literals");
1601 UsedGlobals.push_back(Entry);
1602 }
1603
1604 return getConstantGEP(Entry, 0, 0);
1605}
1606
1607// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001608// FIXME: This Decl should be more precise.
1609llvm::Constant *CGObjCMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
1610 const Decl *Container) {
1611 std::string TypeStr;
1612 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001613 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
1614}
1615
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001616void CGObjCMac::GetNameForMethod(const ObjCMethodDecl *D,
1617 std::string &NameOut) {
1618 // FIXME: Find the mangling GCC uses.
1619 std::stringstream s;
1620 s << (D->isInstance() ? "-" : "+");
1621 s << "[";
1622 s << D->getClassInterface()->getName();
1623 s << " ";
1624 s << D->getSelector().getName();
1625 s << "]";
1626 NameOut = s.str();
1627}
1628
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001629void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001630 EmitModuleInfo();
1631
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001632 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001633
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001634 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001635 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001636 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001637 }
1638
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001639 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001640 llvm::GlobalValue *GV =
1641 new llvm::GlobalVariable(AT, false,
1642 llvm::GlobalValue::AppendingLinkage,
1643 llvm::ConstantArray::get(AT, Used),
1644 "llvm.used",
1645 &CGM.getModule());
1646
1647 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001648
1649 // Add assembler directives to add lazy undefined symbol references
1650 // for classes which are referenced but not defined. This is
1651 // important for correct linker interaction.
1652
1653 // FIXME: Uh, this isn't particularly portable.
1654 std::stringstream s;
1655 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
1656 e = LazySymbols.end(); i != e; ++i) {
1657 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
1658 }
1659 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
1660 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001661 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001662 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
1663 }
1664 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001665}
1666
1667/* *** */
1668
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001669ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Daniel Dunbar3e9df992008-08-23 18:37:06 +00001670 : CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001671{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001672 CodeGen::CodeGenTypes &Types = CGM.getTypes();
1673 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001674
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001675 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001676 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001677 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001678 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
1679
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001680 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
1681 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001682
1683 // FIXME: It would be nice to unify this with the opaque type, so
1684 // that the IR comes out a bit cleaner.
1685 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
1686 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001687
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001688 MethodDescriptionTy =
1689 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001690 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001691 NULL);
1692 CGM.getModule().addTypeName("struct._objc_method_description",
1693 MethodDescriptionTy);
1694
1695 MethodDescriptionListTy =
1696 llvm::StructType::get(IntTy,
1697 llvm::ArrayType::get(MethodDescriptionTy, 0),
1698 NULL);
1699 CGM.getModule().addTypeName("struct._objc_method_description_list",
1700 MethodDescriptionListTy);
1701 MethodDescriptionListPtrTy =
1702 llvm::PointerType::getUnqual(MethodDescriptionListTy);
1703
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001704 PropertyTy = llvm::StructType::get(Int8PtrTy,
1705 Int8PtrTy,
1706 NULL);
1707 CGM.getModule().addTypeName("struct._objc_property",
1708 PropertyTy);
1709
1710 PropertyListTy = llvm::StructType::get(IntTy,
1711 IntTy,
1712 llvm::ArrayType::get(PropertyTy, 0),
1713 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001714 CGM.getModule().addTypeName("struct._objc_property_list",
1715 PropertyListTy);
1716 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
1717
1718 // Protocol description structures
1719
1720 ProtocolExtensionTy =
1721 llvm::StructType::get(Types.ConvertType(Ctx.IntTy),
1722 llvm::PointerType::getUnqual(MethodDescriptionListTy),
1723 llvm::PointerType::getUnqual(MethodDescriptionListTy),
1724 PropertyListPtrTy,
1725 NULL);
1726 CGM.getModule().addTypeName("struct._objc_protocol_extension",
1727 ProtocolExtensionTy);
1728 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
1729
1730 // Handle recursive construction of Protocl and ProtocolList types
1731
1732 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
1733 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
1734
1735 T = llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
1736 LongTy,
1737 llvm::ArrayType::get(ProtocolTyHolder, 0),
1738 NULL);
1739 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
1740
1741 T = llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolExtensionTy),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001742 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001743 llvm::PointerType::getUnqual(ProtocolListTyHolder),
1744 MethodDescriptionListPtrTy,
1745 MethodDescriptionListPtrTy,
1746 NULL);
1747 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
1748
1749 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
1750 CGM.getModule().addTypeName("struct._objc_protocol_list",
1751 ProtocolListTy);
1752 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
1753
1754 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
1755 CGM.getModule().addTypeName("struct.__objc_protocol", ProtocolTy);
1756 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001757
1758 // Class description structures
1759
1760 IvarTy = llvm::StructType::get(Int8PtrTy,
1761 Int8PtrTy,
1762 IntTy,
1763 NULL);
1764 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
1765
1766 IvarListTy = llvm::OpaqueType::get();
1767 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
1768 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
1769
1770 MethodTy = llvm::StructType::get(SelectorPtrTy,
1771 Int8PtrTy,
1772 Int8PtrTy,
1773 NULL);
1774 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
1775
1776 MethodListTy = llvm::OpaqueType::get();
1777 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
1778 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
1779
1780 CacheTy = llvm::OpaqueType::get();
1781 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
1782 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
1783
1784 ClassExtensionTy =
1785 llvm::StructType::get(IntTy,
1786 Int8PtrTy,
1787 PropertyListPtrTy,
1788 NULL);
1789 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
1790 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
1791
1792 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
1793
1794 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
1795 llvm::PointerType::getUnqual(ClassTyHolder),
1796 Int8PtrTy,
1797 LongTy,
1798 LongTy,
1799 LongTy,
1800 IvarListPtrTy,
1801 MethodListPtrTy,
1802 CachePtrTy,
1803 ProtocolListPtrTy,
1804 Int8PtrTy,
1805 ClassExtensionPtrTy,
1806 NULL);
1807 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
1808
1809 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
1810 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
1811 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
1812
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001813 CategoryTy = llvm::StructType::get(Int8PtrTy,
1814 Int8PtrTy,
1815 MethodListPtrTy,
1816 MethodListPtrTy,
1817 ProtocolListPtrTy,
1818 IntTy,
1819 PropertyListPtrTy,
1820 NULL);
1821 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
1822
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001823 SuperTy =
1824 llvm::StructType::get(ObjectPtrTy,
1825 ClassPtrTy,
1826 NULL);
1827 CGM.getModule().addTypeName("struct._objc_super",
1828 SuperTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001829 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001830
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001831 // Global metadata structures
1832
1833 SymtabTy = llvm::StructType::get(LongTy,
1834 SelectorPtrTy,
1835 ShortTy,
1836 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001837 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001838 NULL);
1839 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
1840 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
1841
1842 ModuleTy =
1843 llvm::StructType::get(LongTy,
1844 LongTy,
1845 Int8PtrTy,
1846 SymtabPtrTy,
1847 NULL);
1848 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001849
1850 // Message send functions
1851
1852 std::vector<const llvm::Type*> Params;
1853 Params.push_back(ObjectPtrTy);
1854 Params.push_back(SelectorPtrTy);
1855 MessageSendFn = llvm::Function::Create(llvm::FunctionType::get(ObjectPtrTy,
1856 Params,
1857 true),
1858 llvm::Function::ExternalLinkage,
1859 "objc_msgSend",
1860 &CGM.getModule());
1861
1862 Params.clear();
1863 Params.push_back(Int8PtrTy);
1864 Params.push_back(ObjectPtrTy);
1865 Params.push_back(SelectorPtrTy);
1866 MessageSendStretFn =
1867 llvm::Function::Create(llvm::FunctionType::get(llvm::Type::VoidTy,
1868 Params,
1869 true),
1870 llvm::Function::ExternalLinkage,
1871 "objc_msgSend_stret",
1872 &CGM.getModule());
1873
1874 Params.clear();
1875 Params.push_back(SuperPtrTy);
1876 Params.push_back(SelectorPtrTy);
1877 MessageSendSuperFn =
1878 llvm::Function::Create(llvm::FunctionType::get(ObjectPtrTy,
1879 Params,
1880 true),
1881 llvm::Function::ExternalLinkage,
1882 "objc_msgSendSuper",
1883 &CGM.getModule());
1884
1885 Params.clear();
1886 Params.push_back(Int8PtrTy);
1887 Params.push_back(SuperPtrTy);
1888 Params.push_back(SelectorPtrTy);
1889 MessageSendSuperStretFn =
1890 llvm::Function::Create(llvm::FunctionType::get(llvm::Type::VoidTy,
1891 Params,
1892 true),
1893 llvm::Function::ExternalLinkage,
1894 "objc_msgSendSuper_stret",
1895 &CGM.getModule());
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001896}
1897
1898ObjCTypesHelper::~ObjCTypesHelper() {
1899}
1900
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001901llvm::Value *ObjCTypesHelper::getMessageSendFn(bool IsSuper,
1902 const llvm::Type *ReturnTy) {
1903 llvm::Function *F;
1904 llvm::FunctionType *CallFTy;
1905
1906 // FIXME: Should we be caching any of this?
1907 if (!ReturnTy->isSingleValueType()) {
1908 F = IsSuper ? MessageSendSuperStretFn : MessageSendStretFn;
1909 std::vector<const llvm::Type*> Params(3);
1910 Params[0] = llvm::PointerType::getUnqual(ReturnTy);
1911 Params[1] = IsSuper ? SuperPtrTy : ObjectPtrTy;
1912 Params[2] = SelectorPtrTy;
1913 CallFTy = llvm::FunctionType::get(llvm::Type::VoidTy, Params, true);
1914 } else { // XXX floating point?
1915 F = IsSuper ? MessageSendSuperFn : MessageSendFn;
1916 std::vector<const llvm::Type*> Params(2);
1917 Params[0] = IsSuper ? SuperPtrTy : ObjectPtrTy;
1918 Params[1] = SelectorPtrTy;
1919 CallFTy = llvm::FunctionType::get(ReturnTy, Params, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001920 }
1921
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001922 return llvm::ConstantExpr::getBitCast(F,
1923 llvm::PointerType::getUnqual(CallFTy));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001924}
1925
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001926/* *** */
1927
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001928CodeGen::CGObjCRuntime *
1929CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001930 return new CGObjCMac(CGM);
1931}