blob: 526a95a96366649ee77a65ec4bda2a639c84a042 [file] [log] [blame]
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001//===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides Objective-C code generation targetting the Apple runtime.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGObjCRuntime.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000015
16#include "CodeGenModule.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000017#include "CodeGenFunction.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000019#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000020#include "clang/AST/DeclObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000021#include "clang/Basic/LangOptions.h"
22
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000023#include "llvm/Module.h"
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +000024#include "llvm/ADT/DenseSet.h"
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000025#include "llvm/Target/TargetData.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000026#include <sstream>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000027
28using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000029using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000030
31namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000032
Daniel Dunbarae226fa2008-08-27 02:31:56 +000033 typedef std::vector<llvm::Constant*> ConstantVector;
34
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000035 // FIXME: We should find a nicer way to make the labels for
36 // metadata, string concatenation is lame.
37
Fariborz Jahanianee0af742009-01-21 22:04:16 +000038class ObjCCommonTypesHelper {
39protected:
40 CodeGen::CodeGenModule &CGM;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000041
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000042public:
Daniel Dunbar27f9d772008-08-21 04:36:09 +000043 const llvm::Type *ShortTy, *IntTy, *LongTy;
44 const llvm::Type *Int8PtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +000045
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000046 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
47 const llvm::Type *ObjectPtrTy;
Fariborz Jahanian6d657c42008-11-18 20:18:11 +000048
49 /// PtrObjectPtrTy - LLVM type for id *
50 const llvm::Type *PtrObjectPtrTy;
51
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000052 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000053 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000054 /// ProtocolPtrTy - LLVM type for external protocol handles
55 /// (typeof(Protocol))
56 const llvm::Type *ExternalProtocolPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +000057
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000058 // SuperCTy - clang type for struct objc_super.
59 QualType SuperCTy;
60 // SuperPtrCTy - clang type for struct objc_super *.
61 QualType SuperPtrCTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +000062
Daniel Dunbare8b470d2008-08-23 04:28:29 +000063 /// SuperTy - LLVM type for struct objc_super.
64 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +000065 /// SuperPtrTy - LLVM type for struct objc_super *.
66 const llvm::Type *SuperPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +000067
Fariborz Jahanian30bc5712009-01-22 23:02:58 +000068 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
69 /// in GCC parlance).
70 const llvm::StructType *PropertyTy;
71
72 /// PropertyListTy - LLVM type for struct objc_property_list
73 /// (_prop_list_t in GCC parlance).
74 const llvm::StructType *PropertyListTy;
75 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
76 const llvm::Type *PropertyListPtrTy;
77
78 // MethodTy - LLVM type for struct objc_method.
79 const llvm::StructType *MethodTy;
80
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +000081 /// CacheTy - LLVM type for struct objc_cache.
82 const llvm::Type *CacheTy;
83 /// CachePtrTy - LLVM type for struct objc_cache *.
84 const llvm::Type *CachePtrTy;
85
Fariborz Jahaniandb286862009-01-22 00:37:21 +000086 llvm::Function *GetPropertyFn, *SetPropertyFn;
87
88 llvm::Function *EnumerationMutationFn;
89
90 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
91 llvm::Function *GcReadWeakFn;
92
93 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
94 llvm::Function *GcAssignWeakFn;
95
96 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
97 llvm::Function *GcAssignGlobalFn;
98
99 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
100 llvm::Function *GcAssignIvarFn;
101
102 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
103 llvm::Function *GcAssignStrongCastFn;
104
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000105 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
106 ~ObjCCommonTypesHelper(){}
107};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000108
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000109/// ObjCTypesHelper - Helper class that encapsulates lazy
110/// construction of varies types used during ObjC generation.
111class ObjCTypesHelper : public ObjCCommonTypesHelper {
112private:
113
114 llvm::Function *MessageSendFn, *MessageSendStretFn, *MessageSendFpretFn;
115 llvm::Function *MessageSendSuperFn, *MessageSendSuperStretFn,
116 *MessageSendSuperFpretFn;
117
118public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000119 /// SymtabTy - LLVM type for struct objc_symtab.
120 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000121 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
122 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000123 /// ModuleTy - LLVM type for struct objc_module.
124 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000125
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000126 /// ProtocolTy - LLVM type for struct objc_protocol.
127 const llvm::StructType *ProtocolTy;
128 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
129 const llvm::Type *ProtocolPtrTy;
130 /// ProtocolExtensionTy - LLVM type for struct
131 /// objc_protocol_extension.
132 const llvm::StructType *ProtocolExtensionTy;
133 /// ProtocolExtensionTy - LLVM type for struct
134 /// objc_protocol_extension *.
135 const llvm::Type *ProtocolExtensionPtrTy;
136 /// MethodDescriptionTy - LLVM type for struct
137 /// objc_method_description.
138 const llvm::StructType *MethodDescriptionTy;
139 /// MethodDescriptionListTy - LLVM type for struct
140 /// objc_method_description_list.
141 const llvm::StructType *MethodDescriptionListTy;
142 /// MethodDescriptionListPtrTy - LLVM type for struct
143 /// objc_method_description_list *.
144 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000145 /// ProtocolListTy - LLVM type for struct objc_property_list.
146 const llvm::Type *ProtocolListTy;
147 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
148 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000149 /// CategoryTy - LLVM type for struct objc_category.
150 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000151 /// ClassTy - LLVM type for struct objc_class.
152 const llvm::StructType *ClassTy;
153 /// ClassPtrTy - LLVM type for struct objc_class *.
154 const llvm::Type *ClassPtrTy;
155 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
156 const llvm::StructType *ClassExtensionTy;
157 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
158 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000159 // IvarTy - LLVM type for struct objc_ivar.
160 const llvm::StructType *IvarTy;
161 /// IvarListTy - LLVM type for struct objc_ivar_list.
162 const llvm::Type *IvarListTy;
163 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
164 const llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000165 /// MethodListTy - LLVM type for struct objc_method_list.
166 const llvm::Type *MethodListTy;
167 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
168 const llvm::Type *MethodListPtrTy;
Anders Carlsson124526b2008-09-09 10:10:21 +0000169
170 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
171 const llvm::Type *ExceptionDataTy;
172
173 /// ExceptionThrowFn - LLVM objc_exception_throw function.
174 llvm::Function *ExceptionThrowFn;
175
176 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
177 llvm::Function *ExceptionTryEnterFn;
178
179 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
180 llvm::Function *ExceptionTryExitFn;
181
182 /// ExceptionExtractFn - LLVM objc_exception_extract function.
183 llvm::Function *ExceptionExtractFn;
184
185 /// ExceptionMatchFn - LLVM objc_exception_match function.
186 llvm::Function *ExceptionMatchFn;
187
188 /// SetJmpFn - LLVM _setjmp function.
189 llvm::Function *SetJmpFn;
Chris Lattner10cac6f2008-11-15 21:26:17 +0000190
191 /// SyncEnterFn - LLVM object_sync_enter function.
192 llvm::Function *SyncEnterFn;
193
194 /// SyncExitFn - LLVM object_sync_exit function.
195 llvm::Function *SyncExitFn;
196
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000197public:
198 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000199 ~ObjCTypesHelper() {}
Daniel Dunbar5669e572008-10-17 03:24:53 +0000200
201
202 llvm::Function *getSendFn(bool IsSuper) {
203 return IsSuper ? MessageSendSuperFn : MessageSendFn;
204 }
205
206 llvm::Function *getSendStretFn(bool IsSuper) {
207 return IsSuper ? MessageSendSuperStretFn : MessageSendStretFn;
208 }
209
210 llvm::Function *getSendFpretFn(bool IsSuper) {
211 return IsSuper ? MessageSendSuperFpretFn : MessageSendFpretFn;
212 }
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000213};
214
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000215/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000216/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000217class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000218public:
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000219 // MethodListnfABITy - LLVM for struct _method_list_t
220 const llvm::StructType *MethodListnfABITy;
221
222 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
223 const llvm::Type *MethodListnfABIPtrTy;
224
225 // ProtocolnfABITy = LLVM for struct _protocol_t
226 const llvm::StructType *ProtocolnfABITy;
227
228 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
229 const llvm::StructType *ProtocolListnfABITy;
230
231 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
232 const llvm::Type *ProtocolListnfABIPtrTy;
233
234 // ClassnfABITy - LLVM for struct _class_t
235 const llvm::StructType *ClassnfABITy;
236
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000237 // ClassnfABIPtrTy - LLVM for struct _class_t*
238 const llvm::Type *ClassnfABIPtrTy;
239
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000240 // IvarnfABITy - LLVM for struct _ivar_t
241 const llvm::StructType *IvarnfABITy;
242
243 // IvarListnfABITy - LLVM for struct _ivar_list_t
244 const llvm::StructType *IvarListnfABITy;
245
246 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
247 const llvm::Type *IvarListnfABIPtrTy;
248
249 // ClassRonfABITy - LLVM for struct _class_ro_t
250 const llvm::StructType *ClassRonfABITy;
251
252 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
253 const llvm::Type *ImpnfABITy;
254
255 // CategorynfABITy - LLVM for struct _category_t
256 const llvm::StructType *CategorynfABITy;
257
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000258 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
259 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000260};
261
262class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
263protected:
264 CodeGen::CodeGenModule &CGM;
265 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000266 unsigned ObjCABI;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000267
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000268 /// LazySymbols - Symbols to generate a lazy reference for. See
269 /// DefinedSymbols and FinishModule().
270 std::set<IdentifierInfo*> LazySymbols;
271
272 /// DefinedSymbols - External symbols which are defined by this
273 /// module. The symbols in this list and LazySymbols are used to add
274 /// special linker symbols which ensure that Objective-C modules are
275 /// linked properly.
276 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000277
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000278 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000279 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000280
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000281 /// MethodVarNames - uniqued method variable names.
282 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000283
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000284 /// MethodVarTypes - uniqued method type signatures. We have to use
285 /// a StringMap here because have no other unique reference.
286 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000287
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000288 /// MethodDefinitions - map of methods which have been defined in
289 /// this translation unit.
290 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000291
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000292 /// PropertyNames - uniqued method variable names.
293 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000294
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000295 /// ClassReferences - uniqued class references.
296 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000297
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000298 /// SelectorReferences - uniqued selector references.
299 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000300
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000301 /// Protocols - Protocols for which an objc_protocol structure has
302 /// been emitted. Forward declarations are handled by creating an
303 /// empty structure whose initializer is filled in when/if defined.
304 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000305
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000306 /// DefinedProtocols - Protocols which have actually been
307 /// defined. We should not need this, see FIXME in GenerateProtocol.
308 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000309
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000310 /// DefinedClasses - List of defined classes.
311 std::vector<llvm::GlobalValue*> DefinedClasses;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000312
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000313 /// DefinedCategories - List of defined categories.
314 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000315
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000316 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000317 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000318 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000319
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000320 /// GetNameForMethod - Return a name for the given method.
321 /// \param[out] NameOut - The return value.
322 void GetNameForMethod(const ObjCMethodDecl *OMD,
323 const ObjCContainerDecl *CD,
324 std::string &NameOut);
325
326 /// GetMethodVarName - Return a unique constant for the given
327 /// selector's name. The return value has type char *.
328 llvm::Constant *GetMethodVarName(Selector Sel);
329 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
330 llvm::Constant *GetMethodVarName(const std::string &Name);
331
332 /// GetMethodVarType - Return a unique constant for the given
333 /// selector's name. The return value has type char *.
334
335 // FIXME: This is a horrible name.
336 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
337 llvm::Constant *GetMethodVarType(const std::string &Name);
338
339 /// GetPropertyName - Return a unique constant for the given
340 /// name. The return value has type char *.
341 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
342
343 // FIXME: This can be dropped once string functions are unified.
344 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
345 const Decl *Container);
346
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000347 /// GetClassName - Return a unique constant for the given selector's
348 /// name. The return value has type char *.
349 llvm::Constant *GetClassName(IdentifierInfo *Ident);
350
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000351 const RecordDecl *GetFirstIvarInRecord(const ObjCInterfaceDecl *OID,
352 RecordDecl::field_iterator &FIV,
353 RecordDecl::field_iterator &PIV);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000354 /// EmitPropertyList - Emit the given property list. The return
355 /// value has type PropertyListPtrTy.
356 llvm::Constant *EmitPropertyList(const std::string &Name,
357 const Decl *Container,
358 const ObjCContainerDecl *OCD,
359 const ObjCCommonTypesHelper &ObjCTypes);
360
Fariborz Jahanianda320092009-01-29 19:24:30 +0000361 /// GetProtocolRef - Return a reference to the internal protocol
362 /// description, creating an empty one if it has not been
363 /// defined. The return value has type ProtocolPtrTy.
364 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
365
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000366public:
367 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
368 { }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000369
370 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000371
372 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
373 const ObjCContainerDecl *CD=0);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000374
375 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
376
377 /// GetOrEmitProtocol - Get the protocol object for the given
378 /// declaration, emitting it if necessary. The return value has type
379 /// ProtocolPtrTy.
380 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
381
382 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
383 /// object for the given declaration, emitting it if needed. These
384 /// forward references will be filled in with empty bodies if no
385 /// definition is seen. The return value has type ProtocolPtrTy.
386 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000387};
388
389class CGObjCMac : public CGObjCCommonMac {
390private:
391 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000392 /// EmitImageInfo - Emit the image info marker used to encode some module
393 /// level information.
394 void EmitImageInfo();
395
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000396 /// EmitModuleInfo - Another marker encoding module level
397 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000398 void EmitModuleInfo();
399
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000400 /// EmitModuleSymols - Emit module symbols, the list of defined
401 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000402 llvm::Constant *EmitModuleSymbols();
403
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000404 /// FinishModule - Write out global data structures at the end of
405 /// processing a translation unit.
406 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000407
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000408 /// EmitClassExtension - Generate the class extension structure used
409 /// to store the weak ivar layout and properties. The return value
410 /// has type ClassExtensionPtrTy.
411 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
412
413 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
414 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000415 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000416 const ObjCInterfaceDecl *ID);
417
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000418 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000419 QualType ResultType,
420 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000421 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000422 QualType Arg0Ty,
423 bool IsSuper,
424 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000425
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000426 /// EmitIvarList - Emit the ivar list for the given
427 /// implementation. If ForClass is true the list of class ivars
428 /// (i.e. metaclass ivars) is emitted, otherwise the list of
429 /// interface ivars will be emitted. The return value has type
430 /// IvarListPtrTy.
431 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000432 bool ForClass);
433
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000434 /// EmitMetaClass - Emit a forward reference to the class structure
435 /// for the metaclass of the given interface. The return value has
436 /// type ClassPtrTy.
437 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
438
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000439 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000440 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000441 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
442 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000443 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000444 const ConstantVector &Methods);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000445
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000446 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000447
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000448 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000449
450 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000451 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000452 llvm::Constant *EmitMethodList(const std::string &Name,
453 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000454 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000455
456 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000457 /// method declarations.
458 /// - TypeName: The name for the type containing the methods.
459 /// - IsProtocol: True iff these methods are for a protocol.
460 /// - ClassMethds: True iff these are class methods.
461 /// - Required: When true, only "required" methods are
462 /// listed. Similarly, when false only "optional" methods are
463 /// listed. For classes this should always be true.
464 /// - begin, end: The method list to output.
465 ///
466 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000467 llvm::Constant *EmitMethodDescList(const std::string &Name,
468 const char *Section,
469 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000470
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000471 /// GetOrEmitProtocol - Get the protocol object for the given
472 /// declaration, emitting it if necessary. The return value has type
473 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000474 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000475
476 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
477 /// object for the given declaration, emitting it if needed. These
478 /// forward references will be filled in with empty bodies if no
479 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000480 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000481
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000482 /// EmitProtocolExtension - Generate the protocol extension
483 /// structure used to store optional instance and class methods, and
484 /// protocol properties. The return value has type
485 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000486 llvm::Constant *
487 EmitProtocolExtension(const ObjCProtocolDecl *PD,
488 const ConstantVector &OptInstanceMethods,
489 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000490
491 /// EmitProtocolList - Generate the list of referenced
492 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc933702008-08-21 21:57:41 +0000493 llvm::Constant *EmitProtocolList(const std::string &Name,
494 ObjCProtocolDecl::protocol_iterator begin,
495 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000496
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000497 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
498 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000499 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000500
Fariborz Jahanianda320092009-01-29 19:24:30 +0000501 public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000502 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000503
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000504 virtual llvm::Function *ModuleInitFunction();
505
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000506 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000507 QualType ResultType,
508 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000509 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000510 bool IsClassMessage,
511 const CallArgList &CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000512
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000513 virtual CodeGen::RValue
514 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000515 QualType ResultType,
516 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000517 const ObjCInterfaceDecl *Class,
518 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000519 bool IsClassMessage,
520 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000521
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000522 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000523 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000524
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000525 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000526
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000527 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000528
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000529 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000530
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000531 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000532 const ObjCProtocolDecl *PD);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000533
Daniel Dunbar49f66022008-09-24 03:38:44 +0000534 virtual llvm::Function *GetPropertyGetFunction();
535 virtual llvm::Function *GetPropertySetFunction();
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000536 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000537
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000538 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
539 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000540 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
541 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000542 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000543 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000544 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
545 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000546 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
547 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000548 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
549 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000550 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
551 llvm::Value *src, llvm::Value *dest);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000552};
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000553
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000554class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000555private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000556 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000557 llvm::GlobalVariable* ObjCEmptyCacheVar;
558 llvm::GlobalVariable* ObjCEmptyVtableVar;
559
560 /// FinishNonFragileABIModule - Write out global data structures at the end of
561 /// processing a translation unit.
562 void FinishNonFragileABIModule();
563
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000564 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
565 unsigned InstanceStart,
566 unsigned InstanceSize,
567 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +0000568 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
569 llvm::Constant *IsAGV,
570 llvm::Constant *SuperClassGV,
571 llvm::Constant *ClassRoGV);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000572
573 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
574
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +0000575 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
576
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000577 /// EmitMethodList - Emit the method list for the given
578 /// implementation. The return value has type MethodListnfABITy.
579 llvm::Constant *EmitMethodList(const std::string &Name,
580 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +0000581 const ConstantVector &Methods);
582 /// EmitIvarList - Emit the ivar list for the given
583 /// implementation. If ForClass is true the list of class ivars
584 /// (i.e. metaclass ivars) is emitted, otherwise the list of
585 /// interface ivars will be emitted. The return value has type
586 /// IvarListnfABIPtrTy.
587 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000588
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +0000589 llvm::Constant *EmitIvarOffsetVar(const ObjCImplementationDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +0000590 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +0000591 unsigned long int offset);
592
Fariborz Jahanianda320092009-01-29 19:24:30 +0000593 /// GetOrEmitProtocol - Get the protocol object for the given
594 /// declaration, emitting it if necessary. The return value has type
595 /// ProtocolPtrTy.
596 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
597
598 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
599 /// object for the given declaration, emitting it if needed. These
600 /// forward references will be filled in with empty bodies if no
601 /// definition is seen. The return value has type ProtocolPtrTy.
602 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
603
604 /// EmitProtocolList - Generate the list of referenced
605 /// protocols. The return value has type ProtocolListPtrTy.
606 llvm::Constant *EmitProtocolList(const std::string &Name,
607 ObjCProtocolDecl::protocol_iterator begin,
608 ObjCProtocolDecl::protocol_iterator end);
609
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000610public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000611 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000612 // FIXME. All stubs for now!
613 virtual llvm::Function *ModuleInitFunction();
614
615 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
616 QualType ResultType,
617 Selector Sel,
618 llvm::Value *Receiver,
619 bool IsClassMessage,
620 const CallArgList &CallArgs)
621 {return RValue::get(0);}
622
623 virtual CodeGen::RValue
624 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
625 QualType ResultType,
626 Selector Sel,
627 const ObjCInterfaceDecl *Class,
628 llvm::Value *Receiver,
629 bool IsClassMessage,
630 const CallArgList &CallArgs){ return RValue::get(0);}
631
632 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
633 const ObjCInterfaceDecl *ID){ return 0; }
634
635 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
636 { return 0; }
637
Fariborz Jahanianeb062d92009-01-26 18:32:24 +0000638 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000639
640 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
641
642 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
643 const ObjCProtocolDecl *PD)
644 { return 0; }
645
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000646 virtual llvm::Function *GetPropertyGetFunction(){ return 0; }
647 virtual llvm::Function *GetPropertySetFunction()
648 { return 0; }
649 virtual llvm::Function *EnumerationMutationFunction()
650 { return 0; }
651
652 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
653 const Stmt &S)
654 { return; }
655 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
656 const ObjCAtThrowStmt &S)
657 { return; }
658 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
659 llvm::Value *AddrWeakObj)
660 { return 0; }
661 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
662 llvm::Value *src, llvm::Value *dst)
663 { return; }
664 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
665 llvm::Value *src, llvm::Value *dest)
666 { return; }
667 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
668 llvm::Value *src, llvm::Value *dest)
669 { return; }
670 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
671 llvm::Value *src, llvm::Value *dest)
672 { return; }
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000673};
674
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000675} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000676
677/* *** Helper Functions *** */
678
679/// getConstantGEP() - Help routine to construct simple GEPs.
680static llvm::Constant *getConstantGEP(llvm::Constant *C,
681 unsigned idx0,
682 unsigned idx1) {
683 llvm::Value *Idxs[] = {
684 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
685 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
686 };
687 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
688}
689
690/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000691
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000692CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
693 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000694{
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000695 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000696 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000697}
698
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000699/// GetClass - Return a reference to the class for the given interface
700/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000701llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000702 const ObjCInterfaceDecl *ID) {
703 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000704}
705
706/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000707llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000708 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000709}
710
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000711/// Generate a constant CFString object.
712/*
713 struct __builtin_CFString {
714 const int *isa; // point to __CFConstantStringClassReference
715 int flags;
716 const char *str;
717 long length;
718 };
719*/
720
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000721llvm::Constant *CGObjCCommonMac::GenerateConstantString(
722 const std::string &String) {
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000723 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000724}
725
726/// Generates a message send where the super is the receiver. This is
727/// a message send to self with special delivery semantics indicating
728/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000729CodeGen::RValue
730CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000731 QualType ResultType,
732 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000733 const ObjCInterfaceDecl *Class,
734 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000735 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000736 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000737 // Create and init a super structure; this is a (receiver, class)
738 // pair we will pass to objc_msgSendSuper.
739 llvm::Value *ObjCSuper =
740 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
741 llvm::Value *ReceiverAsObject =
742 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
743 CGF.Builder.CreateStore(ReceiverAsObject,
744 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000745
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000746 // If this is a class message the metaclass is passed as the target.
747 llvm::Value *Target;
748 if (IsClassMessage) {
749 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
750 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
751 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
752 Target = Super;
753 } else {
754 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
755 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000756 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
757 // and ObjCTypes types.
758 const llvm::Type *ClassTy =
759 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000760 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000761 CGF.Builder.CreateStore(Target,
762 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
763
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000764 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000765 ObjCSuper, ObjCTypes.SuperPtrCTy,
766 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000767}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000768
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000769/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000770CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000771 QualType ResultType,
772 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000773 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000774 bool IsClassMessage,
775 const CallArgList &CallArgs) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000776 llvm::Value *Arg0 =
777 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000778 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000779 Arg0, CGF.getContext().getObjCIdType(),
780 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000781}
782
783CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000784 QualType ResultType,
785 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000786 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000787 QualType Arg0Ty,
788 bool IsSuper,
789 const CallArgList &CallArgs) {
790 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000791 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
792 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
793 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000794 CGF.getContext().getObjCSelType()));
795 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000796
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000797 const llvm::FunctionType *FTy =
798 CGM.getTypes().GetFunctionType(CGCallInfo(ResultType, ActualArgs),
799 false);
Daniel Dunbar5669e572008-10-17 03:24:53 +0000800
801 llvm::Constant *Fn;
802 if (CGM.ReturnTypeUsesSret(ResultType)) {
803 Fn = ObjCTypes.getSendStretFn(IsSuper);
804 } else if (ResultType->isFloatingType()) {
805 // FIXME: Sadly, this is wrong. This actually depends on the
806 // architecture. This happens to be right for x86-32 though.
807 Fn = ObjCTypes.getSendFpretFn(IsSuper);
808 } else {
809 Fn = ObjCTypes.getSendFn(IsSuper);
810 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000811 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar3913f182008-09-09 23:48:28 +0000812 return CGF.EmitCall(Fn, ResultType, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000813}
814
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000815llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000816 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +0000817 // FIXME: I don't understand why gcc generates this, or where it is
818 // resolved. Investigate. Its also wasteful to look this up over and
819 // over.
820 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
821
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000822 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
823 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000824}
825
Fariborz Jahanianda320092009-01-29 19:24:30 +0000826void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000827 // FIXME: We shouldn't need this, the protocol decl should contain
828 // enough information to tell us whether this was a declaration or a
829 // definition.
830 DefinedProtocols.insert(PD->getIdentifier());
831
832 // If we have generated a forward reference to this protocol, emit
833 // it now. Otherwise do nothing, the protocol objects are lazily
834 // emitted.
835 if (Protocols.count(PD->getIdentifier()))
836 GetOrEmitProtocol(PD);
837}
838
Fariborz Jahanianda320092009-01-29 19:24:30 +0000839llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000840 if (DefinedProtocols.count(PD->getIdentifier()))
841 return GetOrEmitProtocol(PD);
842 return GetOrEmitProtocolRef(PD);
843}
844
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000845/*
846 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
847 struct _objc_protocol {
848 struct _objc_protocol_extension *isa;
849 char *protocol_name;
850 struct _objc_protocol_list *protocol_list;
851 struct _objc__method_prototype_list *instance_methods;
852 struct _objc__method_prototype_list *class_methods
853 };
854
855 See EmitProtocolExtension().
856*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000857llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
858 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
859
860 // Early exit if a defining object has already been generated.
861 if (Entry && Entry->hasInitializer())
862 return Entry;
863
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000864 // FIXME: I don't understand why gcc generates this, or where it is
865 // resolved. Investigate. Its also wasteful to look this up over and
866 // over.
867 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
868
Chris Lattner8ec03f52008-11-24 03:54:41 +0000869 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000870
871 // Construct method lists.
872 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
873 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
874 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
875 e = PD->instmeth_end(); i != e; ++i) {
876 ObjCMethodDecl *MD = *i;
877 llvm::Constant *C = GetMethodDescriptionConstant(MD);
878 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
879 OptInstanceMethods.push_back(C);
880 } else {
881 InstanceMethods.push_back(C);
882 }
883 }
884
885 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
886 e = PD->classmeth_end(); i != e; ++i) {
887 ObjCMethodDecl *MD = *i;
888 llvm::Constant *C = GetMethodDescriptionConstant(MD);
889 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
890 OptClassMethods.push_back(C);
891 } else {
892 ClassMethods.push_back(C);
893 }
894 }
895
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000896 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000897 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000898 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +0000899 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000900 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +0000901 PD->protocol_begin(),
902 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000903 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000904 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
905 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000906 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
907 InstanceMethods);
908 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000909 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
910 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000911 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
912 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000913 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
914 Values);
915
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000916 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000917 // Already created, fix the linkage and update the initializer.
918 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000919 Entry->setInitializer(Init);
920 } else {
921 Entry =
922 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
923 llvm::GlobalValue::InternalLinkage,
924 Init,
925 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
926 &CGM.getModule());
927 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
928 UsedGlobals.push_back(Entry);
929 // FIXME: Is this necessary? Why only for protocol?
930 Entry->setAlignment(4);
931 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000932
933 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000934}
935
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000936llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000937 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
938
939 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000940 // We use the initializer as a marker of whether this is a forward
941 // reference or not. At module finalization we add the empty
942 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000943 Entry =
944 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000945 llvm::GlobalValue::ExternalLinkage,
946 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000947 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000948 &CGM.getModule());
949 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
950 UsedGlobals.push_back(Entry);
951 // FIXME: Is this necessary? Why only for protocol?
952 Entry->setAlignment(4);
953 }
954
955 return Entry;
956}
957
958/*
959 struct _objc_protocol_extension {
960 uint32_t size;
961 struct objc_method_description_list *optional_instance_methods;
962 struct objc_method_description_list *optional_class_methods;
963 struct objc_property_list *instance_properties;
964 };
965*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000966llvm::Constant *
967CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
968 const ConstantVector &OptInstanceMethods,
969 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000970 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +0000971 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000972 std::vector<llvm::Constant*> Values(4);
973 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000974 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000975 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
976 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000977 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
978 OptInstanceMethods);
979 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000980 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
981 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000982 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
983 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000984 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
985 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000986 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000987
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000988 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000989 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
990 Values[3]->isNullValue())
991 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
992
993 llvm::Constant *Init =
994 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
995 llvm::GlobalVariable *GV =
996 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
997 llvm::GlobalValue::InternalLinkage,
998 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000999 "\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001000 &CGM.getModule());
1001 // No special section, but goes in llvm.used
1002 UsedGlobals.push_back(GV);
1003
1004 return GV;
1005}
1006
1007/*
1008 struct objc_protocol_list {
1009 struct objc_protocol_list *next;
1010 long count;
1011 Protocol *list[];
1012 };
1013*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001014llvm::Constant *
1015CGObjCMac::EmitProtocolList(const std::string &Name,
1016 ObjCProtocolDecl::protocol_iterator begin,
1017 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001018 std::vector<llvm::Constant*> ProtocolRefs;
1019
Daniel Dunbardbc933702008-08-21 21:57:41 +00001020 for (; begin != end; ++begin)
1021 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001022
1023 // Just return null for empty protocol lists
1024 if (ProtocolRefs.empty())
1025 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1026
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001027 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001028 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1029
1030 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001031 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001032 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1033 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1034 Values[2] =
1035 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1036 ProtocolRefs.size()),
1037 ProtocolRefs);
1038
1039 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1040 llvm::GlobalVariable *GV =
1041 new llvm::GlobalVariable(Init->getType(), false,
1042 llvm::GlobalValue::InternalLinkage,
1043 Init,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001044 Name,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001045 &CGM.getModule());
1046 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
1047 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1048}
1049
1050/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001051 struct _objc_property {
1052 const char * const name;
1053 const char * const attributes;
1054 };
1055
1056 struct _objc_property_list {
1057 uint32_t entsize; // sizeof (struct _objc_property)
1058 uint32_t prop_count;
1059 struct _objc_property[prop_count];
1060 };
1061*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001062llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1063 const Decl *Container,
1064 const ObjCContainerDecl *OCD,
1065 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001066 std::vector<llvm::Constant*> Properties, Prop(2);
Steve Naroff93983f82009-01-11 12:47:58 +00001067 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1068 E = OCD->prop_end(); I != E; ++I) {
1069 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001070 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001071 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001072 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1073 Prop));
1074 }
1075
1076 // Return null for empty list.
1077 if (Properties.empty())
1078 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1079
1080 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001081 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001082 std::vector<llvm::Constant*> Values(3);
1083 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1084 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1085 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1086 Properties.size());
1087 Values[2] = llvm::ConstantArray::get(AT, Properties);
1088 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1089
1090 llvm::GlobalVariable *GV =
1091 new llvm::GlobalVariable(Init->getType(), false,
1092 llvm::GlobalValue::InternalLinkage,
1093 Init,
1094 Name,
1095 &CGM.getModule());
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001096 if (ObjCABI == 2)
1097 GV->setSection("__DATA, __objc_const");
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001098 // No special section on property lists?
1099 UsedGlobals.push_back(GV);
1100 return llvm::ConstantExpr::getBitCast(GV,
1101 ObjCTypes.PropertyListPtrTy);
1102
1103}
1104
1105/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001106 struct objc_method_description_list {
1107 int count;
1108 struct objc_method_description list[];
1109 };
1110*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001111llvm::Constant *
1112CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1113 std::vector<llvm::Constant*> Desc(2);
1114 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1115 ObjCTypes.SelectorPtrTy);
1116 Desc[1] = GetMethodVarType(MD);
1117 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1118 Desc);
1119}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001120
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001121llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1122 const char *Section,
1123 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001124 // Return null for empty list.
1125 if (Methods.empty())
1126 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1127
1128 std::vector<llvm::Constant*> Values(2);
1129 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1130 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1131 Methods.size());
1132 Values[1] = llvm::ConstantArray::get(AT, Methods);
1133 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1134
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001135 llvm::GlobalVariable *GV =
1136 new llvm::GlobalVariable(Init->getType(), false,
1137 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001138 Init, Name, &CGM.getModule());
1139 GV->setSection(Section);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001140 UsedGlobals.push_back(GV);
1141 return llvm::ConstantExpr::getBitCast(GV,
1142 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001143}
1144
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001145/*
1146 struct _objc_category {
1147 char *category_name;
1148 char *class_name;
1149 struct _objc_method_list *instance_methods;
1150 struct _objc_method_list *class_methods;
1151 struct _objc_protocol_list *protocols;
1152 uint32_t size; // <rdar://4585769>
1153 struct _objc_property_list *instance_properties;
1154 };
1155 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001156void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001157 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001158
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001159 // FIXME: This is poor design, the OCD should have a pointer to the
1160 // category decl. Additionally, note that Category can be null for
1161 // the @implementation w/o an @interface case. Sema should just
1162 // create one for us as it does for @implementation so everyone else
1163 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001164 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001165 const ObjCCategoryDecl *Category =
1166 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001167 std::string ExtName(Interface->getNameAsString() + "_" +
1168 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001169
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001170 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1171 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
1172 e = OCD->instmeth_end(); i != e; ++i) {
1173 // Instance methods should always be defined.
1174 InstanceMethods.push_back(GetMethodConstant(*i));
1175 }
1176 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
1177 e = OCD->classmeth_end(); i != e; ++i) {
1178 // Class methods should always be defined.
1179 ClassMethods.push_back(GetMethodConstant(*i));
1180 }
1181
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001182 std::vector<llvm::Constant*> Values(7);
1183 Values[0] = GetClassName(OCD->getIdentifier());
1184 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001185 Values[2] =
1186 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1187 ExtName,
1188 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001189 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001190 Values[3] =
1191 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
1192 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001193 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001194 if (Category) {
1195 Values[4] =
1196 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1197 Category->protocol_begin(),
1198 Category->protocol_end());
1199 } else {
1200 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1201 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001202 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001203
1204 // If there is no category @interface then there can be no properties.
1205 if (Category) {
1206 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001207 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001208 } else {
1209 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1210 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001211
1212 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1213 Values);
1214
1215 llvm::GlobalVariable *GV =
1216 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
1217 llvm::GlobalValue::InternalLinkage,
1218 Init,
1219 std::string("\01L_OBJC_CATEGORY_")+ExtName,
1220 &CGM.getModule());
1221 GV->setSection("__OBJC,__category,regular,no_dead_strip");
1222 UsedGlobals.push_back(GV);
1223 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001224}
1225
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001226// FIXME: Get from somewhere?
1227enum ClassFlags {
1228 eClassFlags_Factory = 0x00001,
1229 eClassFlags_Meta = 0x00002,
1230 // <rdr://5142207>
1231 eClassFlags_HasCXXStructors = 0x02000,
1232 eClassFlags_Hidden = 0x20000,
1233 eClassFlags_ABI2_Hidden = 0x00010,
1234 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1235};
1236
1237// <rdr://5142207&4705298&4843145>
1238static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
1239 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
1240 // FIXME: Support -fvisibility
1241 switch (attr->getVisibility()) {
1242 default:
1243 assert(0 && "Unknown visibility");
1244 return false;
1245 case VisibilityAttr::DefaultVisibility:
1246 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
1247 return false;
1248 case VisibilityAttr::HiddenVisibility:
1249 return true;
1250 }
1251 } else {
1252 return false; // FIXME: Support -fvisibility
1253 }
1254}
1255
1256/*
1257 struct _objc_class {
1258 Class isa;
1259 Class super_class;
1260 const char *name;
1261 long version;
1262 long info;
1263 long instance_size;
1264 struct _objc_ivar_list *ivars;
1265 struct _objc_method_list *methods;
1266 struct _objc_cache *cache;
1267 struct _objc_protocol_list *protocols;
1268 // Objective-C 1.0 extensions (<rdr://4585769>)
1269 const char *ivar_layout;
1270 struct _objc_class_ext *ext;
1271 };
1272
1273 See EmitClassExtension();
1274 */
1275void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001276 DefinedSymbols.insert(ID->getIdentifier());
1277
Chris Lattner8ec03f52008-11-24 03:54:41 +00001278 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001279 // FIXME: Gross
1280 ObjCInterfaceDecl *Interface =
1281 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001282 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001283 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001284 Interface->protocol_begin(),
1285 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001286 const llvm::Type *InterfaceTy =
1287 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
1288 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001289 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001290
1291 // FIXME: Set CXX-structors flag.
1292 if (IsClassHidden(ID->getClassInterface()))
1293 Flags |= eClassFlags_Hidden;
1294
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001295 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1296 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
1297 e = ID->instmeth_end(); i != e; ++i) {
1298 // Instance methods should always be defined.
1299 InstanceMethods.push_back(GetMethodConstant(*i));
1300 }
1301 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
1302 e = ID->classmeth_end(); i != e; ++i) {
1303 // Class methods should always be defined.
1304 ClassMethods.push_back(GetMethodConstant(*i));
1305 }
1306
1307 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
1308 e = ID->propimpl_end(); i != e; ++i) {
1309 ObjCPropertyImplDecl *PID = *i;
1310
1311 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1312 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1313
1314 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1315 if (llvm::Constant *C = GetMethodConstant(MD))
1316 InstanceMethods.push_back(C);
1317 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1318 if (llvm::Constant *C = GetMethodConstant(MD))
1319 InstanceMethods.push_back(C);
1320 }
1321 }
1322
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001323 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001324 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001325 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001326 // Record a reference to the super class.
1327 LazySymbols.insert(Super->getIdentifier());
1328
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001329 Values[ 1] =
1330 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1331 ObjCTypes.ClassPtrTy);
1332 } else {
1333 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1334 }
1335 Values[ 2] = GetClassName(ID->getIdentifier());
1336 // Version is always 0.
1337 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1338 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1339 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001340 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001341 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001342 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001343 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001344 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001345 // cache is always NULL.
1346 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1347 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001348 // FIXME: Set ivar_layout
1349 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001350 Values[11] = EmitClassExtension(ID);
1351 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1352 Values);
1353
1354 llvm::GlobalVariable *GV =
1355 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1356 llvm::GlobalValue::InternalLinkage,
1357 Init,
1358 std::string("\01L_OBJC_CLASS_")+ClassName,
1359 &CGM.getModule());
1360 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1361 UsedGlobals.push_back(GV);
1362 // FIXME: Why?
1363 GV->setAlignment(32);
1364 DefinedClasses.push_back(GV);
1365}
1366
1367llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1368 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001369 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001370 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001371 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001372 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001373
1374 if (IsClassHidden(ID->getClassInterface()))
1375 Flags |= eClassFlags_Hidden;
1376
1377 std::vector<llvm::Constant*> Values(12);
1378 // The isa for the metaclass is the root of the hierarchy.
1379 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1380 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1381 Root = Super;
1382 Values[ 0] =
1383 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1384 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001385 // The super class for the metaclass is emitted as the name of the
1386 // super class. The runtime fixes this up to point to the
1387 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001388 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1389 Values[ 1] =
1390 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1391 ObjCTypes.ClassPtrTy);
1392 } else {
1393 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1394 }
1395 Values[ 2] = GetClassName(ID->getIdentifier());
1396 // Version is always 0.
1397 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1398 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1399 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001400 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001401 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001402 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001403 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001404 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001405 // cache is always NULL.
1406 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1407 Values[ 9] = Protocols;
1408 // ivar_layout for metaclass is always NULL.
1409 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1410 // The class extension is always unused for metaclasses.
1411 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1412 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1413 Values);
1414
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001415 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00001416 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001417
1418 // Check for a forward reference.
1419 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1420 if (GV) {
1421 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1422 "Forward metaclass reference has incorrect type.");
1423 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1424 GV->setInitializer(Init);
1425 } else {
1426 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1427 llvm::GlobalValue::InternalLinkage,
1428 Init, Name,
1429 &CGM.getModule());
1430 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001431 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1432 UsedGlobals.push_back(GV);
1433 // FIXME: Why?
1434 GV->setAlignment(32);
1435
1436 return GV;
1437}
1438
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001439llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001440 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001441
1442 // FIXME: Should we look these up somewhere other than the
1443 // module. Its a bit silly since we only generate these while
1444 // processing an implementation, so exactly one pointer would work
1445 // if know when we entered/exitted an implementation block.
1446
1447 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00001448 // Previously, metaclass with internal linkage may have been defined.
1449 // pass 'true' as 2nd argument so it is returned.
1450 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001451 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1452 "Forward metaclass reference has incorrect type.");
1453 return GV;
1454 } else {
1455 // Generate as an external reference to keep a consistent
1456 // module. This will be patched up when we emit the metaclass.
1457 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1458 llvm::GlobalValue::ExternalLinkage,
1459 0,
1460 Name,
1461 &CGM.getModule());
1462 }
1463}
1464
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001465/*
1466 struct objc_class_ext {
1467 uint32_t size;
1468 const char *weak_ivar_layout;
1469 struct _objc_property_list *properties;
1470 };
1471*/
1472llvm::Constant *
1473CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1474 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001475 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001476
1477 std::vector<llvm::Constant*> Values(3);
1478 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001479 // FIXME: Output weak_ivar_layout string.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001480 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001481 Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001482 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001483
1484 // Return null if no extension bits are used.
1485 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1486 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1487
1488 llvm::Constant *Init =
1489 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1490 llvm::GlobalVariable *GV =
1491 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1492 llvm::GlobalValue::InternalLinkage,
1493 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001494 "\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001495 &CGM.getModule());
1496 // No special section, but goes in llvm.used
1497 UsedGlobals.push_back(GV);
1498
1499 return GV;
1500}
1501
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001502/// countInheritedIvars - count number of ivars in class and its super class(s)
1503///
1504static int countInheritedIvars(const ObjCInterfaceDecl *OI) {
1505 int count = 0;
1506 if (!OI)
1507 return 0;
1508 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
1509 if (SuperClass)
1510 count += countInheritedIvars(SuperClass);
1511 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1512 E = OI->ivar_end(); I != E; ++I)
1513 ++count;
1514 return count;
1515}
1516
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001517/*
1518 struct objc_ivar {
1519 char *ivar_name;
1520 char *ivar_type;
1521 int ivar_offset;
1522 };
1523
1524 struct objc_ivar_list {
1525 int ivar_count;
1526 struct objc_ivar list[count];
1527 };
1528 */
1529llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001530 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001531 std::vector<llvm::Constant*> Ivars, Ivar(3);
1532
1533 // When emitting the root class GCC emits ivar entries for the
1534 // actual class structure. It is not clear if we need to follow this
1535 // behavior; for now lets try and get away with not doing it. If so,
1536 // the cleanest solution would be to make up an ObjCInterfaceDecl
1537 // for the class.
1538 if (ForClass)
1539 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001540
1541 ObjCInterfaceDecl *OID =
1542 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
1543 const llvm::Type *InterfaceTy =
1544 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001545 const llvm::StructLayout *Layout =
1546 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001547
1548 RecordDecl::field_iterator ifield, pfield;
1549 const RecordDecl *RD = GetFirstIvarInRecord(OID, ifield, pfield);
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001550 for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) {
1551 FieldDecl *Field = *ifield;
1552 unsigned Offset = Layout->getElementOffset(CGM.getTypes().
1553 getLLVMFieldNo(Field));
1554 if (Field->getIdentifier())
1555 Ivar[0] = GetMethodVarName(Field->getIdentifier());
1556 else
1557 Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001558 std::string TypeStr;
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001559 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001560 Ivar[1] = GetMethodVarType(TypeStr);
1561 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001562 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001563 }
1564
1565 // Return null for empty list.
1566 if (Ivars.empty())
1567 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1568
1569 std::vector<llvm::Constant*> Values(2);
1570 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1571 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1572 Ivars.size());
1573 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1574 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1575
1576 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1577 "\01L_OBJC_INSTANCE_VARIABLES_");
1578 llvm::GlobalVariable *GV =
1579 new llvm::GlobalVariable(Init->getType(), false,
1580 llvm::GlobalValue::InternalLinkage,
1581 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001582 Prefix + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001583 &CGM.getModule());
1584 if (ForClass) {
1585 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1586 // FIXME: Why is this only here?
1587 GV->setAlignment(32);
1588 } else {
1589 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1590 }
1591 UsedGlobals.push_back(GV);
1592 return llvm::ConstantExpr::getBitCast(GV,
1593 ObjCTypes.IvarListPtrTy);
1594}
1595
1596/*
1597 struct objc_method {
1598 SEL method_name;
1599 char *method_types;
1600 void *method;
1601 };
1602
1603 struct objc_method_list {
1604 struct objc_method_list *obsolete;
1605 int count;
1606 struct objc_method methods_list[count];
1607 };
1608*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001609
1610/// GetMethodConstant - Return a struct objc_method constant for the
1611/// given method if it has been defined. The result is null if the
1612/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001613llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001614 // FIXME: Use DenseMap::lookup
1615 llvm::Function *Fn = MethodDefinitions[MD];
1616 if (!Fn)
1617 return 0;
1618
1619 std::vector<llvm::Constant*> Method(3);
1620 Method[0] =
1621 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1622 ObjCTypes.SelectorPtrTy);
1623 Method[1] = GetMethodVarType(MD);
1624 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1625 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1626}
1627
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001628llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1629 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001630 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001631 // Return null for empty list.
1632 if (Methods.empty())
1633 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1634
1635 std::vector<llvm::Constant*> Values(3);
1636 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1637 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1638 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1639 Methods.size());
1640 Values[2] = llvm::ConstantArray::get(AT, Methods);
1641 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1642
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001643 llvm::GlobalVariable *GV =
1644 new llvm::GlobalVariable(Init->getType(), false,
1645 llvm::GlobalValue::InternalLinkage,
1646 Init,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001647 Name,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001648 &CGM.getModule());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001649 GV->setSection(Section);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001650 UsedGlobals.push_back(GV);
1651 return llvm::ConstantExpr::getBitCast(GV,
1652 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001653}
1654
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001655llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001656 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001657 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001658 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001659
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001660 const llvm::FunctionType *MethodTy =
1661 CGM.getTypes().GetFunctionType(CGFunctionInfo(OMD, CGM.getContext()));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001662 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001663 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001664 llvm::GlobalValue::InternalLinkage,
1665 Name,
1666 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001667 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001668
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001669 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001670}
1671
1672llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001673 // Abuse this interface function as a place to finalize.
1674 FinishModule();
1675
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001676 return NULL;
1677}
1678
Daniel Dunbar49f66022008-09-24 03:38:44 +00001679llvm::Function *CGObjCMac::GetPropertyGetFunction() {
1680 return ObjCTypes.GetPropertyFn;
1681}
1682
1683llvm::Function *CGObjCMac::GetPropertySetFunction() {
1684 return ObjCTypes.SetPropertyFn;
1685}
1686
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001687llvm::Function *CGObjCMac::EnumerationMutationFunction()
1688{
1689 return ObjCTypes.EnumerationMutationFn;
1690}
1691
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001692/*
1693
1694Objective-C setjmp-longjmp (sjlj) Exception Handling
1695--
1696
1697The basic framework for a @try-catch-finally is as follows:
1698{
1699 objc_exception_data d;
1700 id _rethrow = null;
1701
1702 objc_exception_try_enter(&d);
1703 if (!setjmp(d.jmp_buf)) {
1704 ... try body ...
1705 } else {
1706 // exception path
1707 id _caught = objc_exception_extract(&d);
1708
1709 // enter new try scope for handlers
1710 if (!setjmp(d.jmp_buf)) {
1711 ... match exception and execute catch blocks ...
1712
1713 // fell off end, rethrow.
1714 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00001715 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001716 } else {
1717 // exception in catch block
1718 _rethrow = objc_exception_extract(&d);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001719 ... jump-through-finally_no_exit to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001720 }
1721 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00001722 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001723
1724finally:
1725 // match either the initial try_enter or the catch try_enter,
1726 // depending on the path followed.
1727 objc_exception_try_exit(&d);
1728finally_no_exit:
1729 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00001730 ... dispatch to finally destination ...
1731
1732finally_rethrow:
1733 objc_exception_throw(_rethrow);
1734
1735finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001736}
1737
1738This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00001739uses _rethrow to determine if objc_exception_try_exit should be called
1740and if the object should be rethrown. This breaks in the face of
1741throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001742
1743We specialize this framework for a few particular circumstances:
1744
1745 - If there are no catch blocks, then we avoid emitting the second
1746 exception handling context.
1747
1748 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
1749 e)) we avoid emitting the code to rethrow an uncaught exception.
1750
1751 - FIXME: If there is no @finally block we can do a few more
1752 simplifications.
1753
1754Rethrows and Jumps-Through-Finally
1755--
1756
1757Support for implicit rethrows and jumping through the finally block is
1758handled by storing the current exception-handling context in
1759ObjCEHStack.
1760
Daniel Dunbar898d5082008-09-30 01:06:03 +00001761In order to implement proper @finally semantics, we support one basic
1762mechanism for jumping through the finally block to an arbitrary
1763destination. Constructs which generate exits from a @try or @catch
1764block use this mechanism to implement the proper semantics by chaining
1765jumps, as necessary.
1766
1767This mechanism works like the one used for indirect goto: we
1768arbitrarily assign an ID to each destination and store the ID for the
1769destination in a variable prior to entering the finally block. At the
1770end of the finally block we simply create a switch to the proper
1771destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001772
1773Code gen for @synchronized(expr) stmt;
1774Effectively generating code for:
1775objc_sync_enter(expr);
1776@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001777*/
1778
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001779void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1780 const Stmt &S) {
1781 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001782 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001783 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
1784 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
1785 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1786 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001787 llvm::Value *DestCode =
1788 CGF.CreateTempAlloca(llvm::Type::Int32Ty, "finally.dst");
1789
1790 // Generate jump code. Done here so we can directly add things to
1791 // the switch instruction.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001792 llvm::BasicBlock *FinallyJump = CGF.createBasicBlock("finally.jump");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001793 llvm::SwitchInst *FinallySwitch =
1794 llvm::SwitchInst::Create(new llvm::LoadInst(DestCode, "", FinallyJump),
1795 FinallyEnd, 10, FinallyJump);
1796
1797 // Push an EH context entry, used for handling rethrows and jumps
1798 // through finally.
1799 CodeGenFunction::ObjCEHEntry EHEntry(FinallyBlock, FinallyNoExit,
1800 FinallySwitch, DestCode);
1801 CGF.ObjCEHStack.push_back(&EHEntry);
1802
1803 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00001804 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
1805 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001806 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
1807 "_rethrow");
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001808 if (!isTry) {
1809 // For @synchronized, call objc_sync_enter(sync.expr)
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001810 llvm::Value *Arg = CGF.EmitScalarExpr(
1811 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1812 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1813 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, Arg);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001814 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001815
1816 // Enter a new try block and call setjmp.
1817 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
1818 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
1819 "jmpbufarray");
1820 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
1821 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1822 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001823
Daniel Dunbar55e87422008-11-11 02:29:29 +00001824 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1825 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001826 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001827 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001828
1829 // Emit the @try block.
1830 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001831 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
1832 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001833 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001834
1835 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001836 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001837
1838 // Retrieve the exception object. We may emit multiple blocks but
1839 // nothing can cross this so the value is already in SSA form.
1840 llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1841 ExceptionData,
1842 "caught");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001843 EHEntry.Exception = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001844 if (!isTry)
1845 {
1846 CGF.Builder.CreateStore(Caught, RethrowPtr);
1847 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
1848 }
1849 else if (const ObjCAtCatchStmt* CatchStmt =
1850 cast<ObjCAtTryStmt>(S).getCatchStmts())
1851 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00001852 // Enter a new exception try block (in case a @catch block throws
1853 // an exception).
Anders Carlsson80f25672008-09-09 17:59:25 +00001854 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001855
Anders Carlsson80f25672008-09-09 17:59:25 +00001856 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1857 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001858 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00001859
Daniel Dunbar55e87422008-11-11 02:29:29 +00001860 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
1861 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001862 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001863
1864 CGF.EmitBlock(CatchBlock);
1865
Daniel Dunbar55e40722008-09-27 07:03:52 +00001866 // Handle catch list. As a special case we check if everything is
1867 // matched and avoid generating code for falling off the end if
1868 // so.
1869 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00001870 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00001871 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00001872
Anders Carlssondde0a942008-09-11 09:15:33 +00001873 const DeclStmt *CatchParam =
1874 cast_or_null<DeclStmt>(CatchStmt->getCatchParamStmt());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001875 const VarDecl *VD = 0;
1876 const PointerType *PT = 0;
1877
Anders Carlsson80f25672008-09-09 17:59:25 +00001878 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00001879 if (!CatchParam) {
1880 AllMatched = true;
1881 } else {
Ted Kremenekde3b8fb2008-10-06 20:58:56 +00001882 VD = cast<VarDecl>(CatchParam->getSolitaryDecl());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001883 PT = VD->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001884
Daniel Dunbar97f61d12008-09-27 22:21:14 +00001885 // catch(id e) always matches.
1886 // FIXME: For the time being we also match id<X>; this should
1887 // be rejected by Sema instead.
1888 if ((PT && CGF.getContext().isObjCIdType(PT->getPointeeType())) ||
1889 VD->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00001890 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00001891 }
1892
Daniel Dunbar55e40722008-09-27 07:03:52 +00001893 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00001894 if (CatchParam) {
1895 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001896 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar129271a2008-09-27 07:36:24 +00001897 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00001898 }
Anders Carlsson1452f552008-09-11 08:21:54 +00001899
Anders Carlssondde0a942008-09-11 09:15:33 +00001900 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001901 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001902 break;
1903 }
1904
Daniel Dunbar129271a2008-09-27 07:36:24 +00001905 assert(PT && "Unexpected non-pointer type in @catch");
1906 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00001907 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001908 assert(ObjCType && "Catch parameter must have Objective-C type!");
1909
1910 // Check if the @catch block matches the exception object.
1911 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
1912
Anders Carlsson80f25672008-09-09 17:59:25 +00001913 llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn,
1914 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00001915
Daniel Dunbar55e87422008-11-11 02:29:29 +00001916 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00001917
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001918 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001919 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001920
1921 // Emit the @catch block.
1922 CGF.EmitBlock(MatchedBlock);
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001923 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001924 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001925
1926 llvm::Value *Tmp =
1927 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(VD->getType()),
1928 "tmp");
1929 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00001930
1931 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001932 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001933
1934 CGF.EmitBlock(NextCatchBlock);
1935 }
1936
Daniel Dunbar55e40722008-09-27 07:03:52 +00001937 if (!AllMatched) {
1938 // None of the handlers caught the exception, so store it to be
1939 // rethrown at the end of the @finally block.
1940 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001941 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001942 }
1943
1944 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001945 CGF.EmitBlock(CatchHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001946 CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1947 ExceptionData),
1948 RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001949 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001950 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00001951 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001952 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Anders Carlsson80f25672008-09-09 17:59:25 +00001953 }
1954
Daniel Dunbar898d5082008-09-30 01:06:03 +00001955 // Pop the exception-handling stack entry. It is important to do
1956 // this now, because the code in the @finally block is not in this
1957 // context.
1958 CGF.ObjCEHStack.pop_back();
1959
Anders Carlsson80f25672008-09-09 17:59:25 +00001960 // Emit the @finally block.
1961 CGF.EmitBlock(FinallyBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001962 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00001963
1964 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001965 if (isTry) {
1966 if (const ObjCAtFinallyStmt* FinallyStmt =
1967 cast<ObjCAtTryStmt>(S).getFinallyStmt())
1968 CGF.EmitStmt(FinallyStmt->getFinallyBody());
1969 }
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001970 else {
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001971 // For @synchronized objc_sync_exit(expr); As finally's sole statement.
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001972 // For @synchronized, call objc_sync_enter(sync.expr)
1973 llvm::Value *Arg = CGF.EmitScalarExpr(
1974 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1975 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1976 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, Arg);
1977 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001978
Daniel Dunbar898d5082008-09-30 01:06:03 +00001979 CGF.EmitBlock(FinallyJump);
1980
1981 CGF.EmitBlock(FinallyRethrow);
1982 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn,
1983 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001984 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00001985
1986 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001987}
1988
1989void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00001990 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00001991 llvm::Value *ExceptionAsObject;
1992
1993 if (const Expr *ThrowExpr = S.getThrowExpr()) {
1994 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
1995 ExceptionAsObject =
1996 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
1997 } else {
Daniel Dunbar898d5082008-09-30 01:06:03 +00001998 assert((!CGF.ObjCEHStack.empty() && CGF.ObjCEHStack.back()->Exception) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001999 "Unexpected rethrow outside @catch block.");
Daniel Dunbar898d5082008-09-30 01:06:03 +00002000 ExceptionAsObject = CGF.ObjCEHStack.back()->Exception;
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002001 }
2002
2003 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002004 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002005
2006 // Clear the insertion point to indicate we are in unreachable code.
2007 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002008}
2009
Daniel Dunbar898d5082008-09-30 01:06:03 +00002010void CodeGenFunction::EmitJumpThroughFinally(ObjCEHEntry *E,
2011 llvm::BasicBlock *Dst,
2012 bool ExecuteTryExit) {
Daniel Dunbara448fb22008-11-11 23:11:34 +00002013 if (!HaveInsertPoint())
Daniel Dunbar898d5082008-09-30 01:06:03 +00002014 return;
2015
2016 // Find the destination code for this block. We always use 0 for the
2017 // fallthrough block (default destination).
2018 llvm::SwitchInst *SI = E->FinallySwitch;
2019 llvm::ConstantInt *ID;
2020 if (Dst == SI->getDefaultDest()) {
2021 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
2022 } else {
2023 ID = SI->findCaseDest(Dst);
2024 if (!ID) {
2025 // No code found, get a new unique one by just using the number
2026 // of switch successors.
2027 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, SI->getNumSuccessors());
2028 SI->addCase(ID, Dst);
2029 }
2030 }
2031
2032 // Set the destination code and branch.
2033 Builder.CreateStore(ID, E->DestCode);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002034 EmitBranch(ExecuteTryExit ? E->FinallyBlock : E->FinallyNoExit);
Daniel Dunbar898d5082008-09-30 01:06:03 +00002035}
2036
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002037/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002038/// object: objc_read_weak (id *src)
2039///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002040llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002041 llvm::Value *AddrWeakObj)
2042{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002043 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002044 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002045 AddrWeakObj, "weakread");
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002046 return read_weak;
2047}
2048
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002049/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2050/// objc_assign_weak (id src, id *dst)
2051///
2052void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2053 llvm::Value *src, llvm::Value *dst)
2054{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002055 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2056 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002057 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
2058 src, dst, "weakassign");
2059 return;
2060}
2061
Fariborz Jahanian58626502008-11-19 00:59:10 +00002062/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2063/// objc_assign_global (id src, id *dst)
2064///
2065void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2066 llvm::Value *src, llvm::Value *dst)
2067{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002068 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2069 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002070 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
2071 src, dst, "globalassign");
2072 return;
2073}
2074
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002075/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2076/// objc_assign_ivar (id src, id *dst)
2077///
2078void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2079 llvm::Value *src, llvm::Value *dst)
2080{
2081 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2082 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
2083 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
2084 src, dst, "assignivar");
2085 return;
2086}
2087
Fariborz Jahanian58626502008-11-19 00:59:10 +00002088/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2089/// objc_assign_strongCast (id src, id *dst)
2090///
2091void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2092 llvm::Value *src, llvm::Value *dst)
2093{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002094 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2095 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002096 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
2097 src, dst, "weakassign");
2098 return;
2099}
2100
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002101/* *** Private Interface *** */
2102
2103/// EmitImageInfo - Emit the image info marker used to encode some module
2104/// level information.
2105///
2106/// See: <rdr://4810609&4810587&4810587>
2107/// struct IMAGE_INFO {
2108/// unsigned version;
2109/// unsigned flags;
2110/// };
2111enum ImageInfoFlags {
2112 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
2113 eImageInfo_GarbageCollected = (1 << 1),
2114 eImageInfo_GCOnly = (1 << 2)
2115};
2116
2117void CGObjCMac::EmitImageInfo() {
2118 unsigned version = 0; // Version is unused?
2119 unsigned flags = 0;
2120
2121 // FIXME: Fix and continue?
2122 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2123 flags |= eImageInfo_GarbageCollected;
2124 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2125 flags |= eImageInfo_GCOnly;
2126
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002127 // Emitted as int[2];
2128 llvm::Constant *values[2] = {
2129 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2130 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2131 };
2132 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002133 llvm::GlobalVariable *GV =
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002134 new llvm::GlobalVariable(AT, true,
2135 llvm::GlobalValue::InternalLinkage,
2136 llvm::ConstantArray::get(AT, values, 2),
2137 "\01L_OBJC_IMAGE_INFO",
2138 &CGM.getModule());
2139
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002140 if (ObjCABI == 1) {
2141 GV->setSection("__OBJC, __image_info,regular");
2142 } else {
2143 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
2144 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002145
2146 UsedGlobals.push_back(GV);
2147}
2148
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002149
2150// struct objc_module {
2151// unsigned long version;
2152// unsigned long size;
2153// const char *name;
2154// Symtab symtab;
2155// };
2156
2157// FIXME: Get from somewhere
2158static const int ModuleVersion = 7;
2159
2160void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002161 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002162
2163 std::vector<llvm::Constant*> Values(4);
2164 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2165 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002166 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002167 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002168 Values[3] = EmitModuleSymbols();
2169
2170 llvm::GlobalVariable *GV =
2171 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
2172 llvm::GlobalValue::InternalLinkage,
2173 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
2174 Values),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002175 "\01L_OBJC_MODULES",
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002176 &CGM.getModule());
2177 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
2178 UsedGlobals.push_back(GV);
2179}
2180
2181llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002182 unsigned NumClasses = DefinedClasses.size();
2183 unsigned NumCategories = DefinedCategories.size();
2184
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002185 // Return null if no symbols were defined.
2186 if (!NumClasses && !NumCategories)
2187 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2188
2189 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002190 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2191 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2192 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2193 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2194
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002195 // The runtime expects exactly the list of defined classes followed
2196 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002197 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002198 for (unsigned i=0; i<NumClasses; i++)
2199 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2200 ObjCTypes.Int8PtrTy);
2201 for (unsigned i=0; i<NumCategories; i++)
2202 Symbols[NumClasses + i] =
2203 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2204 ObjCTypes.Int8PtrTy);
2205
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002206 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002207 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002208 NumClasses + NumCategories),
2209 Symbols);
2210
2211 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2212
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002213 llvm::GlobalVariable *GV =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002214 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002215 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002216 Init,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002217 "\01L_OBJC_SYMBOLS",
2218 &CGM.getModule());
2219 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
2220 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002221 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2222}
2223
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002224llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002225 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002226 LazySymbols.insert(ID->getIdentifier());
2227
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002228 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2229
2230 if (!Entry) {
2231 llvm::Constant *Casted =
2232 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2233 ObjCTypes.ClassPtrTy);
2234 Entry =
2235 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
2236 llvm::GlobalValue::InternalLinkage,
2237 Casted, "\01L_OBJC_CLASS_REFERENCES_",
2238 &CGM.getModule());
2239 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
2240 UsedGlobals.push_back(Entry);
2241 }
2242
2243 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002244}
2245
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002246llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002247 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2248
2249 if (!Entry) {
2250 llvm::Constant *Casted =
2251 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2252 ObjCTypes.SelectorPtrTy);
2253 Entry =
2254 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
2255 llvm::GlobalValue::InternalLinkage,
2256 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
2257 &CGM.getModule());
2258 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
2259 UsedGlobals.push_back(Entry);
2260 }
2261
2262 return Builder.CreateLoad(Entry, false, "tmp");
2263}
2264
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002265llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002266 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002267
2268 if (!Entry) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002269 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002270 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002271 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002272 llvm::GlobalValue::InternalLinkage,
2273 C, "\01L_OBJC_CLASS_NAME_",
2274 &CGM.getModule());
2275 Entry->setSection("__TEXT,__cstring,cstring_literals");
2276 UsedGlobals.push_back(Entry);
2277 }
2278
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002279 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002280}
2281
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002282llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002283 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
2284
2285 if (!Entry) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00002286 // FIXME: Avoid std::string copying.
2287 llvm::Constant *C = llvm::ConstantArray::get(Sel.getAsString());
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002288 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002289 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002290 llvm::GlobalValue::InternalLinkage,
2291 C, "\01L_OBJC_METH_VAR_NAME_",
2292 &CGM.getModule());
2293 Entry->setSection("__TEXT,__cstring,cstring_literals");
2294 UsedGlobals.push_back(Entry);
2295 }
2296
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002297 return getConstantGEP(Entry, 0, 0);
2298}
2299
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002300// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002301llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002302 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
2303}
2304
2305// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002306llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002307 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
2308}
2309
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002310llvm::Constant *CGObjCCommonMac::GetMethodVarType(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002311 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002312
2313 if (!Entry) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002314 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002315 Entry =
2316 new llvm::GlobalVariable(C->getType(), false,
2317 llvm::GlobalValue::InternalLinkage,
2318 C, "\01L_OBJC_METH_VAR_TYPE_",
2319 &CGM.getModule());
2320 Entry->setSection("__TEXT,__cstring,cstring_literals");
2321 UsedGlobals.push_back(Entry);
2322 }
2323
2324 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002325}
2326
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002327// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002328llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002329 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002330 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
2331 TypeStr);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002332 return GetMethodVarType(TypeStr);
2333}
2334
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002335// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002336llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002337 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
2338
2339 if (!Entry) {
2340 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
2341 Entry =
2342 new llvm::GlobalVariable(C->getType(), false,
2343 llvm::GlobalValue::InternalLinkage,
2344 C, "\01L_OBJC_PROP_NAME_ATTR_",
2345 &CGM.getModule());
2346 Entry->setSection("__TEXT,__cstring,cstring_literals");
2347 UsedGlobals.push_back(Entry);
2348 }
2349
2350 return getConstantGEP(Entry, 0, 0);
2351}
2352
2353// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002354// FIXME: This Decl should be more precise.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002355llvm::Constant *CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002356 const Decl *Container) {
2357 std::string TypeStr;
2358 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002359 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
2360}
2361
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002362void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
2363 const ObjCContainerDecl *CD,
2364 std::string &NameOut) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002365 // FIXME: Find the mangling GCC uses.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002366 NameOut = (D->isInstanceMethod() ? "-" : "+");
Chris Lattner077bf5e2008-11-24 03:33:13 +00002367 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002368 assert (CD && "Missing container decl in GetNameForMethod");
2369 NameOut += CD->getNameAsString();
Fariborz Jahanian52847332009-01-26 23:49:05 +00002370 // FIXME. For a method in a category, (CAT_NAME) is inserted here.
2371 // Right now! there is not enough info. to do this.
Chris Lattner077bf5e2008-11-24 03:33:13 +00002372 NameOut += ' ';
2373 NameOut += D->getSelector().getAsString();
2374 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002375}
2376
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002377/// GetFirstIvarInRecord - This routine returns the record for the
2378/// implementation of the fiven class OID. It also returns field
2379/// corresponding to the first ivar in the class in FIV. It also
2380/// returns the one before the first ivar.
2381///
2382const RecordDecl *CGObjCCommonMac::GetFirstIvarInRecord(
2383 const ObjCInterfaceDecl *OID,
2384 RecordDecl::field_iterator &FIV,
2385 RecordDecl::field_iterator &PIV) {
2386 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass());
2387 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
2388 RecordDecl::field_iterator ifield = RD->field_begin();
2389 RecordDecl::field_iterator pfield = RD->field_end();
2390 while (countSuperClassIvars-- > 0) {
2391 pfield = ifield;
2392 ++ifield;
2393 }
2394 FIV = ifield;
2395 PIV = pfield;
2396 return RD;
2397}
2398
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002399void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002400 EmitModuleInfo();
2401
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002402 // Emit the dummy bodies for any protocols which were referenced but
2403 // never defined.
2404 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
2405 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
2406 if (i->second->hasInitializer())
2407 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002408
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002409 std::vector<llvm::Constant*> Values(5);
2410 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
2411 Values[1] = GetClassName(i->first);
2412 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
2413 Values[3] = Values[4] =
2414 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
2415 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
2416 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
2417 Values));
2418 }
2419
2420 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002421 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002422 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002423 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002424 }
2425
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002426 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002427 llvm::GlobalValue *GV =
2428 new llvm::GlobalVariable(AT, false,
2429 llvm::GlobalValue::AppendingLinkage,
2430 llvm::ConstantArray::get(AT, Used),
2431 "llvm.used",
2432 &CGM.getModule());
2433
2434 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002435
2436 // Add assembler directives to add lazy undefined symbol references
2437 // for classes which are referenced but not defined. This is
2438 // important for correct linker interaction.
2439
2440 // FIXME: Uh, this isn't particularly portable.
2441 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00002442
2443 if (!CGM.getModule().getModuleInlineAsm().empty())
2444 s << "\n";
2445
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002446 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
2447 e = LazySymbols.end(); i != e; ++i) {
2448 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
2449 }
2450 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
2451 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002452 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002453 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
2454 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00002455
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002456 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002457}
2458
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002459CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002460 : CGObjCCommonMac(cgm),
2461 ObjCTypes(cgm)
2462{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00002463 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002464 ObjCABI = 2;
2465}
2466
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002467/* *** */
2468
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002469ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
2470: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002471{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002472 CodeGen::CodeGenTypes &Types = CGM.getTypes();
2473 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002474
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002475 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002476 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002477 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002478 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
2479
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002480 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002481 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002482 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002483
2484 // FIXME: It would be nice to unify this with the opaque type, so
2485 // that the IR comes out a bit cleaner.
2486 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
2487 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002488
2489 // I'm not sure I like this. The implicit coordination is a bit
2490 // gross. We should solve this in a reasonable fashion because this
2491 // is a pretty common task (match some runtime data structure with
2492 // an LLVM data structure).
2493
2494 // FIXME: This is leaked.
2495 // FIXME: Merge with rewriter code?
2496
2497 // struct _objc_super {
2498 // id self;
2499 // Class cls;
2500 // }
2501 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
2502 SourceLocation(),
2503 &Ctx.Idents.get("_objc_super"));
2504 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2505 Ctx.getObjCIdType(), 0, false));
2506 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2507 Ctx.getObjCClassType(), 0, false));
2508 RD->completeDefinition(Ctx);
2509
2510 SuperCTy = Ctx.getTagDeclType(RD);
2511 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
2512
2513 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002514 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
2515
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002516 // struct _prop_t {
2517 // char *name;
2518 // char *attributes;
2519 // }
2520 PropertyTy = llvm::StructType::get(Int8PtrTy,
2521 Int8PtrTy,
2522 NULL);
2523 CGM.getModule().addTypeName("struct._prop_t",
2524 PropertyTy);
2525
2526 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002527 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002528 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002529 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002530 // }
2531 PropertyListTy = llvm::StructType::get(IntTy,
2532 IntTy,
2533 llvm::ArrayType::get(PropertyTy, 0),
2534 NULL);
2535 CGM.getModule().addTypeName("struct._prop_list_t",
2536 PropertyListTy);
2537 // struct _prop_list_t *
2538 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
2539
2540 // struct _objc_method {
2541 // SEL _cmd;
2542 // char *method_type;
2543 // char *_imp;
2544 // }
2545 MethodTy = llvm::StructType::get(SelectorPtrTy,
2546 Int8PtrTy,
2547 Int8PtrTy,
2548 NULL);
2549 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002550
2551 // struct _objc_cache *
2552 CacheTy = llvm::OpaqueType::get();
2553 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
2554 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002555
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002556 // Property manipulation functions.
2557
2558 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
2559 std::vector<const llvm::Type*> Params;
2560 Params.push_back(ObjectPtrTy);
2561 Params.push_back(SelectorPtrTy);
2562 Params.push_back(LongTy);
2563 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2564 GetPropertyFn =
2565 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2566 Params,
2567 false),
2568 "objc_getProperty");
2569
2570 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
2571 Params.clear();
2572 Params.push_back(ObjectPtrTy);
2573 Params.push_back(SelectorPtrTy);
2574 Params.push_back(LongTy);
2575 Params.push_back(ObjectPtrTy);
2576 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2577 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2578 SetPropertyFn =
2579 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2580 Params,
2581 false),
2582 "objc_setProperty");
2583 // Enumeration mutation.
2584
2585 Params.clear();
2586 Params.push_back(ObjectPtrTy);
2587 EnumerationMutationFn =
2588 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2589 Params,
2590 false),
2591 "objc_enumerationMutation");
2592
2593 // gc's API
2594 // id objc_read_weak (id *)
2595 Params.clear();
2596 Params.push_back(PtrObjectPtrTy);
2597 GcReadWeakFn =
2598 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2599 Params,
2600 false),
2601 "objc_read_weak");
2602 // id objc_assign_weak (id, id *)
2603 Params.clear();
2604 Params.push_back(ObjectPtrTy);
2605 Params.push_back(PtrObjectPtrTy);
2606 GcAssignWeakFn =
2607 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2608 Params,
2609 false),
2610 "objc_assign_weak");
2611 GcAssignGlobalFn =
2612 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2613 Params,
2614 false),
2615 "objc_assign_global");
2616 GcAssignIvarFn =
2617 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2618 Params,
2619 false),
2620 "objc_assign_ivar");
2621 GcAssignStrongCastFn =
2622 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2623 Params,
2624 false),
2625 "objc_assign_strongCast");
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002626}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002627
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002628ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
2629 : ObjCCommonTypesHelper(cgm)
2630{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002631 // struct _objc_method_description {
2632 // SEL name;
2633 // char *types;
2634 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002635 MethodDescriptionTy =
2636 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002637 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002638 NULL);
2639 CGM.getModule().addTypeName("struct._objc_method_description",
2640 MethodDescriptionTy);
2641
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002642 // struct _objc_method_description_list {
2643 // int count;
2644 // struct _objc_method_description[1];
2645 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002646 MethodDescriptionListTy =
2647 llvm::StructType::get(IntTy,
2648 llvm::ArrayType::get(MethodDescriptionTy, 0),
2649 NULL);
2650 CGM.getModule().addTypeName("struct._objc_method_description_list",
2651 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002652
2653 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002654 MethodDescriptionListPtrTy =
2655 llvm::PointerType::getUnqual(MethodDescriptionListTy);
2656
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002657 // Protocol description structures
2658
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002659 // struct _objc_protocol_extension {
2660 // uint32_t size; // sizeof(struct _objc_protocol_extension)
2661 // struct _objc_method_description_list *optional_instance_methods;
2662 // struct _objc_method_description_list *optional_class_methods;
2663 // struct _objc_property_list *instance_properties;
2664 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002665 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002666 llvm::StructType::get(IntTy,
2667 MethodDescriptionListPtrTy,
2668 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002669 PropertyListPtrTy,
2670 NULL);
2671 CGM.getModule().addTypeName("struct._objc_protocol_extension",
2672 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002673
2674 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002675 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
2676
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002677 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002678
2679 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
2680 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2681
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002682 const llvm::Type *T =
2683 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
2684 LongTy,
2685 llvm::ArrayType::get(ProtocolTyHolder, 0),
2686 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002687 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
2688
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002689 // struct _objc_protocol {
2690 // struct _objc_protocol_extension *isa;
2691 // char *protocol_name;
2692 // struct _objc_protocol **_objc_protocol_list;
2693 // struct _objc_method_description_list *instance_methods;
2694 // struct _objc_method_description_list *class_methods;
2695 // }
2696 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002697 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002698 llvm::PointerType::getUnqual(ProtocolListTyHolder),
2699 MethodDescriptionListPtrTy,
2700 MethodDescriptionListPtrTy,
2701 NULL);
2702 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
2703
2704 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
2705 CGM.getModule().addTypeName("struct._objc_protocol_list",
2706 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002707 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002708 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
2709
2710 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002711 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002712 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002713
2714 // Class description structures
2715
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002716 // struct _objc_ivar {
2717 // char *ivar_name;
2718 // char *ivar_type;
2719 // int ivar_offset;
2720 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002721 IvarTy = llvm::StructType::get(Int8PtrTy,
2722 Int8PtrTy,
2723 IntTy,
2724 NULL);
2725 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
2726
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002727 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002728 IvarListTy = llvm::OpaqueType::get();
2729 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
2730 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
2731
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002732 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002733 MethodListTy = llvm::OpaqueType::get();
2734 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
2735 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
2736
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002737 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002738 ClassExtensionTy =
2739 llvm::StructType::get(IntTy,
2740 Int8PtrTy,
2741 PropertyListPtrTy,
2742 NULL);
2743 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
2744 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
2745
2746 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
2747
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002748 // struct _objc_class {
2749 // Class isa;
2750 // Class super_class;
2751 // char *name;
2752 // long version;
2753 // long info;
2754 // long instance_size;
2755 // struct _objc_ivar_list *ivars;
2756 // struct _objc_method_list *methods;
2757 // struct _objc_cache *cache;
2758 // struct _objc_protocol_list *protocols;
2759 // char *ivar_layout;
2760 // struct _objc_class_ext *ext;
2761 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002762 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
2763 llvm::PointerType::getUnqual(ClassTyHolder),
2764 Int8PtrTy,
2765 LongTy,
2766 LongTy,
2767 LongTy,
2768 IvarListPtrTy,
2769 MethodListPtrTy,
2770 CachePtrTy,
2771 ProtocolListPtrTy,
2772 Int8PtrTy,
2773 ClassExtensionPtrTy,
2774 NULL);
2775 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
2776
2777 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
2778 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
2779 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
2780
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002781 // struct _objc_category {
2782 // char *category_name;
2783 // char *class_name;
2784 // struct _objc_method_list *instance_method;
2785 // struct _objc_method_list *class_method;
2786 // uint32_t size; // sizeof(struct _objc_category)
2787 // struct _objc_property_list *instance_properties;// category's @property
2788 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002789 CategoryTy = llvm::StructType::get(Int8PtrTy,
2790 Int8PtrTy,
2791 MethodListPtrTy,
2792 MethodListPtrTy,
2793 ProtocolListPtrTy,
2794 IntTy,
2795 PropertyListPtrTy,
2796 NULL);
2797 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
2798
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002799 // Global metadata structures
2800
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002801 // struct _objc_symtab {
2802 // long sel_ref_cnt;
2803 // SEL *refs;
2804 // short cls_def_cnt;
2805 // short cat_def_cnt;
2806 // char *defs[cls_def_cnt + cat_def_cnt];
2807 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002808 SymtabTy = llvm::StructType::get(LongTy,
2809 SelectorPtrTy,
2810 ShortTy,
2811 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002812 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002813 NULL);
2814 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
2815 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
2816
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002817 // struct _objc_module {
2818 // long version;
2819 // long size; // sizeof(struct _objc_module)
2820 // char *name;
2821 // struct _objc_symtab* symtab;
2822 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002823 ModuleTy =
2824 llvm::StructType::get(LongTy,
2825 LongTy,
2826 Int8PtrTy,
2827 SymtabPtrTy,
2828 NULL);
2829 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002830
Daniel Dunbar49f66022008-09-24 03:38:44 +00002831 // Message send functions.
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002832
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002833 // id objc_msgSend (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002834 std::vector<const llvm::Type*> Params;
2835 Params.push_back(ObjectPtrTy);
2836 Params.push_back(SelectorPtrTy);
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002837 MessageSendFn =
2838 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2839 Params,
2840 true),
2841 "objc_msgSend");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002842
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002843 // id objc_msgSend_stret (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002844 Params.clear();
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002845 Params.push_back(ObjectPtrTy);
2846 Params.push_back(SelectorPtrTy);
2847 MessageSendStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002848 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2849 Params,
2850 true),
2851 "objc_msgSend_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002852
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002853 //
Daniel Dunbar5669e572008-10-17 03:24:53 +00002854 Params.clear();
2855 Params.push_back(ObjectPtrTy);
2856 Params.push_back(SelectorPtrTy);
2857 // FIXME: This should be long double on x86_64?
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002858 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
Daniel Dunbar5669e572008-10-17 03:24:53 +00002859 MessageSendFpretFn =
2860 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
2861 Params,
2862 true),
2863 "objc_msgSend_fpret");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002864
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002865 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002866 Params.clear();
2867 Params.push_back(SuperPtrTy);
2868 Params.push_back(SelectorPtrTy);
2869 MessageSendSuperFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002870 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2871 Params,
2872 true),
2873 "objc_msgSendSuper");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002874
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002875 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
2876 // SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002877 Params.clear();
2878 Params.push_back(Int8PtrTy);
2879 Params.push_back(SuperPtrTy);
2880 Params.push_back(SelectorPtrTy);
2881 MessageSendSuperStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002882 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2883 Params,
2884 true),
2885 "objc_msgSendSuper_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002886
2887 // There is no objc_msgSendSuper_fpret? How can that work?
2888 MessageSendSuperFpretFn = MessageSendSuperFn;
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002889
Anders Carlsson124526b2008-09-09 10:10:21 +00002890 // FIXME: This is the size of the setjmp buffer and should be
2891 // target specific. 18 is what's used on 32-bit X86.
2892 uint64_t SetJmpBufferSize = 18;
2893
2894 // Exceptions
2895 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00002896 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00002897
2898 ExceptionDataTy =
2899 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
2900 SetJmpBufferSize),
2901 StackPtrTy, NULL);
2902 CGM.getModule().addTypeName("struct._objc_exception_data",
2903 ExceptionDataTy);
2904
2905 Params.clear();
2906 Params.push_back(ObjectPtrTy);
2907 ExceptionThrowFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002908 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2909 Params,
2910 false),
2911 "objc_exception_throw");
Anders Carlsson124526b2008-09-09 10:10:21 +00002912
2913 Params.clear();
2914 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
2915 ExceptionTryEnterFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002916 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2917 Params,
2918 false),
2919 "objc_exception_try_enter");
Anders Carlsson124526b2008-09-09 10:10:21 +00002920 ExceptionTryExitFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002921 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2922 Params,
2923 false),
2924 "objc_exception_try_exit");
Anders Carlsson124526b2008-09-09 10:10:21 +00002925 ExceptionExtractFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002926 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2927 Params,
2928 false),
2929 "objc_exception_extract");
Anders Carlsson124526b2008-09-09 10:10:21 +00002930
2931 Params.clear();
2932 Params.push_back(ClassPtrTy);
2933 Params.push_back(ObjectPtrTy);
2934 ExceptionMatchFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002935 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
2936 Params,
2937 false),
2938 "objc_exception_match");
Chris Lattner10cac6f2008-11-15 21:26:17 +00002939
2940 // synchronized APIs
2941 // void objc_sync_enter (id)
2942 Params.clear();
2943 Params.push_back(ObjectPtrTy);
2944 SyncEnterFn =
2945 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2946 Params,
2947 false),
2948 "objc_sync_enter");
2949 // void objc_sync_exit (id)
2950 SyncExitFn =
2951 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2952 Params,
2953 false),
2954 "objc_sync_exit");
2955
Anders Carlsson124526b2008-09-09 10:10:21 +00002956
2957 Params.clear();
2958 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
2959 SetJmpFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002960 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
2961 Params,
2962 false),
2963 "_setjmp");
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002964
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002965}
2966
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002967ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002968: ObjCCommonTypesHelper(cgm)
2969{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002970 // struct _method_list_t {
2971 // uint32_t entsize; // sizeof(struct _objc_method)
2972 // uint32_t method_count;
2973 // struct _objc_method method_list[method_count];
2974 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002975 MethodListnfABITy = llvm::StructType::get(IntTy,
2976 IntTy,
2977 llvm::ArrayType::get(MethodTy, 0),
2978 NULL);
2979 CGM.getModule().addTypeName("struct.__method_list_t",
2980 MethodListnfABITy);
2981 // struct method_list_t *
2982 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002983
2984 // struct _protocol_t {
2985 // id isa; // NULL
2986 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002987 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002988 // const struct method_list_t * const instance_methods;
2989 // const struct method_list_t * const class_methods;
2990 // const struct method_list_t *optionalInstanceMethods;
2991 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002992 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002993 // const uint32_t size; // sizeof(struct _protocol_t)
2994 // const uint32_t flags; // = 0
2995 // }
2996
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002997 // Holder for struct _protocol_list_t *
2998 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2999
3000 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
3001 Int8PtrTy,
3002 llvm::PointerType::getUnqual(
3003 ProtocolListTyHolder),
3004 MethodListnfABIPtrTy,
3005 MethodListnfABIPtrTy,
3006 MethodListnfABIPtrTy,
3007 MethodListnfABIPtrTy,
3008 PropertyListPtrTy,
3009 IntTy,
3010 IntTy,
3011 NULL);
3012 CGM.getModule().addTypeName("struct._protocol_t",
3013 ProtocolnfABITy);
3014
Fariborz Jahanianda320092009-01-29 19:24:30 +00003015 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003016 // long protocol_count; // Note, this is 32/64 bit
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003017 // struct _protocol_t[protocol_count];
3018 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003019 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3020 llvm::ArrayType::get(
3021 ProtocolnfABITy, 0),
3022 NULL);
3023 CGM.getModule().addTypeName("struct._objc_protocol_list",
3024 ProtocolListnfABITy);
3025
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003026 // struct _objc_protocol_list*
3027 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003028
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003029 // FIXME! Is this doing the right thing?
3030 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3031 ProtocolListnfABIPtrTy);
3032
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003033 // struct _ivar_t {
3034 // unsigned long int *offset; // pointer to ivar offset location
3035 // char *name;
3036 // char *type;
3037 // uint32_t alignment;
3038 // uint32_t size;
3039 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003040 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3041 Int8PtrTy,
3042 Int8PtrTy,
3043 IntTy,
3044 IntTy,
3045 NULL);
3046 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3047
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003048 // struct _ivar_list_t {
3049 // uint32 entsize; // sizeof(struct _ivar_t)
3050 // uint32 count;
3051 // struct _iver_t list[count];
3052 // }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003053 IvarListnfABITy = llvm::StructType::get(IntTy,
3054 IntTy,
3055 llvm::ArrayType::get(
3056 IvarnfABITy, 0),
3057 NULL);
3058 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3059
3060 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003061
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003062 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003063 // uint32_t const flags;
3064 // uint32_t const instanceStart;
3065 // uint32_t const instanceSize;
3066 // uint32_t const reserved; // only when building for 64bit targets
3067 // const uint8_t * const ivarLayout;
3068 // const char *const name;
3069 // const struct _method_list_t * const baseMethods;
3070 // const struct _objc_protocol_list *const baseProtocols;
3071 // const struct _ivar_list_t *const ivars;
3072 // const uint8_t * const weakIvarLayout;
3073 // const struct _prop_list_t * const properties;
3074 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003075
3076 // FIXME. Add 'reserved' field in 64bit abi mode!
3077 ClassRonfABITy = llvm::StructType::get(IntTy,
3078 IntTy,
3079 IntTy,
3080 Int8PtrTy,
3081 Int8PtrTy,
3082 MethodListnfABIPtrTy,
3083 ProtocolListnfABIPtrTy,
3084 IvarListnfABIPtrTy,
3085 Int8PtrTy,
3086 PropertyListPtrTy,
3087 NULL);
3088 CGM.getModule().addTypeName("struct._class_ro_t",
3089 ClassRonfABITy);
3090
3091 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3092 std::vector<const llvm::Type*> Params;
3093 Params.push_back(ObjectPtrTy);
3094 Params.push_back(SelectorPtrTy);
3095 ImpnfABITy = llvm::PointerType::getUnqual(
3096 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3097
3098 // struct _class_t {
3099 // struct _class_t *isa;
3100 // struct _class_t * const superclass;
3101 // void *cache;
3102 // IMP *vtable;
3103 // struct class_ro_t *ro;
3104 // }
3105
3106 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3107 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3108 llvm::PointerType::getUnqual(ClassTyHolder),
3109 CachePtrTy,
3110 llvm::PointerType::getUnqual(ImpnfABITy),
3111 llvm::PointerType::getUnqual(
3112 ClassRonfABITy),
3113 NULL);
3114 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3115
3116 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3117 ClassnfABITy);
3118
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003119 // LLVM for struct _class_t *
3120 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3121
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003122 // struct _category_t {
3123 // const char * const name;
3124 // struct _class_t *const cls;
3125 // const struct _method_list_t * const instance_methods;
3126 // const struct _method_list_t * const class_methods;
3127 // const struct _protocol_list_t * const protocols;
3128 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003129 // }
3130 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003131 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003132 MethodListnfABIPtrTy,
3133 MethodListnfABIPtrTy,
3134 ProtocolListnfABIPtrTy,
3135 PropertyListPtrTy,
3136 NULL);
3137 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
3138
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003139}
3140
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003141llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
3142 FinishNonFragileABIModule();
3143
3144 return NULL;
3145}
3146
3147void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
3148 // nonfragile abi has no module definition.
3149 std::vector<llvm::Constant*> Used;
3150 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
3151 e = UsedGlobals.end(); i != e; ++i) {
3152 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
3153 }
3154
3155 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
3156 llvm::GlobalValue *GV =
3157 new llvm::GlobalVariable(AT, false,
3158 llvm::GlobalValue::AppendingLinkage,
3159 llvm::ConstantArray::get(AT, Used),
3160 "llvm.used",
3161 &CGM.getModule());
3162
3163 GV->setSection("llvm.metadata");
3164
3165}
3166
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003167// Metadata flags
3168enum MetaDataDlags {
3169 CLS = 0x0,
3170 CLS_META = 0x1,
3171 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003172 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003173 CLS_EXCEPTION = 0x20
3174};
3175/// BuildClassRoTInitializer - generate meta-data for:
3176/// struct _class_ro_t {
3177/// uint32_t const flags;
3178/// uint32_t const instanceStart;
3179/// uint32_t const instanceSize;
3180/// uint32_t const reserved; // only when building for 64bit targets
3181/// const uint8_t * const ivarLayout;
3182/// const char *const name;
3183/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00003184/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003185/// const struct _ivar_list_t *const ivars;
3186/// const uint8_t * const weakIvarLayout;
3187/// const struct _prop_list_t * const properties;
3188/// }
3189///
3190llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
3191 unsigned flags,
3192 unsigned InstanceStart,
3193 unsigned InstanceSize,
3194 const ObjCImplementationDecl *ID) {
3195 std::string ClassName = ID->getNameAsString();
3196 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
3197 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
3198 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
3199 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
3200 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianda320092009-01-29 19:24:30 +00003201 // FIXME. ivarLayout is currently null!
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003202 Values[ 3] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3203 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003204 // const struct _method_list_t * const baseMethods;
3205 std::vector<llvm::Constant*> Methods;
3206 std::string MethodListName("\01l_OBJC_$_");
3207 if (flags & CLS_META) {
3208 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
3209 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
3210 e = ID->classmeth_end(); i != e; ++i) {
3211 // Class methods should always be defined.
3212 Methods.push_back(GetMethodConstant(*i));
3213 }
3214 } else {
3215 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
3216 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
3217 e = ID->instmeth_end(); i != e; ++i) {
3218 // Instance methods should always be defined.
3219 Methods.push_back(GetMethodConstant(*i));
3220 }
Fariborz Jahanian939abce2009-01-28 22:46:49 +00003221 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
3222 e = ID->propimpl_end(); i != e; ++i) {
3223 ObjCPropertyImplDecl *PID = *i;
3224
3225 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
3226 ObjCPropertyDecl *PD = PID->getPropertyDecl();
3227
3228 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
3229 if (llvm::Constant *C = GetMethodConstant(MD))
3230 Methods.push_back(C);
3231 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
3232 if (llvm::Constant *C = GetMethodConstant(MD))
3233 Methods.push_back(C);
3234 }
3235 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003236 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003237 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003238 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003239
3240 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3241 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
3242 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
3243 + OID->getNameAsString(),
3244 OID->protocol_begin(),
3245 OID->protocol_end());
3246
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003247 if (flags & CLS_META)
3248 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3249 else
3250 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003251 // FIXME. weakIvarLayout is currently null.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003252 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003253 if (flags & CLS_META)
3254 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3255 else
3256 Values[ 9] =
3257 EmitPropertyList(
3258 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
3259 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003260 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
3261 Values);
3262 llvm::GlobalVariable *CLASS_RO_GV =
3263 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
3264 llvm::GlobalValue::InternalLinkage,
3265 Init,
3266 (flags & CLS_META) ?
3267 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
3268 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
3269 &CGM.getModule());
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003270 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003271 UsedGlobals.push_back(CLASS_RO_GV);
3272 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003273
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003274}
3275
3276/// BuildClassMetaData - This routine defines that to-level meta-data
3277/// for the given ClassName for:
3278/// struct _class_t {
3279/// struct _class_t *isa;
3280/// struct _class_t * const superclass;
3281/// void *cache;
3282/// IMP *vtable;
3283/// struct class_ro_t *ro;
3284/// }
3285///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003286llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
3287 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003288 llvm::Constant *IsAGV,
3289 llvm::Constant *SuperClassGV,
3290 llvm::Constant *ClassRoGV) {
3291 std::vector<llvm::Constant*> Values(5);
3292 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003293 Values[1] = SuperClassGV
3294 ? SuperClassGV
3295 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003296 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
3297 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
3298 Values[4] = ClassRoGV; // &CLASS_RO_GV
3299 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
3300 Values);
3301 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(ClassName);
3302 if (GV)
3303 GV->setInitializer(Init);
3304 else
3305 GV =
3306 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3307 llvm::GlobalValue::ExternalLinkage,
3308 Init,
3309 ClassName,
3310 &CGM.getModule());
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003311 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003312 UsedGlobals.push_back(GV);
3313 // FIXME! why?
3314 GV->setAlignment(32);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003315 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003316}
3317
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003318void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
3319 std::string ClassName = ID->getNameAsString();
3320 if (!ObjCEmptyCacheVar) {
3321 ObjCEmptyCacheVar = new llvm::GlobalVariable(
3322 ObjCTypes.CachePtrTy,
3323 false,
3324 llvm::GlobalValue::ExternalLinkage,
3325 0,
3326 "\01_objc_empty_cache",
3327 &CGM.getModule());
3328 UsedGlobals.push_back(ObjCEmptyCacheVar);
3329
3330 ObjCEmptyVtableVar = new llvm::GlobalVariable(
3331 llvm::PointerType::getUnqual(
3332 ObjCTypes.ImpnfABITy),
3333 false,
3334 llvm::GlobalValue::ExternalLinkage,
3335 0,
3336 "\01_objc_empty_vtable",
3337 &CGM.getModule());
3338 UsedGlobals.push_back(ObjCEmptyVtableVar);
3339 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003340 assert(ID->getClassInterface() &&
3341 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003342 uint32_t InstanceStart =
3343 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
3344 uint32_t InstanceSize = InstanceStart;
3345 uint32_t flags = CLS_META;
3346 std::string ObjCMetaClassName("\01_OBJC_METACLASS_$_");
3347 std::string ObjCClassName("\01_OBJC_CLASS_$_");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003348
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003349 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003350
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003351 if (IsClassHidden(ID->getClassInterface()))
3352 flags |= OBJC2_CLS_HIDDEN;
3353 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003354 // class is root
3355 flags |= CLS_ROOT;
3356 std::string SuperClassName = ObjCClassName + ClassName;
3357 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3358 if (!SuperClassGV)
3359 SuperClassGV =
3360 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3361 llvm::GlobalValue::ExternalLinkage,
3362 0,
3363 SuperClassName,
3364 &CGM.getModule());
3365 UsedGlobals.push_back(SuperClassGV);
3366 std::string IsAClassName = ObjCMetaClassName + ClassName;
3367 IsAGV = CGM.getModule().getGlobalVariable(IsAClassName);
3368 if (!IsAGV)
3369 IsAGV =
3370 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3371 llvm::GlobalValue::ExternalLinkage,
3372 0,
3373 IsAClassName,
3374 &CGM.getModule());
3375 UsedGlobals.push_back(IsAGV);
3376 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003377 // Has a root. Current class is not a root.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003378 std::string RootClassName =
3379 ID->getClassInterface()->getSuperClass()->getNameAsString();
3380 std::string SuperClassName = ObjCMetaClassName + RootClassName;
3381 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3382 if (!SuperClassGV)
3383 SuperClassGV =
3384 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3385 llvm::GlobalValue::ExternalLinkage,
3386 0,
3387 SuperClassName,
3388 &CGM.getModule());
3389 UsedGlobals.push_back(SuperClassGV);
3390 IsAGV = SuperClassGV;
3391 }
3392 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
3393 InstanceStart,
3394 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003395 std::string TClassName = ObjCMetaClassName + ClassName;
3396 llvm::GlobalVariable *MetaTClass =
3397 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV);
3398
3399 // Metadata for the class
3400 flags = CLS;
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003401 if (IsClassHidden(ID->getClassInterface()))
3402 flags |= OBJC2_CLS_HIDDEN;
3403 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003404 flags |= CLS_ROOT;
3405 SuperClassGV = 0;
3406 }
3407 else {
3408 // Has a root. Current class is not a root.
3409 std::string RootClassName =
3410 ID->getClassInterface()->getSuperClass()->getNameAsString();
3411 std::string SuperClassName = ObjCClassName + RootClassName;
3412 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3413 if (!SuperClassGV)
3414 SuperClassGV =
3415 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3416 llvm::GlobalValue::ExternalLinkage,
3417 0,
3418 SuperClassName,
3419 &CGM.getModule());
3420 UsedGlobals.push_back(SuperClassGV);
3421
3422 }
3423
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003424 InstanceStart = InstanceSize = 0;
3425 if (ObjCInterfaceDecl *OID =
3426 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface())) {
3427 // FIXME. Share this with the one in EmitIvarList.
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003428 const llvm::Type *InterfaceTy =
3429 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
3430 const llvm::StructLayout *Layout =
3431 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003432
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003433 RecordDecl::field_iterator firstField, lastField;
3434 const RecordDecl *RD = GetFirstIvarInRecord(OID, firstField, lastField);
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003435
3436 for (RecordDecl::field_iterator e = RD->field_end(),
3437 ifield = firstField; ifield != e; ++ifield)
3438 lastField = ifield;
3439
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003440 if (lastField != RD->field_end()) {
3441 FieldDecl *Field = *lastField;
3442 const llvm::Type *FieldTy =
3443 CGM.getTypes().ConvertTypeForMem(Field->getType());
3444 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3445 InstanceSize = Layout->getElementOffset(
3446 CGM.getTypes().getLLVMFieldNo(Field)) +
3447 Size;
3448 if (firstField == RD->field_end())
3449 InstanceStart = InstanceSize;
3450 else
3451 InstanceStart = Layout->getElementOffset(CGM.getTypes().
3452 getLLVMFieldNo(*firstField));
3453 }
3454 }
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003455 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003456 InstanceStart,
3457 InstanceSize,
3458 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003459
3460 TClassName = ObjCClassName + ClassName;
3461 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003462}
3463
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003464/// GenerateCategory - Build metadata for a category implementation.
3465/// struct _category_t {
3466/// const char * const name;
3467/// struct _class_t *const cls;
3468/// const struct _method_list_t * const instance_methods;
3469/// const struct _method_list_t * const class_methods;
3470/// const struct _protocol_list_t * const protocols;
3471/// const struct _prop_list_t * const properties;
3472/// }
3473///
3474void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD)
3475{
3476 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003477 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
3478 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003479 "_$_" + OCD->getNameAsString());
3480 std::string ExtClassName("\01_OBJC_CLASS_$_" + Interface->getNameAsString());
3481
3482 std::vector<llvm::Constant*> Values(6);
3483 Values[0] = GetClassName(OCD->getIdentifier());
3484 // meta-class entry symbol
3485 llvm::GlobalVariable *ClassGV =
3486 CGM.getModule().getGlobalVariable(ExtClassName);
3487 if (!ClassGV)
3488 ClassGV =
3489 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3490 llvm::GlobalValue::ExternalLinkage,
3491 0,
3492 ExtClassName,
3493 &CGM.getModule());
3494 UsedGlobals.push_back(ClassGV);
3495 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003496 std::vector<llvm::Constant*> Methods;
3497 std::string MethodListName(Prefix);
3498 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
3499 "_$_" + OCD->getNameAsString();
3500
3501 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
3502 e = OCD->instmeth_end(); i != e; ++i) {
3503 // Instance methods should always be defined.
3504 Methods.push_back(GetMethodConstant(*i));
3505 }
3506
3507 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003508 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003509 Methods);
3510
3511 MethodListName = Prefix;
3512 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
3513 OCD->getNameAsString();
3514 Methods.clear();
3515 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
3516 e = OCD->classmeth_end(); i != e; ++i) {
3517 // Class methods should always be defined.
3518 Methods.push_back(GetMethodConstant(*i));
3519 }
3520
3521 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003522 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003523 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003524 const ObjCCategoryDecl *Category =
3525 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanianeb732132009-01-29 23:23:06 +00003526 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
3527 + Interface->getNameAsString() + "_$_"
3528 + Category->getNameAsString(),
3529 Category->protocol_begin(),
3530 Category->protocol_end());
3531
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003532 std::string ExtName(Interface->getNameAsString() + "_$_" +
3533 OCD->getNameAsString());
3534 Values[5] =
3535 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
3536 OCD, Category, ObjCTypes);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003537 llvm::Constant *Init =
3538 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
3539 Values);
3540 llvm::GlobalVariable *GCATV
3541 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
3542 false,
3543 llvm::GlobalValue::InternalLinkage,
3544 Init,
3545 ExtCatName,
3546 &CGM.getModule());
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003547 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003548 UsedGlobals.push_back(GCATV);
3549 DefinedCategories.push_back(GCATV);
3550}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003551
3552/// GetMethodConstant - Return a struct objc_method constant for the
3553/// given method if it has been defined. The result is null if the
3554/// method has not been defined. The return value has type MethodPtrTy.
3555llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
3556 const ObjCMethodDecl *MD) {
3557 // FIXME: Use DenseMap::lookup
3558 llvm::Function *Fn = MethodDefinitions[MD];
3559 if (!Fn)
3560 return 0;
3561
3562 std::vector<llvm::Constant*> Method(3);
3563 Method[0] =
3564 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
3565 ObjCTypes.SelectorPtrTy);
3566 Method[1] = GetMethodVarType(MD);
3567 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
3568 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
3569}
3570
3571/// EmitMethodList - Build meta-data for method declarations
3572/// struct _method_list_t {
3573/// uint32_t entsize; // sizeof(struct _objc_method)
3574/// uint32_t method_count;
3575/// struct _objc_method method_list[method_count];
3576/// }
3577///
3578llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
3579 const std::string &Name,
3580 const char *Section,
3581 const ConstantVector &Methods) {
3582 // Return null for empty list.
3583 if (Methods.empty())
3584 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
3585
3586 std::vector<llvm::Constant*> Values(3);
3587 // sizeof(struct _objc_method)
3588 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy);
3589 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3590 // method_count
3591 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
3592 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
3593 Methods.size());
3594 Values[2] = llvm::ConstantArray::get(AT, Methods);
3595 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
3596
3597 llvm::GlobalVariable *GV =
3598 new llvm::GlobalVariable(Init->getType(), false,
3599 llvm::GlobalValue::InternalLinkage,
3600 Init,
3601 Name,
3602 &CGM.getModule());
3603 GV->setSection(Section);
3604 UsedGlobals.push_back(GV);
3605 return llvm::ConstantExpr::getBitCast(GV,
3606 ObjCTypes.MethodListnfABIPtrTy);
3607}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003608
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003609llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
3610 const ObjCImplementationDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003611 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003612 unsigned long int Offset) {
3613
3614 std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.'
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003615 + Ivar->getNameAsString());
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003616 llvm::Constant *Init = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
3617
3618 llvm::GlobalVariable *IvarOffsetGV =
3619 CGM.getModule().getGlobalVariable(ExternalName);
3620 if (IvarOffsetGV) {
3621 // ivar offset symbol already built due to user code referencing it.
3622 IvarOffsetGV->setInitializer(Init);
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003623 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003624 return IvarOffsetGV;
3625 }
3626
3627 IvarOffsetGV =
3628 new llvm::GlobalVariable(Init->getType(),
3629 false,
3630 llvm::GlobalValue::ExternalLinkage,
3631 Init,
3632 ExternalName,
3633 &CGM.getModule());
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003634 // @private and @package have hidden visibility.
3635 bool globalVisibility = (Ivar->getAccessControl() == ObjCIvarDecl::Public ||
3636 Ivar->getAccessControl() == ObjCIvarDecl::Protected);
3637 if (!globalVisibility)
3638 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003639 IvarOffsetGV->setSection("__DATA, __objc_const");
3640 UsedGlobals.push_back(IvarOffsetGV);
3641
3642 return llvm::ConstantExpr::getBitCast(
3643 IvarOffsetGV,
3644 llvm::PointerType::getUnqual(ObjCTypes.LongTy));
3645}
3646
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003647/// EmitIvarList - Emit the ivar list for the given
3648/// implementation. If ForClass is true the list of class ivars
3649/// (i.e. metaclass ivars) is emitted, otherwise the list of
3650/// interface ivars will be emitted. The return value has type
3651/// IvarListnfABIPtrTy.
3652/// struct _ivar_t {
3653/// unsigned long int *offset; // pointer to ivar offset location
3654/// char *name;
3655/// char *type;
3656/// uint32_t alignment;
3657/// uint32_t size;
3658/// }
3659/// struct _ivar_list_t {
3660/// uint32 entsize; // sizeof(struct _ivar_t)
3661/// uint32 count;
3662/// struct _iver_t list[count];
3663/// }
3664///
3665llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
3666 const ObjCImplementationDecl *ID) {
3667
3668 std::vector<llvm::Constant*> Ivars, Ivar(5);
3669
3670 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3671 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
3672
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003673 // FIXME. Consolidate this with similar code in GenerateClass.
3674 const llvm::Type *InterfaceTy =
3675 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(
3676 const_cast<ObjCInterfaceDecl*>(OID)));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003677 const llvm::StructLayout *Layout =
3678 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003679
3680 RecordDecl::field_iterator i,p;
3681 const RecordDecl *RD = GetFirstIvarInRecord(OID, i,p);
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003682 ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin();
3683
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003684 for (RecordDecl::field_iterator e = RD->field_end(); i != e; ++i) {
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003685 FieldDecl *Field = *i;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003686 unsigned long offset = Layout->getElementOffset(CGM.getTypes().
3687 getLLVMFieldNo(Field));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003688 const ObjCIvarDecl *ivarDecl = *I++;
3689 Ivar[0] = EmitIvarOffsetVar(ID, ivarDecl, offset);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003690 if (Field->getIdentifier())
3691 Ivar[1] = GetMethodVarName(Field->getIdentifier());
3692 else
3693 Ivar[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3694 std::string TypeStr;
3695 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3696 Ivar[2] = GetMethodVarType(TypeStr);
3697 const llvm::Type *FieldTy =
3698 CGM.getTypes().ConvertTypeForMem(Field->getType());
3699 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3700 unsigned Align = CGM.getContext().getPreferredTypeAlign(
3701 Field->getType().getTypePtr()) >> 3;
3702 Align = llvm::Log2_32(Align);
3703 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Fariborz Jahanian07236ba2009-01-27 22:27:56 +00003704 // NOTE. Size of a bitfield does not match gcc's, because of the way
3705 // bitfields are treated special in each. But I am told that 'size'
3706 // for bitfield ivars is ignored by the runtime so it does not matter.
3707 // (even if it matters, some day, there is enough info. to get the bitfield
3708 // right!
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003709 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3710 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
3711 }
3712 // Return null for empty list.
3713 if (Ivars.empty())
3714 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3715 std::vector<llvm::Constant*> Values(3);
3716 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy);
3717 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3718 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
3719 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
3720 Ivars.size());
3721 Values[2] = llvm::ConstantArray::get(AT, Ivars);
3722 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
3723 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
3724 llvm::GlobalVariable *GV =
3725 new llvm::GlobalVariable(Init->getType(), false,
3726 llvm::GlobalValue::InternalLinkage,
3727 Init,
3728 Prefix + OID->getNameAsString(),
3729 &CGM.getModule());
3730
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003731 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003732
3733 UsedGlobals.push_back(GV);
3734 return llvm::ConstantExpr::getBitCast(GV,
3735 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003736}
3737
3738llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
3739 const ObjCProtocolDecl *PD) {
3740 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
3741
3742 if (!Entry) {
3743 // We use the initializer as a marker of whether this is a forward
3744 // reference or not. At module finalization we add the empty
3745 // contents for protocols which were referenced but never defined.
3746 Entry =
3747 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
3748 llvm::GlobalValue::ExternalLinkage,
3749 0,
3750 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(),
3751 &CGM.getModule());
3752 Entry->setSection("__DATA,__datacoal_nt,coalesced");
3753 UsedGlobals.push_back(Entry);
3754 // FIXME: Is this necessary? Why only for protocol?
3755 Entry->setAlignment(4);
3756 }
3757
3758 return Entry;
3759}
3760
3761/// GetOrEmitProtocol - Generate the protocol meta-data:
3762/// @code
3763/// struct _protocol_t {
3764/// id isa; // NULL
3765/// const char * const protocol_name;
3766/// const struct _protocol_list_t * protocol_list; // super protocols
3767/// const struct method_list_t * const instance_methods;
3768/// const struct method_list_t * const class_methods;
3769/// const struct method_list_t *optionalInstanceMethods;
3770/// const struct method_list_t *optionalClassMethods;
3771/// const struct _prop_list_t * properties;
3772/// const uint32_t size; // sizeof(struct _protocol_t)
3773/// const uint32_t flags; // = 0
3774/// }
3775/// @endcode
3776///
3777
3778llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
3779 const ObjCProtocolDecl *PD) {
3780 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
3781
3782 // Early exit if a defining object has already been generated.
3783 if (Entry && Entry->hasInitializer())
3784 return Entry;
3785
3786 const char *ProtocolName = PD->getNameAsCString();
3787
3788 // Construct method lists.
3789 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
3790 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
3791 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
3792 e = PD->instmeth_end(); i != e; ++i) {
3793 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003794 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003795 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
3796 OptInstanceMethods.push_back(C);
3797 } else {
3798 InstanceMethods.push_back(C);
3799 }
3800 }
3801
3802 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
3803 e = PD->classmeth_end(); i != e; ++i) {
3804 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003805 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003806 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
3807 OptClassMethods.push_back(C);
3808 } else {
3809 ClassMethods.push_back(C);
3810 }
3811 }
3812
3813 std::vector<llvm::Constant*> Values(10);
3814 // isa is NULL
3815 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
3816 Values[1] = GetClassName(PD->getIdentifier());
3817 Values[2] = EmitProtocolList(
3818 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
3819 PD->protocol_begin(),
3820 PD->protocol_end());
3821
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003822 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00003823 + PD->getNameAsString(),
3824 "__DATA, __objc_const",
3825 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003826 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00003827 + PD->getNameAsString(),
3828 "__DATA, __objc_const",
3829 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003830 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00003831 + PD->getNameAsString(),
3832 "__DATA, __objc_const",
3833 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003834 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00003835 + PD->getNameAsString(),
3836 "__DATA, __objc_const",
3837 OptClassMethods);
3838 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
3839 0, PD, ObjCTypes);
3840 uint32_t Size =
3841 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy);
3842 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3843 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
3844 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
3845 Values);
3846
3847 if (Entry) {
3848 // Already created, fix the linkage and update the initializer.
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00003849 Entry->setLinkage(llvm::GlobalValue::WeakLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003850 Entry->setInitializer(Init);
3851 } else {
3852 Entry =
3853 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00003854 llvm::GlobalValue::WeakLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00003855 Init,
3856 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName,
3857 &CGM.getModule());
3858 Entry->setSection("__DATA,__datacoal_nt,coalesced");
3859 // FIXME: Is this necessary? Why only for protocol?
3860 Entry->setAlignment(4);
3861 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00003862 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
3863
3864 // Use this protocol meta-data to build protocol list table in section
3865 // __DATA, __objc_protolist
3866 llvm::Type *ptype = llvm::PointerType::getUnqual(ObjCTypes.ProtocolnfABITy);
3867 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
3868 ptype, false,
3869 llvm::GlobalValue::WeakLinkage,
3870 Entry,
3871 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
3872 +ProtocolName,
3873 &CGM.getModule());
3874 PTGV->setSection("__DATA, __objc_protolist");
3875 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
3876 UsedGlobals.push_back(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003877 return Entry;
3878}
3879
3880/// EmitProtocolList - Generate protocol list meta-data:
3881/// @code
3882/// struct _protocol_list_t {
3883/// long protocol_count; // Note, this is 32/64 bit
3884/// struct _protocol_t[protocol_count];
3885/// }
3886/// @endcode
3887///
3888llvm::Constant *
3889CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
3890 ObjCProtocolDecl::protocol_iterator begin,
3891 ObjCProtocolDecl::protocol_iterator end) {
3892 std::vector<llvm::Constant*> ProtocolRefs;
3893
3894 for (; begin != end; ++begin)
3895 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
3896
3897 // Just return null for empty protocol lists
3898 if (ProtocolRefs.empty())
3899 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
3900
3901 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
3902 if (GV)
3903 return GV;
3904 // This list is null terminated.
3905 ProtocolRefs.push_back(llvm::Constant::getNullValue(
3906 ObjCTypes.ProtocolListnfABIPtrTy));
3907
3908 std::vector<llvm::Constant*> Values(2);
3909 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
3910 Values[1] =
3911 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolListnfABIPtrTy,
3912 ProtocolRefs.size()),
3913 ProtocolRefs);
3914
3915 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
3916 GV = new llvm::GlobalVariable(Init->getType(), false,
3917 llvm::GlobalValue::InternalLinkage,
3918 Init,
3919 Name,
3920 &CGM.getModule());
3921 GV->setSection("__DATA, __objc_const");
3922 UsedGlobals.push_back(GV);
3923 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
3924}
3925
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003926/// GetMethodDescriptionConstant - This routine build following meta-data:
3927/// struct _objc_method {
3928/// SEL _cmd;
3929/// char *method_type;
3930/// char *_imp;
3931/// }
3932
3933llvm::Constant *
3934CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
3935 std::vector<llvm::Constant*> Desc(3);
3936 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
3937 ObjCTypes.SelectorPtrTy);
3938 Desc[1] = GetMethodVarType(MD);
3939 // FIXME. This is really always NULL?
3940 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3941 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
3942}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003943/* *** */
3944
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003945CodeGen::CGObjCRuntime *
3946CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00003947 return new CGObjCMac(CGM);
3948}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003949
3950CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003951CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003952 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003953}