blob: 23d62097e64c161f0f0c80c25c2a00367b14b571 [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,
Fariborz Jahaniancf555162009-01-31 00:59:10 +0000571 llvm::Constant *ClassRoGV,
572 bool HiddenVisibility);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000573
574 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
575
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +0000576 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
577
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000578 /// EmitMethodList - Emit the method list for the given
579 /// implementation. The return value has type MethodListnfABITy.
580 llvm::Constant *EmitMethodList(const std::string &Name,
581 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +0000582 const ConstantVector &Methods);
583 /// EmitIvarList - Emit the ivar list for the given
584 /// implementation. If ForClass is true the list of class ivars
585 /// (i.e. metaclass ivars) is emitted, otherwise the list of
586 /// interface ivars will be emitted. The return value has type
587 /// IvarListnfABIPtrTy.
588 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000589
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +0000590 llvm::Constant *EmitIvarOffsetVar(const ObjCImplementationDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +0000591 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +0000592 unsigned long int offset);
593
Fariborz Jahanianda320092009-01-29 19:24:30 +0000594 /// GetOrEmitProtocol - Get the protocol object for the given
595 /// declaration, emitting it if necessary. The return value has type
596 /// ProtocolPtrTy.
597 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
598
599 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
600 /// object for the given declaration, emitting it if needed. These
601 /// forward references will be filled in with empty bodies if no
602 /// definition is seen. The return value has type ProtocolPtrTy.
603 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
604
605 /// EmitProtocolList - Generate the list of referenced
606 /// protocols. The return value has type ProtocolListPtrTy.
607 llvm::Constant *EmitProtocolList(const std::string &Name,
608 ObjCProtocolDecl::protocol_iterator begin,
609 ObjCProtocolDecl::protocol_iterator end);
610
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000611public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000612 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000613 // FIXME. All stubs for now!
614 virtual llvm::Function *ModuleInitFunction();
615
616 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
617 QualType ResultType,
618 Selector Sel,
619 llvm::Value *Receiver,
620 bool IsClassMessage,
621 const CallArgList &CallArgs)
622 {return RValue::get(0);}
623
624 virtual CodeGen::RValue
625 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
626 QualType ResultType,
627 Selector Sel,
628 const ObjCInterfaceDecl *Class,
629 llvm::Value *Receiver,
630 bool IsClassMessage,
631 const CallArgList &CallArgs){ return RValue::get(0);}
632
633 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
634 const ObjCInterfaceDecl *ID){ return 0; }
635
636 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
637 { return 0; }
638
Fariborz Jahanianeb062d92009-01-26 18:32:24 +0000639 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000640
641 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000642 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +0000643 const ObjCProtocolDecl *PD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000644
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000645 virtual llvm::Function *GetPropertyGetFunction(){ return 0; }
646 virtual llvm::Function *GetPropertySetFunction()
647 { return 0; }
648 virtual llvm::Function *EnumerationMutationFunction()
649 { return 0; }
650
651 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
652 const Stmt &S)
653 { return; }
654 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
655 const ObjCAtThrowStmt &S)
656 { return; }
657 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
658 llvm::Value *AddrWeakObj)
659 { return 0; }
660 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
661 llvm::Value *src, llvm::Value *dst)
662 { return; }
663 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
664 llvm::Value *src, llvm::Value *dest)
665 { return; }
666 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
667 llvm::Value *src, llvm::Value *dest)
668 { return; }
669 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
670 llvm::Value *src, llvm::Value *dest)
671 { return; }
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000672};
673
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000674} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000675
676/* *** Helper Functions *** */
677
678/// getConstantGEP() - Help routine to construct simple GEPs.
679static llvm::Constant *getConstantGEP(llvm::Constant *C,
680 unsigned idx0,
681 unsigned idx1) {
682 llvm::Value *Idxs[] = {
683 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
684 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
685 };
686 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
687}
688
689/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000690
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000691CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
692 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000693{
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000694 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000695 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000696}
697
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000698/// GetClass - Return a reference to the class for the given interface
699/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000700llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000701 const ObjCInterfaceDecl *ID) {
702 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000703}
704
705/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000706llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000707 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000708}
709
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000710/// Generate a constant CFString object.
711/*
712 struct __builtin_CFString {
713 const int *isa; // point to __CFConstantStringClassReference
714 int flags;
715 const char *str;
716 long length;
717 };
718*/
719
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000720llvm::Constant *CGObjCCommonMac::GenerateConstantString(
721 const std::string &String) {
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000722 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000723}
724
725/// Generates a message send where the super is the receiver. This is
726/// a message send to self with special delivery semantics indicating
727/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000728CodeGen::RValue
729CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000730 QualType ResultType,
731 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000732 const ObjCInterfaceDecl *Class,
733 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000734 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000735 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000736 // Create and init a super structure; this is a (receiver, class)
737 // pair we will pass to objc_msgSendSuper.
738 llvm::Value *ObjCSuper =
739 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
740 llvm::Value *ReceiverAsObject =
741 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
742 CGF.Builder.CreateStore(ReceiverAsObject,
743 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000744
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000745 // If this is a class message the metaclass is passed as the target.
746 llvm::Value *Target;
747 if (IsClassMessage) {
748 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
749 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
750 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
751 Target = Super;
752 } else {
753 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
754 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000755 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
756 // and ObjCTypes types.
757 const llvm::Type *ClassTy =
758 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000759 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000760 CGF.Builder.CreateStore(Target,
761 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
762
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000763 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000764 ObjCSuper, ObjCTypes.SuperPtrCTy,
765 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000766}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000767
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000768/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000769CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000770 QualType ResultType,
771 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000772 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000773 bool IsClassMessage,
774 const CallArgList &CallArgs) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000775 llvm::Value *Arg0 =
776 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000777 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000778 Arg0, CGF.getContext().getObjCIdType(),
779 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000780}
781
782CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000783 QualType ResultType,
784 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000785 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000786 QualType Arg0Ty,
787 bool IsSuper,
788 const CallArgList &CallArgs) {
789 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000790 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
791 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
792 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000793 CGF.getContext().getObjCSelType()));
794 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000795
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000796 const llvm::FunctionType *FTy =
Daniel Dunbar725ad312009-01-31 02:19:00 +0000797 CGM.getTypes().GetFunctionType(CGFunctionInfo(ResultType, ActualArgs,
798 false));
Daniel Dunbar5669e572008-10-17 03:24:53 +0000799
800 llvm::Constant *Fn;
801 if (CGM.ReturnTypeUsesSret(ResultType)) {
802 Fn = ObjCTypes.getSendStretFn(IsSuper);
803 } else if (ResultType->isFloatingType()) {
804 // FIXME: Sadly, this is wrong. This actually depends on the
805 // architecture. This happens to be right for x86-32 though.
806 Fn = ObjCTypes.getSendFpretFn(IsSuper);
807 } else {
808 Fn = ObjCTypes.getSendFn(IsSuper);
809 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000810 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar3913f182008-09-09 23:48:28 +0000811 return CGF.EmitCall(Fn, ResultType, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000812}
813
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000814llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000815 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +0000816 // FIXME: I don't understand why gcc generates this, or where it is
817 // resolved. Investigate. Its also wasteful to look this up over and
818 // over.
819 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
820
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000821 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
822 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000823}
824
Fariborz Jahanianda320092009-01-29 19:24:30 +0000825void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000826 // FIXME: We shouldn't need this, the protocol decl should contain
827 // enough information to tell us whether this was a declaration or a
828 // definition.
829 DefinedProtocols.insert(PD->getIdentifier());
830
831 // If we have generated a forward reference to this protocol, emit
832 // it now. Otherwise do nothing, the protocol objects are lazily
833 // emitted.
834 if (Protocols.count(PD->getIdentifier()))
835 GetOrEmitProtocol(PD);
836}
837
Fariborz Jahanianda320092009-01-29 19:24:30 +0000838llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000839 if (DefinedProtocols.count(PD->getIdentifier()))
840 return GetOrEmitProtocol(PD);
841 return GetOrEmitProtocolRef(PD);
842}
843
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000844/*
845 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
846 struct _objc_protocol {
847 struct _objc_protocol_extension *isa;
848 char *protocol_name;
849 struct _objc_protocol_list *protocol_list;
850 struct _objc__method_prototype_list *instance_methods;
851 struct _objc__method_prototype_list *class_methods
852 };
853
854 See EmitProtocolExtension().
855*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000856llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
857 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
858
859 // Early exit if a defining object has already been generated.
860 if (Entry && Entry->hasInitializer())
861 return Entry;
862
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000863 // FIXME: I don't understand why gcc generates this, or where it is
864 // resolved. Investigate. Its also wasteful to look this up over and
865 // over.
866 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
867
Chris Lattner8ec03f52008-11-24 03:54:41 +0000868 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000869
870 // Construct method lists.
871 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
872 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
873 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
874 e = PD->instmeth_end(); i != e; ++i) {
875 ObjCMethodDecl *MD = *i;
876 llvm::Constant *C = GetMethodDescriptionConstant(MD);
877 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
878 OptInstanceMethods.push_back(C);
879 } else {
880 InstanceMethods.push_back(C);
881 }
882 }
883
884 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
885 e = PD->classmeth_end(); i != e; ++i) {
886 ObjCMethodDecl *MD = *i;
887 llvm::Constant *C = GetMethodDescriptionConstant(MD);
888 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
889 OptClassMethods.push_back(C);
890 } else {
891 ClassMethods.push_back(C);
892 }
893 }
894
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000895 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000896 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000897 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +0000898 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000899 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +0000900 PD->protocol_begin(),
901 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000902 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000903 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
904 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000905 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
906 InstanceMethods);
907 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000908 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
909 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000910 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
911 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000912 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
913 Values);
914
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000915 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000916 // Already created, fix the linkage and update the initializer.
917 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000918 Entry->setInitializer(Init);
919 } else {
920 Entry =
921 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
922 llvm::GlobalValue::InternalLinkage,
923 Init,
924 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
925 &CGM.getModule());
926 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
927 UsedGlobals.push_back(Entry);
928 // FIXME: Is this necessary? Why only for protocol?
929 Entry->setAlignment(4);
930 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000931
932 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000933}
934
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000935llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000936 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
937
938 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000939 // We use the initializer as a marker of whether this is a forward
940 // reference or not. At module finalization we add the empty
941 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000942 Entry =
943 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000944 llvm::GlobalValue::ExternalLinkage,
945 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000946 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000947 &CGM.getModule());
948 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
949 UsedGlobals.push_back(Entry);
950 // FIXME: Is this necessary? Why only for protocol?
951 Entry->setAlignment(4);
952 }
953
954 return Entry;
955}
956
957/*
958 struct _objc_protocol_extension {
959 uint32_t size;
960 struct objc_method_description_list *optional_instance_methods;
961 struct objc_method_description_list *optional_class_methods;
962 struct objc_property_list *instance_properties;
963 };
964*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000965llvm::Constant *
966CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
967 const ConstantVector &OptInstanceMethods,
968 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000969 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +0000970 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000971 std::vector<llvm::Constant*> Values(4);
972 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000973 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000974 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
975 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000976 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
977 OptInstanceMethods);
978 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000979 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
980 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000981 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
982 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000983 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
984 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000985 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000986
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000987 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000988 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
989 Values[3]->isNullValue())
990 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
991
992 llvm::Constant *Init =
993 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
994 llvm::GlobalVariable *GV =
995 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
996 llvm::GlobalValue::InternalLinkage,
997 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000998 "\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000999 &CGM.getModule());
1000 // No special section, but goes in llvm.used
1001 UsedGlobals.push_back(GV);
1002
1003 return GV;
1004}
1005
1006/*
1007 struct objc_protocol_list {
1008 struct objc_protocol_list *next;
1009 long count;
1010 Protocol *list[];
1011 };
1012*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001013llvm::Constant *
1014CGObjCMac::EmitProtocolList(const std::string &Name,
1015 ObjCProtocolDecl::protocol_iterator begin,
1016 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001017 std::vector<llvm::Constant*> ProtocolRefs;
1018
Daniel Dunbardbc933702008-08-21 21:57:41 +00001019 for (; begin != end; ++begin)
1020 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001021
1022 // Just return null for empty protocol lists
1023 if (ProtocolRefs.empty())
1024 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1025
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001026 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001027 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1028
1029 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001030 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001031 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1032 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1033 Values[2] =
1034 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1035 ProtocolRefs.size()),
1036 ProtocolRefs);
1037
1038 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1039 llvm::GlobalVariable *GV =
1040 new llvm::GlobalVariable(Init->getType(), false,
1041 llvm::GlobalValue::InternalLinkage,
1042 Init,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001043 Name,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001044 &CGM.getModule());
1045 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
1046 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1047}
1048
1049/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001050 struct _objc_property {
1051 const char * const name;
1052 const char * const attributes;
1053 };
1054
1055 struct _objc_property_list {
1056 uint32_t entsize; // sizeof (struct _objc_property)
1057 uint32_t prop_count;
1058 struct _objc_property[prop_count];
1059 };
1060*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001061llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1062 const Decl *Container,
1063 const ObjCContainerDecl *OCD,
1064 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001065 std::vector<llvm::Constant*> Properties, Prop(2);
Steve Naroff93983f82009-01-11 12:47:58 +00001066 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1067 E = OCD->prop_end(); I != E; ++I) {
1068 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001069 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001070 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001071 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1072 Prop));
1073 }
1074
1075 // Return null for empty list.
1076 if (Properties.empty())
1077 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1078
1079 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001080 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001081 std::vector<llvm::Constant*> Values(3);
1082 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1083 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1084 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1085 Properties.size());
1086 Values[2] = llvm::ConstantArray::get(AT, Properties);
1087 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1088
1089 llvm::GlobalVariable *GV =
1090 new llvm::GlobalVariable(Init->getType(), false,
1091 llvm::GlobalValue::InternalLinkage,
1092 Init,
1093 Name,
1094 &CGM.getModule());
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001095 if (ObjCABI == 2)
1096 GV->setSection("__DATA, __objc_const");
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001097 // No special section on property lists?
1098 UsedGlobals.push_back(GV);
1099 return llvm::ConstantExpr::getBitCast(GV,
1100 ObjCTypes.PropertyListPtrTy);
1101
1102}
1103
1104/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001105 struct objc_method_description_list {
1106 int count;
1107 struct objc_method_description list[];
1108 };
1109*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001110llvm::Constant *
1111CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1112 std::vector<llvm::Constant*> Desc(2);
1113 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1114 ObjCTypes.SelectorPtrTy);
1115 Desc[1] = GetMethodVarType(MD);
1116 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1117 Desc);
1118}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001119
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001120llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1121 const char *Section,
1122 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001123 // Return null for empty list.
1124 if (Methods.empty())
1125 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1126
1127 std::vector<llvm::Constant*> Values(2);
1128 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1129 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1130 Methods.size());
1131 Values[1] = llvm::ConstantArray::get(AT, Methods);
1132 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1133
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001134 llvm::GlobalVariable *GV =
1135 new llvm::GlobalVariable(Init->getType(), false,
1136 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001137 Init, Name, &CGM.getModule());
1138 GV->setSection(Section);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001139 UsedGlobals.push_back(GV);
1140 return llvm::ConstantExpr::getBitCast(GV,
1141 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001142}
1143
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001144/*
1145 struct _objc_category {
1146 char *category_name;
1147 char *class_name;
1148 struct _objc_method_list *instance_methods;
1149 struct _objc_method_list *class_methods;
1150 struct _objc_protocol_list *protocols;
1151 uint32_t size; // <rdar://4585769>
1152 struct _objc_property_list *instance_properties;
1153 };
1154 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001155void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001156 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001157
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001158 // FIXME: This is poor design, the OCD should have a pointer to the
1159 // category decl. Additionally, note that Category can be null for
1160 // the @implementation w/o an @interface case. Sema should just
1161 // create one for us as it does for @implementation so everyone else
1162 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001163 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001164 const ObjCCategoryDecl *Category =
1165 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001166 std::string ExtName(Interface->getNameAsString() + "_" +
1167 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001168
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001169 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1170 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
1171 e = OCD->instmeth_end(); i != e; ++i) {
1172 // Instance methods should always be defined.
1173 InstanceMethods.push_back(GetMethodConstant(*i));
1174 }
1175 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
1176 e = OCD->classmeth_end(); i != e; ++i) {
1177 // Class methods should always be defined.
1178 ClassMethods.push_back(GetMethodConstant(*i));
1179 }
1180
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001181 std::vector<llvm::Constant*> Values(7);
1182 Values[0] = GetClassName(OCD->getIdentifier());
1183 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001184 Values[2] =
1185 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1186 ExtName,
1187 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001188 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001189 Values[3] =
1190 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
1191 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001192 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001193 if (Category) {
1194 Values[4] =
1195 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1196 Category->protocol_begin(),
1197 Category->protocol_end());
1198 } else {
1199 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1200 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001201 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001202
1203 // If there is no category @interface then there can be no properties.
1204 if (Category) {
1205 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001206 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001207 } else {
1208 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1209 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001210
1211 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1212 Values);
1213
1214 llvm::GlobalVariable *GV =
1215 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
1216 llvm::GlobalValue::InternalLinkage,
1217 Init,
1218 std::string("\01L_OBJC_CATEGORY_")+ExtName,
1219 &CGM.getModule());
1220 GV->setSection("__OBJC,__category,regular,no_dead_strip");
1221 UsedGlobals.push_back(GV);
1222 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001223}
1224
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001225// FIXME: Get from somewhere?
1226enum ClassFlags {
1227 eClassFlags_Factory = 0x00001,
1228 eClassFlags_Meta = 0x00002,
1229 // <rdr://5142207>
1230 eClassFlags_HasCXXStructors = 0x02000,
1231 eClassFlags_Hidden = 0x20000,
1232 eClassFlags_ABI2_Hidden = 0x00010,
1233 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1234};
1235
1236// <rdr://5142207&4705298&4843145>
1237static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
1238 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
1239 // FIXME: Support -fvisibility
1240 switch (attr->getVisibility()) {
1241 default:
1242 assert(0 && "Unknown visibility");
1243 return false;
1244 case VisibilityAttr::DefaultVisibility:
1245 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
1246 return false;
1247 case VisibilityAttr::HiddenVisibility:
1248 return true;
1249 }
1250 } else {
1251 return false; // FIXME: Support -fvisibility
1252 }
1253}
1254
1255/*
1256 struct _objc_class {
1257 Class isa;
1258 Class super_class;
1259 const char *name;
1260 long version;
1261 long info;
1262 long instance_size;
1263 struct _objc_ivar_list *ivars;
1264 struct _objc_method_list *methods;
1265 struct _objc_cache *cache;
1266 struct _objc_protocol_list *protocols;
1267 // Objective-C 1.0 extensions (<rdr://4585769>)
1268 const char *ivar_layout;
1269 struct _objc_class_ext *ext;
1270 };
1271
1272 See EmitClassExtension();
1273 */
1274void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001275 DefinedSymbols.insert(ID->getIdentifier());
1276
Chris Lattner8ec03f52008-11-24 03:54:41 +00001277 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001278 // FIXME: Gross
1279 ObjCInterfaceDecl *Interface =
1280 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001281 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001282 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001283 Interface->protocol_begin(),
1284 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001285 const llvm::Type *InterfaceTy =
1286 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
1287 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001288 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001289
1290 // FIXME: Set CXX-structors flag.
1291 if (IsClassHidden(ID->getClassInterface()))
1292 Flags |= eClassFlags_Hidden;
1293
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001294 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1295 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
1296 e = ID->instmeth_end(); i != e; ++i) {
1297 // Instance methods should always be defined.
1298 InstanceMethods.push_back(GetMethodConstant(*i));
1299 }
1300 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
1301 e = ID->classmeth_end(); i != e; ++i) {
1302 // Class methods should always be defined.
1303 ClassMethods.push_back(GetMethodConstant(*i));
1304 }
1305
1306 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
1307 e = ID->propimpl_end(); i != e; ++i) {
1308 ObjCPropertyImplDecl *PID = *i;
1309
1310 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1311 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1312
1313 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1314 if (llvm::Constant *C = GetMethodConstant(MD))
1315 InstanceMethods.push_back(C);
1316 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1317 if (llvm::Constant *C = GetMethodConstant(MD))
1318 InstanceMethods.push_back(C);
1319 }
1320 }
1321
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001322 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001323 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001324 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001325 // Record a reference to the super class.
1326 LazySymbols.insert(Super->getIdentifier());
1327
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001328 Values[ 1] =
1329 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1330 ObjCTypes.ClassPtrTy);
1331 } else {
1332 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1333 }
1334 Values[ 2] = GetClassName(ID->getIdentifier());
1335 // Version is always 0.
1336 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1337 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1338 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001339 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001340 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001341 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001342 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001343 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001344 // cache is always NULL.
1345 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1346 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001347 // FIXME: Set ivar_layout
1348 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001349 Values[11] = EmitClassExtension(ID);
1350 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1351 Values);
1352
1353 llvm::GlobalVariable *GV =
1354 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1355 llvm::GlobalValue::InternalLinkage,
1356 Init,
1357 std::string("\01L_OBJC_CLASS_")+ClassName,
1358 &CGM.getModule());
1359 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1360 UsedGlobals.push_back(GV);
1361 // FIXME: Why?
1362 GV->setAlignment(32);
1363 DefinedClasses.push_back(GV);
1364}
1365
1366llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1367 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001368 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001369 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001370 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001371 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001372
1373 if (IsClassHidden(ID->getClassInterface()))
1374 Flags |= eClassFlags_Hidden;
1375
1376 std::vector<llvm::Constant*> Values(12);
1377 // The isa for the metaclass is the root of the hierarchy.
1378 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1379 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1380 Root = Super;
1381 Values[ 0] =
1382 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1383 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001384 // The super class for the metaclass is emitted as the name of the
1385 // super class. The runtime fixes this up to point to the
1386 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001387 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1388 Values[ 1] =
1389 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1390 ObjCTypes.ClassPtrTy);
1391 } else {
1392 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1393 }
1394 Values[ 2] = GetClassName(ID->getIdentifier());
1395 // Version is always 0.
1396 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1397 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1398 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001399 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001400 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001401 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001402 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001403 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001404 // cache is always NULL.
1405 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1406 Values[ 9] = Protocols;
1407 // ivar_layout for metaclass is always NULL.
1408 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1409 // The class extension is always unused for metaclasses.
1410 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1411 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1412 Values);
1413
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001414 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00001415 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001416
1417 // Check for a forward reference.
1418 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1419 if (GV) {
1420 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1421 "Forward metaclass reference has incorrect type.");
1422 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1423 GV->setInitializer(Init);
1424 } else {
1425 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1426 llvm::GlobalValue::InternalLinkage,
1427 Init, Name,
1428 &CGM.getModule());
1429 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001430 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1431 UsedGlobals.push_back(GV);
1432 // FIXME: Why?
1433 GV->setAlignment(32);
1434
1435 return GV;
1436}
1437
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001438llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001439 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001440
1441 // FIXME: Should we look these up somewhere other than the
1442 // module. Its a bit silly since we only generate these while
1443 // processing an implementation, so exactly one pointer would work
1444 // if know when we entered/exitted an implementation block.
1445
1446 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00001447 // Previously, metaclass with internal linkage may have been defined.
1448 // pass 'true' as 2nd argument so it is returned.
1449 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001450 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1451 "Forward metaclass reference has incorrect type.");
1452 return GV;
1453 } else {
1454 // Generate as an external reference to keep a consistent
1455 // module. This will be patched up when we emit the metaclass.
1456 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1457 llvm::GlobalValue::ExternalLinkage,
1458 0,
1459 Name,
1460 &CGM.getModule());
1461 }
1462}
1463
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001464/*
1465 struct objc_class_ext {
1466 uint32_t size;
1467 const char *weak_ivar_layout;
1468 struct _objc_property_list *properties;
1469 };
1470*/
1471llvm::Constant *
1472CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1473 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001474 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001475
1476 std::vector<llvm::Constant*> Values(3);
1477 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001478 // FIXME: Output weak_ivar_layout string.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001479 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001480 Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001481 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001482
1483 // Return null if no extension bits are used.
1484 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1485 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1486
1487 llvm::Constant *Init =
1488 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1489 llvm::GlobalVariable *GV =
1490 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1491 llvm::GlobalValue::InternalLinkage,
1492 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001493 "\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001494 &CGM.getModule());
1495 // No special section, but goes in llvm.used
1496 UsedGlobals.push_back(GV);
1497
1498 return GV;
1499}
1500
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001501/// countInheritedIvars - count number of ivars in class and its super class(s)
1502///
1503static int countInheritedIvars(const ObjCInterfaceDecl *OI) {
1504 int count = 0;
1505 if (!OI)
1506 return 0;
1507 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
1508 if (SuperClass)
1509 count += countInheritedIvars(SuperClass);
1510 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1511 E = OI->ivar_end(); I != E; ++I)
1512 ++count;
1513 return count;
1514}
1515
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001516/*
1517 struct objc_ivar {
1518 char *ivar_name;
1519 char *ivar_type;
1520 int ivar_offset;
1521 };
1522
1523 struct objc_ivar_list {
1524 int ivar_count;
1525 struct objc_ivar list[count];
1526 };
1527 */
1528llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001529 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001530 std::vector<llvm::Constant*> Ivars, Ivar(3);
1531
1532 // When emitting the root class GCC emits ivar entries for the
1533 // actual class structure. It is not clear if we need to follow this
1534 // behavior; for now lets try and get away with not doing it. If so,
1535 // the cleanest solution would be to make up an ObjCInterfaceDecl
1536 // for the class.
1537 if (ForClass)
1538 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001539
1540 ObjCInterfaceDecl *OID =
1541 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
1542 const llvm::Type *InterfaceTy =
1543 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001544 const llvm::StructLayout *Layout =
1545 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001546
1547 RecordDecl::field_iterator ifield, pfield;
1548 const RecordDecl *RD = GetFirstIvarInRecord(OID, ifield, pfield);
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001549 for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) {
1550 FieldDecl *Field = *ifield;
1551 unsigned Offset = Layout->getElementOffset(CGM.getTypes().
1552 getLLVMFieldNo(Field));
1553 if (Field->getIdentifier())
1554 Ivar[0] = GetMethodVarName(Field->getIdentifier());
1555 else
1556 Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001557 std::string TypeStr;
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001558 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001559 Ivar[1] = GetMethodVarType(TypeStr);
1560 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001561 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001562 }
1563
1564 // Return null for empty list.
1565 if (Ivars.empty())
1566 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1567
1568 std::vector<llvm::Constant*> Values(2);
1569 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1570 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1571 Ivars.size());
1572 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1573 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1574
1575 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1576 "\01L_OBJC_INSTANCE_VARIABLES_");
1577 llvm::GlobalVariable *GV =
1578 new llvm::GlobalVariable(Init->getType(), false,
1579 llvm::GlobalValue::InternalLinkage,
1580 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001581 Prefix + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001582 &CGM.getModule());
1583 if (ForClass) {
1584 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1585 // FIXME: Why is this only here?
1586 GV->setAlignment(32);
1587 } else {
1588 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1589 }
1590 UsedGlobals.push_back(GV);
1591 return llvm::ConstantExpr::getBitCast(GV,
1592 ObjCTypes.IvarListPtrTy);
1593}
1594
1595/*
1596 struct objc_method {
1597 SEL method_name;
1598 char *method_types;
1599 void *method;
1600 };
1601
1602 struct objc_method_list {
1603 struct objc_method_list *obsolete;
1604 int count;
1605 struct objc_method methods_list[count];
1606 };
1607*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001608
1609/// GetMethodConstant - Return a struct objc_method constant for the
1610/// given method if it has been defined. The result is null if the
1611/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001612llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001613 // FIXME: Use DenseMap::lookup
1614 llvm::Function *Fn = MethodDefinitions[MD];
1615 if (!Fn)
1616 return 0;
1617
1618 std::vector<llvm::Constant*> Method(3);
1619 Method[0] =
1620 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1621 ObjCTypes.SelectorPtrTy);
1622 Method[1] = GetMethodVarType(MD);
1623 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1624 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1625}
1626
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001627llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1628 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001629 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001630 // Return null for empty list.
1631 if (Methods.empty())
1632 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1633
1634 std::vector<llvm::Constant*> Values(3);
1635 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1636 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1637 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1638 Methods.size());
1639 Values[2] = llvm::ConstantArray::get(AT, Methods);
1640 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1641
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001642 llvm::GlobalVariable *GV =
1643 new llvm::GlobalVariable(Init->getType(), false,
1644 llvm::GlobalValue::InternalLinkage,
1645 Init,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001646 Name,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001647 &CGM.getModule());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001648 GV->setSection(Section);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001649 UsedGlobals.push_back(GV);
1650 return llvm::ConstantExpr::getBitCast(GV,
1651 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001652}
1653
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001654llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001655 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001656 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001657 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001658
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001659 const llvm::FunctionType *MethodTy =
1660 CGM.getTypes().GetFunctionType(CGFunctionInfo(OMD, CGM.getContext()));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001661 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001662 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001663 llvm::GlobalValue::InternalLinkage,
1664 Name,
1665 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001666 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001667
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001668 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001669}
1670
1671llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001672 // Abuse this interface function as a place to finalize.
1673 FinishModule();
1674
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001675 return NULL;
1676}
1677
Daniel Dunbar49f66022008-09-24 03:38:44 +00001678llvm::Function *CGObjCMac::GetPropertyGetFunction() {
1679 return ObjCTypes.GetPropertyFn;
1680}
1681
1682llvm::Function *CGObjCMac::GetPropertySetFunction() {
1683 return ObjCTypes.SetPropertyFn;
1684}
1685
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001686llvm::Function *CGObjCMac::EnumerationMutationFunction()
1687{
1688 return ObjCTypes.EnumerationMutationFn;
1689}
1690
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001691/*
1692
1693Objective-C setjmp-longjmp (sjlj) Exception Handling
1694--
1695
1696The basic framework for a @try-catch-finally is as follows:
1697{
1698 objc_exception_data d;
1699 id _rethrow = null;
1700
1701 objc_exception_try_enter(&d);
1702 if (!setjmp(d.jmp_buf)) {
1703 ... try body ...
1704 } else {
1705 // exception path
1706 id _caught = objc_exception_extract(&d);
1707
1708 // enter new try scope for handlers
1709 if (!setjmp(d.jmp_buf)) {
1710 ... match exception and execute catch blocks ...
1711
1712 // fell off end, rethrow.
1713 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00001714 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001715 } else {
1716 // exception in catch block
1717 _rethrow = objc_exception_extract(&d);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001718 ... jump-through-finally_no_exit to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001719 }
1720 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00001721 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001722
1723finally:
1724 // match either the initial try_enter or the catch try_enter,
1725 // depending on the path followed.
1726 objc_exception_try_exit(&d);
1727finally_no_exit:
1728 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00001729 ... dispatch to finally destination ...
1730
1731finally_rethrow:
1732 objc_exception_throw(_rethrow);
1733
1734finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001735}
1736
1737This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00001738uses _rethrow to determine if objc_exception_try_exit should be called
1739and if the object should be rethrown. This breaks in the face of
1740throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001741
1742We specialize this framework for a few particular circumstances:
1743
1744 - If there are no catch blocks, then we avoid emitting the second
1745 exception handling context.
1746
1747 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
1748 e)) we avoid emitting the code to rethrow an uncaught exception.
1749
1750 - FIXME: If there is no @finally block we can do a few more
1751 simplifications.
1752
1753Rethrows and Jumps-Through-Finally
1754--
1755
1756Support for implicit rethrows and jumping through the finally block is
1757handled by storing the current exception-handling context in
1758ObjCEHStack.
1759
Daniel Dunbar898d5082008-09-30 01:06:03 +00001760In order to implement proper @finally semantics, we support one basic
1761mechanism for jumping through the finally block to an arbitrary
1762destination. Constructs which generate exits from a @try or @catch
1763block use this mechanism to implement the proper semantics by chaining
1764jumps, as necessary.
1765
1766This mechanism works like the one used for indirect goto: we
1767arbitrarily assign an ID to each destination and store the ID for the
1768destination in a variable prior to entering the finally block. At the
1769end of the finally block we simply create a switch to the proper
1770destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001771
1772Code gen for @synchronized(expr) stmt;
1773Effectively generating code for:
1774objc_sync_enter(expr);
1775@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001776*/
1777
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001778void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1779 const Stmt &S) {
1780 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001781 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001782 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
1783 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
1784 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1785 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001786 llvm::Value *DestCode =
1787 CGF.CreateTempAlloca(llvm::Type::Int32Ty, "finally.dst");
1788
1789 // Generate jump code. Done here so we can directly add things to
1790 // the switch instruction.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001791 llvm::BasicBlock *FinallyJump = CGF.createBasicBlock("finally.jump");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001792 llvm::SwitchInst *FinallySwitch =
1793 llvm::SwitchInst::Create(new llvm::LoadInst(DestCode, "", FinallyJump),
1794 FinallyEnd, 10, FinallyJump);
1795
1796 // Push an EH context entry, used for handling rethrows and jumps
1797 // through finally.
1798 CodeGenFunction::ObjCEHEntry EHEntry(FinallyBlock, FinallyNoExit,
1799 FinallySwitch, DestCode);
1800 CGF.ObjCEHStack.push_back(&EHEntry);
1801
1802 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00001803 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
1804 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001805 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
1806 "_rethrow");
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001807 if (!isTry) {
1808 // For @synchronized, call objc_sync_enter(sync.expr)
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001809 llvm::Value *Arg = CGF.EmitScalarExpr(
1810 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1811 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1812 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, Arg);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001813 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001814
1815 // Enter a new try block and call setjmp.
1816 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
1817 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
1818 "jmpbufarray");
1819 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
1820 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1821 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001822
Daniel Dunbar55e87422008-11-11 02:29:29 +00001823 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1824 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001825 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001826 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001827
1828 // Emit the @try block.
1829 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001830 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
1831 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001832 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001833
1834 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001835 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001836
1837 // Retrieve the exception object. We may emit multiple blocks but
1838 // nothing can cross this so the value is already in SSA form.
1839 llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1840 ExceptionData,
1841 "caught");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001842 EHEntry.Exception = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001843 if (!isTry)
1844 {
1845 CGF.Builder.CreateStore(Caught, RethrowPtr);
1846 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
1847 }
1848 else if (const ObjCAtCatchStmt* CatchStmt =
1849 cast<ObjCAtTryStmt>(S).getCatchStmts())
1850 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00001851 // Enter a new exception try block (in case a @catch block throws
1852 // an exception).
Anders Carlsson80f25672008-09-09 17:59:25 +00001853 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001854
Anders Carlsson80f25672008-09-09 17:59:25 +00001855 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1856 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001857 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00001858
Daniel Dunbar55e87422008-11-11 02:29:29 +00001859 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
1860 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001861 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001862
1863 CGF.EmitBlock(CatchBlock);
1864
Daniel Dunbar55e40722008-09-27 07:03:52 +00001865 // Handle catch list. As a special case we check if everything is
1866 // matched and avoid generating code for falling off the end if
1867 // so.
1868 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00001869 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00001870 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00001871
Anders Carlssondde0a942008-09-11 09:15:33 +00001872 const DeclStmt *CatchParam =
1873 cast_or_null<DeclStmt>(CatchStmt->getCatchParamStmt());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001874 const VarDecl *VD = 0;
1875 const PointerType *PT = 0;
1876
Anders Carlsson80f25672008-09-09 17:59:25 +00001877 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00001878 if (!CatchParam) {
1879 AllMatched = true;
1880 } else {
Ted Kremenekde3b8fb2008-10-06 20:58:56 +00001881 VD = cast<VarDecl>(CatchParam->getSolitaryDecl());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001882 PT = VD->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001883
Daniel Dunbar97f61d12008-09-27 22:21:14 +00001884 // catch(id e) always matches.
1885 // FIXME: For the time being we also match id<X>; this should
1886 // be rejected by Sema instead.
1887 if ((PT && CGF.getContext().isObjCIdType(PT->getPointeeType())) ||
1888 VD->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00001889 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00001890 }
1891
Daniel Dunbar55e40722008-09-27 07:03:52 +00001892 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00001893 if (CatchParam) {
1894 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001895 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar129271a2008-09-27 07:36:24 +00001896 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00001897 }
Anders Carlsson1452f552008-09-11 08:21:54 +00001898
Anders Carlssondde0a942008-09-11 09:15:33 +00001899 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001900 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001901 break;
1902 }
1903
Daniel Dunbar129271a2008-09-27 07:36:24 +00001904 assert(PT && "Unexpected non-pointer type in @catch");
1905 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00001906 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001907 assert(ObjCType && "Catch parameter must have Objective-C type!");
1908
1909 // Check if the @catch block matches the exception object.
1910 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
1911
Anders Carlsson80f25672008-09-09 17:59:25 +00001912 llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn,
1913 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00001914
Daniel Dunbar55e87422008-11-11 02:29:29 +00001915 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00001916
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001917 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001918 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001919
1920 // Emit the @catch block.
1921 CGF.EmitBlock(MatchedBlock);
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001922 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001923 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001924
1925 llvm::Value *Tmp =
1926 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(VD->getType()),
1927 "tmp");
1928 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00001929
1930 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001931 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001932
1933 CGF.EmitBlock(NextCatchBlock);
1934 }
1935
Daniel Dunbar55e40722008-09-27 07:03:52 +00001936 if (!AllMatched) {
1937 // None of the handlers caught the exception, so store it to be
1938 // rethrown at the end of the @finally block.
1939 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001940 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001941 }
1942
1943 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001944 CGF.EmitBlock(CatchHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001945 CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1946 ExceptionData),
1947 RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001948 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001949 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00001950 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001951 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Anders Carlsson80f25672008-09-09 17:59:25 +00001952 }
1953
Daniel Dunbar898d5082008-09-30 01:06:03 +00001954 // Pop the exception-handling stack entry. It is important to do
1955 // this now, because the code in the @finally block is not in this
1956 // context.
1957 CGF.ObjCEHStack.pop_back();
1958
Anders Carlsson80f25672008-09-09 17:59:25 +00001959 // Emit the @finally block.
1960 CGF.EmitBlock(FinallyBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001961 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00001962
1963 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001964 if (isTry) {
1965 if (const ObjCAtFinallyStmt* FinallyStmt =
1966 cast<ObjCAtTryStmt>(S).getFinallyStmt())
1967 CGF.EmitStmt(FinallyStmt->getFinallyBody());
1968 }
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001969 else {
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001970 // For @synchronized objc_sync_exit(expr); As finally's sole statement.
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001971 // For @synchronized, call objc_sync_enter(sync.expr)
1972 llvm::Value *Arg = CGF.EmitScalarExpr(
1973 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1974 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1975 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, Arg);
1976 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001977
Daniel Dunbar898d5082008-09-30 01:06:03 +00001978 CGF.EmitBlock(FinallyJump);
1979
1980 CGF.EmitBlock(FinallyRethrow);
1981 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn,
1982 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001983 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00001984
1985 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001986}
1987
1988void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00001989 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00001990 llvm::Value *ExceptionAsObject;
1991
1992 if (const Expr *ThrowExpr = S.getThrowExpr()) {
1993 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
1994 ExceptionAsObject =
1995 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
1996 } else {
Daniel Dunbar898d5082008-09-30 01:06:03 +00001997 assert((!CGF.ObjCEHStack.empty() && CGF.ObjCEHStack.back()->Exception) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001998 "Unexpected rethrow outside @catch block.");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001999 ExceptionAsObject = CGF.ObjCEHStack.back()->Exception;
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002000 }
2001
2002 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002003 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002004
2005 // Clear the insertion point to indicate we are in unreachable code.
2006 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002007}
2008
Daniel Dunbar898d5082008-09-30 01:06:03 +00002009void CodeGenFunction::EmitJumpThroughFinally(ObjCEHEntry *E,
2010 llvm::BasicBlock *Dst,
2011 bool ExecuteTryExit) {
Daniel Dunbara448fb22008-11-11 23:11:34 +00002012 if (!HaveInsertPoint())
Daniel Dunbar898d5082008-09-30 01:06:03 +00002013 return;
2014
2015 // Find the destination code for this block. We always use 0 for the
2016 // fallthrough block (default destination).
2017 llvm::SwitchInst *SI = E->FinallySwitch;
2018 llvm::ConstantInt *ID;
2019 if (Dst == SI->getDefaultDest()) {
2020 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
2021 } else {
2022 ID = SI->findCaseDest(Dst);
2023 if (!ID) {
2024 // No code found, get a new unique one by just using the number
2025 // of switch successors.
2026 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, SI->getNumSuccessors());
2027 SI->addCase(ID, Dst);
2028 }
2029 }
2030
2031 // Set the destination code and branch.
2032 Builder.CreateStore(ID, E->DestCode);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002033 EmitBranch(ExecuteTryExit ? E->FinallyBlock : E->FinallyNoExit);
Daniel Dunbar898d5082008-09-30 01:06:03 +00002034}
2035
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002036/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002037/// object: objc_read_weak (id *src)
2038///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002039llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002040 llvm::Value *AddrWeakObj)
2041{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002042 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002043 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002044 AddrWeakObj, "weakread");
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002045 return read_weak;
2046}
2047
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002048/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2049/// objc_assign_weak (id src, id *dst)
2050///
2051void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2052 llvm::Value *src, llvm::Value *dst)
2053{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002054 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2055 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002056 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
2057 src, dst, "weakassign");
2058 return;
2059}
2060
Fariborz Jahanian58626502008-11-19 00:59:10 +00002061/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2062/// objc_assign_global (id src, id *dst)
2063///
2064void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2065 llvm::Value *src, llvm::Value *dst)
2066{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002067 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2068 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002069 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
2070 src, dst, "globalassign");
2071 return;
2072}
2073
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002074/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2075/// objc_assign_ivar (id src, id *dst)
2076///
2077void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2078 llvm::Value *src, llvm::Value *dst)
2079{
2080 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2081 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
2082 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
2083 src, dst, "assignivar");
2084 return;
2085}
2086
Fariborz Jahanian58626502008-11-19 00:59:10 +00002087/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2088/// objc_assign_strongCast (id src, id *dst)
2089///
2090void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2091 llvm::Value *src, llvm::Value *dst)
2092{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002093 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2094 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002095 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
2096 src, dst, "weakassign");
2097 return;
2098}
2099
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002100/* *** Private Interface *** */
2101
2102/// EmitImageInfo - Emit the image info marker used to encode some module
2103/// level information.
2104///
2105/// See: <rdr://4810609&4810587&4810587>
2106/// struct IMAGE_INFO {
2107/// unsigned version;
2108/// unsigned flags;
2109/// };
2110enum ImageInfoFlags {
2111 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
2112 eImageInfo_GarbageCollected = (1 << 1),
2113 eImageInfo_GCOnly = (1 << 2)
2114};
2115
2116void CGObjCMac::EmitImageInfo() {
2117 unsigned version = 0; // Version is unused?
2118 unsigned flags = 0;
2119
2120 // FIXME: Fix and continue?
2121 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2122 flags |= eImageInfo_GarbageCollected;
2123 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2124 flags |= eImageInfo_GCOnly;
2125
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002126 // Emitted as int[2];
2127 llvm::Constant *values[2] = {
2128 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2129 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2130 };
2131 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002132 llvm::GlobalVariable *GV =
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002133 new llvm::GlobalVariable(AT, true,
2134 llvm::GlobalValue::InternalLinkage,
2135 llvm::ConstantArray::get(AT, values, 2),
2136 "\01L_OBJC_IMAGE_INFO",
2137 &CGM.getModule());
2138
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002139 if (ObjCABI == 1) {
2140 GV->setSection("__OBJC, __image_info,regular");
2141 } else {
2142 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
2143 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002144
2145 UsedGlobals.push_back(GV);
2146}
2147
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002148
2149// struct objc_module {
2150// unsigned long version;
2151// unsigned long size;
2152// const char *name;
2153// Symtab symtab;
2154// };
2155
2156// FIXME: Get from somewhere
2157static const int ModuleVersion = 7;
2158
2159void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002160 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002161
2162 std::vector<llvm::Constant*> Values(4);
2163 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2164 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002165 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002166 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002167 Values[3] = EmitModuleSymbols();
2168
2169 llvm::GlobalVariable *GV =
2170 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
2171 llvm::GlobalValue::InternalLinkage,
2172 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
2173 Values),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002174 "\01L_OBJC_MODULES",
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002175 &CGM.getModule());
2176 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
2177 UsedGlobals.push_back(GV);
2178}
2179
2180llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002181 unsigned NumClasses = DefinedClasses.size();
2182 unsigned NumCategories = DefinedCategories.size();
2183
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002184 // Return null if no symbols were defined.
2185 if (!NumClasses && !NumCategories)
2186 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2187
2188 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002189 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2190 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2191 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2192 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2193
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002194 // The runtime expects exactly the list of defined classes followed
2195 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002196 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002197 for (unsigned i=0; i<NumClasses; i++)
2198 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2199 ObjCTypes.Int8PtrTy);
2200 for (unsigned i=0; i<NumCategories; i++)
2201 Symbols[NumClasses + i] =
2202 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2203 ObjCTypes.Int8PtrTy);
2204
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002205 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002206 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002207 NumClasses + NumCategories),
2208 Symbols);
2209
2210 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2211
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002212 llvm::GlobalVariable *GV =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002213 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002214 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002215 Init,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002216 "\01L_OBJC_SYMBOLS",
2217 &CGM.getModule());
2218 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
2219 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002220 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2221}
2222
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002223llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002224 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002225 LazySymbols.insert(ID->getIdentifier());
2226
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002227 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2228
2229 if (!Entry) {
2230 llvm::Constant *Casted =
2231 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2232 ObjCTypes.ClassPtrTy);
2233 Entry =
2234 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
2235 llvm::GlobalValue::InternalLinkage,
2236 Casted, "\01L_OBJC_CLASS_REFERENCES_",
2237 &CGM.getModule());
2238 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
2239 UsedGlobals.push_back(Entry);
2240 }
2241
2242 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002243}
2244
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002245llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002246 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2247
2248 if (!Entry) {
2249 llvm::Constant *Casted =
2250 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2251 ObjCTypes.SelectorPtrTy);
2252 Entry =
2253 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
2254 llvm::GlobalValue::InternalLinkage,
2255 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
2256 &CGM.getModule());
2257 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
2258 UsedGlobals.push_back(Entry);
2259 }
2260
2261 return Builder.CreateLoad(Entry, false, "tmp");
2262}
2263
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002264llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002265 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002266
2267 if (!Entry) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002268 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002269 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002270 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002271 llvm::GlobalValue::InternalLinkage,
2272 C, "\01L_OBJC_CLASS_NAME_",
2273 &CGM.getModule());
2274 Entry->setSection("__TEXT,__cstring,cstring_literals");
2275 UsedGlobals.push_back(Entry);
2276 }
2277
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002278 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002279}
2280
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002281llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002282 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
2283
2284 if (!Entry) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00002285 // FIXME: Avoid std::string copying.
2286 llvm::Constant *C = llvm::ConstantArray::get(Sel.getAsString());
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002287 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002288 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002289 llvm::GlobalValue::InternalLinkage,
2290 C, "\01L_OBJC_METH_VAR_NAME_",
2291 &CGM.getModule());
2292 Entry->setSection("__TEXT,__cstring,cstring_literals");
2293 UsedGlobals.push_back(Entry);
2294 }
2295
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002296 return getConstantGEP(Entry, 0, 0);
2297}
2298
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002299// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002300llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002301 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
2302}
2303
2304// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002305llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002306 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
2307}
2308
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002309llvm::Constant *CGObjCCommonMac::GetMethodVarType(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002310 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002311
2312 if (!Entry) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002313 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002314 Entry =
2315 new llvm::GlobalVariable(C->getType(), false,
2316 llvm::GlobalValue::InternalLinkage,
2317 C, "\01L_OBJC_METH_VAR_TYPE_",
2318 &CGM.getModule());
2319 Entry->setSection("__TEXT,__cstring,cstring_literals");
2320 UsedGlobals.push_back(Entry);
2321 }
2322
2323 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002324}
2325
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002326// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002327llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002328 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002329 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
2330 TypeStr);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002331 return GetMethodVarType(TypeStr);
2332}
2333
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002334// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002335llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002336 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
2337
2338 if (!Entry) {
2339 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
2340 Entry =
2341 new llvm::GlobalVariable(C->getType(), false,
2342 llvm::GlobalValue::InternalLinkage,
2343 C, "\01L_OBJC_PROP_NAME_ATTR_",
2344 &CGM.getModule());
2345 Entry->setSection("__TEXT,__cstring,cstring_literals");
2346 UsedGlobals.push_back(Entry);
2347 }
2348
2349 return getConstantGEP(Entry, 0, 0);
2350}
2351
2352// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002353// FIXME: This Decl should be more precise.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002354llvm::Constant *CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002355 const Decl *Container) {
2356 std::string TypeStr;
2357 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002358 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
2359}
2360
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002361void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
2362 const ObjCContainerDecl *CD,
2363 std::string &NameOut) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002364 // FIXME: Find the mangling GCC uses.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002365 NameOut = (D->isInstanceMethod() ? "-" : "+");
Chris Lattner077bf5e2008-11-24 03:33:13 +00002366 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002367 assert (CD && "Missing container decl in GetNameForMethod");
2368 NameOut += CD->getNameAsString();
Fariborz Jahanian52847332009-01-26 23:49:05 +00002369 // FIXME. For a method in a category, (CAT_NAME) is inserted here.
2370 // Right now! there is not enough info. to do this.
Chris Lattner077bf5e2008-11-24 03:33:13 +00002371 NameOut += ' ';
2372 NameOut += D->getSelector().getAsString();
2373 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002374}
2375
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002376/// GetFirstIvarInRecord - This routine returns the record for the
2377/// implementation of the fiven class OID. It also returns field
2378/// corresponding to the first ivar in the class in FIV. It also
2379/// returns the one before the first ivar.
2380///
2381const RecordDecl *CGObjCCommonMac::GetFirstIvarInRecord(
2382 const ObjCInterfaceDecl *OID,
2383 RecordDecl::field_iterator &FIV,
2384 RecordDecl::field_iterator &PIV) {
2385 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass());
2386 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
2387 RecordDecl::field_iterator ifield = RD->field_begin();
2388 RecordDecl::field_iterator pfield = RD->field_end();
2389 while (countSuperClassIvars-- > 0) {
2390 pfield = ifield;
2391 ++ifield;
2392 }
2393 FIV = ifield;
2394 PIV = pfield;
2395 return RD;
2396}
2397
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002398void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002399 EmitModuleInfo();
2400
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002401 // Emit the dummy bodies for any protocols which were referenced but
2402 // never defined.
2403 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
2404 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
2405 if (i->second->hasInitializer())
2406 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002407
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002408 std::vector<llvm::Constant*> Values(5);
2409 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
2410 Values[1] = GetClassName(i->first);
2411 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
2412 Values[3] = Values[4] =
2413 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
2414 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
2415 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
2416 Values));
2417 }
2418
2419 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002420 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002421 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002422 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002423 }
2424
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002425 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002426 llvm::GlobalValue *GV =
2427 new llvm::GlobalVariable(AT, false,
2428 llvm::GlobalValue::AppendingLinkage,
2429 llvm::ConstantArray::get(AT, Used),
2430 "llvm.used",
2431 &CGM.getModule());
2432
2433 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002434
2435 // Add assembler directives to add lazy undefined symbol references
2436 // for classes which are referenced but not defined. This is
2437 // important for correct linker interaction.
2438
2439 // FIXME: Uh, this isn't particularly portable.
2440 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00002441
2442 if (!CGM.getModule().getModuleInlineAsm().empty())
2443 s << "\n";
2444
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002445 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
2446 e = LazySymbols.end(); i != e; ++i) {
2447 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
2448 }
2449 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
2450 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002451 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002452 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
2453 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00002454
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002455 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002456}
2457
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002458CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002459 : CGObjCCommonMac(cgm),
2460 ObjCTypes(cgm)
2461{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00002462 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002463 ObjCABI = 2;
2464}
2465
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002466/* *** */
2467
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002468ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
2469: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002470{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002471 CodeGen::CodeGenTypes &Types = CGM.getTypes();
2472 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002473
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002474 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002475 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002476 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002477 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
2478
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002479 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002480 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002481 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002482
2483 // FIXME: It would be nice to unify this with the opaque type, so
2484 // that the IR comes out a bit cleaner.
2485 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
2486 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002487
2488 // I'm not sure I like this. The implicit coordination is a bit
2489 // gross. We should solve this in a reasonable fashion because this
2490 // is a pretty common task (match some runtime data structure with
2491 // an LLVM data structure).
2492
2493 // FIXME: This is leaked.
2494 // FIXME: Merge with rewriter code?
2495
2496 // struct _objc_super {
2497 // id self;
2498 // Class cls;
2499 // }
2500 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
2501 SourceLocation(),
2502 &Ctx.Idents.get("_objc_super"));
2503 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2504 Ctx.getObjCIdType(), 0, false));
2505 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2506 Ctx.getObjCClassType(), 0, false));
2507 RD->completeDefinition(Ctx);
2508
2509 SuperCTy = Ctx.getTagDeclType(RD);
2510 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
2511
2512 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002513 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
2514
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002515 // struct _prop_t {
2516 // char *name;
2517 // char *attributes;
2518 // }
2519 PropertyTy = llvm::StructType::get(Int8PtrTy,
2520 Int8PtrTy,
2521 NULL);
2522 CGM.getModule().addTypeName("struct._prop_t",
2523 PropertyTy);
2524
2525 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002526 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002527 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002528 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002529 // }
2530 PropertyListTy = llvm::StructType::get(IntTy,
2531 IntTy,
2532 llvm::ArrayType::get(PropertyTy, 0),
2533 NULL);
2534 CGM.getModule().addTypeName("struct._prop_list_t",
2535 PropertyListTy);
2536 // struct _prop_list_t *
2537 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
2538
2539 // struct _objc_method {
2540 // SEL _cmd;
2541 // char *method_type;
2542 // char *_imp;
2543 // }
2544 MethodTy = llvm::StructType::get(SelectorPtrTy,
2545 Int8PtrTy,
2546 Int8PtrTy,
2547 NULL);
2548 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002549
2550 // struct _objc_cache *
2551 CacheTy = llvm::OpaqueType::get();
2552 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
2553 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002554
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002555 // Property manipulation functions.
2556
2557 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
2558 std::vector<const llvm::Type*> Params;
2559 Params.push_back(ObjectPtrTy);
2560 Params.push_back(SelectorPtrTy);
2561 Params.push_back(LongTy);
2562 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2563 GetPropertyFn =
2564 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2565 Params,
2566 false),
2567 "objc_getProperty");
2568
2569 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
2570 Params.clear();
2571 Params.push_back(ObjectPtrTy);
2572 Params.push_back(SelectorPtrTy);
2573 Params.push_back(LongTy);
2574 Params.push_back(ObjectPtrTy);
2575 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2576 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2577 SetPropertyFn =
2578 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2579 Params,
2580 false),
2581 "objc_setProperty");
2582 // Enumeration mutation.
2583
2584 Params.clear();
2585 Params.push_back(ObjectPtrTy);
2586 EnumerationMutationFn =
2587 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2588 Params,
2589 false),
2590 "objc_enumerationMutation");
2591
2592 // gc's API
2593 // id objc_read_weak (id *)
2594 Params.clear();
2595 Params.push_back(PtrObjectPtrTy);
2596 GcReadWeakFn =
2597 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2598 Params,
2599 false),
2600 "objc_read_weak");
2601 // id objc_assign_weak (id, id *)
2602 Params.clear();
2603 Params.push_back(ObjectPtrTy);
2604 Params.push_back(PtrObjectPtrTy);
2605 GcAssignWeakFn =
2606 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2607 Params,
2608 false),
2609 "objc_assign_weak");
2610 GcAssignGlobalFn =
2611 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2612 Params,
2613 false),
2614 "objc_assign_global");
2615 GcAssignIvarFn =
2616 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2617 Params,
2618 false),
2619 "objc_assign_ivar");
2620 GcAssignStrongCastFn =
2621 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2622 Params,
2623 false),
2624 "objc_assign_strongCast");
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002625}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002626
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002627ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
2628 : ObjCCommonTypesHelper(cgm)
2629{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002630 // struct _objc_method_description {
2631 // SEL name;
2632 // char *types;
2633 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002634 MethodDescriptionTy =
2635 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002636 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002637 NULL);
2638 CGM.getModule().addTypeName("struct._objc_method_description",
2639 MethodDescriptionTy);
2640
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002641 // struct _objc_method_description_list {
2642 // int count;
2643 // struct _objc_method_description[1];
2644 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002645 MethodDescriptionListTy =
2646 llvm::StructType::get(IntTy,
2647 llvm::ArrayType::get(MethodDescriptionTy, 0),
2648 NULL);
2649 CGM.getModule().addTypeName("struct._objc_method_description_list",
2650 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002651
2652 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002653 MethodDescriptionListPtrTy =
2654 llvm::PointerType::getUnqual(MethodDescriptionListTy);
2655
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002656 // Protocol description structures
2657
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002658 // struct _objc_protocol_extension {
2659 // uint32_t size; // sizeof(struct _objc_protocol_extension)
2660 // struct _objc_method_description_list *optional_instance_methods;
2661 // struct _objc_method_description_list *optional_class_methods;
2662 // struct _objc_property_list *instance_properties;
2663 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002664 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002665 llvm::StructType::get(IntTy,
2666 MethodDescriptionListPtrTy,
2667 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002668 PropertyListPtrTy,
2669 NULL);
2670 CGM.getModule().addTypeName("struct._objc_protocol_extension",
2671 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002672
2673 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002674 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
2675
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002676 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002677
2678 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
2679 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2680
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002681 const llvm::Type *T =
2682 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
2683 LongTy,
2684 llvm::ArrayType::get(ProtocolTyHolder, 0),
2685 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002686 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
2687
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002688 // struct _objc_protocol {
2689 // struct _objc_protocol_extension *isa;
2690 // char *protocol_name;
2691 // struct _objc_protocol **_objc_protocol_list;
2692 // struct _objc_method_description_list *instance_methods;
2693 // struct _objc_method_description_list *class_methods;
2694 // }
2695 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002696 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002697 llvm::PointerType::getUnqual(ProtocolListTyHolder),
2698 MethodDescriptionListPtrTy,
2699 MethodDescriptionListPtrTy,
2700 NULL);
2701 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
2702
2703 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
2704 CGM.getModule().addTypeName("struct._objc_protocol_list",
2705 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002706 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002707 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
2708
2709 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002710 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002711 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002712
2713 // Class description structures
2714
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002715 // struct _objc_ivar {
2716 // char *ivar_name;
2717 // char *ivar_type;
2718 // int ivar_offset;
2719 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002720 IvarTy = llvm::StructType::get(Int8PtrTy,
2721 Int8PtrTy,
2722 IntTy,
2723 NULL);
2724 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
2725
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002726 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002727 IvarListTy = llvm::OpaqueType::get();
2728 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
2729 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
2730
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002731 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002732 MethodListTy = llvm::OpaqueType::get();
2733 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
2734 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
2735
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002736 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002737 ClassExtensionTy =
2738 llvm::StructType::get(IntTy,
2739 Int8PtrTy,
2740 PropertyListPtrTy,
2741 NULL);
2742 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
2743 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
2744
2745 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
2746
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002747 // struct _objc_class {
2748 // Class isa;
2749 // Class super_class;
2750 // char *name;
2751 // long version;
2752 // long info;
2753 // long instance_size;
2754 // struct _objc_ivar_list *ivars;
2755 // struct _objc_method_list *methods;
2756 // struct _objc_cache *cache;
2757 // struct _objc_protocol_list *protocols;
2758 // char *ivar_layout;
2759 // struct _objc_class_ext *ext;
2760 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002761 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
2762 llvm::PointerType::getUnqual(ClassTyHolder),
2763 Int8PtrTy,
2764 LongTy,
2765 LongTy,
2766 LongTy,
2767 IvarListPtrTy,
2768 MethodListPtrTy,
2769 CachePtrTy,
2770 ProtocolListPtrTy,
2771 Int8PtrTy,
2772 ClassExtensionPtrTy,
2773 NULL);
2774 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
2775
2776 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
2777 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
2778 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
2779
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002780 // struct _objc_category {
2781 // char *category_name;
2782 // char *class_name;
2783 // struct _objc_method_list *instance_method;
2784 // struct _objc_method_list *class_method;
2785 // uint32_t size; // sizeof(struct _objc_category)
2786 // struct _objc_property_list *instance_properties;// category's @property
2787 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002788 CategoryTy = llvm::StructType::get(Int8PtrTy,
2789 Int8PtrTy,
2790 MethodListPtrTy,
2791 MethodListPtrTy,
2792 ProtocolListPtrTy,
2793 IntTy,
2794 PropertyListPtrTy,
2795 NULL);
2796 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
2797
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002798 // Global metadata structures
2799
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002800 // struct _objc_symtab {
2801 // long sel_ref_cnt;
2802 // SEL *refs;
2803 // short cls_def_cnt;
2804 // short cat_def_cnt;
2805 // char *defs[cls_def_cnt + cat_def_cnt];
2806 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002807 SymtabTy = llvm::StructType::get(LongTy,
2808 SelectorPtrTy,
2809 ShortTy,
2810 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002811 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002812 NULL);
2813 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
2814 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
2815
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002816 // struct _objc_module {
2817 // long version;
2818 // long size; // sizeof(struct _objc_module)
2819 // char *name;
2820 // struct _objc_symtab* symtab;
2821 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002822 ModuleTy =
2823 llvm::StructType::get(LongTy,
2824 LongTy,
2825 Int8PtrTy,
2826 SymtabPtrTy,
2827 NULL);
2828 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002829
Daniel Dunbar49f66022008-09-24 03:38:44 +00002830 // Message send functions.
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002831
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002832 // id objc_msgSend (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002833 std::vector<const llvm::Type*> Params;
2834 Params.push_back(ObjectPtrTy);
2835 Params.push_back(SelectorPtrTy);
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002836 MessageSendFn =
2837 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2838 Params,
2839 true),
2840 "objc_msgSend");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002841
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002842 // id objc_msgSend_stret (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002843 Params.clear();
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002844 Params.push_back(ObjectPtrTy);
2845 Params.push_back(SelectorPtrTy);
2846 MessageSendStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002847 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2848 Params,
2849 true),
2850 "objc_msgSend_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002851
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002852 //
Daniel Dunbar5669e572008-10-17 03:24:53 +00002853 Params.clear();
2854 Params.push_back(ObjectPtrTy);
2855 Params.push_back(SelectorPtrTy);
2856 // FIXME: This should be long double on x86_64?
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002857 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
Daniel Dunbar5669e572008-10-17 03:24:53 +00002858 MessageSendFpretFn =
2859 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
2860 Params,
2861 true),
2862 "objc_msgSend_fpret");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002863
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002864 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002865 Params.clear();
2866 Params.push_back(SuperPtrTy);
2867 Params.push_back(SelectorPtrTy);
2868 MessageSendSuperFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002869 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2870 Params,
2871 true),
2872 "objc_msgSendSuper");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002873
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002874 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
2875 // SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002876 Params.clear();
2877 Params.push_back(Int8PtrTy);
2878 Params.push_back(SuperPtrTy);
2879 Params.push_back(SelectorPtrTy);
2880 MessageSendSuperStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002881 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2882 Params,
2883 true),
2884 "objc_msgSendSuper_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002885
2886 // There is no objc_msgSendSuper_fpret? How can that work?
2887 MessageSendSuperFpretFn = MessageSendSuperFn;
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002888
Anders Carlsson124526b2008-09-09 10:10:21 +00002889 // FIXME: This is the size of the setjmp buffer and should be
2890 // target specific. 18 is what's used on 32-bit X86.
2891 uint64_t SetJmpBufferSize = 18;
2892
2893 // Exceptions
2894 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00002895 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00002896
2897 ExceptionDataTy =
2898 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
2899 SetJmpBufferSize),
2900 StackPtrTy, NULL);
2901 CGM.getModule().addTypeName("struct._objc_exception_data",
2902 ExceptionDataTy);
2903
2904 Params.clear();
2905 Params.push_back(ObjectPtrTy);
2906 ExceptionThrowFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002907 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2908 Params,
2909 false),
2910 "objc_exception_throw");
Anders Carlsson124526b2008-09-09 10:10:21 +00002911
2912 Params.clear();
2913 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
2914 ExceptionTryEnterFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002915 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2916 Params,
2917 false),
2918 "objc_exception_try_enter");
Anders Carlsson124526b2008-09-09 10:10:21 +00002919 ExceptionTryExitFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002920 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2921 Params,
2922 false),
2923 "objc_exception_try_exit");
Anders Carlsson124526b2008-09-09 10:10:21 +00002924 ExceptionExtractFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002925 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2926 Params,
2927 false),
2928 "objc_exception_extract");
Anders Carlsson124526b2008-09-09 10:10:21 +00002929
2930 Params.clear();
2931 Params.push_back(ClassPtrTy);
2932 Params.push_back(ObjectPtrTy);
2933 ExceptionMatchFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002934 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
2935 Params,
2936 false),
2937 "objc_exception_match");
Chris Lattner10cac6f2008-11-15 21:26:17 +00002938
2939 // synchronized APIs
2940 // void objc_sync_enter (id)
2941 Params.clear();
2942 Params.push_back(ObjectPtrTy);
2943 SyncEnterFn =
2944 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2945 Params,
2946 false),
2947 "objc_sync_enter");
2948 // void objc_sync_exit (id)
2949 SyncExitFn =
2950 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2951 Params,
2952 false),
2953 "objc_sync_exit");
2954
Anders Carlsson124526b2008-09-09 10:10:21 +00002955
2956 Params.clear();
2957 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
2958 SetJmpFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002959 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
2960 Params,
2961 false),
2962 "_setjmp");
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002963
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002964}
2965
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002966ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002967: ObjCCommonTypesHelper(cgm)
2968{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002969 // struct _method_list_t {
2970 // uint32_t entsize; // sizeof(struct _objc_method)
2971 // uint32_t method_count;
2972 // struct _objc_method method_list[method_count];
2973 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002974 MethodListnfABITy = llvm::StructType::get(IntTy,
2975 IntTy,
2976 llvm::ArrayType::get(MethodTy, 0),
2977 NULL);
2978 CGM.getModule().addTypeName("struct.__method_list_t",
2979 MethodListnfABITy);
2980 // struct method_list_t *
2981 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002982
2983 // struct _protocol_t {
2984 // id isa; // NULL
2985 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002986 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002987 // const struct method_list_t * const instance_methods;
2988 // const struct method_list_t * const class_methods;
2989 // const struct method_list_t *optionalInstanceMethods;
2990 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002991 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002992 // const uint32_t size; // sizeof(struct _protocol_t)
2993 // const uint32_t flags; // = 0
2994 // }
2995
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002996 // Holder for struct _protocol_list_t *
2997 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2998
2999 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
3000 Int8PtrTy,
3001 llvm::PointerType::getUnqual(
3002 ProtocolListTyHolder),
3003 MethodListnfABIPtrTy,
3004 MethodListnfABIPtrTy,
3005 MethodListnfABIPtrTy,
3006 MethodListnfABIPtrTy,
3007 PropertyListPtrTy,
3008 IntTy,
3009 IntTy,
3010 NULL);
3011 CGM.getModule().addTypeName("struct._protocol_t",
3012 ProtocolnfABITy);
3013
Fariborz Jahanianda320092009-01-29 19:24:30 +00003014 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003015 // long protocol_count; // Note, this is 32/64 bit
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003016 // struct _protocol_t[protocol_count];
3017 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003018 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3019 llvm::ArrayType::get(
3020 ProtocolnfABITy, 0),
3021 NULL);
3022 CGM.getModule().addTypeName("struct._objc_protocol_list",
3023 ProtocolListnfABITy);
3024
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003025 // struct _objc_protocol_list*
3026 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003027
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003028 // FIXME! Is this doing the right thing?
3029 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3030 ProtocolListnfABIPtrTy);
3031
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003032 // struct _ivar_t {
3033 // unsigned long int *offset; // pointer to ivar offset location
3034 // char *name;
3035 // char *type;
3036 // uint32_t alignment;
3037 // uint32_t size;
3038 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003039 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3040 Int8PtrTy,
3041 Int8PtrTy,
3042 IntTy,
3043 IntTy,
3044 NULL);
3045 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3046
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003047 // struct _ivar_list_t {
3048 // uint32 entsize; // sizeof(struct _ivar_t)
3049 // uint32 count;
3050 // struct _iver_t list[count];
3051 // }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003052 IvarListnfABITy = llvm::StructType::get(IntTy,
3053 IntTy,
3054 llvm::ArrayType::get(
3055 IvarnfABITy, 0),
3056 NULL);
3057 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3058
3059 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003060
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003061 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003062 // uint32_t const flags;
3063 // uint32_t const instanceStart;
3064 // uint32_t const instanceSize;
3065 // uint32_t const reserved; // only when building for 64bit targets
3066 // const uint8_t * const ivarLayout;
3067 // const char *const name;
3068 // const struct _method_list_t * const baseMethods;
3069 // const struct _objc_protocol_list *const baseProtocols;
3070 // const struct _ivar_list_t *const ivars;
3071 // const uint8_t * const weakIvarLayout;
3072 // const struct _prop_list_t * const properties;
3073 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003074
3075 // FIXME. Add 'reserved' field in 64bit abi mode!
3076 ClassRonfABITy = llvm::StructType::get(IntTy,
3077 IntTy,
3078 IntTy,
3079 Int8PtrTy,
3080 Int8PtrTy,
3081 MethodListnfABIPtrTy,
3082 ProtocolListnfABIPtrTy,
3083 IvarListnfABIPtrTy,
3084 Int8PtrTy,
3085 PropertyListPtrTy,
3086 NULL);
3087 CGM.getModule().addTypeName("struct._class_ro_t",
3088 ClassRonfABITy);
3089
3090 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3091 std::vector<const llvm::Type*> Params;
3092 Params.push_back(ObjectPtrTy);
3093 Params.push_back(SelectorPtrTy);
3094 ImpnfABITy = llvm::PointerType::getUnqual(
3095 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3096
3097 // struct _class_t {
3098 // struct _class_t *isa;
3099 // struct _class_t * const superclass;
3100 // void *cache;
3101 // IMP *vtable;
3102 // struct class_ro_t *ro;
3103 // }
3104
3105 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3106 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3107 llvm::PointerType::getUnqual(ClassTyHolder),
3108 CachePtrTy,
3109 llvm::PointerType::getUnqual(ImpnfABITy),
3110 llvm::PointerType::getUnqual(
3111 ClassRonfABITy),
3112 NULL);
3113 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3114
3115 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3116 ClassnfABITy);
3117
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003118 // LLVM for struct _class_t *
3119 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3120
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003121 // struct _category_t {
3122 // const char * const name;
3123 // struct _class_t *const cls;
3124 // const struct _method_list_t * const instance_methods;
3125 // const struct _method_list_t * const class_methods;
3126 // const struct _protocol_list_t * const protocols;
3127 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003128 // }
3129 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003130 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003131 MethodListnfABIPtrTy,
3132 MethodListnfABIPtrTy,
3133 ProtocolListnfABIPtrTy,
3134 PropertyListPtrTy,
3135 NULL);
3136 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
3137
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003138}
3139
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003140llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
3141 FinishNonFragileABIModule();
3142
3143 return NULL;
3144}
3145
3146void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
3147 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003148
3149 // Build list of all implemented classe addresses in array
3150 // L_OBJC_LABEL_CLASS_$.
3151 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$
3152 // list of 'nonlazy' implementations (defined as those with a +load{}
3153 // method!!).
3154 unsigned NumClasses = DefinedClasses.size();
3155 if (NumClasses) {
3156 std::vector<llvm::Constant*> Symbols(NumClasses);
3157 for (unsigned i=0; i<NumClasses; i++)
3158 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
3159 ObjCTypes.Int8PtrTy);
3160 llvm::Constant* Init =
3161 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3162 NumClasses),
3163 Symbols);
3164
3165 llvm::GlobalVariable *GV =
3166 new llvm::GlobalVariable(Init->getType(), false,
3167 llvm::GlobalValue::InternalLinkage,
3168 Init,
3169 "\01L_OBJC_LABEL_CLASS_$",
3170 &CGM.getModule());
3171 GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip");
3172 UsedGlobals.push_back(GV);
3173 }
3174
3175 // Build list of all implemented category addresses in array
3176 // L_OBJC_LABEL_CATEGORY_$.
3177 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$
3178 // list of 'nonlazy' category implementations (defined as those with a +load{}
3179 // method!!).
3180 unsigned NumCategory = DefinedCategories.size();
3181 if (NumCategory) {
3182 std::vector<llvm::Constant*> Symbols(NumCategory);
3183 for (unsigned i=0; i<NumCategory; i++)
3184 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i],
3185 ObjCTypes.Int8PtrTy);
3186 llvm::Constant* Init =
3187 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3188 NumCategory),
3189 Symbols);
3190
3191 llvm::GlobalVariable *GV =
3192 new llvm::GlobalVariable(Init->getType(), false,
3193 llvm::GlobalValue::InternalLinkage,
3194 Init,
3195 "\01L_OBJC_LABEL_CATEGORY_$",
3196 &CGM.getModule());
3197 GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip");
3198 UsedGlobals.push_back(GV);
3199 }
3200
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00003201 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
3202 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
3203 std::vector<llvm::Constant*> Values(2);
3204 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3205 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3206 llvm::Constant* Init = llvm::ConstantArray::get(
3207 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
3208 Values);
3209 llvm::GlobalVariable *IMGV =
3210 new llvm::GlobalVariable(Init->getType(), false,
3211 llvm::GlobalValue::InternalLinkage,
3212 Init,
3213 "\01L_OBJC_IMAGE_INFO",
3214 &CGM.getModule());
3215 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
3216 UsedGlobals.push_back(IMGV);
3217
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003218 std::vector<llvm::Constant*> Used;
3219 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
3220 e = UsedGlobals.end(); i != e; ++i) {
3221 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
3222 }
3223
3224 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
3225 llvm::GlobalValue *GV =
3226 new llvm::GlobalVariable(AT, false,
3227 llvm::GlobalValue::AppendingLinkage,
3228 llvm::ConstantArray::get(AT, Used),
3229 "llvm.used",
3230 &CGM.getModule());
3231
3232 GV->setSection("llvm.metadata");
3233
3234}
3235
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003236// Metadata flags
3237enum MetaDataDlags {
3238 CLS = 0x0,
3239 CLS_META = 0x1,
3240 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003241 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003242 CLS_EXCEPTION = 0x20
3243};
3244/// BuildClassRoTInitializer - generate meta-data for:
3245/// struct _class_ro_t {
3246/// uint32_t const flags;
3247/// uint32_t const instanceStart;
3248/// uint32_t const instanceSize;
3249/// uint32_t const reserved; // only when building for 64bit targets
3250/// const uint8_t * const ivarLayout;
3251/// const char *const name;
3252/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00003253/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003254/// const struct _ivar_list_t *const ivars;
3255/// const uint8_t * const weakIvarLayout;
3256/// const struct _prop_list_t * const properties;
3257/// }
3258///
3259llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
3260 unsigned flags,
3261 unsigned InstanceStart,
3262 unsigned InstanceSize,
3263 const ObjCImplementationDecl *ID) {
3264 std::string ClassName = ID->getNameAsString();
3265 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
3266 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
3267 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
3268 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
3269 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianda320092009-01-29 19:24:30 +00003270 // FIXME. ivarLayout is currently null!
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003271 Values[ 3] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3272 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003273 // const struct _method_list_t * const baseMethods;
3274 std::vector<llvm::Constant*> Methods;
3275 std::string MethodListName("\01l_OBJC_$_");
3276 if (flags & CLS_META) {
3277 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
3278 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
3279 e = ID->classmeth_end(); i != e; ++i) {
3280 // Class methods should always be defined.
3281 Methods.push_back(GetMethodConstant(*i));
3282 }
3283 } else {
3284 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
3285 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
3286 e = ID->instmeth_end(); i != e; ++i) {
3287 // Instance methods should always be defined.
3288 Methods.push_back(GetMethodConstant(*i));
3289 }
Fariborz Jahanian939abce2009-01-28 22:46:49 +00003290 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
3291 e = ID->propimpl_end(); i != e; ++i) {
3292 ObjCPropertyImplDecl *PID = *i;
3293
3294 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
3295 ObjCPropertyDecl *PD = PID->getPropertyDecl();
3296
3297 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
3298 if (llvm::Constant *C = GetMethodConstant(MD))
3299 Methods.push_back(C);
3300 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
3301 if (llvm::Constant *C = GetMethodConstant(MD))
3302 Methods.push_back(C);
3303 }
3304 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003305 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003306 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003307 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003308
3309 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3310 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
3311 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
3312 + OID->getNameAsString(),
3313 OID->protocol_begin(),
3314 OID->protocol_end());
3315
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003316 if (flags & CLS_META)
3317 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3318 else
3319 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003320 // FIXME. weakIvarLayout is currently null.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003321 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003322 if (flags & CLS_META)
3323 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3324 else
3325 Values[ 9] =
3326 EmitPropertyList(
3327 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
3328 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003329 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
3330 Values);
3331 llvm::GlobalVariable *CLASS_RO_GV =
3332 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
3333 llvm::GlobalValue::InternalLinkage,
3334 Init,
3335 (flags & CLS_META) ?
3336 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
3337 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
3338 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003339 CLASS_RO_GV->setAlignment(
3340 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003341 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003342 UsedGlobals.push_back(CLASS_RO_GV);
3343 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003344
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003345}
3346
3347/// BuildClassMetaData - This routine defines that to-level meta-data
3348/// for the given ClassName for:
3349/// struct _class_t {
3350/// struct _class_t *isa;
3351/// struct _class_t * const superclass;
3352/// void *cache;
3353/// IMP *vtable;
3354/// struct class_ro_t *ro;
3355/// }
3356///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003357llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
3358 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003359 llvm::Constant *IsAGV,
3360 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003361 llvm::Constant *ClassRoGV,
3362 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003363 std::vector<llvm::Constant*> Values(5);
3364 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003365 Values[1] = SuperClassGV
3366 ? SuperClassGV
3367 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003368 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
3369 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
3370 Values[4] = ClassRoGV; // &CLASS_RO_GV
3371 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
3372 Values);
3373 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(ClassName);
3374 if (GV)
3375 GV->setInitializer(Init);
3376 else
3377 GV =
3378 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3379 llvm::GlobalValue::ExternalLinkage,
3380 Init,
3381 ClassName,
3382 &CGM.getModule());
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00003383 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003384 GV->setAlignment(
3385 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003386 if (HiddenVisibility)
3387 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003388 UsedGlobals.push_back(GV);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003389 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003390}
3391
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003392void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
3393 std::string ClassName = ID->getNameAsString();
3394 if (!ObjCEmptyCacheVar) {
3395 ObjCEmptyCacheVar = new llvm::GlobalVariable(
3396 ObjCTypes.CachePtrTy,
3397 false,
3398 llvm::GlobalValue::ExternalLinkage,
3399 0,
3400 "\01_objc_empty_cache",
3401 &CGM.getModule());
3402 UsedGlobals.push_back(ObjCEmptyCacheVar);
3403
3404 ObjCEmptyVtableVar = new llvm::GlobalVariable(
3405 llvm::PointerType::getUnqual(
3406 ObjCTypes.ImpnfABITy),
3407 false,
3408 llvm::GlobalValue::ExternalLinkage,
3409 0,
3410 "\01_objc_empty_vtable",
3411 &CGM.getModule());
3412 UsedGlobals.push_back(ObjCEmptyVtableVar);
3413 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003414 assert(ID->getClassInterface() &&
3415 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003416 uint32_t InstanceStart =
3417 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
3418 uint32_t InstanceSize = InstanceStart;
3419 uint32_t flags = CLS_META;
3420 std::string ObjCMetaClassName("\01_OBJC_METACLASS_$_");
3421 std::string ObjCClassName("\01_OBJC_CLASS_$_");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003422
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003423 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003424
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003425 bool classIsHidden = IsClassHidden(ID->getClassInterface());
3426 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003427 flags |= OBJC2_CLS_HIDDEN;
3428 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003429 // class is root
3430 flags |= CLS_ROOT;
3431 std::string SuperClassName = ObjCClassName + ClassName;
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 std::string IsAClassName = ObjCMetaClassName + ClassName;
3442 IsAGV = CGM.getModule().getGlobalVariable(IsAClassName);
3443 if (!IsAGV)
3444 IsAGV =
3445 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3446 llvm::GlobalValue::ExternalLinkage,
3447 0,
3448 IsAClassName,
3449 &CGM.getModule());
3450 UsedGlobals.push_back(IsAGV);
3451 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003452 // Has a root. Current class is not a root.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003453 std::string RootClassName =
3454 ID->getClassInterface()->getSuperClass()->getNameAsString();
3455 std::string SuperClassName = ObjCMetaClassName + RootClassName;
3456 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3457 if (!SuperClassGV)
3458 SuperClassGV =
3459 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3460 llvm::GlobalValue::ExternalLinkage,
3461 0,
3462 SuperClassName,
3463 &CGM.getModule());
3464 UsedGlobals.push_back(SuperClassGV);
3465 IsAGV = SuperClassGV;
3466 }
3467 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
3468 InstanceStart,
3469 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003470 std::string TClassName = ObjCMetaClassName + ClassName;
3471 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003472 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
3473 classIsHidden);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003474
3475 // Metadata for the class
3476 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003477 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003478 flags |= OBJC2_CLS_HIDDEN;
3479 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003480 flags |= CLS_ROOT;
3481 SuperClassGV = 0;
3482 }
3483 else {
3484 // Has a root. Current class is not a root.
3485 std::string RootClassName =
3486 ID->getClassInterface()->getSuperClass()->getNameAsString();
3487 std::string SuperClassName = ObjCClassName + RootClassName;
3488 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3489 if (!SuperClassGV)
3490 SuperClassGV =
3491 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3492 llvm::GlobalValue::ExternalLinkage,
3493 0,
3494 SuperClassName,
3495 &CGM.getModule());
3496 UsedGlobals.push_back(SuperClassGV);
3497
3498 }
3499
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003500 InstanceStart = InstanceSize = 0;
3501 if (ObjCInterfaceDecl *OID =
3502 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface())) {
3503 // FIXME. Share this with the one in EmitIvarList.
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003504 const llvm::Type *InterfaceTy =
3505 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
3506 const llvm::StructLayout *Layout =
3507 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003508
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003509 RecordDecl::field_iterator firstField, lastField;
3510 const RecordDecl *RD = GetFirstIvarInRecord(OID, firstField, lastField);
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003511
3512 for (RecordDecl::field_iterator e = RD->field_end(),
3513 ifield = firstField; ifield != e; ++ifield)
3514 lastField = ifield;
3515
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003516 if (lastField != RD->field_end()) {
3517 FieldDecl *Field = *lastField;
3518 const llvm::Type *FieldTy =
3519 CGM.getTypes().ConvertTypeForMem(Field->getType());
3520 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3521 InstanceSize = Layout->getElementOffset(
3522 CGM.getTypes().getLLVMFieldNo(Field)) +
3523 Size;
3524 if (firstField == RD->field_end())
3525 InstanceStart = InstanceSize;
3526 else
3527 InstanceStart = Layout->getElementOffset(CGM.getTypes().
3528 getLLVMFieldNo(*firstField));
3529 }
3530 }
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003531 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003532 InstanceStart,
3533 InstanceSize,
3534 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003535
3536 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003537 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003538 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
3539 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003540 DefinedClasses.push_back(ClassMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003541}
3542
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00003543/// GenerateProtocolRef - This routine is called to generate code for
3544/// a protocol reference expression; as in:
3545/// @code
3546/// @protocol(Proto1);
3547/// @endcode
3548/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
3549/// which will hold address of the protocol meta-data.
3550///
3551llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
3552 const ObjCProtocolDecl *PD) {
3553
3554 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
3555 ObjCTypes.ExternalProtocolPtrTy);
3556
3557 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
3558 ProtocolName += PD->getNameAsCString();
3559
3560 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
3561 if (PTGV)
3562 return Builder.CreateLoad(PTGV, false, "tmp");
3563 PTGV = new llvm::GlobalVariable(
3564 Init->getType(), false,
3565 llvm::GlobalValue::WeakLinkage,
3566 Init,
3567 ProtocolName,
3568 &CGM.getModule());
3569 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
3570 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
3571 UsedGlobals.push_back(PTGV);
3572 return Builder.CreateLoad(PTGV, false, "tmp");
3573}
3574
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003575/// GenerateCategory - Build metadata for a category implementation.
3576/// struct _category_t {
3577/// const char * const name;
3578/// struct _class_t *const cls;
3579/// const struct _method_list_t * const instance_methods;
3580/// const struct _method_list_t * const class_methods;
3581/// const struct _protocol_list_t * const protocols;
3582/// const struct _prop_list_t * const properties;
3583/// }
3584///
3585void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD)
3586{
3587 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003588 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
3589 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003590 "_$_" + OCD->getNameAsString());
3591 std::string ExtClassName("\01_OBJC_CLASS_$_" + Interface->getNameAsString());
3592
3593 std::vector<llvm::Constant*> Values(6);
3594 Values[0] = GetClassName(OCD->getIdentifier());
3595 // meta-class entry symbol
3596 llvm::GlobalVariable *ClassGV =
3597 CGM.getModule().getGlobalVariable(ExtClassName);
3598 if (!ClassGV)
3599 ClassGV =
3600 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3601 llvm::GlobalValue::ExternalLinkage,
3602 0,
3603 ExtClassName,
3604 &CGM.getModule());
3605 UsedGlobals.push_back(ClassGV);
3606 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003607 std::vector<llvm::Constant*> Methods;
3608 std::string MethodListName(Prefix);
3609 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
3610 "_$_" + OCD->getNameAsString();
3611
3612 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
3613 e = OCD->instmeth_end(); i != e; ++i) {
3614 // Instance methods should always be defined.
3615 Methods.push_back(GetMethodConstant(*i));
3616 }
3617
3618 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003619 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003620 Methods);
3621
3622 MethodListName = Prefix;
3623 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
3624 OCD->getNameAsString();
3625 Methods.clear();
3626 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
3627 e = OCD->classmeth_end(); i != e; ++i) {
3628 // Class methods should always be defined.
3629 Methods.push_back(GetMethodConstant(*i));
3630 }
3631
3632 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003633 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003634 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003635 const ObjCCategoryDecl *Category =
3636 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanianeb732132009-01-29 23:23:06 +00003637 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
3638 + Interface->getNameAsString() + "_$_"
3639 + Category->getNameAsString(),
3640 Category->protocol_begin(),
3641 Category->protocol_end());
3642
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003643 std::string ExtName(Interface->getNameAsString() + "_$_" +
3644 OCD->getNameAsString());
3645 Values[5] =
3646 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
3647 OCD, Category, ObjCTypes);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003648 llvm::Constant *Init =
3649 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
3650 Values);
3651 llvm::GlobalVariable *GCATV
3652 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
3653 false,
3654 llvm::GlobalValue::InternalLinkage,
3655 Init,
3656 ExtCatName,
3657 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003658 GCATV->setAlignment(
3659 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003660 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003661 UsedGlobals.push_back(GCATV);
3662 DefinedCategories.push_back(GCATV);
3663}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003664
3665/// GetMethodConstant - Return a struct objc_method constant for the
3666/// given method if it has been defined. The result is null if the
3667/// method has not been defined. The return value has type MethodPtrTy.
3668llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
3669 const ObjCMethodDecl *MD) {
3670 // FIXME: Use DenseMap::lookup
3671 llvm::Function *Fn = MethodDefinitions[MD];
3672 if (!Fn)
3673 return 0;
3674
3675 std::vector<llvm::Constant*> Method(3);
3676 Method[0] =
3677 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
3678 ObjCTypes.SelectorPtrTy);
3679 Method[1] = GetMethodVarType(MD);
3680 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
3681 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
3682}
3683
3684/// EmitMethodList - Build meta-data for method declarations
3685/// struct _method_list_t {
3686/// uint32_t entsize; // sizeof(struct _objc_method)
3687/// uint32_t method_count;
3688/// struct _objc_method method_list[method_count];
3689/// }
3690///
3691llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
3692 const std::string &Name,
3693 const char *Section,
3694 const ConstantVector &Methods) {
3695 // Return null for empty list.
3696 if (Methods.empty())
3697 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
3698
3699 std::vector<llvm::Constant*> Values(3);
3700 // sizeof(struct _objc_method)
3701 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy);
3702 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3703 // method_count
3704 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
3705 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
3706 Methods.size());
3707 Values[2] = llvm::ConstantArray::get(AT, Methods);
3708 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
3709
3710 llvm::GlobalVariable *GV =
3711 new llvm::GlobalVariable(Init->getType(), false,
3712 llvm::GlobalValue::InternalLinkage,
3713 Init,
3714 Name,
3715 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003716 GV->setAlignment(
3717 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003718 GV->setSection(Section);
3719 UsedGlobals.push_back(GV);
3720 return llvm::ConstantExpr::getBitCast(GV,
3721 ObjCTypes.MethodListnfABIPtrTy);
3722}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003723
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003724llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
3725 const ObjCImplementationDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003726 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003727 unsigned long int Offset) {
3728
3729 std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.'
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003730 + Ivar->getNameAsString());
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003731 llvm::Constant *Init = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
3732
3733 llvm::GlobalVariable *IvarOffsetGV =
3734 CGM.getModule().getGlobalVariable(ExternalName);
3735 if (IvarOffsetGV) {
3736 // ivar offset symbol already built due to user code referencing it.
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003737 IvarOffsetGV->setAlignment(
3738 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.Int8PtrTy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003739 IvarOffsetGV->setInitializer(Init);
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003740 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003741 return IvarOffsetGV;
3742 }
3743
3744 IvarOffsetGV =
3745 new llvm::GlobalVariable(Init->getType(),
3746 false,
3747 llvm::GlobalValue::ExternalLinkage,
3748 Init,
3749 ExternalName,
3750 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003751 IvarOffsetGV->setAlignment(
3752 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.Int8PtrTy));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003753 // @private and @package have hidden visibility.
3754 bool globalVisibility = (Ivar->getAccessControl() == ObjCIvarDecl::Public ||
3755 Ivar->getAccessControl() == ObjCIvarDecl::Protected);
3756 if (!globalVisibility)
3757 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003758 else
3759 if (const ObjCInterfaceDecl *OID = ID->getClassInterface())
3760 if (IsClassHidden(OID))
3761 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
3762
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003763 IvarOffsetGV->setSection("__DATA, __objc_const");
3764 UsedGlobals.push_back(IvarOffsetGV);
3765
3766 return llvm::ConstantExpr::getBitCast(
3767 IvarOffsetGV,
3768 llvm::PointerType::getUnqual(ObjCTypes.LongTy));
3769}
3770
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003771/// EmitIvarList - Emit the ivar list for the given
3772/// implementation. If ForClass is true the list of class ivars
3773/// (i.e. metaclass ivars) is emitted, otherwise the list of
3774/// interface ivars will be emitted. The return value has type
3775/// IvarListnfABIPtrTy.
3776/// struct _ivar_t {
3777/// unsigned long int *offset; // pointer to ivar offset location
3778/// char *name;
3779/// char *type;
3780/// uint32_t alignment;
3781/// uint32_t size;
3782/// }
3783/// struct _ivar_list_t {
3784/// uint32 entsize; // sizeof(struct _ivar_t)
3785/// uint32 count;
3786/// struct _iver_t list[count];
3787/// }
3788///
3789llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
3790 const ObjCImplementationDecl *ID) {
3791
3792 std::vector<llvm::Constant*> Ivars, Ivar(5);
3793
3794 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3795 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
3796
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003797 // FIXME. Consolidate this with similar code in GenerateClass.
3798 const llvm::Type *InterfaceTy =
3799 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(
3800 const_cast<ObjCInterfaceDecl*>(OID)));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003801 const llvm::StructLayout *Layout =
3802 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003803
3804 RecordDecl::field_iterator i,p;
3805 const RecordDecl *RD = GetFirstIvarInRecord(OID, i,p);
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003806 ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin();
3807
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003808 for (RecordDecl::field_iterator e = RD->field_end(); i != e; ++i) {
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003809 FieldDecl *Field = *i;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003810 unsigned long offset = Layout->getElementOffset(CGM.getTypes().
3811 getLLVMFieldNo(Field));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003812 const ObjCIvarDecl *ivarDecl = *I++;
3813 Ivar[0] = EmitIvarOffsetVar(ID, ivarDecl, offset);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003814 if (Field->getIdentifier())
3815 Ivar[1] = GetMethodVarName(Field->getIdentifier());
3816 else
3817 Ivar[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3818 std::string TypeStr;
3819 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3820 Ivar[2] = GetMethodVarType(TypeStr);
3821 const llvm::Type *FieldTy =
3822 CGM.getTypes().ConvertTypeForMem(Field->getType());
3823 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3824 unsigned Align = CGM.getContext().getPreferredTypeAlign(
3825 Field->getType().getTypePtr()) >> 3;
3826 Align = llvm::Log2_32(Align);
3827 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Fariborz Jahanian07236ba2009-01-27 22:27:56 +00003828 // NOTE. Size of a bitfield does not match gcc's, because of the way
3829 // bitfields are treated special in each. But I am told that 'size'
3830 // for bitfield ivars is ignored by the runtime so it does not matter.
3831 // (even if it matters, some day, there is enough info. to get the bitfield
3832 // right!
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003833 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3834 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
3835 }
3836 // Return null for empty list.
3837 if (Ivars.empty())
3838 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3839 std::vector<llvm::Constant*> Values(3);
3840 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy);
3841 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3842 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
3843 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
3844 Ivars.size());
3845 Values[2] = llvm::ConstantArray::get(AT, Ivars);
3846 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
3847 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
3848 llvm::GlobalVariable *GV =
3849 new llvm::GlobalVariable(Init->getType(), false,
3850 llvm::GlobalValue::InternalLinkage,
3851 Init,
3852 Prefix + OID->getNameAsString(),
3853 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003854 GV->setAlignment(
3855 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003856 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003857
3858 UsedGlobals.push_back(GV);
3859 return llvm::ConstantExpr::getBitCast(GV,
3860 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003861}
3862
3863llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
3864 const ObjCProtocolDecl *PD) {
3865 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
3866
3867 if (!Entry) {
3868 // We use the initializer as a marker of whether this is a forward
3869 // reference or not. At module finalization we add the empty
3870 // contents for protocols which were referenced but never defined.
3871 Entry =
3872 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
3873 llvm::GlobalValue::ExternalLinkage,
3874 0,
3875 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(),
3876 &CGM.getModule());
3877 Entry->setSection("__DATA,__datacoal_nt,coalesced");
3878 UsedGlobals.push_back(Entry);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003879 }
3880
3881 return Entry;
3882}
3883
3884/// GetOrEmitProtocol - Generate the protocol meta-data:
3885/// @code
3886/// struct _protocol_t {
3887/// id isa; // NULL
3888/// const char * const protocol_name;
3889/// const struct _protocol_list_t * protocol_list; // super protocols
3890/// const struct method_list_t * const instance_methods;
3891/// const struct method_list_t * const class_methods;
3892/// const struct method_list_t *optionalInstanceMethods;
3893/// const struct method_list_t *optionalClassMethods;
3894/// const struct _prop_list_t * properties;
3895/// const uint32_t size; // sizeof(struct _protocol_t)
3896/// const uint32_t flags; // = 0
3897/// }
3898/// @endcode
3899///
3900
3901llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
3902 const ObjCProtocolDecl *PD) {
3903 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
3904
3905 // Early exit if a defining object has already been generated.
3906 if (Entry && Entry->hasInitializer())
3907 return Entry;
3908
3909 const char *ProtocolName = PD->getNameAsCString();
3910
3911 // Construct method lists.
3912 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
3913 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
3914 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
3915 e = PD->instmeth_end(); i != e; ++i) {
3916 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003917 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003918 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
3919 OptInstanceMethods.push_back(C);
3920 } else {
3921 InstanceMethods.push_back(C);
3922 }
3923 }
3924
3925 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
3926 e = PD->classmeth_end(); i != e; ++i) {
3927 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003928 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003929 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
3930 OptClassMethods.push_back(C);
3931 } else {
3932 ClassMethods.push_back(C);
3933 }
3934 }
3935
3936 std::vector<llvm::Constant*> Values(10);
3937 // isa is NULL
3938 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
3939 Values[1] = GetClassName(PD->getIdentifier());
3940 Values[2] = EmitProtocolList(
3941 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
3942 PD->protocol_begin(),
3943 PD->protocol_end());
3944
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003945 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00003946 + PD->getNameAsString(),
3947 "__DATA, __objc_const",
3948 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003949 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00003950 + PD->getNameAsString(),
3951 "__DATA, __objc_const",
3952 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003953 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00003954 + PD->getNameAsString(),
3955 "__DATA, __objc_const",
3956 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003957 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00003958 + PD->getNameAsString(),
3959 "__DATA, __objc_const",
3960 OptClassMethods);
3961 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
3962 0, PD, ObjCTypes);
3963 uint32_t Size =
3964 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy);
3965 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3966 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
3967 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
3968 Values);
3969
3970 if (Entry) {
3971 // Already created, fix the linkage and update the initializer.
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00003972 Entry->setLinkage(llvm::GlobalValue::WeakLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003973 Entry->setInitializer(Init);
3974 } else {
3975 Entry =
3976 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00003977 llvm::GlobalValue::WeakLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00003978 Init,
3979 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName,
3980 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003981 Entry->setAlignment(
3982 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00003983 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00003984 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00003985 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
3986
3987 // Use this protocol meta-data to build protocol list table in section
3988 // __DATA, __objc_protolist
3989 llvm::Type *ptype = llvm::PointerType::getUnqual(ObjCTypes.ProtocolnfABITy);
3990 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
3991 ptype, false,
3992 llvm::GlobalValue::WeakLinkage,
3993 Entry,
3994 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
3995 +ProtocolName,
3996 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003997 PTGV->setAlignment(
3998 CGM.getTargetData().getPrefTypeAlignment(ptype));
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00003999 PTGV->setSection("__DATA, __objc_protolist");
4000 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4001 UsedGlobals.push_back(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004002 return Entry;
4003}
4004
4005/// EmitProtocolList - Generate protocol list meta-data:
4006/// @code
4007/// struct _protocol_list_t {
4008/// long protocol_count; // Note, this is 32/64 bit
4009/// struct _protocol_t[protocol_count];
4010/// }
4011/// @endcode
4012///
4013llvm::Constant *
4014CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4015 ObjCProtocolDecl::protocol_iterator begin,
4016 ObjCProtocolDecl::protocol_iterator end) {
4017 std::vector<llvm::Constant*> ProtocolRefs;
4018
4019 for (; begin != end; ++begin)
4020 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4021
4022 // Just return null for empty protocol lists
4023 if (ProtocolRefs.empty())
4024 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4025
4026 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4027 if (GV)
4028 return GV;
4029 // This list is null terminated.
4030 ProtocolRefs.push_back(llvm::Constant::getNullValue(
4031 ObjCTypes.ProtocolListnfABIPtrTy));
4032
4033 std::vector<llvm::Constant*> Values(2);
4034 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
4035 Values[1] =
4036 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolListnfABIPtrTy,
4037 ProtocolRefs.size()),
4038 ProtocolRefs);
4039
4040 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4041 GV = new llvm::GlobalVariable(Init->getType(), false,
4042 llvm::GlobalValue::InternalLinkage,
4043 Init,
4044 Name,
4045 &CGM.getModule());
4046 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004047 GV->setAlignment(
4048 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004049 UsedGlobals.push_back(GV);
4050 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
4051}
4052
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004053/// GetMethodDescriptionConstant - This routine build following meta-data:
4054/// struct _objc_method {
4055/// SEL _cmd;
4056/// char *method_type;
4057/// char *_imp;
4058/// }
4059
4060llvm::Constant *
4061CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4062 std::vector<llvm::Constant*> Desc(3);
4063 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4064 ObjCTypes.SelectorPtrTy);
4065 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004066 // Protocol methods have no implementation. So, this entry is always NULL.
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004067 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4068 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
4069}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004070/* *** */
4071
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004072CodeGen::CGObjCRuntime *
4073CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00004074 return new CGObjCMac(CGM);
4075}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004076
4077CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004078CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004079 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004080}