blob: 67ef1ae0e8308d5e4be001ed1419c72b66c54caf [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 Dunbar0c0e7a62008-10-29 22:36:39 +000024#include "llvm/ADT/DenseSet.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;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000029using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000030
31namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000032
Daniel Dunbarae226fa2008-08-27 02:31:56 +000033 typedef std::vector<llvm::Constant*> ConstantVector;
34
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000035 // FIXME: We should find a nicer way to make the labels for
36 // metadata, string concatenation is lame.
37
Fariborz Jahanianee0af742009-01-21 22:04:16 +000038class ObjCCommonTypesHelper {
39protected:
40 CodeGen::CodeGenModule &CGM;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000041
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000042public:
Daniel Dunbar27f9d772008-08-21 04:36:09 +000043 const llvm::Type *ShortTy, *IntTy, *LongTy;
44 const llvm::Type *Int8PtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +000045
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000046 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
47 const llvm::Type *ObjectPtrTy;
Fariborz Jahanian6d657c42008-11-18 20:18:11 +000048
49 /// PtrObjectPtrTy - LLVM type for id *
50 const llvm::Type *PtrObjectPtrTy;
51
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;
Fariborz Jahanianee0af742009-01-21 22:04:16 +000057
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000058 // SuperCTy - clang type for struct objc_super.
59 QualType SuperCTy;
60 // SuperPtrCTy - clang type for struct objc_super *.
61 QualType SuperPtrCTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +000062
Daniel Dunbare8b470d2008-08-23 04:28:29 +000063 /// SuperTy - LLVM type for struct objc_super.
64 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +000065 /// SuperPtrTy - LLVM type for struct objc_super *.
66 const llvm::Type *SuperPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +000067
Fariborz Jahanian30bc5712009-01-22 23:02:58 +000068 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
69 /// in GCC parlance).
70 const llvm::StructType *PropertyTy;
71
72 /// PropertyListTy - LLVM type for struct objc_property_list
73 /// (_prop_list_t in GCC parlance).
74 const llvm::StructType *PropertyListTy;
75 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
76 const llvm::Type *PropertyListPtrTy;
77
78 // MethodTy - LLVM type for struct objc_method.
79 const llvm::StructType *MethodTy;
80
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +000081 /// CacheTy - LLVM type for struct objc_cache.
82 const llvm::Type *CacheTy;
83 /// CachePtrTy - LLVM type for struct objc_cache *.
84 const llvm::Type *CachePtrTy;
85
Fariborz Jahaniandb286862009-01-22 00:37:21 +000086 llvm::Function *GetPropertyFn, *SetPropertyFn;
87
88 llvm::Function *EnumerationMutationFn;
89
90 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
91 llvm::Function *GcReadWeakFn;
92
93 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
94 llvm::Function *GcAssignWeakFn;
95
96 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
97 llvm::Function *GcAssignGlobalFn;
98
99 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
100 llvm::Function *GcAssignIvarFn;
101
102 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
103 llvm::Function *GcAssignStrongCastFn;
104
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000105 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
106 ~ObjCCommonTypesHelper(){}
107};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000108
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000109/// ObjCTypesHelper - Helper class that encapsulates lazy
110/// construction of varies types used during ObjC generation.
111class ObjCTypesHelper : public ObjCCommonTypesHelper {
112private:
113
114 llvm::Function *MessageSendFn, *MessageSendStretFn, *MessageSendFpretFn;
115 llvm::Function *MessageSendSuperFn, *MessageSendSuperStretFn,
116 *MessageSendSuperFpretFn;
117
118public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000119 /// SymtabTy - LLVM type for struct objc_symtab.
120 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000121 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
122 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000123 /// ModuleTy - LLVM type for struct objc_module.
124 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000125
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000126 /// ProtocolTy - LLVM type for struct objc_protocol.
127 const llvm::StructType *ProtocolTy;
128 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
129 const llvm::Type *ProtocolPtrTy;
130 /// ProtocolExtensionTy - LLVM type for struct
131 /// objc_protocol_extension.
132 const llvm::StructType *ProtocolExtensionTy;
133 /// ProtocolExtensionTy - LLVM type for struct
134 /// objc_protocol_extension *.
135 const llvm::Type *ProtocolExtensionPtrTy;
136 /// MethodDescriptionTy - LLVM type for struct
137 /// objc_method_description.
138 const llvm::StructType *MethodDescriptionTy;
139 /// MethodDescriptionListTy - LLVM type for struct
140 /// objc_method_description_list.
141 const llvm::StructType *MethodDescriptionListTy;
142 /// MethodDescriptionListPtrTy - LLVM type for struct
143 /// objc_method_description_list *.
144 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000145 /// ProtocolListTy - LLVM type for struct objc_property_list.
146 const llvm::Type *ProtocolListTy;
147 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
148 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000149 /// CategoryTy - LLVM type for struct objc_category.
150 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000151 /// ClassTy - LLVM type for struct objc_class.
152 const llvm::StructType *ClassTy;
153 /// ClassPtrTy - LLVM type for struct objc_class *.
154 const llvm::Type *ClassPtrTy;
155 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
156 const llvm::StructType *ClassExtensionTy;
157 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
158 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000159 // IvarTy - LLVM type for struct objc_ivar.
160 const llvm::StructType *IvarTy;
161 /// IvarListTy - LLVM type for struct objc_ivar_list.
162 const llvm::Type *IvarListTy;
163 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
164 const llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000165 /// MethodListTy - LLVM type for struct objc_method_list.
166 const llvm::Type *MethodListTy;
167 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
168 const llvm::Type *MethodListPtrTy;
Anders Carlsson124526b2008-09-09 10:10:21 +0000169
170 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
171 const llvm::Type *ExceptionDataTy;
172
173 /// ExceptionThrowFn - LLVM objc_exception_throw function.
174 llvm::Function *ExceptionThrowFn;
175
176 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
177 llvm::Function *ExceptionTryEnterFn;
178
179 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
180 llvm::Function *ExceptionTryExitFn;
181
182 /// ExceptionExtractFn - LLVM objc_exception_extract function.
183 llvm::Function *ExceptionExtractFn;
184
185 /// ExceptionMatchFn - LLVM objc_exception_match function.
186 llvm::Function *ExceptionMatchFn;
187
188 /// SetJmpFn - LLVM _setjmp function.
189 llvm::Function *SetJmpFn;
Chris Lattner10cac6f2008-11-15 21:26:17 +0000190
191 /// SyncEnterFn - LLVM object_sync_enter function.
192 llvm::Function *SyncEnterFn;
193
194 /// SyncExitFn - LLVM object_sync_exit function.
195 llvm::Function *SyncExitFn;
196
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000197public:
198 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000199 ~ObjCTypesHelper() {}
Daniel Dunbar5669e572008-10-17 03:24:53 +0000200
201
202 llvm::Function *getSendFn(bool IsSuper) {
203 return IsSuper ? MessageSendSuperFn : MessageSendFn;
204 }
205
206 llvm::Function *getSendStretFn(bool IsSuper) {
207 return IsSuper ? MessageSendSuperStretFn : MessageSendStretFn;
208 }
209
210 llvm::Function *getSendFpretFn(bool IsSuper) {
211 return IsSuper ? MessageSendSuperFpretFn : MessageSendFpretFn;
212 }
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000213};
214
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000215/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000216/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000217class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000218public:
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000219 // MethodListnfABITy - LLVM for struct _method_list_t
220 const llvm::StructType *MethodListnfABITy;
221
222 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
223 const llvm::Type *MethodListnfABIPtrTy;
224
225 // ProtocolnfABITy = LLVM for struct _protocol_t
226 const llvm::StructType *ProtocolnfABITy;
227
228 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
229 const llvm::StructType *ProtocolListnfABITy;
230
231 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
232 const llvm::Type *ProtocolListnfABIPtrTy;
233
234 // ClassnfABITy - LLVM for struct _class_t
235 const llvm::StructType *ClassnfABITy;
236
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000237 // ClassnfABIPtrTy - LLVM for struct _class_t*
238 const llvm::Type *ClassnfABIPtrTy;
239
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000240 // IvarnfABITy - LLVM for struct _ivar_t
241 const llvm::StructType *IvarnfABITy;
242
243 // IvarListnfABITy - LLVM for struct _ivar_list_t
244 const llvm::StructType *IvarListnfABITy;
245
246 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
247 const llvm::Type *IvarListnfABIPtrTy;
248
249 // ClassRonfABITy - LLVM for struct _class_ro_t
250 const llvm::StructType *ClassRonfABITy;
251
252 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
253 const llvm::Type *ImpnfABITy;
254
255 // CategorynfABITy - LLVM for struct _category_t
256 const llvm::StructType *CategorynfABITy;
257
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000258 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
259 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000260};
261
262class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
263protected:
264 CodeGen::CodeGenModule &CGM;
265 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000266 unsigned ObjCABI;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000267
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000268 /// LazySymbols - Symbols to generate a lazy reference for. See
269 /// DefinedSymbols and FinishModule().
270 std::set<IdentifierInfo*> LazySymbols;
271
272 /// DefinedSymbols - External symbols which are defined by this
273 /// module. The symbols in this list and LazySymbols are used to add
274 /// special linker symbols which ensure that Objective-C modules are
275 /// linked properly.
276 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000277
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000278 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000279 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000280
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000281 /// MethodVarNames - uniqued method variable names.
282 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000283
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000284 /// MethodVarTypes - uniqued method type signatures. We have to use
285 /// a StringMap here because have no other unique reference.
286 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000287
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000288 /// MethodDefinitions - map of methods which have been defined in
289 /// this translation unit.
290 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000291
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000292 /// PropertyNames - uniqued method variable names.
293 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000294
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000295 /// ClassReferences - uniqued class references.
296 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000297
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000298 /// SelectorReferences - uniqued selector references.
299 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000300
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000301 /// Protocols - Protocols for which an objc_protocol structure has
302 /// been emitted. Forward declarations are handled by creating an
303 /// empty structure whose initializer is filled in when/if defined.
304 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000305
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000306 /// DefinedProtocols - Protocols which have actually been
307 /// defined. We should not need this, see FIXME in GenerateProtocol.
308 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000309
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000310 /// DefinedClasses - List of defined classes.
311 std::vector<llvm::GlobalValue*> DefinedClasses;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000312
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000313 /// DefinedCategories - List of defined categories.
314 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000315
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000316 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000317 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000318 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000319
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000320 /// GetNameForMethod - Return a name for the given method.
321 /// \param[out] NameOut - The return value.
322 void GetNameForMethod(const ObjCMethodDecl *OMD,
323 const ObjCContainerDecl *CD,
324 std::string &NameOut);
325
326 /// GetMethodVarName - Return a unique constant for the given
327 /// selector's name. The return value has type char *.
328 llvm::Constant *GetMethodVarName(Selector Sel);
329 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
330 llvm::Constant *GetMethodVarName(const std::string &Name);
331
332 /// GetMethodVarType - Return a unique constant for the given
333 /// selector's name. The return value has type char *.
334
335 // FIXME: This is a horrible name.
336 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
337 llvm::Constant *GetMethodVarType(const std::string &Name);
338
339 /// GetPropertyName - Return a unique constant for the given
340 /// name. The return value has type char *.
341 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
342
343 // FIXME: This can be dropped once string functions are unified.
344 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
345 const Decl *Container);
346
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000347 /// GetClassName - Return a unique constant for the given selector's
348 /// name. The return value has type char *.
349 llvm::Constant *GetClassName(IdentifierInfo *Ident);
350
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000351 const RecordDecl *GetFirstIvarInRecord(const ObjCInterfaceDecl *OID,
352 RecordDecl::field_iterator &FIV,
353 RecordDecl::field_iterator &PIV);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000354 /// EmitPropertyList - Emit the given property list. The return
355 /// value has type PropertyListPtrTy.
356 llvm::Constant *EmitPropertyList(const std::string &Name,
357 const Decl *Container,
358 const ObjCContainerDecl *OCD,
359 const ObjCCommonTypesHelper &ObjCTypes);
360
Fariborz Jahanianda320092009-01-29 19:24:30 +0000361 /// GetProtocolRef - Return a reference to the internal protocol
362 /// description, creating an empty one if it has not been
363 /// defined. The return value has type ProtocolPtrTy.
364 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
365
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000366public:
367 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
368 { }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000369
370 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000371
372 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
373 const ObjCContainerDecl *CD=0);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000374
375 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
376
377 /// GetOrEmitProtocol - Get the protocol object for the given
378 /// declaration, emitting it if necessary. The return value has type
379 /// ProtocolPtrTy.
380 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
381
382 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
383 /// object for the given declaration, emitting it if needed. These
384 /// forward references will be filled in with empty bodies if no
385 /// definition is seen. The return value has type ProtocolPtrTy.
386 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000387};
388
389class CGObjCMac : public CGObjCCommonMac {
390private:
391 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000392 /// EmitImageInfo - Emit the image info marker used to encode some module
393 /// level information.
394 void EmitImageInfo();
395
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000396 /// EmitModuleInfo - Another marker encoding module level
397 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000398 void EmitModuleInfo();
399
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000400 /// EmitModuleSymols - Emit module symbols, the list of defined
401 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000402 llvm::Constant *EmitModuleSymbols();
403
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000404 /// FinishModule - Write out global data structures at the end of
405 /// processing a translation unit.
406 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000407
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000408 /// EmitClassExtension - Generate the class extension structure used
409 /// to store the weak ivar layout and properties. The return value
410 /// has type ClassExtensionPtrTy.
411 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
412
413 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
414 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000415 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000416 const ObjCInterfaceDecl *ID);
417
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000418 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000419 QualType ResultType,
420 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000421 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000422 QualType Arg0Ty,
423 bool IsSuper,
424 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000425
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000426 /// EmitIvarList - Emit the ivar list for the given
427 /// implementation. If ForClass is true the list of class ivars
428 /// (i.e. metaclass ivars) is emitted, otherwise the list of
429 /// interface ivars will be emitted. The return value has type
430 /// IvarListPtrTy.
431 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000432 bool ForClass);
433
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000434 /// EmitMetaClass - Emit a forward reference to the class structure
435 /// for the metaclass of the given interface. The return value has
436 /// type ClassPtrTy.
437 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
438
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000439 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000440 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000441 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
442 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000443 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000444 const ConstantVector &Methods);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000445
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000446 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000447
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000448 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000449
450 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000451 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000452 llvm::Constant *EmitMethodList(const std::string &Name,
453 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000454 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000455
456 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000457 /// method declarations.
458 /// - TypeName: The name for the type containing the methods.
459 /// - IsProtocol: True iff these methods are for a protocol.
460 /// - ClassMethds: True iff these are class methods.
461 /// - Required: When true, only "required" methods are
462 /// listed. Similarly, when false only "optional" methods are
463 /// listed. For classes this should always be true.
464 /// - begin, end: The method list to output.
465 ///
466 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000467 llvm::Constant *EmitMethodDescList(const std::string &Name,
468 const char *Section,
469 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000470
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000471 /// GetOrEmitProtocol - Get the protocol object for the given
472 /// declaration, emitting it if necessary. The return value has type
473 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000474 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000475
476 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
477 /// object for the given declaration, emitting it if needed. These
478 /// forward references will be filled in with empty bodies if no
479 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000480 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000481
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000482 /// EmitProtocolExtension - Generate the protocol extension
483 /// structure used to store optional instance and class methods, and
484 /// protocol properties. The return value has type
485 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000486 llvm::Constant *
487 EmitProtocolExtension(const ObjCProtocolDecl *PD,
488 const ConstantVector &OptInstanceMethods,
489 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000490
491 /// EmitProtocolList - Generate the list of referenced
492 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc933702008-08-21 21:57:41 +0000493 llvm::Constant *EmitProtocolList(const std::string &Name,
494 ObjCProtocolDecl::protocol_iterator begin,
495 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000496
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000497 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
498 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000499 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000500
Fariborz Jahanianda320092009-01-29 19:24:30 +0000501 public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000502 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000503
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000504 virtual llvm::Function *ModuleInitFunction();
505
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000506 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000507 QualType ResultType,
508 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000509 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000510 bool IsClassMessage,
511 const CallArgList &CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000512
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000513 virtual CodeGen::RValue
514 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000515 QualType ResultType,
516 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000517 const ObjCInterfaceDecl *Class,
518 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000519 bool IsClassMessage,
520 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000521
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000522 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000523 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000524
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000525 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000526
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000527 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000528
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000529 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000530
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000531 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000532 const ObjCProtocolDecl *PD);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000533
Daniel Dunbar49f66022008-09-24 03:38:44 +0000534 virtual llvm::Function *GetPropertyGetFunction();
535 virtual llvm::Function *GetPropertySetFunction();
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000536 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000537
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000538 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
539 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000540 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
541 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000542 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000543 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000544 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
545 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000546 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
547 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000548 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
549 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000550 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
551 llvm::Value *src, llvm::Value *dest);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000552};
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000553
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000554class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000555private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000556 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000557 llvm::GlobalVariable* ObjCEmptyCacheVar;
558 llvm::GlobalVariable* ObjCEmptyVtableVar;
559
560 /// FinishNonFragileABIModule - Write out global data structures at the end of
561 /// processing a translation unit.
562 void FinishNonFragileABIModule();
563
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000564 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
565 unsigned InstanceStart,
566 unsigned InstanceSize,
567 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +0000568 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
569 llvm::Constant *IsAGV,
570 llvm::Constant *SuperClassGV,
571 llvm::Constant *ClassRoGV);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000572
573 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
574
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +0000575 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
576
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000577 /// EmitMethodList - Emit the method list for the given
578 /// implementation. The return value has type MethodListnfABITy.
579 llvm::Constant *EmitMethodList(const std::string &Name,
580 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +0000581 const ConstantVector &Methods);
582 /// EmitIvarList - Emit the ivar list for the given
583 /// implementation. If ForClass is true the list of class ivars
584 /// (i.e. metaclass ivars) is emitted, otherwise the list of
585 /// interface ivars will be emitted. The return value has type
586 /// IvarListnfABIPtrTy.
587 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000588
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +0000589 llvm::Constant *EmitIvarOffsetVar(const ObjCImplementationDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +0000590 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +0000591 unsigned long int offset);
592
Fariborz Jahanianda320092009-01-29 19:24:30 +0000593 /// GetOrEmitProtocol - Get the protocol object for the given
594 /// declaration, emitting it if necessary. The return value has type
595 /// ProtocolPtrTy.
596 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
597
598 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
599 /// object for the given declaration, emitting it if needed. These
600 /// forward references will be filled in with empty bodies if no
601 /// definition is seen. The return value has type ProtocolPtrTy.
602 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
603
604 /// EmitProtocolList - Generate the list of referenced
605 /// protocols. The return value has type ProtocolListPtrTy.
606 llvm::Constant *EmitProtocolList(const std::string &Name,
607 ObjCProtocolDecl::protocol_iterator begin,
608 ObjCProtocolDecl::protocol_iterator end);
609
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000610public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000611 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000612 // FIXME. All stubs for now!
613 virtual llvm::Function *ModuleInitFunction();
614
615 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
616 QualType ResultType,
617 Selector Sel,
618 llvm::Value *Receiver,
619 bool IsClassMessage,
620 const CallArgList &CallArgs)
621 {return RValue::get(0);}
622
623 virtual CodeGen::RValue
624 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
625 QualType ResultType,
626 Selector Sel,
627 const ObjCInterfaceDecl *Class,
628 llvm::Value *Receiver,
629 bool IsClassMessage,
630 const CallArgList &CallArgs){ return RValue::get(0);}
631
632 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
633 const ObjCInterfaceDecl *ID){ return 0; }
634
635 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
636 { return 0; }
637
Fariborz Jahanianeb062d92009-01-26 18:32:24 +0000638 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000639
640 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000641 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +0000642 const ObjCProtocolDecl *PD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000643
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000644 virtual llvm::Function *GetPropertyGetFunction(){ return 0; }
645 virtual llvm::Function *GetPropertySetFunction()
646 { return 0; }
647 virtual llvm::Function *EnumerationMutationFunction()
648 { return 0; }
649
650 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
651 const Stmt &S)
652 { return; }
653 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
654 const ObjCAtThrowStmt &S)
655 { return; }
656 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
657 llvm::Value *AddrWeakObj)
658 { return 0; }
659 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
660 llvm::Value *src, llvm::Value *dst)
661 { return; }
662 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
663 llvm::Value *src, llvm::Value *dest)
664 { return; }
665 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
666 llvm::Value *src, llvm::Value *dest)
667 { return; }
668 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
669 llvm::Value *src, llvm::Value *dest)
670 { return; }
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000671};
672
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000673} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000674
675/* *** Helper Functions *** */
676
677/// getConstantGEP() - Help routine to construct simple GEPs.
678static llvm::Constant *getConstantGEP(llvm::Constant *C,
679 unsigned idx0,
680 unsigned idx1) {
681 llvm::Value *Idxs[] = {
682 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
683 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
684 };
685 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
686}
687
688/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000689
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000690CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
691 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000692{
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000693 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000694 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000695}
696
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000697/// GetClass - Return a reference to the class for the given interface
698/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000699llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000700 const ObjCInterfaceDecl *ID) {
701 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000702}
703
704/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000705llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000706 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000707}
708
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000709/// Generate a constant CFString object.
710/*
711 struct __builtin_CFString {
712 const int *isa; // point to __CFConstantStringClassReference
713 int flags;
714 const char *str;
715 long length;
716 };
717*/
718
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000719llvm::Constant *CGObjCCommonMac::GenerateConstantString(
720 const std::string &String) {
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000721 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000722}
723
724/// Generates a message send where the super is the receiver. This is
725/// a message send to self with special delivery semantics indicating
726/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000727CodeGen::RValue
728CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000729 QualType ResultType,
730 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000731 const ObjCInterfaceDecl *Class,
732 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000733 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000734 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000735 // Create and init a super structure; this is a (receiver, class)
736 // pair we will pass to objc_msgSendSuper.
737 llvm::Value *ObjCSuper =
738 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
739 llvm::Value *ReceiverAsObject =
740 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
741 CGF.Builder.CreateStore(ReceiverAsObject,
742 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000743
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000744 // If this is a class message the metaclass is passed as the target.
745 llvm::Value *Target;
746 if (IsClassMessage) {
747 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
748 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
749 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
750 Target = Super;
751 } else {
752 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
753 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000754 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
755 // and ObjCTypes types.
756 const llvm::Type *ClassTy =
757 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000758 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000759 CGF.Builder.CreateStore(Target,
760 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
761
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000762 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000763 ObjCSuper, ObjCTypes.SuperPtrCTy,
764 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000765}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000766
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000767/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000768CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000769 QualType ResultType,
770 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000771 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000772 bool IsClassMessage,
773 const CallArgList &CallArgs) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000774 llvm::Value *Arg0 =
775 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000776 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000777 Arg0, CGF.getContext().getObjCIdType(),
778 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000779}
780
781CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000782 QualType ResultType,
783 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000784 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000785 QualType Arg0Ty,
786 bool IsSuper,
787 const CallArgList &CallArgs) {
788 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000789 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
790 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
791 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000792 CGF.getContext().getObjCSelType()));
793 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000794
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000795 const llvm::FunctionType *FTy =
796 CGM.getTypes().GetFunctionType(CGCallInfo(ResultType, ActualArgs),
797 false);
Daniel Dunbar5669e572008-10-17 03:24:53 +0000798
799 llvm::Constant *Fn;
800 if (CGM.ReturnTypeUsesSret(ResultType)) {
801 Fn = ObjCTypes.getSendStretFn(IsSuper);
802 } else if (ResultType->isFloatingType()) {
803 // FIXME: Sadly, this is wrong. This actually depends on the
804 // architecture. This happens to be right for x86-32 though.
805 Fn = ObjCTypes.getSendFpretFn(IsSuper);
806 } else {
807 Fn = ObjCTypes.getSendFn(IsSuper);
808 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000809 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar3913f182008-09-09 23:48:28 +0000810 return CGF.EmitCall(Fn, ResultType, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000811}
812
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000813llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000814 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +0000815 // FIXME: I don't understand why gcc generates this, or where it is
816 // resolved. Investigate. Its also wasteful to look this up over and
817 // over.
818 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
819
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000820 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
821 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000822}
823
Fariborz Jahanianda320092009-01-29 19:24:30 +0000824void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000825 // FIXME: We shouldn't need this, the protocol decl should contain
826 // enough information to tell us whether this was a declaration or a
827 // definition.
828 DefinedProtocols.insert(PD->getIdentifier());
829
830 // If we have generated a forward reference to this protocol, emit
831 // it now. Otherwise do nothing, the protocol objects are lazily
832 // emitted.
833 if (Protocols.count(PD->getIdentifier()))
834 GetOrEmitProtocol(PD);
835}
836
Fariborz Jahanianda320092009-01-29 19:24:30 +0000837llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000838 if (DefinedProtocols.count(PD->getIdentifier()))
839 return GetOrEmitProtocol(PD);
840 return GetOrEmitProtocolRef(PD);
841}
842
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000843/*
844 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
845 struct _objc_protocol {
846 struct _objc_protocol_extension *isa;
847 char *protocol_name;
848 struct _objc_protocol_list *protocol_list;
849 struct _objc__method_prototype_list *instance_methods;
850 struct _objc__method_prototype_list *class_methods
851 };
852
853 See EmitProtocolExtension().
854*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000855llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
856 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
857
858 // Early exit if a defining object has already been generated.
859 if (Entry && Entry->hasInitializer())
860 return Entry;
861
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000862 // FIXME: I don't understand why gcc generates this, or where it is
863 // resolved. Investigate. Its also wasteful to look this up over and
864 // over.
865 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
866
Chris Lattner8ec03f52008-11-24 03:54:41 +0000867 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000868
869 // Construct method lists.
870 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
871 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
872 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
873 e = PD->instmeth_end(); i != e; ++i) {
874 ObjCMethodDecl *MD = *i;
875 llvm::Constant *C = GetMethodDescriptionConstant(MD);
876 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
877 OptInstanceMethods.push_back(C);
878 } else {
879 InstanceMethods.push_back(C);
880 }
881 }
882
883 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
884 e = PD->classmeth_end(); i != e; ++i) {
885 ObjCMethodDecl *MD = *i;
886 llvm::Constant *C = GetMethodDescriptionConstant(MD);
887 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
888 OptClassMethods.push_back(C);
889 } else {
890 ClassMethods.push_back(C);
891 }
892 }
893
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000894 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000895 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000896 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +0000897 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000898 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +0000899 PD->protocol_begin(),
900 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000901 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000902 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
903 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000904 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
905 InstanceMethods);
906 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000907 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
908 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000909 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
910 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000911 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
912 Values);
913
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000914 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000915 // Already created, fix the linkage and update the initializer.
916 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000917 Entry->setInitializer(Init);
918 } else {
919 Entry =
920 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
921 llvm::GlobalValue::InternalLinkage,
922 Init,
923 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
924 &CGM.getModule());
925 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
926 UsedGlobals.push_back(Entry);
927 // FIXME: Is this necessary? Why only for protocol?
928 Entry->setAlignment(4);
929 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000930
931 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000932}
933
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000934llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000935 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
936
937 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000938 // We use the initializer as a marker of whether this is a forward
939 // reference or not. At module finalization we add the empty
940 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000941 Entry =
942 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000943 llvm::GlobalValue::ExternalLinkage,
944 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000945 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000946 &CGM.getModule());
947 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
948 UsedGlobals.push_back(Entry);
949 // FIXME: Is this necessary? Why only for protocol?
950 Entry->setAlignment(4);
951 }
952
953 return Entry;
954}
955
956/*
957 struct _objc_protocol_extension {
958 uint32_t size;
959 struct objc_method_description_list *optional_instance_methods;
960 struct objc_method_description_list *optional_class_methods;
961 struct objc_property_list *instance_properties;
962 };
963*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000964llvm::Constant *
965CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
966 const ConstantVector &OptInstanceMethods,
967 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000968 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +0000969 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000970 std::vector<llvm::Constant*> Values(4);
971 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000972 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000973 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
974 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000975 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
976 OptInstanceMethods);
977 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000978 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
979 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000980 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
981 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000982 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
983 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000984 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000985
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000986 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000987 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
988 Values[3]->isNullValue())
989 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
990
991 llvm::Constant *Init =
992 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
993 llvm::GlobalVariable *GV =
994 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
995 llvm::GlobalValue::InternalLinkage,
996 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000997 "\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000998 &CGM.getModule());
999 // No special section, but goes in llvm.used
1000 UsedGlobals.push_back(GV);
1001
1002 return GV;
1003}
1004
1005/*
1006 struct objc_protocol_list {
1007 struct objc_protocol_list *next;
1008 long count;
1009 Protocol *list[];
1010 };
1011*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001012llvm::Constant *
1013CGObjCMac::EmitProtocolList(const std::string &Name,
1014 ObjCProtocolDecl::protocol_iterator begin,
1015 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001016 std::vector<llvm::Constant*> ProtocolRefs;
1017
Daniel Dunbardbc933702008-08-21 21:57:41 +00001018 for (; begin != end; ++begin)
1019 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001020
1021 // Just return null for empty protocol lists
1022 if (ProtocolRefs.empty())
1023 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1024
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001025 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001026 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1027
1028 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001029 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001030 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1031 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1032 Values[2] =
1033 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1034 ProtocolRefs.size()),
1035 ProtocolRefs);
1036
1037 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1038 llvm::GlobalVariable *GV =
1039 new llvm::GlobalVariable(Init->getType(), false,
1040 llvm::GlobalValue::InternalLinkage,
1041 Init,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001042 Name,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001043 &CGM.getModule());
1044 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
1045 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1046}
1047
1048/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001049 struct _objc_property {
1050 const char * const name;
1051 const char * const attributes;
1052 };
1053
1054 struct _objc_property_list {
1055 uint32_t entsize; // sizeof (struct _objc_property)
1056 uint32_t prop_count;
1057 struct _objc_property[prop_count];
1058 };
1059*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001060llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1061 const Decl *Container,
1062 const ObjCContainerDecl *OCD,
1063 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001064 std::vector<llvm::Constant*> Properties, Prop(2);
Steve Naroff93983f82009-01-11 12:47:58 +00001065 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1066 E = OCD->prop_end(); I != E; ++I) {
1067 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001068 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001069 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001070 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1071 Prop));
1072 }
1073
1074 // Return null for empty list.
1075 if (Properties.empty())
1076 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1077
1078 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001079 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001080 std::vector<llvm::Constant*> Values(3);
1081 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1082 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1083 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1084 Properties.size());
1085 Values[2] = llvm::ConstantArray::get(AT, Properties);
1086 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1087
1088 llvm::GlobalVariable *GV =
1089 new llvm::GlobalVariable(Init->getType(), false,
1090 llvm::GlobalValue::InternalLinkage,
1091 Init,
1092 Name,
1093 &CGM.getModule());
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001094 if (ObjCABI == 2)
1095 GV->setSection("__DATA, __objc_const");
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001096 // No special section on property lists?
1097 UsedGlobals.push_back(GV);
1098 return llvm::ConstantExpr::getBitCast(GV,
1099 ObjCTypes.PropertyListPtrTy);
1100
1101}
1102
1103/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001104 struct objc_method_description_list {
1105 int count;
1106 struct objc_method_description list[];
1107 };
1108*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001109llvm::Constant *
1110CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1111 std::vector<llvm::Constant*> Desc(2);
1112 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1113 ObjCTypes.SelectorPtrTy);
1114 Desc[1] = GetMethodVarType(MD);
1115 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1116 Desc);
1117}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001118
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001119llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1120 const char *Section,
1121 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001122 // Return null for empty list.
1123 if (Methods.empty())
1124 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1125
1126 std::vector<llvm::Constant*> Values(2);
1127 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1128 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1129 Methods.size());
1130 Values[1] = llvm::ConstantArray::get(AT, Methods);
1131 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1132
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001133 llvm::GlobalVariable *GV =
1134 new llvm::GlobalVariable(Init->getType(), false,
1135 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001136 Init, Name, &CGM.getModule());
1137 GV->setSection(Section);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001138 UsedGlobals.push_back(GV);
1139 return llvm::ConstantExpr::getBitCast(GV,
1140 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001141}
1142
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001143/*
1144 struct _objc_category {
1145 char *category_name;
1146 char *class_name;
1147 struct _objc_method_list *instance_methods;
1148 struct _objc_method_list *class_methods;
1149 struct _objc_protocol_list *protocols;
1150 uint32_t size; // <rdar://4585769>
1151 struct _objc_property_list *instance_properties;
1152 };
1153 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001154void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001155 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001156
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001157 // FIXME: This is poor design, the OCD should have a pointer to the
1158 // category decl. Additionally, note that Category can be null for
1159 // the @implementation w/o an @interface case. Sema should just
1160 // create one for us as it does for @implementation so everyone else
1161 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001162 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001163 const ObjCCategoryDecl *Category =
1164 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001165 std::string ExtName(Interface->getNameAsString() + "_" +
1166 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001167
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001168 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1169 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
1170 e = OCD->instmeth_end(); i != e; ++i) {
1171 // Instance methods should always be defined.
1172 InstanceMethods.push_back(GetMethodConstant(*i));
1173 }
1174 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
1175 e = OCD->classmeth_end(); i != e; ++i) {
1176 // Class methods should always be defined.
1177 ClassMethods.push_back(GetMethodConstant(*i));
1178 }
1179
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001180 std::vector<llvm::Constant*> Values(7);
1181 Values[0] = GetClassName(OCD->getIdentifier());
1182 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001183 Values[2] =
1184 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1185 ExtName,
1186 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001187 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001188 Values[3] =
1189 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
1190 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001191 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001192 if (Category) {
1193 Values[4] =
1194 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1195 Category->protocol_begin(),
1196 Category->protocol_end());
1197 } else {
1198 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1199 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001200 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001201
1202 // If there is no category @interface then there can be no properties.
1203 if (Category) {
1204 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001205 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001206 } else {
1207 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1208 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001209
1210 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1211 Values);
1212
1213 llvm::GlobalVariable *GV =
1214 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
1215 llvm::GlobalValue::InternalLinkage,
1216 Init,
1217 std::string("\01L_OBJC_CATEGORY_")+ExtName,
1218 &CGM.getModule());
1219 GV->setSection("__OBJC,__category,regular,no_dead_strip");
1220 UsedGlobals.push_back(GV);
1221 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001222}
1223
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001224// FIXME: Get from somewhere?
1225enum ClassFlags {
1226 eClassFlags_Factory = 0x00001,
1227 eClassFlags_Meta = 0x00002,
1228 // <rdr://5142207>
1229 eClassFlags_HasCXXStructors = 0x02000,
1230 eClassFlags_Hidden = 0x20000,
1231 eClassFlags_ABI2_Hidden = 0x00010,
1232 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1233};
1234
1235// <rdr://5142207&4705298&4843145>
1236static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
1237 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
1238 // FIXME: Support -fvisibility
1239 switch (attr->getVisibility()) {
1240 default:
1241 assert(0 && "Unknown visibility");
1242 return false;
1243 case VisibilityAttr::DefaultVisibility:
1244 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
1245 return false;
1246 case VisibilityAttr::HiddenVisibility:
1247 return true;
1248 }
1249 } else {
1250 return false; // FIXME: Support -fvisibility
1251 }
1252}
1253
1254/*
1255 struct _objc_class {
1256 Class isa;
1257 Class super_class;
1258 const char *name;
1259 long version;
1260 long info;
1261 long instance_size;
1262 struct _objc_ivar_list *ivars;
1263 struct _objc_method_list *methods;
1264 struct _objc_cache *cache;
1265 struct _objc_protocol_list *protocols;
1266 // Objective-C 1.0 extensions (<rdr://4585769>)
1267 const char *ivar_layout;
1268 struct _objc_class_ext *ext;
1269 };
1270
1271 See EmitClassExtension();
1272 */
1273void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001274 DefinedSymbols.insert(ID->getIdentifier());
1275
Chris Lattner8ec03f52008-11-24 03:54:41 +00001276 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001277 // FIXME: Gross
1278 ObjCInterfaceDecl *Interface =
1279 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001280 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001281 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001282 Interface->protocol_begin(),
1283 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001284 const llvm::Type *InterfaceTy =
1285 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
1286 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001287 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001288
1289 // FIXME: Set CXX-structors flag.
1290 if (IsClassHidden(ID->getClassInterface()))
1291 Flags |= eClassFlags_Hidden;
1292
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001293 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1294 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
1295 e = ID->instmeth_end(); i != e; ++i) {
1296 // Instance methods should always be defined.
1297 InstanceMethods.push_back(GetMethodConstant(*i));
1298 }
1299 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
1300 e = ID->classmeth_end(); i != e; ++i) {
1301 // Class methods should always be defined.
1302 ClassMethods.push_back(GetMethodConstant(*i));
1303 }
1304
1305 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
1306 e = ID->propimpl_end(); i != e; ++i) {
1307 ObjCPropertyImplDecl *PID = *i;
1308
1309 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1310 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1311
1312 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1313 if (llvm::Constant *C = GetMethodConstant(MD))
1314 InstanceMethods.push_back(C);
1315 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1316 if (llvm::Constant *C = GetMethodConstant(MD))
1317 InstanceMethods.push_back(C);
1318 }
1319 }
1320
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001321 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001322 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001323 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001324 // Record a reference to the super class.
1325 LazySymbols.insert(Super->getIdentifier());
1326
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001327 Values[ 1] =
1328 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1329 ObjCTypes.ClassPtrTy);
1330 } else {
1331 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1332 }
1333 Values[ 2] = GetClassName(ID->getIdentifier());
1334 // Version is always 0.
1335 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1336 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1337 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001338 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001339 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001340 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001341 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001342 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001343 // cache is always NULL.
1344 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1345 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001346 // FIXME: Set ivar_layout
1347 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001348 Values[11] = EmitClassExtension(ID);
1349 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1350 Values);
1351
1352 llvm::GlobalVariable *GV =
1353 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1354 llvm::GlobalValue::InternalLinkage,
1355 Init,
1356 std::string("\01L_OBJC_CLASS_")+ClassName,
1357 &CGM.getModule());
1358 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1359 UsedGlobals.push_back(GV);
1360 // FIXME: Why?
1361 GV->setAlignment(32);
1362 DefinedClasses.push_back(GV);
1363}
1364
1365llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1366 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001367 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001368 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001369 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001370 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001371
1372 if (IsClassHidden(ID->getClassInterface()))
1373 Flags |= eClassFlags_Hidden;
1374
1375 std::vector<llvm::Constant*> Values(12);
1376 // The isa for the metaclass is the root of the hierarchy.
1377 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1378 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1379 Root = Super;
1380 Values[ 0] =
1381 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1382 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001383 // The super class for the metaclass is emitted as the name of the
1384 // super class. The runtime fixes this up to point to the
1385 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001386 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1387 Values[ 1] =
1388 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1389 ObjCTypes.ClassPtrTy);
1390 } else {
1391 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1392 }
1393 Values[ 2] = GetClassName(ID->getIdentifier());
1394 // Version is always 0.
1395 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1396 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1397 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001398 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001399 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001400 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001401 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001402 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001403 // cache is always NULL.
1404 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1405 Values[ 9] = Protocols;
1406 // ivar_layout for metaclass is always NULL.
1407 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1408 // The class extension is always unused for metaclasses.
1409 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1410 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1411 Values);
1412
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001413 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00001414 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001415
1416 // Check for a forward reference.
1417 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1418 if (GV) {
1419 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1420 "Forward metaclass reference has incorrect type.");
1421 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1422 GV->setInitializer(Init);
1423 } else {
1424 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1425 llvm::GlobalValue::InternalLinkage,
1426 Init, Name,
1427 &CGM.getModule());
1428 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001429 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1430 UsedGlobals.push_back(GV);
1431 // FIXME: Why?
1432 GV->setAlignment(32);
1433
1434 return GV;
1435}
1436
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001437llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001438 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001439
1440 // FIXME: Should we look these up somewhere other than the
1441 // module. Its a bit silly since we only generate these while
1442 // processing an implementation, so exactly one pointer would work
1443 // if know when we entered/exitted an implementation block.
1444
1445 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00001446 // Previously, metaclass with internal linkage may have been defined.
1447 // pass 'true' as 2nd argument so it is returned.
1448 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001449 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1450 "Forward metaclass reference has incorrect type.");
1451 return GV;
1452 } else {
1453 // Generate as an external reference to keep a consistent
1454 // module. This will be patched up when we emit the metaclass.
1455 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1456 llvm::GlobalValue::ExternalLinkage,
1457 0,
1458 Name,
1459 &CGM.getModule());
1460 }
1461}
1462
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001463/*
1464 struct objc_class_ext {
1465 uint32_t size;
1466 const char *weak_ivar_layout;
1467 struct _objc_property_list *properties;
1468 };
1469*/
1470llvm::Constant *
1471CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1472 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001473 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001474
1475 std::vector<llvm::Constant*> Values(3);
1476 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001477 // FIXME: Output weak_ivar_layout string.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001478 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001479 Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001480 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001481
1482 // Return null if no extension bits are used.
1483 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1484 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1485
1486 llvm::Constant *Init =
1487 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1488 llvm::GlobalVariable *GV =
1489 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1490 llvm::GlobalValue::InternalLinkage,
1491 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001492 "\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001493 &CGM.getModule());
1494 // No special section, but goes in llvm.used
1495 UsedGlobals.push_back(GV);
1496
1497 return GV;
1498}
1499
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001500/// countInheritedIvars - count number of ivars in class and its super class(s)
1501///
1502static int countInheritedIvars(const ObjCInterfaceDecl *OI) {
1503 int count = 0;
1504 if (!OI)
1505 return 0;
1506 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
1507 if (SuperClass)
1508 count += countInheritedIvars(SuperClass);
1509 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1510 E = OI->ivar_end(); I != E; ++I)
1511 ++count;
1512 return count;
1513}
1514
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001515/*
1516 struct objc_ivar {
1517 char *ivar_name;
1518 char *ivar_type;
1519 int ivar_offset;
1520 };
1521
1522 struct objc_ivar_list {
1523 int ivar_count;
1524 struct objc_ivar list[count];
1525 };
1526 */
1527llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001528 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001529 std::vector<llvm::Constant*> Ivars, Ivar(3);
1530
1531 // When emitting the root class GCC emits ivar entries for the
1532 // actual class structure. It is not clear if we need to follow this
1533 // behavior; for now lets try and get away with not doing it. If so,
1534 // the cleanest solution would be to make up an ObjCInterfaceDecl
1535 // for the class.
1536 if (ForClass)
1537 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001538
1539 ObjCInterfaceDecl *OID =
1540 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
1541 const llvm::Type *InterfaceTy =
1542 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001543 const llvm::StructLayout *Layout =
1544 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001545
1546 RecordDecl::field_iterator ifield, pfield;
1547 const RecordDecl *RD = GetFirstIvarInRecord(OID, ifield, pfield);
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001548 for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) {
1549 FieldDecl *Field = *ifield;
1550 unsigned Offset = Layout->getElementOffset(CGM.getTypes().
1551 getLLVMFieldNo(Field));
1552 if (Field->getIdentifier())
1553 Ivar[0] = GetMethodVarName(Field->getIdentifier());
1554 else
1555 Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001556 std::string TypeStr;
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001557 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001558 Ivar[1] = GetMethodVarType(TypeStr);
1559 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001560 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001561 }
1562
1563 // Return null for empty list.
1564 if (Ivars.empty())
1565 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1566
1567 std::vector<llvm::Constant*> Values(2);
1568 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1569 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1570 Ivars.size());
1571 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1572 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1573
1574 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1575 "\01L_OBJC_INSTANCE_VARIABLES_");
1576 llvm::GlobalVariable *GV =
1577 new llvm::GlobalVariable(Init->getType(), false,
1578 llvm::GlobalValue::InternalLinkage,
1579 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001580 Prefix + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001581 &CGM.getModule());
1582 if (ForClass) {
1583 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1584 // FIXME: Why is this only here?
1585 GV->setAlignment(32);
1586 } else {
1587 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1588 }
1589 UsedGlobals.push_back(GV);
1590 return llvm::ConstantExpr::getBitCast(GV,
1591 ObjCTypes.IvarListPtrTy);
1592}
1593
1594/*
1595 struct objc_method {
1596 SEL method_name;
1597 char *method_types;
1598 void *method;
1599 };
1600
1601 struct objc_method_list {
1602 struct objc_method_list *obsolete;
1603 int count;
1604 struct objc_method methods_list[count];
1605 };
1606*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001607
1608/// GetMethodConstant - Return a struct objc_method constant for the
1609/// given method if it has been defined. The result is null if the
1610/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001611llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001612 // FIXME: Use DenseMap::lookup
1613 llvm::Function *Fn = MethodDefinitions[MD];
1614 if (!Fn)
1615 return 0;
1616
1617 std::vector<llvm::Constant*> Method(3);
1618 Method[0] =
1619 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1620 ObjCTypes.SelectorPtrTy);
1621 Method[1] = GetMethodVarType(MD);
1622 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1623 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1624}
1625
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001626llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1627 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001628 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001629 // Return null for empty list.
1630 if (Methods.empty())
1631 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1632
1633 std::vector<llvm::Constant*> Values(3);
1634 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1635 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1636 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1637 Methods.size());
1638 Values[2] = llvm::ConstantArray::get(AT, Methods);
1639 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1640
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001641 llvm::GlobalVariable *GV =
1642 new llvm::GlobalVariable(Init->getType(), false,
1643 llvm::GlobalValue::InternalLinkage,
1644 Init,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001645 Name,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001646 &CGM.getModule());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001647 GV->setSection(Section);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001648 UsedGlobals.push_back(GV);
1649 return llvm::ConstantExpr::getBitCast(GV,
1650 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001651}
1652
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001653llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001654 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001655 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001656 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001657
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001658 const llvm::FunctionType *MethodTy =
1659 CGM.getTypes().GetFunctionType(CGFunctionInfo(OMD, CGM.getContext()));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001660 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001661 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001662 llvm::GlobalValue::InternalLinkage,
1663 Name,
1664 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001665 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001666
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001667 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001668}
1669
1670llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001671 // Abuse this interface function as a place to finalize.
1672 FinishModule();
1673
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001674 return NULL;
1675}
1676
Daniel Dunbar49f66022008-09-24 03:38:44 +00001677llvm::Function *CGObjCMac::GetPropertyGetFunction() {
1678 return ObjCTypes.GetPropertyFn;
1679}
1680
1681llvm::Function *CGObjCMac::GetPropertySetFunction() {
1682 return ObjCTypes.SetPropertyFn;
1683}
1684
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001685llvm::Function *CGObjCMac::EnumerationMutationFunction()
1686{
1687 return ObjCTypes.EnumerationMutationFn;
1688}
1689
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001690/*
1691
1692Objective-C setjmp-longjmp (sjlj) Exception Handling
1693--
1694
1695The basic framework for a @try-catch-finally is as follows:
1696{
1697 objc_exception_data d;
1698 id _rethrow = null;
1699
1700 objc_exception_try_enter(&d);
1701 if (!setjmp(d.jmp_buf)) {
1702 ... try body ...
1703 } else {
1704 // exception path
1705 id _caught = objc_exception_extract(&d);
1706
1707 // enter new try scope for handlers
1708 if (!setjmp(d.jmp_buf)) {
1709 ... match exception and execute catch blocks ...
1710
1711 // fell off end, rethrow.
1712 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00001713 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001714 } else {
1715 // exception in catch block
1716 _rethrow = objc_exception_extract(&d);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001717 ... jump-through-finally_no_exit to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001718 }
1719 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00001720 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001721
1722finally:
1723 // match either the initial try_enter or the catch try_enter,
1724 // depending on the path followed.
1725 objc_exception_try_exit(&d);
1726finally_no_exit:
1727 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00001728 ... dispatch to finally destination ...
1729
1730finally_rethrow:
1731 objc_exception_throw(_rethrow);
1732
1733finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001734}
1735
1736This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00001737uses _rethrow to determine if objc_exception_try_exit should be called
1738and if the object should be rethrown. This breaks in the face of
1739throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001740
1741We specialize this framework for a few particular circumstances:
1742
1743 - If there are no catch blocks, then we avoid emitting the second
1744 exception handling context.
1745
1746 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
1747 e)) we avoid emitting the code to rethrow an uncaught exception.
1748
1749 - FIXME: If there is no @finally block we can do a few more
1750 simplifications.
1751
1752Rethrows and Jumps-Through-Finally
1753--
1754
1755Support for implicit rethrows and jumping through the finally block is
1756handled by storing the current exception-handling context in
1757ObjCEHStack.
1758
Daniel Dunbar898d5082008-09-30 01:06:03 +00001759In order to implement proper @finally semantics, we support one basic
1760mechanism for jumping through the finally block to an arbitrary
1761destination. Constructs which generate exits from a @try or @catch
1762block use this mechanism to implement the proper semantics by chaining
1763jumps, as necessary.
1764
1765This mechanism works like the one used for indirect goto: we
1766arbitrarily assign an ID to each destination and store the ID for the
1767destination in a variable prior to entering the finally block. At the
1768end of the finally block we simply create a switch to the proper
1769destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001770
1771Code gen for @synchronized(expr) stmt;
1772Effectively generating code for:
1773objc_sync_enter(expr);
1774@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001775*/
1776
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001777void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1778 const Stmt &S) {
1779 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001780 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001781 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
1782 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
1783 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1784 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001785 llvm::Value *DestCode =
1786 CGF.CreateTempAlloca(llvm::Type::Int32Ty, "finally.dst");
1787
1788 // Generate jump code. Done here so we can directly add things to
1789 // the switch instruction.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001790 llvm::BasicBlock *FinallyJump = CGF.createBasicBlock("finally.jump");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001791 llvm::SwitchInst *FinallySwitch =
1792 llvm::SwitchInst::Create(new llvm::LoadInst(DestCode, "", FinallyJump),
1793 FinallyEnd, 10, FinallyJump);
1794
1795 // Push an EH context entry, used for handling rethrows and jumps
1796 // through finally.
1797 CodeGenFunction::ObjCEHEntry EHEntry(FinallyBlock, FinallyNoExit,
1798 FinallySwitch, DestCode);
1799 CGF.ObjCEHStack.push_back(&EHEntry);
1800
1801 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00001802 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
1803 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001804 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
1805 "_rethrow");
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001806 if (!isTry) {
1807 // For @synchronized, call objc_sync_enter(sync.expr)
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001808 llvm::Value *Arg = CGF.EmitScalarExpr(
1809 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1810 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1811 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, Arg);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001812 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001813
1814 // Enter a new try block and call setjmp.
1815 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
1816 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
1817 "jmpbufarray");
1818 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
1819 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1820 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001821
Daniel Dunbar55e87422008-11-11 02:29:29 +00001822 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1823 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001824 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001825 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001826
1827 // Emit the @try block.
1828 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001829 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
1830 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001831 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001832
1833 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001834 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001835
1836 // Retrieve the exception object. We may emit multiple blocks but
1837 // nothing can cross this so the value is already in SSA form.
1838 llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1839 ExceptionData,
1840 "caught");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001841 EHEntry.Exception = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001842 if (!isTry)
1843 {
1844 CGF.Builder.CreateStore(Caught, RethrowPtr);
1845 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
1846 }
1847 else if (const ObjCAtCatchStmt* CatchStmt =
1848 cast<ObjCAtTryStmt>(S).getCatchStmts())
1849 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00001850 // Enter a new exception try block (in case a @catch block throws
1851 // an exception).
Anders Carlsson80f25672008-09-09 17:59:25 +00001852 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001853
Anders Carlsson80f25672008-09-09 17:59:25 +00001854 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1855 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001856 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00001857
Daniel Dunbar55e87422008-11-11 02:29:29 +00001858 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
1859 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001860 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001861
1862 CGF.EmitBlock(CatchBlock);
1863
Daniel Dunbar55e40722008-09-27 07:03:52 +00001864 // Handle catch list. As a special case we check if everything is
1865 // matched and avoid generating code for falling off the end if
1866 // so.
1867 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00001868 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00001869 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00001870
Anders Carlssondde0a942008-09-11 09:15:33 +00001871 const DeclStmt *CatchParam =
1872 cast_or_null<DeclStmt>(CatchStmt->getCatchParamStmt());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001873 const VarDecl *VD = 0;
1874 const PointerType *PT = 0;
1875
Anders Carlsson80f25672008-09-09 17:59:25 +00001876 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00001877 if (!CatchParam) {
1878 AllMatched = true;
1879 } else {
Ted Kremenekde3b8fb2008-10-06 20:58:56 +00001880 VD = cast<VarDecl>(CatchParam->getSolitaryDecl());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001881 PT = VD->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001882
Daniel Dunbar97f61d12008-09-27 22:21:14 +00001883 // catch(id e) always matches.
1884 // FIXME: For the time being we also match id<X>; this should
1885 // be rejected by Sema instead.
1886 if ((PT && CGF.getContext().isObjCIdType(PT->getPointeeType())) ||
1887 VD->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00001888 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00001889 }
1890
Daniel Dunbar55e40722008-09-27 07:03:52 +00001891 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00001892 if (CatchParam) {
1893 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001894 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar129271a2008-09-27 07:36:24 +00001895 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00001896 }
Anders Carlsson1452f552008-09-11 08:21:54 +00001897
Anders Carlssondde0a942008-09-11 09:15:33 +00001898 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001899 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001900 break;
1901 }
1902
Daniel Dunbar129271a2008-09-27 07:36:24 +00001903 assert(PT && "Unexpected non-pointer type in @catch");
1904 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00001905 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001906 assert(ObjCType && "Catch parameter must have Objective-C type!");
1907
1908 // Check if the @catch block matches the exception object.
1909 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
1910
Anders Carlsson80f25672008-09-09 17:59:25 +00001911 llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn,
1912 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00001913
Daniel Dunbar55e87422008-11-11 02:29:29 +00001914 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00001915
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001916 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001917 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001918
1919 // Emit the @catch block.
1920 CGF.EmitBlock(MatchedBlock);
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001921 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001922 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001923
1924 llvm::Value *Tmp =
1925 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(VD->getType()),
1926 "tmp");
1927 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00001928
1929 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001930 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001931
1932 CGF.EmitBlock(NextCatchBlock);
1933 }
1934
Daniel Dunbar55e40722008-09-27 07:03:52 +00001935 if (!AllMatched) {
1936 // None of the handlers caught the exception, so store it to be
1937 // rethrown at the end of the @finally block.
1938 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001939 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001940 }
1941
1942 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001943 CGF.EmitBlock(CatchHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001944 CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1945 ExceptionData),
1946 RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001947 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001948 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00001949 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001950 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Anders Carlsson80f25672008-09-09 17:59:25 +00001951 }
1952
Daniel Dunbar898d5082008-09-30 01:06:03 +00001953 // Pop the exception-handling stack entry. It is important to do
1954 // this now, because the code in the @finally block is not in this
1955 // context.
1956 CGF.ObjCEHStack.pop_back();
1957
Anders Carlsson80f25672008-09-09 17:59:25 +00001958 // Emit the @finally block.
1959 CGF.EmitBlock(FinallyBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001960 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00001961
1962 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001963 if (isTry) {
1964 if (const ObjCAtFinallyStmt* FinallyStmt =
1965 cast<ObjCAtTryStmt>(S).getFinallyStmt())
1966 CGF.EmitStmt(FinallyStmt->getFinallyBody());
1967 }
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001968 else {
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001969 // For @synchronized objc_sync_exit(expr); As finally's sole statement.
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001970 // For @synchronized, call objc_sync_enter(sync.expr)
1971 llvm::Value *Arg = CGF.EmitScalarExpr(
1972 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1973 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1974 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, Arg);
1975 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001976
Daniel Dunbar898d5082008-09-30 01:06:03 +00001977 CGF.EmitBlock(FinallyJump);
1978
1979 CGF.EmitBlock(FinallyRethrow);
1980 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn,
1981 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001982 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00001983
1984 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001985}
1986
1987void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00001988 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00001989 llvm::Value *ExceptionAsObject;
1990
1991 if (const Expr *ThrowExpr = S.getThrowExpr()) {
1992 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
1993 ExceptionAsObject =
1994 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
1995 } else {
Daniel Dunbar898d5082008-09-30 01:06:03 +00001996 assert((!CGF.ObjCEHStack.empty() && CGF.ObjCEHStack.back()->Exception) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001997 "Unexpected rethrow outside @catch block.");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001998 ExceptionAsObject = CGF.ObjCEHStack.back()->Exception;
Anders Carlsson2b1e3112008-09-09 16:16:55 +00001999 }
2000
2001 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002002 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002003
2004 // Clear the insertion point to indicate we are in unreachable code.
2005 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002006}
2007
Daniel Dunbar898d5082008-09-30 01:06:03 +00002008void CodeGenFunction::EmitJumpThroughFinally(ObjCEHEntry *E,
2009 llvm::BasicBlock *Dst,
2010 bool ExecuteTryExit) {
Daniel Dunbara448fb22008-11-11 23:11:34 +00002011 if (!HaveInsertPoint())
Daniel Dunbar898d5082008-09-30 01:06:03 +00002012 return;
2013
2014 // Find the destination code for this block. We always use 0 for the
2015 // fallthrough block (default destination).
2016 llvm::SwitchInst *SI = E->FinallySwitch;
2017 llvm::ConstantInt *ID;
2018 if (Dst == SI->getDefaultDest()) {
2019 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
2020 } else {
2021 ID = SI->findCaseDest(Dst);
2022 if (!ID) {
2023 // No code found, get a new unique one by just using the number
2024 // of switch successors.
2025 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, SI->getNumSuccessors());
2026 SI->addCase(ID, Dst);
2027 }
2028 }
2029
2030 // Set the destination code and branch.
2031 Builder.CreateStore(ID, E->DestCode);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002032 EmitBranch(ExecuteTryExit ? E->FinallyBlock : E->FinallyNoExit);
Daniel Dunbar898d5082008-09-30 01:06:03 +00002033}
2034
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002035/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002036/// object: objc_read_weak (id *src)
2037///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002038llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002039 llvm::Value *AddrWeakObj)
2040{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002041 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002042 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002043 AddrWeakObj, "weakread");
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002044 return read_weak;
2045}
2046
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002047/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2048/// objc_assign_weak (id src, id *dst)
2049///
2050void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2051 llvm::Value *src, llvm::Value *dst)
2052{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002053 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2054 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002055 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
2056 src, dst, "weakassign");
2057 return;
2058}
2059
Fariborz Jahanian58626502008-11-19 00:59:10 +00002060/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2061/// objc_assign_global (id src, id *dst)
2062///
2063void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2064 llvm::Value *src, llvm::Value *dst)
2065{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002066 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2067 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002068 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
2069 src, dst, "globalassign");
2070 return;
2071}
2072
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002073/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2074/// objc_assign_ivar (id src, id *dst)
2075///
2076void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2077 llvm::Value *src, llvm::Value *dst)
2078{
2079 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2080 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
2081 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
2082 src, dst, "assignivar");
2083 return;
2084}
2085
Fariborz Jahanian58626502008-11-19 00:59:10 +00002086/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2087/// objc_assign_strongCast (id src, id *dst)
2088///
2089void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2090 llvm::Value *src, llvm::Value *dst)
2091{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002092 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2093 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002094 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
2095 src, dst, "weakassign");
2096 return;
2097}
2098
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002099/* *** Private Interface *** */
2100
2101/// EmitImageInfo - Emit the image info marker used to encode some module
2102/// level information.
2103///
2104/// See: <rdr://4810609&4810587&4810587>
2105/// struct IMAGE_INFO {
2106/// unsigned version;
2107/// unsigned flags;
2108/// };
2109enum ImageInfoFlags {
2110 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
2111 eImageInfo_GarbageCollected = (1 << 1),
2112 eImageInfo_GCOnly = (1 << 2)
2113};
2114
2115void CGObjCMac::EmitImageInfo() {
2116 unsigned version = 0; // Version is unused?
2117 unsigned flags = 0;
2118
2119 // FIXME: Fix and continue?
2120 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2121 flags |= eImageInfo_GarbageCollected;
2122 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2123 flags |= eImageInfo_GCOnly;
2124
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002125 // Emitted as int[2];
2126 llvm::Constant *values[2] = {
2127 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2128 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2129 };
2130 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002131 llvm::GlobalVariable *GV =
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002132 new llvm::GlobalVariable(AT, true,
2133 llvm::GlobalValue::InternalLinkage,
2134 llvm::ConstantArray::get(AT, values, 2),
2135 "\01L_OBJC_IMAGE_INFO",
2136 &CGM.getModule());
2137
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002138 if (ObjCABI == 1) {
2139 GV->setSection("__OBJC, __image_info,regular");
2140 } else {
2141 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
2142 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002143
2144 UsedGlobals.push_back(GV);
2145}
2146
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002147
2148// struct objc_module {
2149// unsigned long version;
2150// unsigned long size;
2151// const char *name;
2152// Symtab symtab;
2153// };
2154
2155// FIXME: Get from somewhere
2156static const int ModuleVersion = 7;
2157
2158void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002159 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002160
2161 std::vector<llvm::Constant*> Values(4);
2162 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2163 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002164 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002165 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002166 Values[3] = EmitModuleSymbols();
2167
2168 llvm::GlobalVariable *GV =
2169 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
2170 llvm::GlobalValue::InternalLinkage,
2171 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
2172 Values),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002173 "\01L_OBJC_MODULES",
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002174 &CGM.getModule());
2175 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
2176 UsedGlobals.push_back(GV);
2177}
2178
2179llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002180 unsigned NumClasses = DefinedClasses.size();
2181 unsigned NumCategories = DefinedCategories.size();
2182
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002183 // Return null if no symbols were defined.
2184 if (!NumClasses && !NumCategories)
2185 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2186
2187 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002188 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2189 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2190 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2191 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2192
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002193 // The runtime expects exactly the list of defined classes followed
2194 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002195 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002196 for (unsigned i=0; i<NumClasses; i++)
2197 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2198 ObjCTypes.Int8PtrTy);
2199 for (unsigned i=0; i<NumCategories; i++)
2200 Symbols[NumClasses + i] =
2201 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2202 ObjCTypes.Int8PtrTy);
2203
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002204 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002205 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002206 NumClasses + NumCategories),
2207 Symbols);
2208
2209 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2210
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002211 llvm::GlobalVariable *GV =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002212 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002213 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002214 Init,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002215 "\01L_OBJC_SYMBOLS",
2216 &CGM.getModule());
2217 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
2218 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002219 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2220}
2221
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002222llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002223 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002224 LazySymbols.insert(ID->getIdentifier());
2225
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002226 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2227
2228 if (!Entry) {
2229 llvm::Constant *Casted =
2230 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2231 ObjCTypes.ClassPtrTy);
2232 Entry =
2233 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
2234 llvm::GlobalValue::InternalLinkage,
2235 Casted, "\01L_OBJC_CLASS_REFERENCES_",
2236 &CGM.getModule());
2237 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
2238 UsedGlobals.push_back(Entry);
2239 }
2240
2241 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002242}
2243
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002244llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002245 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2246
2247 if (!Entry) {
2248 llvm::Constant *Casted =
2249 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2250 ObjCTypes.SelectorPtrTy);
2251 Entry =
2252 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
2253 llvm::GlobalValue::InternalLinkage,
2254 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
2255 &CGM.getModule());
2256 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
2257 UsedGlobals.push_back(Entry);
2258 }
2259
2260 return Builder.CreateLoad(Entry, false, "tmp");
2261}
2262
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002263llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002264 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002265
2266 if (!Entry) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002267 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002268 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002269 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002270 llvm::GlobalValue::InternalLinkage,
2271 C, "\01L_OBJC_CLASS_NAME_",
2272 &CGM.getModule());
2273 Entry->setSection("__TEXT,__cstring,cstring_literals");
2274 UsedGlobals.push_back(Entry);
2275 }
2276
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002277 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002278}
2279
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002280llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002281 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
2282
2283 if (!Entry) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00002284 // FIXME: Avoid std::string copying.
2285 llvm::Constant *C = llvm::ConstantArray::get(Sel.getAsString());
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002286 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002287 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002288 llvm::GlobalValue::InternalLinkage,
2289 C, "\01L_OBJC_METH_VAR_NAME_",
2290 &CGM.getModule());
2291 Entry->setSection("__TEXT,__cstring,cstring_literals");
2292 UsedGlobals.push_back(Entry);
2293 }
2294
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002295 return getConstantGEP(Entry, 0, 0);
2296}
2297
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002298// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002299llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002300 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
2301}
2302
2303// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002304llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002305 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
2306}
2307
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002308llvm::Constant *CGObjCCommonMac::GetMethodVarType(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002309 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002310
2311 if (!Entry) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002312 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002313 Entry =
2314 new llvm::GlobalVariable(C->getType(), false,
2315 llvm::GlobalValue::InternalLinkage,
2316 C, "\01L_OBJC_METH_VAR_TYPE_",
2317 &CGM.getModule());
2318 Entry->setSection("__TEXT,__cstring,cstring_literals");
2319 UsedGlobals.push_back(Entry);
2320 }
2321
2322 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002323}
2324
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002325// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002326llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002327 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002328 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
2329 TypeStr);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002330 return GetMethodVarType(TypeStr);
2331}
2332
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002333// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002334llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002335 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
2336
2337 if (!Entry) {
2338 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
2339 Entry =
2340 new llvm::GlobalVariable(C->getType(), false,
2341 llvm::GlobalValue::InternalLinkage,
2342 C, "\01L_OBJC_PROP_NAME_ATTR_",
2343 &CGM.getModule());
2344 Entry->setSection("__TEXT,__cstring,cstring_literals");
2345 UsedGlobals.push_back(Entry);
2346 }
2347
2348 return getConstantGEP(Entry, 0, 0);
2349}
2350
2351// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002352// FIXME: This Decl should be more precise.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002353llvm::Constant *CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002354 const Decl *Container) {
2355 std::string TypeStr;
2356 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002357 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
2358}
2359
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002360void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
2361 const ObjCContainerDecl *CD,
2362 std::string &NameOut) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002363 // FIXME: Find the mangling GCC uses.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002364 NameOut = (D->isInstanceMethod() ? "-" : "+");
Chris Lattner077bf5e2008-11-24 03:33:13 +00002365 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002366 assert (CD && "Missing container decl in GetNameForMethod");
2367 NameOut += CD->getNameAsString();
Fariborz Jahanian52847332009-01-26 23:49:05 +00002368 // FIXME. For a method in a category, (CAT_NAME) is inserted here.
2369 // Right now! there is not enough info. to do this.
Chris Lattner077bf5e2008-11-24 03:33:13 +00002370 NameOut += ' ';
2371 NameOut += D->getSelector().getAsString();
2372 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002373}
2374
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002375/// GetFirstIvarInRecord - This routine returns the record for the
2376/// implementation of the fiven class OID. It also returns field
2377/// corresponding to the first ivar in the class in FIV. It also
2378/// returns the one before the first ivar.
2379///
2380const RecordDecl *CGObjCCommonMac::GetFirstIvarInRecord(
2381 const ObjCInterfaceDecl *OID,
2382 RecordDecl::field_iterator &FIV,
2383 RecordDecl::field_iterator &PIV) {
2384 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass());
2385 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
2386 RecordDecl::field_iterator ifield = RD->field_begin();
2387 RecordDecl::field_iterator pfield = RD->field_end();
2388 while (countSuperClassIvars-- > 0) {
2389 pfield = ifield;
2390 ++ifield;
2391 }
2392 FIV = ifield;
2393 PIV = pfield;
2394 return RD;
2395}
2396
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002397void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002398 EmitModuleInfo();
2399
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002400 // Emit the dummy bodies for any protocols which were referenced but
2401 // never defined.
2402 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
2403 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
2404 if (i->second->hasInitializer())
2405 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002406
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002407 std::vector<llvm::Constant*> Values(5);
2408 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
2409 Values[1] = GetClassName(i->first);
2410 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
2411 Values[3] = Values[4] =
2412 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
2413 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
2414 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
2415 Values));
2416 }
2417
2418 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002419 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002420 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002421 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002422 }
2423
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002424 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002425 llvm::GlobalValue *GV =
2426 new llvm::GlobalVariable(AT, false,
2427 llvm::GlobalValue::AppendingLinkage,
2428 llvm::ConstantArray::get(AT, Used),
2429 "llvm.used",
2430 &CGM.getModule());
2431
2432 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002433
2434 // Add assembler directives to add lazy undefined symbol references
2435 // for classes which are referenced but not defined. This is
2436 // important for correct linker interaction.
2437
2438 // FIXME: Uh, this isn't particularly portable.
2439 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00002440
2441 if (!CGM.getModule().getModuleInlineAsm().empty())
2442 s << "\n";
2443
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002444 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
2445 e = LazySymbols.end(); i != e; ++i) {
2446 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
2447 }
2448 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
2449 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002450 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002451 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
2452 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00002453
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002454 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002455}
2456
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002457CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002458 : CGObjCCommonMac(cgm),
2459 ObjCTypes(cgm)
2460{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00002461 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002462 ObjCABI = 2;
2463}
2464
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002465/* *** */
2466
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002467ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
2468: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002469{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002470 CodeGen::CodeGenTypes &Types = CGM.getTypes();
2471 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002472
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002473 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002474 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002475 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002476 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
2477
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002478 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002479 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002480 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002481
2482 // FIXME: It would be nice to unify this with the opaque type, so
2483 // that the IR comes out a bit cleaner.
2484 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
2485 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002486
2487 // I'm not sure I like this. The implicit coordination is a bit
2488 // gross. We should solve this in a reasonable fashion because this
2489 // is a pretty common task (match some runtime data structure with
2490 // an LLVM data structure).
2491
2492 // FIXME: This is leaked.
2493 // FIXME: Merge with rewriter code?
2494
2495 // struct _objc_super {
2496 // id self;
2497 // Class cls;
2498 // }
2499 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
2500 SourceLocation(),
2501 &Ctx.Idents.get("_objc_super"));
2502 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2503 Ctx.getObjCIdType(), 0, false));
2504 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2505 Ctx.getObjCClassType(), 0, false));
2506 RD->completeDefinition(Ctx);
2507
2508 SuperCTy = Ctx.getTagDeclType(RD);
2509 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
2510
2511 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002512 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
2513
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002514 // struct _prop_t {
2515 // char *name;
2516 // char *attributes;
2517 // }
2518 PropertyTy = llvm::StructType::get(Int8PtrTy,
2519 Int8PtrTy,
2520 NULL);
2521 CGM.getModule().addTypeName("struct._prop_t",
2522 PropertyTy);
2523
2524 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002525 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002526 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002527 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002528 // }
2529 PropertyListTy = llvm::StructType::get(IntTy,
2530 IntTy,
2531 llvm::ArrayType::get(PropertyTy, 0),
2532 NULL);
2533 CGM.getModule().addTypeName("struct._prop_list_t",
2534 PropertyListTy);
2535 // struct _prop_list_t *
2536 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
2537
2538 // struct _objc_method {
2539 // SEL _cmd;
2540 // char *method_type;
2541 // char *_imp;
2542 // }
2543 MethodTy = llvm::StructType::get(SelectorPtrTy,
2544 Int8PtrTy,
2545 Int8PtrTy,
2546 NULL);
2547 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002548
2549 // struct _objc_cache *
2550 CacheTy = llvm::OpaqueType::get();
2551 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
2552 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002553
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002554 // Property manipulation functions.
2555
2556 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
2557 std::vector<const llvm::Type*> Params;
2558 Params.push_back(ObjectPtrTy);
2559 Params.push_back(SelectorPtrTy);
2560 Params.push_back(LongTy);
2561 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2562 GetPropertyFn =
2563 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2564 Params,
2565 false),
2566 "objc_getProperty");
2567
2568 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
2569 Params.clear();
2570 Params.push_back(ObjectPtrTy);
2571 Params.push_back(SelectorPtrTy);
2572 Params.push_back(LongTy);
2573 Params.push_back(ObjectPtrTy);
2574 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2575 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2576 SetPropertyFn =
2577 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2578 Params,
2579 false),
2580 "objc_setProperty");
2581 // Enumeration mutation.
2582
2583 Params.clear();
2584 Params.push_back(ObjectPtrTy);
2585 EnumerationMutationFn =
2586 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2587 Params,
2588 false),
2589 "objc_enumerationMutation");
2590
2591 // gc's API
2592 // id objc_read_weak (id *)
2593 Params.clear();
2594 Params.push_back(PtrObjectPtrTy);
2595 GcReadWeakFn =
2596 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2597 Params,
2598 false),
2599 "objc_read_weak");
2600 // id objc_assign_weak (id, id *)
2601 Params.clear();
2602 Params.push_back(ObjectPtrTy);
2603 Params.push_back(PtrObjectPtrTy);
2604 GcAssignWeakFn =
2605 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2606 Params,
2607 false),
2608 "objc_assign_weak");
2609 GcAssignGlobalFn =
2610 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2611 Params,
2612 false),
2613 "objc_assign_global");
2614 GcAssignIvarFn =
2615 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2616 Params,
2617 false),
2618 "objc_assign_ivar");
2619 GcAssignStrongCastFn =
2620 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2621 Params,
2622 false),
2623 "objc_assign_strongCast");
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002624}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002625
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002626ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
2627 : ObjCCommonTypesHelper(cgm)
2628{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002629 // struct _objc_method_description {
2630 // SEL name;
2631 // char *types;
2632 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002633 MethodDescriptionTy =
2634 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002635 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002636 NULL);
2637 CGM.getModule().addTypeName("struct._objc_method_description",
2638 MethodDescriptionTy);
2639
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002640 // struct _objc_method_description_list {
2641 // int count;
2642 // struct _objc_method_description[1];
2643 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002644 MethodDescriptionListTy =
2645 llvm::StructType::get(IntTy,
2646 llvm::ArrayType::get(MethodDescriptionTy, 0),
2647 NULL);
2648 CGM.getModule().addTypeName("struct._objc_method_description_list",
2649 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002650
2651 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002652 MethodDescriptionListPtrTy =
2653 llvm::PointerType::getUnqual(MethodDescriptionListTy);
2654
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002655 // Protocol description structures
2656
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002657 // struct _objc_protocol_extension {
2658 // uint32_t size; // sizeof(struct _objc_protocol_extension)
2659 // struct _objc_method_description_list *optional_instance_methods;
2660 // struct _objc_method_description_list *optional_class_methods;
2661 // struct _objc_property_list *instance_properties;
2662 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002663 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002664 llvm::StructType::get(IntTy,
2665 MethodDescriptionListPtrTy,
2666 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002667 PropertyListPtrTy,
2668 NULL);
2669 CGM.getModule().addTypeName("struct._objc_protocol_extension",
2670 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002671
2672 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002673 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
2674
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002675 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002676
2677 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
2678 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2679
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002680 const llvm::Type *T =
2681 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
2682 LongTy,
2683 llvm::ArrayType::get(ProtocolTyHolder, 0),
2684 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002685 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
2686
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002687 // struct _objc_protocol {
2688 // struct _objc_protocol_extension *isa;
2689 // char *protocol_name;
2690 // struct _objc_protocol **_objc_protocol_list;
2691 // struct _objc_method_description_list *instance_methods;
2692 // struct _objc_method_description_list *class_methods;
2693 // }
2694 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002695 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002696 llvm::PointerType::getUnqual(ProtocolListTyHolder),
2697 MethodDescriptionListPtrTy,
2698 MethodDescriptionListPtrTy,
2699 NULL);
2700 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
2701
2702 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
2703 CGM.getModule().addTypeName("struct._objc_protocol_list",
2704 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002705 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002706 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
2707
2708 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002709 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002710 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002711
2712 // Class description structures
2713
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002714 // struct _objc_ivar {
2715 // char *ivar_name;
2716 // char *ivar_type;
2717 // int ivar_offset;
2718 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002719 IvarTy = llvm::StructType::get(Int8PtrTy,
2720 Int8PtrTy,
2721 IntTy,
2722 NULL);
2723 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
2724
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002725 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002726 IvarListTy = llvm::OpaqueType::get();
2727 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
2728 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
2729
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002730 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002731 MethodListTy = llvm::OpaqueType::get();
2732 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
2733 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
2734
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002735 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002736 ClassExtensionTy =
2737 llvm::StructType::get(IntTy,
2738 Int8PtrTy,
2739 PropertyListPtrTy,
2740 NULL);
2741 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
2742 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
2743
2744 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
2745
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002746 // struct _objc_class {
2747 // Class isa;
2748 // Class super_class;
2749 // char *name;
2750 // long version;
2751 // long info;
2752 // long instance_size;
2753 // struct _objc_ivar_list *ivars;
2754 // struct _objc_method_list *methods;
2755 // struct _objc_cache *cache;
2756 // struct _objc_protocol_list *protocols;
2757 // char *ivar_layout;
2758 // struct _objc_class_ext *ext;
2759 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002760 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
2761 llvm::PointerType::getUnqual(ClassTyHolder),
2762 Int8PtrTy,
2763 LongTy,
2764 LongTy,
2765 LongTy,
2766 IvarListPtrTy,
2767 MethodListPtrTy,
2768 CachePtrTy,
2769 ProtocolListPtrTy,
2770 Int8PtrTy,
2771 ClassExtensionPtrTy,
2772 NULL);
2773 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
2774
2775 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
2776 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
2777 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
2778
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002779 // struct _objc_category {
2780 // char *category_name;
2781 // char *class_name;
2782 // struct _objc_method_list *instance_method;
2783 // struct _objc_method_list *class_method;
2784 // uint32_t size; // sizeof(struct _objc_category)
2785 // struct _objc_property_list *instance_properties;// category's @property
2786 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002787 CategoryTy = llvm::StructType::get(Int8PtrTy,
2788 Int8PtrTy,
2789 MethodListPtrTy,
2790 MethodListPtrTy,
2791 ProtocolListPtrTy,
2792 IntTy,
2793 PropertyListPtrTy,
2794 NULL);
2795 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
2796
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002797 // Global metadata structures
2798
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002799 // struct _objc_symtab {
2800 // long sel_ref_cnt;
2801 // SEL *refs;
2802 // short cls_def_cnt;
2803 // short cat_def_cnt;
2804 // char *defs[cls_def_cnt + cat_def_cnt];
2805 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002806 SymtabTy = llvm::StructType::get(LongTy,
2807 SelectorPtrTy,
2808 ShortTy,
2809 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002810 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002811 NULL);
2812 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
2813 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
2814
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002815 // struct _objc_module {
2816 // long version;
2817 // long size; // sizeof(struct _objc_module)
2818 // char *name;
2819 // struct _objc_symtab* symtab;
2820 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002821 ModuleTy =
2822 llvm::StructType::get(LongTy,
2823 LongTy,
2824 Int8PtrTy,
2825 SymtabPtrTy,
2826 NULL);
2827 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002828
Daniel Dunbar49f66022008-09-24 03:38:44 +00002829 // Message send functions.
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002830
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002831 // id objc_msgSend (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002832 std::vector<const llvm::Type*> Params;
2833 Params.push_back(ObjectPtrTy);
2834 Params.push_back(SelectorPtrTy);
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002835 MessageSendFn =
2836 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2837 Params,
2838 true),
2839 "objc_msgSend");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002840
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002841 // id objc_msgSend_stret (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002842 Params.clear();
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002843 Params.push_back(ObjectPtrTy);
2844 Params.push_back(SelectorPtrTy);
2845 MessageSendStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002846 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2847 Params,
2848 true),
2849 "objc_msgSend_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002850
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002851 //
Daniel Dunbar5669e572008-10-17 03:24:53 +00002852 Params.clear();
2853 Params.push_back(ObjectPtrTy);
2854 Params.push_back(SelectorPtrTy);
2855 // FIXME: This should be long double on x86_64?
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002856 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
Daniel Dunbar5669e572008-10-17 03:24:53 +00002857 MessageSendFpretFn =
2858 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
2859 Params,
2860 true),
2861 "objc_msgSend_fpret");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002862
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002863 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002864 Params.clear();
2865 Params.push_back(SuperPtrTy);
2866 Params.push_back(SelectorPtrTy);
2867 MessageSendSuperFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002868 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2869 Params,
2870 true),
2871 "objc_msgSendSuper");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002872
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002873 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
2874 // SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002875 Params.clear();
2876 Params.push_back(Int8PtrTy);
2877 Params.push_back(SuperPtrTy);
2878 Params.push_back(SelectorPtrTy);
2879 MessageSendSuperStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002880 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2881 Params,
2882 true),
2883 "objc_msgSendSuper_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002884
2885 // There is no objc_msgSendSuper_fpret? How can that work?
2886 MessageSendSuperFpretFn = MessageSendSuperFn;
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002887
Anders Carlsson124526b2008-09-09 10:10:21 +00002888 // FIXME: This is the size of the setjmp buffer and should be
2889 // target specific. 18 is what's used on 32-bit X86.
2890 uint64_t SetJmpBufferSize = 18;
2891
2892 // Exceptions
2893 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00002894 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00002895
2896 ExceptionDataTy =
2897 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
2898 SetJmpBufferSize),
2899 StackPtrTy, NULL);
2900 CGM.getModule().addTypeName("struct._objc_exception_data",
2901 ExceptionDataTy);
2902
2903 Params.clear();
2904 Params.push_back(ObjectPtrTy);
2905 ExceptionThrowFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002906 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2907 Params,
2908 false),
2909 "objc_exception_throw");
Anders Carlsson124526b2008-09-09 10:10:21 +00002910
2911 Params.clear();
2912 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
2913 ExceptionTryEnterFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002914 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2915 Params,
2916 false),
2917 "objc_exception_try_enter");
Anders Carlsson124526b2008-09-09 10:10:21 +00002918 ExceptionTryExitFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002919 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2920 Params,
2921 false),
2922 "objc_exception_try_exit");
Anders Carlsson124526b2008-09-09 10:10:21 +00002923 ExceptionExtractFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002924 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2925 Params,
2926 false),
2927 "objc_exception_extract");
Anders Carlsson124526b2008-09-09 10:10:21 +00002928
2929 Params.clear();
2930 Params.push_back(ClassPtrTy);
2931 Params.push_back(ObjectPtrTy);
2932 ExceptionMatchFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002933 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
2934 Params,
2935 false),
2936 "objc_exception_match");
Chris Lattner10cac6f2008-11-15 21:26:17 +00002937
2938 // synchronized APIs
2939 // void objc_sync_enter (id)
2940 Params.clear();
2941 Params.push_back(ObjectPtrTy);
2942 SyncEnterFn =
2943 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2944 Params,
2945 false),
2946 "objc_sync_enter");
2947 // void objc_sync_exit (id)
2948 SyncExitFn =
2949 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2950 Params,
2951 false),
2952 "objc_sync_exit");
2953
Anders Carlsson124526b2008-09-09 10:10:21 +00002954
2955 Params.clear();
2956 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
2957 SetJmpFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002958 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
2959 Params,
2960 false),
2961 "_setjmp");
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002962
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002963}
2964
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002965ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002966: ObjCCommonTypesHelper(cgm)
2967{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002968 // struct _method_list_t {
2969 // uint32_t entsize; // sizeof(struct _objc_method)
2970 // uint32_t method_count;
2971 // struct _objc_method method_list[method_count];
2972 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002973 MethodListnfABITy = llvm::StructType::get(IntTy,
2974 IntTy,
2975 llvm::ArrayType::get(MethodTy, 0),
2976 NULL);
2977 CGM.getModule().addTypeName("struct.__method_list_t",
2978 MethodListnfABITy);
2979 // struct method_list_t *
2980 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002981
2982 // struct _protocol_t {
2983 // id isa; // NULL
2984 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002985 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002986 // const struct method_list_t * const instance_methods;
2987 // const struct method_list_t * const class_methods;
2988 // const struct method_list_t *optionalInstanceMethods;
2989 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002990 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002991 // const uint32_t size; // sizeof(struct _protocol_t)
2992 // const uint32_t flags; // = 0
2993 // }
2994
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002995 // Holder for struct _protocol_list_t *
2996 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2997
2998 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
2999 Int8PtrTy,
3000 llvm::PointerType::getUnqual(
3001 ProtocolListTyHolder),
3002 MethodListnfABIPtrTy,
3003 MethodListnfABIPtrTy,
3004 MethodListnfABIPtrTy,
3005 MethodListnfABIPtrTy,
3006 PropertyListPtrTy,
3007 IntTy,
3008 IntTy,
3009 NULL);
3010 CGM.getModule().addTypeName("struct._protocol_t",
3011 ProtocolnfABITy);
3012
Fariborz Jahanianda320092009-01-29 19:24:30 +00003013 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003014 // long protocol_count; // Note, this is 32/64 bit
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003015 // struct _protocol_t[protocol_count];
3016 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003017 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3018 llvm::ArrayType::get(
3019 ProtocolnfABITy, 0),
3020 NULL);
3021 CGM.getModule().addTypeName("struct._objc_protocol_list",
3022 ProtocolListnfABITy);
3023
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003024 // struct _objc_protocol_list*
3025 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003026
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003027 // FIXME! Is this doing the right thing?
3028 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3029 ProtocolListnfABIPtrTy);
3030
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003031 // struct _ivar_t {
3032 // unsigned long int *offset; // pointer to ivar offset location
3033 // char *name;
3034 // char *type;
3035 // uint32_t alignment;
3036 // uint32_t size;
3037 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003038 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3039 Int8PtrTy,
3040 Int8PtrTy,
3041 IntTy,
3042 IntTy,
3043 NULL);
3044 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3045
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003046 // struct _ivar_list_t {
3047 // uint32 entsize; // sizeof(struct _ivar_t)
3048 // uint32 count;
3049 // struct _iver_t list[count];
3050 // }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003051 IvarListnfABITy = llvm::StructType::get(IntTy,
3052 IntTy,
3053 llvm::ArrayType::get(
3054 IvarnfABITy, 0),
3055 NULL);
3056 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3057
3058 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003059
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003060 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003061 // uint32_t const flags;
3062 // uint32_t const instanceStart;
3063 // uint32_t const instanceSize;
3064 // uint32_t const reserved; // only when building for 64bit targets
3065 // const uint8_t * const ivarLayout;
3066 // const char *const name;
3067 // const struct _method_list_t * const baseMethods;
3068 // const struct _objc_protocol_list *const baseProtocols;
3069 // const struct _ivar_list_t *const ivars;
3070 // const uint8_t * const weakIvarLayout;
3071 // const struct _prop_list_t * const properties;
3072 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003073
3074 // FIXME. Add 'reserved' field in 64bit abi mode!
3075 ClassRonfABITy = llvm::StructType::get(IntTy,
3076 IntTy,
3077 IntTy,
3078 Int8PtrTy,
3079 Int8PtrTy,
3080 MethodListnfABIPtrTy,
3081 ProtocolListnfABIPtrTy,
3082 IvarListnfABIPtrTy,
3083 Int8PtrTy,
3084 PropertyListPtrTy,
3085 NULL);
3086 CGM.getModule().addTypeName("struct._class_ro_t",
3087 ClassRonfABITy);
3088
3089 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3090 std::vector<const llvm::Type*> Params;
3091 Params.push_back(ObjectPtrTy);
3092 Params.push_back(SelectorPtrTy);
3093 ImpnfABITy = llvm::PointerType::getUnqual(
3094 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3095
3096 // struct _class_t {
3097 // struct _class_t *isa;
3098 // struct _class_t * const superclass;
3099 // void *cache;
3100 // IMP *vtable;
3101 // struct class_ro_t *ro;
3102 // }
3103
3104 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3105 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3106 llvm::PointerType::getUnqual(ClassTyHolder),
3107 CachePtrTy,
3108 llvm::PointerType::getUnqual(ImpnfABITy),
3109 llvm::PointerType::getUnqual(
3110 ClassRonfABITy),
3111 NULL);
3112 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3113
3114 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3115 ClassnfABITy);
3116
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003117 // LLVM for struct _class_t *
3118 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3119
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003120 // struct _category_t {
3121 // const char * const name;
3122 // struct _class_t *const cls;
3123 // const struct _method_list_t * const instance_methods;
3124 // const struct _method_list_t * const class_methods;
3125 // const struct _protocol_list_t * const protocols;
3126 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003127 // }
3128 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003129 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003130 MethodListnfABIPtrTy,
3131 MethodListnfABIPtrTy,
3132 ProtocolListnfABIPtrTy,
3133 PropertyListPtrTy,
3134 NULL);
3135 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
3136
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003137}
3138
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003139llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
3140 FinishNonFragileABIModule();
3141
3142 return NULL;
3143}
3144
3145void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
3146 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003147
3148 // Build list of all implemented classe addresses in array
3149 // L_OBJC_LABEL_CLASS_$.
3150 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$
3151 // list of 'nonlazy' implementations (defined as those with a +load{}
3152 // method!!).
3153 unsigned NumClasses = DefinedClasses.size();
3154 if (NumClasses) {
3155 std::vector<llvm::Constant*> Symbols(NumClasses);
3156 for (unsigned i=0; i<NumClasses; i++)
3157 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
3158 ObjCTypes.Int8PtrTy);
3159 llvm::Constant* Init =
3160 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3161 NumClasses),
3162 Symbols);
3163
3164 llvm::GlobalVariable *GV =
3165 new llvm::GlobalVariable(Init->getType(), false,
3166 llvm::GlobalValue::InternalLinkage,
3167 Init,
3168 "\01L_OBJC_LABEL_CLASS_$",
3169 &CGM.getModule());
3170 GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip");
3171 UsedGlobals.push_back(GV);
3172 }
3173
3174 // Build list of all implemented category addresses in array
3175 // L_OBJC_LABEL_CATEGORY_$.
3176 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$
3177 // list of 'nonlazy' category implementations (defined as those with a +load{}
3178 // method!!).
3179 unsigned NumCategory = DefinedCategories.size();
3180 if (NumCategory) {
3181 std::vector<llvm::Constant*> Symbols(NumCategory);
3182 for (unsigned i=0; i<NumCategory; i++)
3183 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i],
3184 ObjCTypes.Int8PtrTy);
3185 llvm::Constant* Init =
3186 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3187 NumCategory),
3188 Symbols);
3189
3190 llvm::GlobalVariable *GV =
3191 new llvm::GlobalVariable(Init->getType(), false,
3192 llvm::GlobalValue::InternalLinkage,
3193 Init,
3194 "\01L_OBJC_LABEL_CATEGORY_$",
3195 &CGM.getModule());
3196 GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip");
3197 UsedGlobals.push_back(GV);
3198 }
3199
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003200 std::vector<llvm::Constant*> Used;
3201 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
3202 e = UsedGlobals.end(); i != e; ++i) {
3203 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
3204 }
3205
3206 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
3207 llvm::GlobalValue *GV =
3208 new llvm::GlobalVariable(AT, false,
3209 llvm::GlobalValue::AppendingLinkage,
3210 llvm::ConstantArray::get(AT, Used),
3211 "llvm.used",
3212 &CGM.getModule());
3213
3214 GV->setSection("llvm.metadata");
3215
3216}
3217
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003218// Metadata flags
3219enum MetaDataDlags {
3220 CLS = 0x0,
3221 CLS_META = 0x1,
3222 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003223 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003224 CLS_EXCEPTION = 0x20
3225};
3226/// BuildClassRoTInitializer - generate meta-data for:
3227/// struct _class_ro_t {
3228/// uint32_t const flags;
3229/// uint32_t const instanceStart;
3230/// uint32_t const instanceSize;
3231/// uint32_t const reserved; // only when building for 64bit targets
3232/// const uint8_t * const ivarLayout;
3233/// const char *const name;
3234/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00003235/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003236/// const struct _ivar_list_t *const ivars;
3237/// const uint8_t * const weakIvarLayout;
3238/// const struct _prop_list_t * const properties;
3239/// }
3240///
3241llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
3242 unsigned flags,
3243 unsigned InstanceStart,
3244 unsigned InstanceSize,
3245 const ObjCImplementationDecl *ID) {
3246 std::string ClassName = ID->getNameAsString();
3247 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
3248 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
3249 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
3250 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
3251 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianda320092009-01-29 19:24:30 +00003252 // FIXME. ivarLayout is currently null!
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003253 Values[ 3] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3254 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003255 // const struct _method_list_t * const baseMethods;
3256 std::vector<llvm::Constant*> Methods;
3257 std::string MethodListName("\01l_OBJC_$_");
3258 if (flags & CLS_META) {
3259 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
3260 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
3261 e = ID->classmeth_end(); i != e; ++i) {
3262 // Class methods should always be defined.
3263 Methods.push_back(GetMethodConstant(*i));
3264 }
3265 } else {
3266 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
3267 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
3268 e = ID->instmeth_end(); i != e; ++i) {
3269 // Instance methods should always be defined.
3270 Methods.push_back(GetMethodConstant(*i));
3271 }
Fariborz Jahanian939abce2009-01-28 22:46:49 +00003272 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
3273 e = ID->propimpl_end(); i != e; ++i) {
3274 ObjCPropertyImplDecl *PID = *i;
3275
3276 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
3277 ObjCPropertyDecl *PD = PID->getPropertyDecl();
3278
3279 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
3280 if (llvm::Constant *C = GetMethodConstant(MD))
3281 Methods.push_back(C);
3282 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
3283 if (llvm::Constant *C = GetMethodConstant(MD))
3284 Methods.push_back(C);
3285 }
3286 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003287 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003288 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003289 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003290
3291 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3292 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
3293 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
3294 + OID->getNameAsString(),
3295 OID->protocol_begin(),
3296 OID->protocol_end());
3297
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003298 if (flags & CLS_META)
3299 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3300 else
3301 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003302 // FIXME. weakIvarLayout is currently null.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003303 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003304 if (flags & CLS_META)
3305 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3306 else
3307 Values[ 9] =
3308 EmitPropertyList(
3309 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
3310 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003311 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
3312 Values);
3313 llvm::GlobalVariable *CLASS_RO_GV =
3314 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
3315 llvm::GlobalValue::InternalLinkage,
3316 Init,
3317 (flags & CLS_META) ?
3318 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
3319 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
3320 &CGM.getModule());
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003321 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003322 UsedGlobals.push_back(CLASS_RO_GV);
3323 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003324
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003325}
3326
3327/// BuildClassMetaData - This routine defines that to-level meta-data
3328/// for the given ClassName for:
3329/// struct _class_t {
3330/// struct _class_t *isa;
3331/// struct _class_t * const superclass;
3332/// void *cache;
3333/// IMP *vtable;
3334/// struct class_ro_t *ro;
3335/// }
3336///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003337llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
3338 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003339 llvm::Constant *IsAGV,
3340 llvm::Constant *SuperClassGV,
3341 llvm::Constant *ClassRoGV) {
3342 std::vector<llvm::Constant*> Values(5);
3343 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003344 Values[1] = SuperClassGV
3345 ? SuperClassGV
3346 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003347 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
3348 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
3349 Values[4] = ClassRoGV; // &CLASS_RO_GV
3350 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
3351 Values);
3352 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(ClassName);
3353 if (GV)
3354 GV->setInitializer(Init);
3355 else
3356 GV =
3357 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3358 llvm::GlobalValue::ExternalLinkage,
3359 Init,
3360 ClassName,
3361 &CGM.getModule());
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003362 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003363 UsedGlobals.push_back(GV);
3364 // FIXME! why?
3365 GV->setAlignment(32);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003366 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003367}
3368
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003369void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
3370 std::string ClassName = ID->getNameAsString();
3371 if (!ObjCEmptyCacheVar) {
3372 ObjCEmptyCacheVar = new llvm::GlobalVariable(
3373 ObjCTypes.CachePtrTy,
3374 false,
3375 llvm::GlobalValue::ExternalLinkage,
3376 0,
3377 "\01_objc_empty_cache",
3378 &CGM.getModule());
3379 UsedGlobals.push_back(ObjCEmptyCacheVar);
3380
3381 ObjCEmptyVtableVar = new llvm::GlobalVariable(
3382 llvm::PointerType::getUnqual(
3383 ObjCTypes.ImpnfABITy),
3384 false,
3385 llvm::GlobalValue::ExternalLinkage,
3386 0,
3387 "\01_objc_empty_vtable",
3388 &CGM.getModule());
3389 UsedGlobals.push_back(ObjCEmptyVtableVar);
3390 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003391 assert(ID->getClassInterface() &&
3392 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003393 uint32_t InstanceStart =
3394 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
3395 uint32_t InstanceSize = InstanceStart;
3396 uint32_t flags = CLS_META;
3397 std::string ObjCMetaClassName("\01_OBJC_METACLASS_$_");
3398 std::string ObjCClassName("\01_OBJC_CLASS_$_");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003399
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003400 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003401
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003402 if (IsClassHidden(ID->getClassInterface()))
3403 flags |= OBJC2_CLS_HIDDEN;
3404 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003405 // class is root
3406 flags |= CLS_ROOT;
3407 std::string SuperClassName = ObjCClassName + ClassName;
3408 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3409 if (!SuperClassGV)
3410 SuperClassGV =
3411 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3412 llvm::GlobalValue::ExternalLinkage,
3413 0,
3414 SuperClassName,
3415 &CGM.getModule());
3416 UsedGlobals.push_back(SuperClassGV);
3417 std::string IsAClassName = ObjCMetaClassName + ClassName;
3418 IsAGV = CGM.getModule().getGlobalVariable(IsAClassName);
3419 if (!IsAGV)
3420 IsAGV =
3421 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3422 llvm::GlobalValue::ExternalLinkage,
3423 0,
3424 IsAClassName,
3425 &CGM.getModule());
3426 UsedGlobals.push_back(IsAGV);
3427 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003428 // Has a root. Current class is not a root.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003429 std::string RootClassName =
3430 ID->getClassInterface()->getSuperClass()->getNameAsString();
3431 std::string SuperClassName = ObjCMetaClassName + RootClassName;
3432 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3433 if (!SuperClassGV)
3434 SuperClassGV =
3435 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3436 llvm::GlobalValue::ExternalLinkage,
3437 0,
3438 SuperClassName,
3439 &CGM.getModule());
3440 UsedGlobals.push_back(SuperClassGV);
3441 IsAGV = SuperClassGV;
3442 }
3443 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
3444 InstanceStart,
3445 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003446 std::string TClassName = ObjCMetaClassName + ClassName;
3447 llvm::GlobalVariable *MetaTClass =
3448 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV);
3449
3450 // Metadata for the class
3451 flags = CLS;
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003452 if (IsClassHidden(ID->getClassInterface()))
3453 flags |= OBJC2_CLS_HIDDEN;
3454 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003455 flags |= CLS_ROOT;
3456 SuperClassGV = 0;
3457 }
3458 else {
3459 // Has a root. Current class is not a root.
3460 std::string RootClassName =
3461 ID->getClassInterface()->getSuperClass()->getNameAsString();
3462 std::string SuperClassName = ObjCClassName + RootClassName;
3463 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3464 if (!SuperClassGV)
3465 SuperClassGV =
3466 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3467 llvm::GlobalValue::ExternalLinkage,
3468 0,
3469 SuperClassName,
3470 &CGM.getModule());
3471 UsedGlobals.push_back(SuperClassGV);
3472
3473 }
3474
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003475 InstanceStart = InstanceSize = 0;
3476 if (ObjCInterfaceDecl *OID =
3477 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface())) {
3478 // FIXME. Share this with the one in EmitIvarList.
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003479 const llvm::Type *InterfaceTy =
3480 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
3481 const llvm::StructLayout *Layout =
3482 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003483
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003484 RecordDecl::field_iterator firstField, lastField;
3485 const RecordDecl *RD = GetFirstIvarInRecord(OID, firstField, lastField);
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003486
3487 for (RecordDecl::field_iterator e = RD->field_end(),
3488 ifield = firstField; ifield != e; ++ifield)
3489 lastField = ifield;
3490
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003491 if (lastField != RD->field_end()) {
3492 FieldDecl *Field = *lastField;
3493 const llvm::Type *FieldTy =
3494 CGM.getTypes().ConvertTypeForMem(Field->getType());
3495 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3496 InstanceSize = Layout->getElementOffset(
3497 CGM.getTypes().getLLVMFieldNo(Field)) +
3498 Size;
3499 if (firstField == RD->field_end())
3500 InstanceStart = InstanceSize;
3501 else
3502 InstanceStart = Layout->getElementOffset(CGM.getTypes().
3503 getLLVMFieldNo(*firstField));
3504 }
3505 }
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003506 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003507 InstanceStart,
3508 InstanceSize,
3509 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003510
3511 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003512 llvm::GlobalVariable *ClassMD =
3513 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV);
3514 DefinedClasses.push_back(ClassMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003515}
3516
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00003517/// GenerateProtocolRef - This routine is called to generate code for
3518/// a protocol reference expression; as in:
3519/// @code
3520/// @protocol(Proto1);
3521/// @endcode
3522/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
3523/// which will hold address of the protocol meta-data.
3524///
3525llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
3526 const ObjCProtocolDecl *PD) {
3527
3528 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
3529 ObjCTypes.ExternalProtocolPtrTy);
3530
3531 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
3532 ProtocolName += PD->getNameAsCString();
3533
3534 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
3535 if (PTGV)
3536 return Builder.CreateLoad(PTGV, false, "tmp");
3537 PTGV = new llvm::GlobalVariable(
3538 Init->getType(), false,
3539 llvm::GlobalValue::WeakLinkage,
3540 Init,
3541 ProtocolName,
3542 &CGM.getModule());
3543 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
3544 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
3545 UsedGlobals.push_back(PTGV);
3546 return Builder.CreateLoad(PTGV, false, "tmp");
3547}
3548
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003549/// GenerateCategory - Build metadata for a category implementation.
3550/// struct _category_t {
3551/// const char * const name;
3552/// struct _class_t *const cls;
3553/// const struct _method_list_t * const instance_methods;
3554/// const struct _method_list_t * const class_methods;
3555/// const struct _protocol_list_t * const protocols;
3556/// const struct _prop_list_t * const properties;
3557/// }
3558///
3559void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD)
3560{
3561 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003562 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
3563 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003564 "_$_" + OCD->getNameAsString());
3565 std::string ExtClassName("\01_OBJC_CLASS_$_" + Interface->getNameAsString());
3566
3567 std::vector<llvm::Constant*> Values(6);
3568 Values[0] = GetClassName(OCD->getIdentifier());
3569 // meta-class entry symbol
3570 llvm::GlobalVariable *ClassGV =
3571 CGM.getModule().getGlobalVariable(ExtClassName);
3572 if (!ClassGV)
3573 ClassGV =
3574 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3575 llvm::GlobalValue::ExternalLinkage,
3576 0,
3577 ExtClassName,
3578 &CGM.getModule());
3579 UsedGlobals.push_back(ClassGV);
3580 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003581 std::vector<llvm::Constant*> Methods;
3582 std::string MethodListName(Prefix);
3583 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
3584 "_$_" + OCD->getNameAsString();
3585
3586 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
3587 e = OCD->instmeth_end(); i != e; ++i) {
3588 // Instance methods should always be defined.
3589 Methods.push_back(GetMethodConstant(*i));
3590 }
3591
3592 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003593 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003594 Methods);
3595
3596 MethodListName = Prefix;
3597 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
3598 OCD->getNameAsString();
3599 Methods.clear();
3600 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
3601 e = OCD->classmeth_end(); i != e; ++i) {
3602 // Class methods should always be defined.
3603 Methods.push_back(GetMethodConstant(*i));
3604 }
3605
3606 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003607 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003608 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003609 const ObjCCategoryDecl *Category =
3610 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanianeb732132009-01-29 23:23:06 +00003611 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
3612 + Interface->getNameAsString() + "_$_"
3613 + Category->getNameAsString(),
3614 Category->protocol_begin(),
3615 Category->protocol_end());
3616
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003617 std::string ExtName(Interface->getNameAsString() + "_$_" +
3618 OCD->getNameAsString());
3619 Values[5] =
3620 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
3621 OCD, Category, ObjCTypes);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003622 llvm::Constant *Init =
3623 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
3624 Values);
3625 llvm::GlobalVariable *GCATV
3626 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
3627 false,
3628 llvm::GlobalValue::InternalLinkage,
3629 Init,
3630 ExtCatName,
3631 &CGM.getModule());
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003632 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003633 UsedGlobals.push_back(GCATV);
3634 DefinedCategories.push_back(GCATV);
3635}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003636
3637/// GetMethodConstant - Return a struct objc_method constant for the
3638/// given method if it has been defined. The result is null if the
3639/// method has not been defined. The return value has type MethodPtrTy.
3640llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
3641 const ObjCMethodDecl *MD) {
3642 // FIXME: Use DenseMap::lookup
3643 llvm::Function *Fn = MethodDefinitions[MD];
3644 if (!Fn)
3645 return 0;
3646
3647 std::vector<llvm::Constant*> Method(3);
3648 Method[0] =
3649 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
3650 ObjCTypes.SelectorPtrTy);
3651 Method[1] = GetMethodVarType(MD);
3652 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
3653 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
3654}
3655
3656/// EmitMethodList - Build meta-data for method declarations
3657/// struct _method_list_t {
3658/// uint32_t entsize; // sizeof(struct _objc_method)
3659/// uint32_t method_count;
3660/// struct _objc_method method_list[method_count];
3661/// }
3662///
3663llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
3664 const std::string &Name,
3665 const char *Section,
3666 const ConstantVector &Methods) {
3667 // Return null for empty list.
3668 if (Methods.empty())
3669 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
3670
3671 std::vector<llvm::Constant*> Values(3);
3672 // sizeof(struct _objc_method)
3673 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy);
3674 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3675 // method_count
3676 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
3677 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
3678 Methods.size());
3679 Values[2] = llvm::ConstantArray::get(AT, Methods);
3680 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
3681
3682 llvm::GlobalVariable *GV =
3683 new llvm::GlobalVariable(Init->getType(), false,
3684 llvm::GlobalValue::InternalLinkage,
3685 Init,
3686 Name,
3687 &CGM.getModule());
3688 GV->setSection(Section);
3689 UsedGlobals.push_back(GV);
3690 return llvm::ConstantExpr::getBitCast(GV,
3691 ObjCTypes.MethodListnfABIPtrTy);
3692}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003693
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003694llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
3695 const ObjCImplementationDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003696 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003697 unsigned long int Offset) {
3698
3699 std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.'
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003700 + Ivar->getNameAsString());
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003701 llvm::Constant *Init = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
3702
3703 llvm::GlobalVariable *IvarOffsetGV =
3704 CGM.getModule().getGlobalVariable(ExternalName);
3705 if (IvarOffsetGV) {
3706 // ivar offset symbol already built due to user code referencing it.
3707 IvarOffsetGV->setInitializer(Init);
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003708 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003709 return IvarOffsetGV;
3710 }
3711
3712 IvarOffsetGV =
3713 new llvm::GlobalVariable(Init->getType(),
3714 false,
3715 llvm::GlobalValue::ExternalLinkage,
3716 Init,
3717 ExternalName,
3718 &CGM.getModule());
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003719 // @private and @package have hidden visibility.
3720 bool globalVisibility = (Ivar->getAccessControl() == ObjCIvarDecl::Public ||
3721 Ivar->getAccessControl() == ObjCIvarDecl::Protected);
3722 if (!globalVisibility)
3723 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003724 IvarOffsetGV->setSection("__DATA, __objc_const");
3725 UsedGlobals.push_back(IvarOffsetGV);
3726
3727 return llvm::ConstantExpr::getBitCast(
3728 IvarOffsetGV,
3729 llvm::PointerType::getUnqual(ObjCTypes.LongTy));
3730}
3731
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003732/// EmitIvarList - Emit the ivar list for the given
3733/// implementation. If ForClass is true the list of class ivars
3734/// (i.e. metaclass ivars) is emitted, otherwise the list of
3735/// interface ivars will be emitted. The return value has type
3736/// IvarListnfABIPtrTy.
3737/// struct _ivar_t {
3738/// unsigned long int *offset; // pointer to ivar offset location
3739/// char *name;
3740/// char *type;
3741/// uint32_t alignment;
3742/// uint32_t size;
3743/// }
3744/// struct _ivar_list_t {
3745/// uint32 entsize; // sizeof(struct _ivar_t)
3746/// uint32 count;
3747/// struct _iver_t list[count];
3748/// }
3749///
3750llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
3751 const ObjCImplementationDecl *ID) {
3752
3753 std::vector<llvm::Constant*> Ivars, Ivar(5);
3754
3755 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3756 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
3757
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003758 // FIXME. Consolidate this with similar code in GenerateClass.
3759 const llvm::Type *InterfaceTy =
3760 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(
3761 const_cast<ObjCInterfaceDecl*>(OID)));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003762 const llvm::StructLayout *Layout =
3763 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003764
3765 RecordDecl::field_iterator i,p;
3766 const RecordDecl *RD = GetFirstIvarInRecord(OID, i,p);
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003767 ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin();
3768
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003769 for (RecordDecl::field_iterator e = RD->field_end(); i != e; ++i) {
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003770 FieldDecl *Field = *i;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003771 unsigned long offset = Layout->getElementOffset(CGM.getTypes().
3772 getLLVMFieldNo(Field));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003773 const ObjCIvarDecl *ivarDecl = *I++;
3774 Ivar[0] = EmitIvarOffsetVar(ID, ivarDecl, offset);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003775 if (Field->getIdentifier())
3776 Ivar[1] = GetMethodVarName(Field->getIdentifier());
3777 else
3778 Ivar[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3779 std::string TypeStr;
3780 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3781 Ivar[2] = GetMethodVarType(TypeStr);
3782 const llvm::Type *FieldTy =
3783 CGM.getTypes().ConvertTypeForMem(Field->getType());
3784 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3785 unsigned Align = CGM.getContext().getPreferredTypeAlign(
3786 Field->getType().getTypePtr()) >> 3;
3787 Align = llvm::Log2_32(Align);
3788 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Fariborz Jahanian07236ba2009-01-27 22:27:56 +00003789 // NOTE. Size of a bitfield does not match gcc's, because of the way
3790 // bitfields are treated special in each. But I am told that 'size'
3791 // for bitfield ivars is ignored by the runtime so it does not matter.
3792 // (even if it matters, some day, there is enough info. to get the bitfield
3793 // right!
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003794 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3795 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
3796 }
3797 // Return null for empty list.
3798 if (Ivars.empty())
3799 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3800 std::vector<llvm::Constant*> Values(3);
3801 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy);
3802 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3803 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
3804 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
3805 Ivars.size());
3806 Values[2] = llvm::ConstantArray::get(AT, Ivars);
3807 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
3808 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
3809 llvm::GlobalVariable *GV =
3810 new llvm::GlobalVariable(Init->getType(), false,
3811 llvm::GlobalValue::InternalLinkage,
3812 Init,
3813 Prefix + OID->getNameAsString(),
3814 &CGM.getModule());
3815
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003816 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003817
3818 UsedGlobals.push_back(GV);
3819 return llvm::ConstantExpr::getBitCast(GV,
3820 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003821}
3822
3823llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
3824 const ObjCProtocolDecl *PD) {
3825 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
3826
3827 if (!Entry) {
3828 // We use the initializer as a marker of whether this is a forward
3829 // reference or not. At module finalization we add the empty
3830 // contents for protocols which were referenced but never defined.
3831 Entry =
3832 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
3833 llvm::GlobalValue::ExternalLinkage,
3834 0,
3835 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(),
3836 &CGM.getModule());
3837 Entry->setSection("__DATA,__datacoal_nt,coalesced");
3838 UsedGlobals.push_back(Entry);
3839 // FIXME: Is this necessary? Why only for protocol?
3840 Entry->setAlignment(4);
3841 }
3842
3843 return Entry;
3844}
3845
3846/// GetOrEmitProtocol - Generate the protocol meta-data:
3847/// @code
3848/// struct _protocol_t {
3849/// id isa; // NULL
3850/// const char * const protocol_name;
3851/// const struct _protocol_list_t * protocol_list; // super protocols
3852/// const struct method_list_t * const instance_methods;
3853/// const struct method_list_t * const class_methods;
3854/// const struct method_list_t *optionalInstanceMethods;
3855/// const struct method_list_t *optionalClassMethods;
3856/// const struct _prop_list_t * properties;
3857/// const uint32_t size; // sizeof(struct _protocol_t)
3858/// const uint32_t flags; // = 0
3859/// }
3860/// @endcode
3861///
3862
3863llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
3864 const ObjCProtocolDecl *PD) {
3865 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
3866
3867 // Early exit if a defining object has already been generated.
3868 if (Entry && Entry->hasInitializer())
3869 return Entry;
3870
3871 const char *ProtocolName = PD->getNameAsCString();
3872
3873 // Construct method lists.
3874 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
3875 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
3876 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
3877 e = PD->instmeth_end(); i != e; ++i) {
3878 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003879 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003880 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
3881 OptInstanceMethods.push_back(C);
3882 } else {
3883 InstanceMethods.push_back(C);
3884 }
3885 }
3886
3887 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
3888 e = PD->classmeth_end(); i != e; ++i) {
3889 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003890 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003891 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
3892 OptClassMethods.push_back(C);
3893 } else {
3894 ClassMethods.push_back(C);
3895 }
3896 }
3897
3898 std::vector<llvm::Constant*> Values(10);
3899 // isa is NULL
3900 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
3901 Values[1] = GetClassName(PD->getIdentifier());
3902 Values[2] = EmitProtocolList(
3903 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
3904 PD->protocol_begin(),
3905 PD->protocol_end());
3906
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003907 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00003908 + PD->getNameAsString(),
3909 "__DATA, __objc_const",
3910 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003911 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00003912 + PD->getNameAsString(),
3913 "__DATA, __objc_const",
3914 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003915 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00003916 + PD->getNameAsString(),
3917 "__DATA, __objc_const",
3918 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003919 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00003920 + PD->getNameAsString(),
3921 "__DATA, __objc_const",
3922 OptClassMethods);
3923 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
3924 0, PD, ObjCTypes);
3925 uint32_t Size =
3926 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy);
3927 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3928 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
3929 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
3930 Values);
3931
3932 if (Entry) {
3933 // Already created, fix the linkage and update the initializer.
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00003934 Entry->setLinkage(llvm::GlobalValue::WeakLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003935 Entry->setInitializer(Init);
3936 } else {
3937 Entry =
3938 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00003939 llvm::GlobalValue::WeakLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00003940 Init,
3941 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName,
3942 &CGM.getModule());
3943 Entry->setSection("__DATA,__datacoal_nt,coalesced");
3944 // FIXME: Is this necessary? Why only for protocol?
3945 Entry->setAlignment(4);
3946 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00003947 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
3948
3949 // Use this protocol meta-data to build protocol list table in section
3950 // __DATA, __objc_protolist
3951 llvm::Type *ptype = llvm::PointerType::getUnqual(ObjCTypes.ProtocolnfABITy);
3952 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
3953 ptype, false,
3954 llvm::GlobalValue::WeakLinkage,
3955 Entry,
3956 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
3957 +ProtocolName,
3958 &CGM.getModule());
3959 PTGV->setSection("__DATA, __objc_protolist");
3960 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
3961 UsedGlobals.push_back(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003962 return Entry;
3963}
3964
3965/// EmitProtocolList - Generate protocol list meta-data:
3966/// @code
3967/// struct _protocol_list_t {
3968/// long protocol_count; // Note, this is 32/64 bit
3969/// struct _protocol_t[protocol_count];
3970/// }
3971/// @endcode
3972///
3973llvm::Constant *
3974CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
3975 ObjCProtocolDecl::protocol_iterator begin,
3976 ObjCProtocolDecl::protocol_iterator end) {
3977 std::vector<llvm::Constant*> ProtocolRefs;
3978
3979 for (; begin != end; ++begin)
3980 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
3981
3982 // Just return null for empty protocol lists
3983 if (ProtocolRefs.empty())
3984 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
3985
3986 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
3987 if (GV)
3988 return GV;
3989 // This list is null terminated.
3990 ProtocolRefs.push_back(llvm::Constant::getNullValue(
3991 ObjCTypes.ProtocolListnfABIPtrTy));
3992
3993 std::vector<llvm::Constant*> Values(2);
3994 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
3995 Values[1] =
3996 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolListnfABIPtrTy,
3997 ProtocolRefs.size()),
3998 ProtocolRefs);
3999
4000 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4001 GV = new llvm::GlobalVariable(Init->getType(), false,
4002 llvm::GlobalValue::InternalLinkage,
4003 Init,
4004 Name,
4005 &CGM.getModule());
4006 GV->setSection("__DATA, __objc_const");
4007 UsedGlobals.push_back(GV);
4008 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
4009}
4010
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004011/// GetMethodDescriptionConstant - This routine build following meta-data:
4012/// struct _objc_method {
4013/// SEL _cmd;
4014/// char *method_type;
4015/// char *_imp;
4016/// }
4017
4018llvm::Constant *
4019CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4020 std::vector<llvm::Constant*> Desc(3);
4021 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4022 ObjCTypes.SelectorPtrTy);
4023 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004024 // Protocol methods have no implementation. So, this entry is always NULL.
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004025 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4026 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
4027}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004028/* *** */
4029
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004030CodeGen::CGObjCRuntime *
4031CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00004032 return new CGObjCMac(CGM);
4033}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004034
4035CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004036CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004037 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004038}