blob: 60c57289cbbd1a7ba1365cb5e48872e5021f1290 [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 Dunbar6efc0c52008-08-13 03:21:16 +000032 // FIXME: We should find a nicer way to make the labels for
33 // metadata, string concatenation is lame.
34
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000035/// ObjCTypesHelper - Helper class that encapsulates lazy
36/// construction of varies types used during ObjC generation.
37class ObjCTypesHelper {
38private:
39 CodeGen::CodeGenModule &CGM;
40
Daniel Dunbar14c80b72008-08-23 09:25:55 +000041 llvm::Function *MessageSendFn, *MessageSendStretFn;
42 llvm::Function *MessageSendSuperFn, *MessageSendSuperStretFn;
Daniel Dunbar259d93d2008-08-12 03:39:23 +000043
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000044public:
Daniel Dunbar27f9d772008-08-21 04:36:09 +000045 const llvm::Type *ShortTy, *IntTy, *LongTy;
46 const llvm::Type *Int8PtrTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +000047
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000048 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
49 const llvm::Type *ObjectPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000050 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000051 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000052 /// ProtocolPtrTy - LLVM type for external protocol handles
53 /// (typeof(Protocol))
54 const llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000055
Daniel Dunbare8b470d2008-08-23 04:28:29 +000056 /// SuperTy - LLVM type for struct objc_super.
57 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +000058 /// SuperPtrTy - LLVM type for struct objc_super *.
59 const llvm::Type *SuperPtrTy;
Daniel Dunbare8b470d2008-08-23 04:28:29 +000060
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000061 /// SymtabTy - LLVM type for struct objc_symtab.
62 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +000063 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
64 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000065 /// ModuleTy - LLVM type for struct objc_module.
66 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +000067
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000068 /// 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 Dunbarc8ef5512008-08-23 00:19:03 +000087 /// 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 Dunbar6efc0c52008-08-13 03:21:16 +000093 /// 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 Dunbar86e253a2008-08-22 20:34:54 +000099 /// CategoryTy - LLVM type for struct objc_category.
100 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000101 /// 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 Dunbar6efc0c52008-08-13 03:21:16 +0000125
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000126public:
127 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
128 ~ObjCTypesHelper();
129
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000130 llvm::Value *getMessageSendFn(bool IsSuper, const llvm::Type *ReturnTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000131};
132
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000133class CGObjCMac : public CodeGen::CGObjCRuntime {
134private:
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000135 CodeGen::CodeGenModule &CGM;
136 ObjCTypesHelper ObjCTypes;
137 /// ObjCABI - FIXME: Not sure yet.
138 unsigned ObjCABI;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000139
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000140 /// 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 Dunbar4e2d7d02008-08-12 06:48:42 +0000150 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000151 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000152
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000153 /// MethodVarNames - uniqued method variable names.
154 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
155
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000156 /// 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 Dunbarc45ef602008-08-26 21:51:14 +0000160 /// MethodDefinitions - map of methods which have been defined in
161 /// this translation unit.
162 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
163
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000164 /// PropertyNames - uniqued method variable names.
165 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
166
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000167 /// ClassReferences - uniqued class references.
168 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
169
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000170 /// SelectorReferences - uniqued selector references.
171 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
172
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000173 /// 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 Dunbar27f9d772008-08-21 04:36:09 +0000178 /// 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 Dunbar6efc0c52008-08-13 03:21:16 +0000184 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000185 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000186 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000187
188 /// EmitImageInfo - Emit the image info marker used to encode some module
189 /// level information.
190 void EmitImageInfo();
191
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000192 /// EmitModuleInfo - Another marker encoding module level
193 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000194 void EmitModuleInfo();
195
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000196 /// EmitModuleSymols - Emit module symbols, the list of defined
197 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000198 llvm::Constant *EmitModuleSymbols();
199
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000200 /// FinishModule - Write out global data structures at the end of
201 /// processing a translation unit.
202 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000203
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000204 /// 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 Dunbar14c80b72008-08-23 09:25:55 +0000214 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
215 const ObjCMessageExpr *E,
216 llvm::Value *Arg0,
217 bool IsSuper);
218
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000219 /// 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 Dunbarf56f1912008-08-25 08:19:24 +0000227
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 Dunbar27f9d772008-08-21 04:36:09 +0000233 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000234 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000235 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
236 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000237 const llvm::Type *InterfaceTy,
238 const std::vector<llvm::Constant*> &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000239
240 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000241 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000242 llvm::Constant *EmitMethodList(const std::string &Name,
243 const char *Section,
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000244 const std::vector<llvm::Constant*> &Methods);
245
246 llvm::Constant *GetMethodConstant(ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000247
248 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000249 /// 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 Dunbar27f9d772008-08-21 04:36:09 +0000259 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 Dunbar6efc0c52008-08-13 03:21:16 +0000265
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000266 /// 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 Dunbar6efc0c52008-08-13 03:21:16 +0000272 /// 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 Dunbardbc93372008-08-21 21:57:41 +0000280 llvm::Constant *EmitProtocolList(const std::string &Name,
281 ObjCProtocolDecl::protocol_iterator begin,
282 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000283
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000284 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
285 /// for the given selector.
286 llvm::Value *EmitSelector(llvm::IRBuilder<> &Builder, Selector Sel);
287
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000288 /// 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 Dunbar4e2d7d02008-08-12 06:48:42 +0000293 /// GetClassName - Return a unique constant for the given selector's
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000294 /// name. The return value has type char *.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000295 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000296
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000297 /// GetMethodVarName - Return a unique constant for the given
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000298 /// selector's name. The return value has type char *.
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000299 llvm::Constant *GetMethodVarName(Selector Sel);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000300 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000301 llvm::Constant *GetMethodVarName(const std::string &Name);
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000302
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000303 /// GetMethodVarType - Return a unique constant for the given
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000304 /// selector's name. The return value has type char *.
305
306 // FIXME: This is a horrible name.
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000307 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000308 llvm::Constant *GetMethodVarType(const std::string &Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000309
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000310 /// 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 Dunbarb7ec2462008-08-16 03:19:19 +0000317 /// 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 Dunbarc17a4d32008-08-11 02:45:11 +0000322public:
323 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000324 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000325
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000326 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
327 const ObjCMessageExpr *E,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000328 llvm::Value *Receiver,
329 bool IsClassMessage);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000330
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000331 virtual CodeGen::RValue
332 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
333 const ObjCMessageExpr *E,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000334 const ObjCInterfaceDecl *Class,
335 llvm::Value *Receiver,
336 bool IsClassMessage);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000337
338 virtual llvm::Value *GetClass(llvm::IRBuilder<> &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000339 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000340
341 virtual llvm::Value *GetSelector(llvm::IRBuilder<> &Builder, Selector Sel);
342
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000343 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000344
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000345 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000346
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000347 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000348
349 virtual llvm::Value *GenerateProtocolRef(llvm::IRBuilder<> &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000350 const ObjCProtocolDecl *PD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000351
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000352 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000353
354 virtual llvm::Function *ModuleInitFunction();
355};
356} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000357
358/* *** Helper Functions *** */
359
360/// getConstantGEP() - Help routine to construct simple GEPs.
361static 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 Dunbarc17a4d32008-08-11 02:45:11 +0000372
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000373CGObjCMac::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 Dunbarc17a4d32008-08-11 02:45:11 +0000383}
384
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000385/// GetClass - Return a reference to the class for the given interface
386/// decl.
387llvm::Value *CGObjCMac::GetClass(llvm::IRBuilder<> &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000388 const ObjCInterfaceDecl *ID) {
389 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000390}
391
392/// GetSelector - Return the pointer to the unique'd string for this selector.
393llvm::Value *CGObjCMac::GetSelector(llvm::IRBuilder<> &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000394 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000395}
396
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000397/// 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
407llvm::Constant *CGObjCMac::GenerateConstantString(const std::string &String) {
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000408 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000409}
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 Dunbar8f2926b2008-08-23 03:46:30 +0000414CodeGen::RValue
415CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
416 const ObjCMessageExpr *E,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000417 const ObjCInterfaceDecl *Class,
418 llvm::Value *Receiver,
419 bool IsClassMessage) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000420 // 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 Dunbare8b470d2008-08-23 04:28:29 +0000428
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000429 // 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 Dunbar14c80b72008-08-23 09:25:55 +0000442 return EmitMessageSend(CGF, E, ObjCSuper, true);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000443}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000444
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000445/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000446CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
447 const ObjCMessageExpr *E,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000448 llvm::Value *Receiver,
449 bool IsClassMessage) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000450 llvm::Value *Arg0 =
451 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
452 return EmitMessageSend(CGF, E, Arg0, false);
453}
454
455CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
456 const ObjCMessageExpr *E,
457 llvm::Value *Arg0,
458 bool IsSuper) {
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000459 llvm::Value *Args[2];
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000460 Args[0] = Arg0;
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000461 Args[1] = EmitSelector(CGF.Builder, E->getSelector());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000462
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000463 // 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 Dunbar8f2926b2008-08-23 03:46:30 +0000472 E->arg_begin(),
473 E->arg_end(),
474 Args, 2);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000475}
476
477llvm::Value *CGObjCMac::GenerateProtocolRef(llvm::IRBuilder<> &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000478 const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000479 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
480 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000481}
482
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000483/*
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*/
495void CGObjCMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000496 // 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 Dunbar6efc0c52008-08-13 03:21:16 +0000501 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 Dunbardbc93372008-08-21 21:57:41 +0000506 Values[2] =
507 EmitProtocolList(std::string("\01L_OBJC_PROTOCOL_REFS_")+PD->getName(),
508 PD->protocol_begin(),
509 PD->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000510 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 Dunbar6efc0c52008-08-13 03:21:16 +0000522 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
543llvm::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*/
579llvm::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 Dunbar27f9d772008-08-21 04:36:09 +0000584 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 Dunbarc8ef5512008-08-23 00:19:03 +0000596 Values[3] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_PROTO_LIST_") +
597 PD->getName(),
598 PD->classprop_begin(),
599 PD->classprop_end());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000600
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000601 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000602 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 Dunbardbc93372008-08-21 21:57:41 +0000628llvm::Constant *
629CGObjCMac::EmitProtocolList(const std::string &Name,
630 ObjCProtocolDecl::protocol_iterator begin,
631 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000632 std::vector<llvm::Constant*> ProtocolRefs;
633
Daniel Dunbardbc93372008-08-21 21:57:41 +0000634 for (; begin != end; ++begin)
635 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000636
637 // Just return null for empty protocol lists
638 if (ProtocolRefs.empty())
639 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
640
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000641 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000642 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
643
644 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000645 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000646 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 Dunbardbc93372008-08-21 21:57:41 +0000658 Name,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000659 &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 Dunbarc8ef5512008-08-23 00:19:03 +0000665 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*/
676llvm::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 Dunbar6efc0c52008-08-13 03:21:16 +0000716 struct objc_method_description_list {
717 int count;
718 struct objc_method_description list[];
719 };
720*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000721llvm::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 Dunbar6efc0c52008-08-13 03:21:16 +0000727 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 Dunbarc17a4d32008-08-11 02:45:11 +0000774}
775
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000776/*
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 Dunbar7ded7f42008-08-15 22:20:32 +0000787void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000788 unsigned Size = CGM.getTargetData().getABITypeSize(ObjCTypes.CategoryTy);
789
Daniel Dunbar86e2f402008-08-26 23:03:11 +0000790 // 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 Dunbar86e253a2008-08-22 20:34:54 +0000795 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +0000796 const ObjCCategoryDecl *Category =
797 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000798 std::string ExtName(std::string(Interface->getName()) +
799 "_" +
800 OCD->getName());
801
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000802 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 Dunbar86e253a2008-08-22 20:34:54 +0000814 std::vector<llvm::Constant*> Values(7);
815 Values[0] = GetClassName(OCD->getIdentifier());
816 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000817 Values[2] =
818 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
819 ExtName,
820 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000821 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000822 Values[3] =
823 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
824 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000825 ClassMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000826 Values[4] =
827 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
828 Interface->protocol_begin(),
829 Interface->protocol_end());
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000830 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +0000831
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 Dunbar86e253a2008-08-22 20:34:54 +0000840
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 Dunbarc17a4d32008-08-11 02:45:11 +0000853}
854
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000855// FIXME: Get from somewhere?
856enum 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>
867static 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 */
904void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000905 DefinedSymbols.insert(ID->getIdentifier());
906
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000907 const char *ClassName = ID->getName();
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000908 // FIXME: Gross
909 ObjCInterfaceDecl *Interface =
910 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc93372008-08-21 21:57:41 +0000911 llvm::Constant *Protocols =
912 EmitProtocolList(std::string("\01L_OBJC_CLASS_PROTOCOLS_") + ID->getName(),
913 Interface->protocol_begin(),
914 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000915 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 Dunbarc45ef602008-08-26 21:51:14 +0000924 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 Dunbar27f9d772008-08-21 04:36:09 +0000952 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000953 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000954 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000955 // Record a reference to the super class.
956 LazySymbols.insert(Super->getIdentifier());
957
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000958 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 Dunbarc8ef5512008-08-23 00:19:03 +0000970 Values[ 7] =
971 EmitMethodList(std::string("\01L_OBJC_INSTANCE_METHODS_") + ID->getName(),
972 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000973 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000974 // cache is always NULL.
975 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
976 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000977 // FIXME: Set ivar_layout
978 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000979 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
996llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
997 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000998 const llvm::Type *InterfaceTy,
999 const std::vector<llvm::Constant*> &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001000 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 Dunbar86e253a2008-08-22 20:34:54 +00001015 // 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 Dunbar27f9d772008-08-21 04:36:09 +00001018 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 Dunbarc8ef5512008-08-23 00:19:03 +00001031 Values[ 7] =
1032 EmitMethodList(std::string("\01L_OBJC_CLASS_METHODS_") + ID->getName(),
1033 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001034 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001035 // 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 Dunbarf56f1912008-08-25 08:19:24 +00001045 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 Dunbar27f9d772008-08-21 04:36:09 +00001061 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 Dunbarf56f1912008-08-25 08:19:24 +00001069llvm::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 Dunbar27f9d772008-08-21 04:36:09 +00001094/*
1095 struct objc_class_ext {
1096 uint32_t size;
1097 const char *weak_ivar_layout;
1098 struct _objc_property_list *properties;
1099 };
1100*/
1101llvm::Constant *
1102CGObjCMac::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 Dunbar86e253a2008-08-22 20:34:54 +00001108 // FIXME: Output weak_ivar_layout string.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001109 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001110 Values[2] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") +
1111 ID->getName(),
1112 ID->getClassInterface()->classprop_begin(),
1113 ID->getClassInterface()->classprop_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001114
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 */
1146llvm::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 Dunbarc45ef602008-08-26 21:51:14 +00001222
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.
1226llvm::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 Dunbar86e253a2008-08-22 20:34:54 +00001241llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1242 const char *Section,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001243 const std::vector<llvm::Constant*>
1244 &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001245 // 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 Dunbar27f9d772008-08-21 04:36:09 +00001257 llvm::GlobalVariable *GV =
1258 new llvm::GlobalVariable(Init->getType(), false,
1259 llvm::GlobalValue::InternalLinkage,
1260 Init,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001261 Name,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001262 &CGM.getModule());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001263 GV->setSection(Section);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001264 UsedGlobals.push_back(GV);
1265 return llvm::ConstantExpr::getBitCast(GV,
1266 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001267}
1268
1269llvm::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 Dunbar8f2926b2008-08-23 03:46:30 +00001295 if (Ty->isSingleValueType()) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001296 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 Dunbarc45ef602008-08-26 21:51:14 +00001312 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001313
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001314 unsigned Offset = 3; // Return plus self and selector implicit args.
1315 if (useStructRet) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001316 Method->addParamAttr(1, llvm::ParamAttr::StructRet);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001317 ++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 Dunbarb7ec2462008-08-16 03:19:19 +00001329
1330 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001331}
1332
1333llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001334 // Abuse this interface function as a place to finalize.
1335 FinishModule();
1336
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001337 return NULL;
1338}
1339
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001340/* *** 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/// };
1350enum ImageInfoFlags {
1351 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
1352 eImageInfo_GarbageCollected = (1 << 1),
1353 eImageInfo_GCOnly = (1 << 2)
1354};
1355
1356void 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 Dunbarf77ac862008-08-11 21:35:06 +00001366 // 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 Dunbar4e2d7d02008-08-12 06:48:42 +00001372 llvm::GlobalVariable *GV =
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001373 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 Dunbarbbce49b2008-08-12 00:12:39 +00001379 if (ObjCABI == 1) {
1380 GV->setSection("__OBJC, __image_info,regular");
1381 } else {
1382 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
1383 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001384
1385 UsedGlobals.push_back(GV);
1386}
1387
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001388
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
1397static const int ModuleVersion = 7;
1398
1399void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001400 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 Dunbar7ded7f42008-08-15 22:20:32 +00001405 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001406 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001407 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 Dunbar6efc0c52008-08-13 03:21:16 +00001414 "\01L_OBJC_MODULES",
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001415 &CGM.getModule());
1416 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
1417 UsedGlobals.push_back(GV);
1418}
1419
1420llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001421 unsigned NumClasses = DefinedClasses.size();
1422 unsigned NumCategories = DefinedCategories.size();
1423
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001424 // 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 Dunbar27f9d772008-08-21 04:36:09 +00001429 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 Dunbar86e253a2008-08-22 20:34:54 +00001434 // The runtime expects exactly the list of defined classes followed
1435 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001436 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001437 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 Dunbar27f9d772008-08-21 04:36:09 +00001445 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001446 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001447 NumClasses + NumCategories),
1448 Symbols);
1449
1450 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1451
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001452 llvm::GlobalVariable *GV =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001453 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001454 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001455 Init,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001456 "\01L_OBJC_SYMBOLS",
1457 &CGM.getModule());
1458 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
1459 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001460 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
1461}
1462
1463llvm::Value *CGObjCMac::EmitClassRef(llvm::IRBuilder<> &Builder,
1464 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001465 LazySymbols.insert(ID->getIdentifier());
1466
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001467 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 Dunbar4e2d7d02008-08-12 06:48:42 +00001483}
1484
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001485llvm::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 Dunbar6efc0c52008-08-13 03:21:16 +00001504llvm::Constant *CGObjCMac::GetClassName(IdentifierInfo *Ident) {
1505 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001506
1507 if (!Entry) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001508 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001509 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001510 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001511 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 Dunbar6efc0c52008-08-13 03:21:16 +00001518 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001519}
1520
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001521llvm::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 Dunbar6efc0c52008-08-13 03:21:16 +00001527 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001528 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 Dunbar6efc0c52008-08-13 03:21:16 +00001535 return getConstantGEP(Entry, 0, 0);
1536}
1537
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001538// FIXME: Merge into a single cstring creation function.
1539llvm::Constant *CGObjCMac::GetMethodVarName(IdentifierInfo *ID) {
1540 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
1541}
1542
1543// FIXME: Merge into a single cstring creation function.
1544llvm::Constant *CGObjCMac::GetMethodVarName(const std::string &Name) {
1545 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
1546}
1547
1548llvm::Constant *CGObjCMac::GetMethodVarType(const std::string &Name) {
1549 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001550
1551 if (!Entry) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001552 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001553 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 Dunbar259d93d2008-08-12 03:39:23 +00001563}
1564
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001565// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001566llvm::Constant *CGObjCMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001567 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001568 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
1569 TypeStr);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001570 return GetMethodVarType(TypeStr);
1571}
1572
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001573// FIXME: Merge into a single cstring creation function.
1574llvm::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.
1592llvm::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 Dunbarb7ec2462008-08-16 03:19:19 +00001598void 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 Dunbarf77ac862008-08-11 21:35:06 +00001611void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001612 EmitModuleInfo();
1613
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001614 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001615
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001616 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001617 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001618 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001619 }
1620
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001621 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001622 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 Dunbar242d4dc2008-08-25 06:02:07 +00001630
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 Dunbarf77ac862008-08-11 21:35:06 +00001647}
1648
1649/* *** */
1650
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001651ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Daniel Dunbar3e9df992008-08-23 18:37:06 +00001652 : CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001653{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001654 CodeGen::CodeGenTypes &Types = CGM.getTypes();
1655 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001656
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001657 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001658 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001659 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001660 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
1661
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001662 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
1663 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001664
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 Dunbar4e2d7d02008-08-12 06:48:42 +00001669
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001670 MethodDescriptionTy =
1671 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001672 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001673 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 Dunbarc8ef5512008-08-23 00:19:03 +00001686 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 Dunbar6efc0c52008-08-13 03:21:16 +00001696 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 Dunbar27f9d772008-08-21 04:36:09 +00001724 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001725 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 Dunbar27f9d772008-08-21 04:36:09 +00001739
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 Dunbar86e253a2008-08-22 20:34:54 +00001795 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 Dunbare8b470d2008-08-23 04:28:29 +00001805 SuperTy =
1806 llvm::StructType::get(ObjectPtrTy,
1807 ClassPtrTy,
1808 NULL);
1809 CGM.getModule().addTypeName("struct._objc_super",
1810 SuperTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001811 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001812
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001813 // Global metadata structures
1814
1815 SymtabTy = llvm::StructType::get(LongTy,
1816 SelectorPtrTy,
1817 ShortTy,
1818 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001819 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001820 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 Dunbar14c80b72008-08-23 09:25:55 +00001831
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 Dunbarbbce49b2008-08-12 00:12:39 +00001878}
1879
1880ObjCTypesHelper::~ObjCTypesHelper() {
1881}
1882
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001883llvm::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 Dunbar259d93d2008-08-12 03:39:23 +00001902 }
1903
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001904 return llvm::ConstantExpr::getBitCast(F,
1905 llvm::PointerType::getUnqual(CallFTy));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001906}
1907
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001908/* *** */
1909
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001910CodeGen::CGObjCRuntime *
1911CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001912 return new CGObjCMac(CGM);
1913}