blob: b5e380496450adb46729b17ddc783686d0f3b5f0 [file] [log] [blame]
Daniel Dunbar8c85fac2008-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 Dunbar1be1df32008-08-11 21:35:06 +000015
16#include "CodeGenModule.h"
Daniel Dunbarace33292008-08-16 03:19:19 +000017#include "CodeGenFunction.h"
Daniel Dunbardaf4ad42008-08-12 00:12:39 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000019#include "clang/AST/Decl.h"
Daniel Dunbarcffcdac2008-08-13 03:21:16 +000020#include "clang/AST/DeclObjC.h"
Daniel Dunbar1be1df32008-08-11 21:35:06 +000021#include "clang/Basic/LangOptions.h"
22
Daniel Dunbardaf4ad42008-08-12 00:12:39 +000023#include "llvm/Module.h"
Daniel Dunbar35b777f2008-10-29 22:36:39 +000024#include "llvm/ADT/DenseSet.h"
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +000025#include "llvm/Target/TargetData.h"
Daniel Dunbarace33292008-08-16 03:19:19 +000026#include <sstream>
Daniel Dunbar8c85fac2008-08-11 02:45:11 +000027
28using namespace clang;
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +000029using namespace CodeGen;
Daniel Dunbar8c85fac2008-08-11 02:45:11 +000030
31namespace {
Daniel Dunbardaf4ad42008-08-12 00:12:39 +000032
Daniel Dunbarfe131f02008-08-27 02:31:56 +000033 typedef std::vector<llvm::Constant*> ConstantVector;
34
Daniel Dunbarcffcdac2008-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 Jahanian48543f52009-01-21 22:04:16 +000038class ObjCCommonTypesHelper {
39protected:
40 CodeGen::CodeGenModule &CGM;
Daniel Dunbardaf4ad42008-08-12 00:12:39 +000041
Daniel Dunbardaf4ad42008-08-12 00:12:39 +000042public:
Daniel Dunbarb050fa62008-08-21 04:36:09 +000043 const llvm::Type *ShortTy, *IntTy, *LongTy;
44 const llvm::Type *Int8PtrTy;
Fariborz Jahanian48543f52009-01-21 22:04:16 +000045
Daniel Dunbar6fa3daf2008-08-12 05:28:47 +000046 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
47 const llvm::Type *ObjectPtrTy;
Fariborz Jahanianc192d4d2008-11-18 20:18:11 +000048
49 /// PtrObjectPtrTy - LLVM type for id *
50 const llvm::Type *PtrObjectPtrTy;
51
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +000052 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar6fa3daf2008-08-12 05:28:47 +000053 const llvm::Type *SelectorPtrTy;
Daniel Dunbarcffcdac2008-08-13 03:21:16 +000054 /// ProtocolPtrTy - LLVM type for external protocol handles
55 /// (typeof(Protocol))
56 const llvm::Type *ExternalProtocolPtrTy;
Fariborz Jahanian48543f52009-01-21 22:04:16 +000057
Daniel Dunbar0ed60b02008-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 Jahanian48543f52009-01-21 22:04:16 +000062
Daniel Dunbar15245e52008-08-23 04:28:29 +000063 /// SuperTy - LLVM type for struct objc_super.
64 const llvm::StructType *SuperTy;
Daniel Dunbar87062ff2008-08-23 09:25:55 +000065 /// SuperPtrTy - LLVM type for struct objc_super *.
66 const llvm::Type *SuperPtrTy;
Fariborz Jahanian48543f52009-01-21 22:04:16 +000067
Fariborz Jahaniand0374812009-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 Jahanian781f2732009-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 Jahanian4b161702009-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 Jahanian48543f52009-01-21 22:04:16 +0000105 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
106 ~ObjCCommonTypesHelper(){}
107};
Daniel Dunbar15245e52008-08-23 04:28:29 +0000108
Fariborz Jahanian48543f52009-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 Dunbarb8fe21b2008-08-12 06:48:42 +0000119 /// SymtabTy - LLVM type for struct objc_symtab.
120 const llvm::StructType *SymtabTy;
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000121 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
122 const llvm::Type *SymtabPtrTy;
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000123 /// ModuleTy - LLVM type for struct objc_module.
124 const llvm::StructType *ModuleTy;
Daniel Dunbar5eec6142008-08-12 03:39:23 +0000125
Daniel Dunbarcffcdac2008-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 Dunbarcffcdac2008-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 Dunbar4246a8b2008-08-22 20:34:54 +0000149 /// CategoryTy - LLVM type for struct objc_category.
150 const llvm::StructType *CategoryTy;
Daniel Dunbarb050fa62008-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 Dunbarb050fa62008-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 Dunbarb050fa62008-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 Carlsson9acb0a42008-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 Lattnerdd978702008-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 Dunbardaf4ad42008-08-12 00:12:39 +0000197public:
198 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000199 ~ObjCTypesHelper() {}
Daniel Dunbaraecef4c2008-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 Dunbardaf4ad42008-08-12 00:12:39 +0000213};
214
Fariborz Jahaniand0374812009-01-22 23:02:58 +0000215/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000216/// modern abi
Fariborz Jahaniand0374812009-01-22 23:02:58 +0000217class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000218public:
Fariborz Jahanian781f2732009-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 Jahanianc2a1c3e2009-01-23 23:53:38 +0000237 // ClassnfABIPtrTy - LLVM for struct _class_t*
238 const llvm::Type *ClassnfABIPtrTy;
239
Fariborz Jahanian781f2732009-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 Jahaniand0374812009-01-22 23:02:58 +0000258 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
259 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanian48543f52009-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 Dunbardaf4ad42008-08-12 00:12:39 +0000266 unsigned ObjCABI;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000267
Daniel Dunbar8ede0052008-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 Jahanian48543f52009-01-21 22:04:16 +0000277
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000278 /// ClassNames - uniqued class names.
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000279 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000280
Daniel Dunbar5eec6142008-08-12 03:39:23 +0000281 /// MethodVarNames - uniqued method variable names.
282 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000283
Daniel Dunbarcffcdac2008-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 Jahanian48543f52009-01-21 22:04:16 +0000287
Daniel Dunbar12996f52008-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 Jahanian48543f52009-01-21 22:04:16 +0000291
Daniel Dunbara6eb6b72008-08-23 00:19:03 +0000292 /// PropertyNames - uniqued method variable names.
293 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000294
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000295 /// ClassReferences - uniqued class references.
296 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000297
Daniel Dunbar5eec6142008-08-12 03:39:23 +0000298 /// SelectorReferences - uniqued selector references.
299 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000300
Daniel Dunbarcffcdac2008-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 Jahanian48543f52009-01-21 22:04:16 +0000305
Daniel Dunbar35b777f2008-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 Jahanian48543f52009-01-21 22:04:16 +0000309
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000310 /// DefinedClasses - List of defined classes.
311 std::vector<llvm::GlobalValue*> DefinedClasses;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000312
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000313 /// DefinedCategories - List of defined categories.
314 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000315
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000316 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000317 /// to prevent them from being clobbered.
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000318 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000319
Fariborz Jahanian32b5ea22009-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 Jahanian4c1e4612009-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 Jahanianf2a94cd2009-01-28 19:12:34 +0000351 const RecordDecl *GetFirstIvarInRecord(const ObjCInterfaceDecl *OID,
352 RecordDecl::field_iterator &FIV,
353 RecordDecl::field_iterator &PIV);
Fariborz Jahanian7b709bb2009-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 Jahanian5fedf4f2009-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 Jahanianf00a1fa2009-01-30 23:15:42 +0000366 /// GetPointerAlign - get alignment of a pointer.
367 uint32_t GetPointerAlign(void) {
368 return CGM.getContext().getTypeAlign(CGM.getContext().VoidPtrTy) >> 3;
369 }
370
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000371public:
372 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
373 { }
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000374
375 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +0000376
377 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
378 const ObjCContainerDecl *CD=0);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +0000379
380 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
381
382 /// GetOrEmitProtocol - Get the protocol object for the given
383 /// declaration, emitting it if necessary. The return value has type
384 /// ProtocolPtrTy.
385 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
386
387 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
388 /// object for the given declaration, emitting it if needed. These
389 /// forward references will be filled in with empty bodies if no
390 /// definition is seen. The return value has type ProtocolPtrTy.
391 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000392};
393
394class CGObjCMac : public CGObjCCommonMac {
395private:
396 ObjCTypesHelper ObjCTypes;
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000397 /// EmitImageInfo - Emit the image info marker used to encode some module
398 /// level information.
399 void EmitImageInfo();
400
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000401 /// EmitModuleInfo - Another marker encoding module level
402 /// information.
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000403 void EmitModuleInfo();
404
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000405 /// EmitModuleSymols - Emit module symbols, the list of defined
406 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +0000407 llvm::Constant *EmitModuleSymbols();
408
Daniel Dunbar1be1df32008-08-11 21:35:06 +0000409 /// FinishModule - Write out global data structures at the end of
410 /// processing a translation unit.
411 void FinishModule();
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000412
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000413 /// EmitClassExtension - Generate the class extension structure used
414 /// to store the weak ivar layout and properties. The return value
415 /// has type ClassExtensionPtrTy.
416 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
417
418 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
419 /// for the given class.
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000420 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000421 const ObjCInterfaceDecl *ID);
422
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000423 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000424 QualType ResultType,
425 Selector Sel,
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000426 llvm::Value *Arg0,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000427 QualType Arg0Ty,
428 bool IsSuper,
429 const CallArgList &CallArgs);
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000430
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000431 /// EmitIvarList - Emit the ivar list for the given
432 /// implementation. If ForClass is true the list of class ivars
433 /// (i.e. metaclass ivars) is emitted, otherwise the list of
434 /// interface ivars will be emitted. The return value has type
435 /// IvarListPtrTy.
436 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +0000437 bool ForClass);
438
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000439 /// EmitMetaClass - Emit a forward reference to the class structure
440 /// for the metaclass of the given interface. The return value has
441 /// type ClassPtrTy.
442 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
443
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000444 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000445 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000446 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
447 llvm::Constant *Protocols,
Daniel Dunbar12996f52008-08-26 21:51:14 +0000448 const llvm::Type *InterfaceTy,
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000449 const ConstantVector &Methods);
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +0000450
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000451 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +0000452
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000453 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000454
455 /// EmitMethodList - Emit the method list for the given
Daniel Dunbar6b57d432008-08-26 08:29:31 +0000456 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar4246a8b2008-08-22 20:34:54 +0000457 llvm::Constant *EmitMethodList(const std::string &Name,
458 const char *Section,
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000459 const ConstantVector &Methods);
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000460
461 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000462 /// method declarations.
463 /// - TypeName: The name for the type containing the methods.
464 /// - IsProtocol: True iff these methods are for a protocol.
465 /// - ClassMethds: True iff these are class methods.
466 /// - Required: When true, only "required" methods are
467 /// listed. Similarly, when false only "optional" methods are
468 /// listed. For classes this should always be true.
469 /// - begin, end: The method list to output.
470 ///
471 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000472 llvm::Constant *EmitMethodDescList(const std::string &Name,
473 const char *Section,
474 const ConstantVector &Methods);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000475
Daniel Dunbar35b777f2008-10-29 22:36:39 +0000476 /// GetOrEmitProtocol - Get the protocol object for the given
477 /// declaration, emitting it if necessary. The return value has type
478 /// ProtocolPtrTy.
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +0000479 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar35b777f2008-10-29 22:36:39 +0000480
481 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
482 /// object for the given declaration, emitting it if needed. These
483 /// forward references will be filled in with empty bodies if no
484 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +0000485 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar35b777f2008-10-29 22:36:39 +0000486
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000487 /// EmitProtocolExtension - Generate the protocol extension
488 /// structure used to store optional instance and class methods, and
489 /// protocol properties. The return value has type
490 /// ProtocolExtensionPtrTy.
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000491 llvm::Constant *
492 EmitProtocolExtension(const ObjCProtocolDecl *PD,
493 const ConstantVector &OptInstanceMethods,
494 const ConstantVector &OptClassMethods);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000495
496 /// EmitProtocolList - Generate the list of referenced
497 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar67e778b2008-08-21 21:57:41 +0000498 llvm::Constant *EmitProtocolList(const std::string &Name,
499 ObjCProtocolDecl::protocol_iterator begin,
500 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000501
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000502 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
503 /// for the given selector.
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000504 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +0000505
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +0000506 public:
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000507 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000508
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000509 virtual llvm::Function *ModuleInitFunction();
510
Daniel Dunbara04840b2008-08-23 03:46:30 +0000511 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000512 QualType ResultType,
513 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000514 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000515 bool IsClassMessage,
516 const CallArgList &CallArgs);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000517
Daniel Dunbara04840b2008-08-23 03:46:30 +0000518 virtual CodeGen::RValue
519 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000520 QualType ResultType,
521 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000522 const ObjCInterfaceDecl *Class,
523 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000524 bool IsClassMessage,
525 const CallArgList &CallArgs);
Daniel Dunbar434627a2008-08-16 00:25:02 +0000526
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000527 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000528 const ObjCInterfaceDecl *ID);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000529
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000530 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000531
Daniel Dunbarac93e472008-08-15 22:20:32 +0000532 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000533
Daniel Dunbarac93e472008-08-15 22:20:32 +0000534 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000535
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000536 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000537 const ObjCProtocolDecl *PD);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +0000538
Daniel Dunbarf7103722008-09-24 03:38:44 +0000539 virtual llvm::Function *GetPropertyGetFunction();
540 virtual llvm::Function *GetPropertySetFunction();
Anders Carlsson58d16242008-08-31 04:05:03 +0000541 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlssonb01a2112008-09-09 10:04:29 +0000542
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +0000543 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
544 const Stmt &S);
Anders Carlssonb01a2112008-09-09 10:04:29 +0000545 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
546 const ObjCAtThrowStmt &S);
Fariborz Jahanian252d87f2008-11-18 22:37:34 +0000547 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian3305ad32008-11-18 21:45:40 +0000548 llvm::Value *AddrWeakObj);
Fariborz Jahanian252d87f2008-11-18 22:37:34 +0000549 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
550 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian17958902008-11-19 00:59:10 +0000551 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
552 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianf310b592008-11-20 19:23:36 +0000553 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
554 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian17958902008-11-19 00:59:10 +0000555 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
556 llvm::Value *src, llvm::Value *dest);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000557};
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000558
Fariborz Jahaniand0374812009-01-22 23:02:58 +0000559class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000560private:
Fariborz Jahaniand0374812009-01-22 23:02:58 +0000561 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000562 llvm::GlobalVariable* ObjCEmptyCacheVar;
563 llvm::GlobalVariable* ObjCEmptyVtableVar;
564
565 /// FinishNonFragileABIModule - Write out global data structures at the end of
566 /// processing a translation unit.
567 void FinishNonFragileABIModule();
568
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +0000569 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
570 unsigned InstanceStart,
571 unsigned InstanceSize,
572 const ObjCImplementationDecl *ID);
Fariborz Jahanian06726462009-01-24 21:21:53 +0000573 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
574 llvm::Constant *IsAGV,
575 llvm::Constant *SuperClassGV,
576 llvm::Constant *ClassRoGV);
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +0000577
578 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
579
Fariborz Jahanian151747b2009-01-30 00:46:37 +0000580 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
581
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +0000582 /// EmitMethodList - Emit the method list for the given
583 /// implementation. The return value has type MethodListnfABITy.
584 llvm::Constant *EmitMethodList(const std::string &Name,
585 const char *Section,
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +0000586 const ConstantVector &Methods);
587 /// EmitIvarList - Emit the ivar list for the given
588 /// implementation. If ForClass is true the list of class ivars
589 /// (i.e. metaclass ivars) is emitted, otherwise the list of
590 /// interface ivars will be emitted. The return value has type
591 /// IvarListnfABIPtrTy.
592 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +0000593
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +0000594 llvm::Constant *EmitIvarOffsetVar(const ObjCImplementationDecl *ID,
Fariborz Jahanian150f7732009-01-28 01:36:42 +0000595 const ObjCIvarDecl *Ivar,
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +0000596 unsigned long int offset);
597
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +0000598 /// GetOrEmitProtocol - Get the protocol object for the given
599 /// declaration, emitting it if necessary. The return value has type
600 /// ProtocolPtrTy.
601 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
602
603 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
604 /// object for the given declaration, emitting it if needed. These
605 /// forward references will be filled in with empty bodies if no
606 /// definition is seen. The return value has type ProtocolPtrTy.
607 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
608
609 /// EmitProtocolList - Generate the list of referenced
610 /// protocols. The return value has type ProtocolListPtrTy.
611 llvm::Constant *EmitProtocolList(const std::string &Name,
612 ObjCProtocolDecl::protocol_iterator begin,
613 ObjCProtocolDecl::protocol_iterator end);
614
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000615public:
Fariborz Jahaniand0374812009-01-22 23:02:58 +0000616 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000617 // FIXME. All stubs for now!
618 virtual llvm::Function *ModuleInitFunction();
619
620 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
621 QualType ResultType,
622 Selector Sel,
623 llvm::Value *Receiver,
624 bool IsClassMessage,
625 const CallArgList &CallArgs)
626 {return RValue::get(0);}
627
628 virtual CodeGen::RValue
629 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
630 QualType ResultType,
631 Selector Sel,
632 const ObjCInterfaceDecl *Class,
633 llvm::Value *Receiver,
634 bool IsClassMessage,
635 const CallArgList &CallArgs){ return RValue::get(0);}
636
637 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
638 const ObjCInterfaceDecl *ID){ return 0; }
639
640 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
641 { return 0; }
642
Fariborz Jahanianfe49a092009-01-26 18:32:24 +0000643 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000644
645 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000646 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian5d13ab12009-01-30 18:58:59 +0000647 const ObjCProtocolDecl *PD);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000648
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000649 virtual llvm::Function *GetPropertyGetFunction(){ return 0; }
650 virtual llvm::Function *GetPropertySetFunction()
651 { return 0; }
652 virtual llvm::Function *EnumerationMutationFunction()
653 { return 0; }
654
655 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
656 const Stmt &S)
657 { return; }
658 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
659 const ObjCAtThrowStmt &S)
660 { return; }
661 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
662 llvm::Value *AddrWeakObj)
663 { return 0; }
664 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
665 llvm::Value *src, llvm::Value *dst)
666 { return; }
667 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
668 llvm::Value *src, llvm::Value *dest)
669 { return; }
670 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
671 llvm::Value *src, llvm::Value *dest)
672 { return; }
673 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
674 llvm::Value *src, llvm::Value *dest)
675 { return; }
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000676};
677
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000678} // end anonymous namespace
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000679
680/* *** Helper Functions *** */
681
682/// getConstantGEP() - Help routine to construct simple GEPs.
683static llvm::Constant *getConstantGEP(llvm::Constant *C,
684 unsigned idx0,
685 unsigned idx1) {
686 llvm::Value *Idxs[] = {
687 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
688 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
689 };
690 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
691}
692
693/* *** CGObjCMac Public Interface *** */
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000694
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000695CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
696 ObjCTypes(cgm)
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000697{
Fariborz Jahanian48543f52009-01-21 22:04:16 +0000698 ObjCABI = 1;
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000699 EmitImageInfo();
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000700}
701
Daniel Dunbar434627a2008-08-16 00:25:02 +0000702/// GetClass - Return a reference to the class for the given interface
703/// decl.
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000704llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000705 const ObjCInterfaceDecl *ID) {
706 return EmitClassRef(Builder, ID);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000707}
708
709/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000710llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar5eec6142008-08-12 03:39:23 +0000711 return EmitSelector(Builder, Sel);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000712}
713
Daniel Dunbardaf4ad42008-08-12 00:12:39 +0000714/// Generate a constant CFString object.
715/*
716 struct __builtin_CFString {
717 const int *isa; // point to __CFConstantStringClassReference
718 int flags;
719 const char *str;
720 long length;
721 };
722*/
723
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +0000724llvm::Constant *CGObjCCommonMac::GenerateConstantString(
725 const std::string &String) {
Daniel Dunbardbdb9512008-08-23 18:37:06 +0000726 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000727}
728
729/// Generates a message send where the super is the receiver. This is
730/// a message send to self with special delivery semantics indicating
731/// which class's method should be called.
Daniel Dunbara04840b2008-08-23 03:46:30 +0000732CodeGen::RValue
733CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000734 QualType ResultType,
735 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000736 const ObjCInterfaceDecl *Class,
737 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000738 bool IsClassMessage,
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +0000739 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbar15245e52008-08-23 04:28:29 +0000740 // Create and init a super structure; this is a (receiver, class)
741 // pair we will pass to objc_msgSendSuper.
742 llvm::Value *ObjCSuper =
743 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
744 llvm::Value *ReceiverAsObject =
745 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
746 CGF.Builder.CreateStore(ReceiverAsObject,
747 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar15245e52008-08-23 04:28:29 +0000748
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000749 // If this is a class message the metaclass is passed as the target.
750 llvm::Value *Target;
751 if (IsClassMessage) {
752 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
753 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
754 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
755 Target = Super;
756 } else {
757 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
758 }
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000759 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
760 // and ObjCTypes types.
761 const llvm::Type *ClassTy =
762 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar35b777f2008-10-29 22:36:39 +0000763 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000764 CGF.Builder.CreateStore(Target,
765 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
766
Daniel Dunbardd851282008-08-30 05:35:15 +0000767 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000768 ObjCSuper, ObjCTypes.SuperPtrCTy,
769 true, CallArgs);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000770}
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000771
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000772/// Generate code for a message send expression.
Daniel Dunbara04840b2008-08-23 03:46:30 +0000773CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000774 QualType ResultType,
775 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000776 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000777 bool IsClassMessage,
778 const CallArgList &CallArgs) {
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000779 llvm::Value *Arg0 =
780 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbardd851282008-08-30 05:35:15 +0000781 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000782 Arg0, CGF.getContext().getObjCIdType(),
783 false, CallArgs);
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000784}
785
786CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000787 QualType ResultType,
788 Selector Sel,
Daniel Dunbar87062ff2008-08-23 09:25:55 +0000789 llvm::Value *Arg0,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000790 QualType Arg0Ty,
791 bool IsSuper,
792 const CallArgList &CallArgs) {
793 CallArgList ActualArgs;
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +0000794 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
795 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
796 Sel)),
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000797 CGF.getContext().getObjCSelType()));
798 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbarac93e472008-08-15 22:20:32 +0000799
Daniel Dunbara9976a22008-09-10 07:00:50 +0000800 const llvm::FunctionType *FTy =
801 CGM.getTypes().GetFunctionType(CGCallInfo(ResultType, ActualArgs),
802 false);
Daniel Dunbaraecef4c2008-10-17 03:24:53 +0000803
804 llvm::Constant *Fn;
805 if (CGM.ReturnTypeUsesSret(ResultType)) {
806 Fn = ObjCTypes.getSendStretFn(IsSuper);
807 } else if (ResultType->isFloatingType()) {
808 // FIXME: Sadly, this is wrong. This actually depends on the
809 // architecture. This happens to be right for x86-32 though.
810 Fn = ObjCTypes.getSendFpretFn(IsSuper);
811 } else {
812 Fn = ObjCTypes.getSendFn(IsSuper);
813 }
Daniel Dunbara9976a22008-09-10 07:00:50 +0000814 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar49f5a0d2008-09-09 23:48:28 +0000815 return CGF.EmitCall(Fn, ResultType, ActualArgs);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000816}
817
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000818llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000819 const ObjCProtocolDecl *PD) {
Daniel Dunbarb3518152008-09-04 04:33:15 +0000820 // FIXME: I don't understand why gcc generates this, or where it is
821 // resolved. Investigate. Its also wasteful to look this up over and
822 // over.
823 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
824
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000825 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
826 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +0000827}
828
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +0000829void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar35b777f2008-10-29 22:36:39 +0000830 // FIXME: We shouldn't need this, the protocol decl should contain
831 // enough information to tell us whether this was a declaration or a
832 // definition.
833 DefinedProtocols.insert(PD->getIdentifier());
834
835 // If we have generated a forward reference to this protocol, emit
836 // it now. Otherwise do nothing, the protocol objects are lazily
837 // emitted.
838 if (Protocols.count(PD->getIdentifier()))
839 GetOrEmitProtocol(PD);
840}
841
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +0000842llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar35b777f2008-10-29 22:36:39 +0000843 if (DefinedProtocols.count(PD->getIdentifier()))
844 return GetOrEmitProtocol(PD);
845 return GetOrEmitProtocolRef(PD);
846}
847
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000848/*
849 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
850 struct _objc_protocol {
851 struct _objc_protocol_extension *isa;
852 char *protocol_name;
853 struct _objc_protocol_list *protocol_list;
854 struct _objc__method_prototype_list *instance_methods;
855 struct _objc__method_prototype_list *class_methods
856 };
857
858 See EmitProtocolExtension().
859*/
Daniel Dunbar35b777f2008-10-29 22:36:39 +0000860llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
861 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
862
863 // Early exit if a defining object has already been generated.
864 if (Entry && Entry->hasInitializer())
865 return Entry;
866
Daniel Dunbar8ede0052008-08-25 06:02:07 +0000867 // FIXME: I don't understand why gcc generates this, or where it is
868 // resolved. Investigate. Its also wasteful to look this up over and
869 // over.
870 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
871
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000872 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000873
874 // Construct method lists.
875 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
876 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
877 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
878 e = PD->instmeth_end(); i != e; ++i) {
879 ObjCMethodDecl *MD = *i;
880 llvm::Constant *C = GetMethodDescriptionConstant(MD);
881 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
882 OptInstanceMethods.push_back(C);
883 } else {
884 InstanceMethods.push_back(C);
885 }
886 }
887
888 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
889 e = PD->classmeth_end(); i != e; ++i) {
890 ObjCMethodDecl *MD = *i;
891 llvm::Constant *C = GetMethodDescriptionConstant(MD);
892 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
893 OptClassMethods.push_back(C);
894 } else {
895 ClassMethods.push_back(C);
896 }
897 }
898
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000899 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000900 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000901 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar67e778b2008-08-21 21:57:41 +0000902 Values[2] =
Chris Lattner271d4c22008-11-24 05:29:24 +0000903 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbar67e778b2008-08-21 21:57:41 +0000904 PD->protocol_begin(),
905 PD->protocol_end());
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000906 Values[3] =
Chris Lattner271d4c22008-11-24 05:29:24 +0000907 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
908 + PD->getNameAsString(),
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000909 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
910 InstanceMethods);
911 Values[4] =
Chris Lattner271d4c22008-11-24 05:29:24 +0000912 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
913 + PD->getNameAsString(),
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000914 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
915 ClassMethods);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000916 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
917 Values);
918
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000919 if (Entry) {
Daniel Dunbar35b777f2008-10-29 22:36:39 +0000920 // Already created, fix the linkage and update the initializer.
921 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000922 Entry->setInitializer(Init);
923 } else {
924 Entry =
925 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
926 llvm::GlobalValue::InternalLinkage,
927 Init,
928 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
929 &CGM.getModule());
930 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
931 UsedGlobals.push_back(Entry);
932 // FIXME: Is this necessary? Why only for protocol?
933 Entry->setAlignment(4);
934 }
Daniel Dunbar35b777f2008-10-29 22:36:39 +0000935
936 return Entry;
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000937}
938
Daniel Dunbar35b777f2008-10-29 22:36:39 +0000939llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000940 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
941
942 if (!Entry) {
Daniel Dunbar35b777f2008-10-29 22:36:39 +0000943 // We use the initializer as a marker of whether this is a forward
944 // reference or not. At module finalization we add the empty
945 // contents for protocols which were referenced but never defined.
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000946 Entry =
947 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar35b777f2008-10-29 22:36:39 +0000948 llvm::GlobalValue::ExternalLinkage,
949 0,
Chris Lattner271d4c22008-11-24 05:29:24 +0000950 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000951 &CGM.getModule());
952 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
953 UsedGlobals.push_back(Entry);
954 // FIXME: Is this necessary? Why only for protocol?
955 Entry->setAlignment(4);
956 }
957
958 return Entry;
959}
960
961/*
962 struct _objc_protocol_extension {
963 uint32_t size;
964 struct objc_method_description_list *optional_instance_methods;
965 struct objc_method_description_list *optional_class_methods;
966 struct objc_property_list *instance_properties;
967 };
968*/
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000969llvm::Constant *
970CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
971 const ConstantVector &OptInstanceMethods,
972 const ConstantVector &OptClassMethods) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000973 uint64_t Size =
Daniel Dunbard8439f22009-01-12 21:08:18 +0000974 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000975 std::vector<llvm::Constant*> Values(4);
976 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000977 Values[1] =
Chris Lattner271d4c22008-11-24 05:29:24 +0000978 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
979 + PD->getNameAsString(),
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000980 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
981 OptInstanceMethods);
982 Values[2] =
Chris Lattner271d4c22008-11-24 05:29:24 +0000983 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
984 + PD->getNameAsString(),
Daniel Dunbarfe131f02008-08-27 02:31:56 +0000985 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
986 OptClassMethods);
Chris Lattner271d4c22008-11-24 05:29:24 +0000987 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
988 PD->getNameAsString(),
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +0000989 0, PD, ObjCTypes);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000990
Daniel Dunbarb050fa62008-08-21 04:36:09 +0000991 // Return null if no extension bits are used.
Daniel Dunbarcffcdac2008-08-13 03:21:16 +0000992 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
993 Values[3]->isNullValue())
994 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
995
996 llvm::Constant *Init =
997 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
998 llvm::GlobalVariable *GV =
999 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
1000 llvm::GlobalValue::InternalLinkage,
1001 Init,
Chris Lattner271d4c22008-11-24 05:29:24 +00001002 "\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001003 &CGM.getModule());
1004 // No special section, but goes in llvm.used
1005 UsedGlobals.push_back(GV);
1006
1007 return GV;
1008}
1009
1010/*
1011 struct objc_protocol_list {
1012 struct objc_protocol_list *next;
1013 long count;
1014 Protocol *list[];
1015 };
1016*/
Daniel Dunbar67e778b2008-08-21 21:57:41 +00001017llvm::Constant *
1018CGObjCMac::EmitProtocolList(const std::string &Name,
1019 ObjCProtocolDecl::protocol_iterator begin,
1020 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001021 std::vector<llvm::Constant*> ProtocolRefs;
1022
Daniel Dunbar67e778b2008-08-21 21:57:41 +00001023 for (; begin != end; ++begin)
1024 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001025
1026 // Just return null for empty protocol lists
1027 if (ProtocolRefs.empty())
1028 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1029
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001030 // This list is null terminated.
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001031 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1032
1033 std::vector<llvm::Constant*> Values(3);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001034 // This field is only used by the runtime.
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001035 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1036 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1037 Values[2] =
1038 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1039 ProtocolRefs.size()),
1040 ProtocolRefs);
1041
1042 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1043 llvm::GlobalVariable *GV =
1044 new llvm::GlobalVariable(Init->getType(), false,
1045 llvm::GlobalValue::InternalLinkage,
1046 Init,
Daniel Dunbar67e778b2008-08-21 21:57:41 +00001047 Name,
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001048 &CGM.getModule());
1049 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
1050 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1051}
1052
1053/*
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001054 struct _objc_property {
1055 const char * const name;
1056 const char * const attributes;
1057 };
1058
1059 struct _objc_property_list {
1060 uint32_t entsize; // sizeof (struct _objc_property)
1061 uint32_t prop_count;
1062 struct _objc_property[prop_count];
1063 };
1064*/
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +00001065llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1066 const Decl *Container,
1067 const ObjCContainerDecl *OCD,
1068 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001069 std::vector<llvm::Constant*> Properties, Prop(2);
Steve Naroffdcf1e842009-01-11 12:47:58 +00001070 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1071 E = OCD->prop_end(); I != E; ++I) {
1072 const ObjCPropertyDecl *PD = *I;
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001073 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001074 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001075 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1076 Prop));
1077 }
1078
1079 // Return null for empty list.
1080 if (Properties.empty())
1081 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1082
1083 unsigned PropertySize =
Daniel Dunbard8439f22009-01-12 21:08:18 +00001084 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001085 std::vector<llvm::Constant*> Values(3);
1086 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1087 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1088 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1089 Properties.size());
1090 Values[2] = llvm::ConstantArray::get(AT, Properties);
1091 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1092
1093 llvm::GlobalVariable *GV =
1094 new llvm::GlobalVariable(Init->getType(), false,
1095 llvm::GlobalValue::InternalLinkage,
1096 Init,
1097 Name,
1098 &CGM.getModule());
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +00001099 if (ObjCABI == 2)
1100 GV->setSection("__DATA, __objc_const");
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001101 // No special section on property lists?
1102 UsedGlobals.push_back(GV);
1103 return llvm::ConstantExpr::getBitCast(GV,
1104 ObjCTypes.PropertyListPtrTy);
1105
1106}
1107
1108/*
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001109 struct objc_method_description_list {
1110 int count;
1111 struct objc_method_description list[];
1112 };
1113*/
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001114llvm::Constant *
1115CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1116 std::vector<llvm::Constant*> Desc(2);
1117 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1118 ObjCTypes.SelectorPtrTy);
1119 Desc[1] = GetMethodVarType(MD);
1120 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1121 Desc);
1122}
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001123
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001124llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1125 const char *Section,
1126 const ConstantVector &Methods) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001127 // Return null for empty list.
1128 if (Methods.empty())
1129 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1130
1131 std::vector<llvm::Constant*> Values(2);
1132 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1133 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1134 Methods.size());
1135 Values[1] = llvm::ConstantArray::get(AT, Methods);
1136 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1137
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001138 llvm::GlobalVariable *GV =
1139 new llvm::GlobalVariable(Init->getType(), false,
1140 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001141 Init, Name, &CGM.getModule());
1142 GV->setSection(Section);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00001143 UsedGlobals.push_back(GV);
1144 return llvm::ConstantExpr::getBitCast(GV,
1145 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001146}
1147
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001148/*
1149 struct _objc_category {
1150 char *category_name;
1151 char *class_name;
1152 struct _objc_method_list *instance_methods;
1153 struct _objc_method_list *class_methods;
1154 struct _objc_protocol_list *protocols;
1155 uint32_t size; // <rdar://4585769>
1156 struct _objc_property_list *instance_properties;
1157 };
1158 */
Daniel Dunbarac93e472008-08-15 22:20:32 +00001159void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbard8439f22009-01-12 21:08:18 +00001160 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001161
Daniel Dunbar0cd49032008-08-26 23:03:11 +00001162 // FIXME: This is poor design, the OCD should have a pointer to the
1163 // category decl. Additionally, note that Category can be null for
1164 // the @implementation w/o an @interface case. Sema should just
1165 // create one for us as it does for @implementation so everyone else
1166 // can live life under a clear blue sky.
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001167 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar0cd49032008-08-26 23:03:11 +00001168 const ObjCCategoryDecl *Category =
1169 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattner271d4c22008-11-24 05:29:24 +00001170 std::string ExtName(Interface->getNameAsString() + "_" +
1171 OCD->getNameAsString());
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001172
Daniel Dunbar12996f52008-08-26 21:51:14 +00001173 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1174 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
1175 e = OCD->instmeth_end(); i != e; ++i) {
1176 // Instance methods should always be defined.
1177 InstanceMethods.push_back(GetMethodConstant(*i));
1178 }
1179 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
1180 e = OCD->classmeth_end(); i != e; ++i) {
1181 // Class methods should always be defined.
1182 ClassMethods.push_back(GetMethodConstant(*i));
1183 }
1184
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001185 std::vector<llvm::Constant*> Values(7);
1186 Values[0] = GetClassName(OCD->getIdentifier());
1187 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001188 Values[2] =
1189 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1190 ExtName,
1191 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbar12996f52008-08-26 21:51:14 +00001192 InstanceMethods);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001193 Values[3] =
1194 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
1195 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbar12996f52008-08-26 21:51:14 +00001196 ClassMethods);
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001197 if (Category) {
1198 Values[4] =
1199 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1200 Category->protocol_begin(),
1201 Category->protocol_end());
1202 } else {
1203 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1204 }
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001205 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar0cd49032008-08-26 23:03:11 +00001206
1207 // If there is no category @interface then there can be no properties.
1208 if (Category) {
1209 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +00001210 OCD, Category, ObjCTypes);
Daniel Dunbar0cd49032008-08-26 23:03:11 +00001211 } else {
1212 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1213 }
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001214
1215 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1216 Values);
1217
1218 llvm::GlobalVariable *GV =
1219 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
1220 llvm::GlobalValue::InternalLinkage,
1221 Init,
1222 std::string("\01L_OBJC_CATEGORY_")+ExtName,
1223 &CGM.getModule());
1224 GV->setSection("__OBJC,__category,regular,no_dead_strip");
1225 UsedGlobals.push_back(GV);
1226 DefinedCategories.push_back(GV);
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001227}
1228
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001229// FIXME: Get from somewhere?
1230enum ClassFlags {
1231 eClassFlags_Factory = 0x00001,
1232 eClassFlags_Meta = 0x00002,
1233 // <rdr://5142207>
1234 eClassFlags_HasCXXStructors = 0x02000,
1235 eClassFlags_Hidden = 0x20000,
1236 eClassFlags_ABI2_Hidden = 0x00010,
1237 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1238};
1239
1240// <rdr://5142207&4705298&4843145>
1241static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
1242 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
1243 // FIXME: Support -fvisibility
1244 switch (attr->getVisibility()) {
1245 default:
1246 assert(0 && "Unknown visibility");
1247 return false;
1248 case VisibilityAttr::DefaultVisibility:
1249 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
1250 return false;
1251 case VisibilityAttr::HiddenVisibility:
1252 return true;
1253 }
1254 } else {
1255 return false; // FIXME: Support -fvisibility
1256 }
1257}
1258
1259/*
1260 struct _objc_class {
1261 Class isa;
1262 Class super_class;
1263 const char *name;
1264 long version;
1265 long info;
1266 long instance_size;
1267 struct _objc_ivar_list *ivars;
1268 struct _objc_method_list *methods;
1269 struct _objc_cache *cache;
1270 struct _objc_protocol_list *protocols;
1271 // Objective-C 1.0 extensions (<rdr://4585769>)
1272 const char *ivar_layout;
1273 struct _objc_class_ext *ext;
1274 };
1275
1276 See EmitClassExtension();
1277 */
1278void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar8ede0052008-08-25 06:02:07 +00001279 DefinedSymbols.insert(ID->getIdentifier());
1280
Chris Lattnerd120b9e2008-11-24 03:54:41 +00001281 std::string ClassName = ID->getNameAsString();
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001282 // FIXME: Gross
1283 ObjCInterfaceDecl *Interface =
1284 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar67e778b2008-08-21 21:57:41 +00001285 llvm::Constant *Protocols =
Chris Lattner271d4c22008-11-24 05:29:24 +00001286 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbar67e778b2008-08-21 21:57:41 +00001287 Interface->protocol_begin(),
1288 Interface->protocol_end());
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001289 const llvm::Type *InterfaceTy =
1290 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
1291 unsigned Flags = eClassFlags_Factory;
Daniel Dunbard8439f22009-01-12 21:08:18 +00001292 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001293
1294 // FIXME: Set CXX-structors flag.
1295 if (IsClassHidden(ID->getClassInterface()))
1296 Flags |= eClassFlags_Hidden;
1297
Daniel Dunbar12996f52008-08-26 21:51:14 +00001298 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1299 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
1300 e = ID->instmeth_end(); i != e; ++i) {
1301 // Instance methods should always be defined.
1302 InstanceMethods.push_back(GetMethodConstant(*i));
1303 }
1304 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
1305 e = ID->classmeth_end(); i != e; ++i) {
1306 // Class methods should always be defined.
1307 ClassMethods.push_back(GetMethodConstant(*i));
1308 }
1309
1310 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
1311 e = ID->propimpl_end(); i != e; ++i) {
1312 ObjCPropertyImplDecl *PID = *i;
1313
1314 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1315 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1316
1317 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1318 if (llvm::Constant *C = GetMethodConstant(MD))
1319 InstanceMethods.push_back(C);
1320 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1321 if (llvm::Constant *C = GetMethodConstant(MD))
1322 InstanceMethods.push_back(C);
1323 }
1324 }
1325
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001326 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar12996f52008-08-26 21:51:14 +00001327 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001328 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar8ede0052008-08-25 06:02:07 +00001329 // Record a reference to the super class.
1330 LazySymbols.insert(Super->getIdentifier());
1331
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001332 Values[ 1] =
1333 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1334 ObjCTypes.ClassPtrTy);
1335 } else {
1336 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1337 }
1338 Values[ 2] = GetClassName(ID->getIdentifier());
1339 // Version is always 0.
1340 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1341 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1342 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00001343 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001344 Values[ 7] =
Chris Lattner271d4c22008-11-24 05:29:24 +00001345 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001346 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar12996f52008-08-26 21:51:14 +00001347 InstanceMethods);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001348 // cache is always NULL.
1349 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1350 Values[ 9] = Protocols;
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001351 // FIXME: Set ivar_layout
1352 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001353 Values[11] = EmitClassExtension(ID);
1354 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1355 Values);
1356
1357 llvm::GlobalVariable *GV =
1358 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1359 llvm::GlobalValue::InternalLinkage,
1360 Init,
1361 std::string("\01L_OBJC_CLASS_")+ClassName,
1362 &CGM.getModule());
1363 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1364 UsedGlobals.push_back(GV);
1365 // FIXME: Why?
1366 GV->setAlignment(32);
1367 DefinedClasses.push_back(GV);
1368}
1369
1370llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1371 llvm::Constant *Protocols,
Daniel Dunbar12996f52008-08-26 21:51:14 +00001372 const llvm::Type *InterfaceTy,
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001373 const ConstantVector &Methods) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001374 unsigned Flags = eClassFlags_Meta;
Daniel Dunbard8439f22009-01-12 21:08:18 +00001375 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001376
1377 if (IsClassHidden(ID->getClassInterface()))
1378 Flags |= eClassFlags_Hidden;
1379
1380 std::vector<llvm::Constant*> Values(12);
1381 // The isa for the metaclass is the root of the hierarchy.
1382 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1383 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1384 Root = Super;
1385 Values[ 0] =
1386 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1387 ObjCTypes.ClassPtrTy);
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001388 // The super class for the metaclass is emitted as the name of the
1389 // super class. The runtime fixes this up to point to the
1390 // *metaclass* for the super class.
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001391 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1392 Values[ 1] =
1393 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1394 ObjCTypes.ClassPtrTy);
1395 } else {
1396 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1397 }
1398 Values[ 2] = GetClassName(ID->getIdentifier());
1399 // Version is always 0.
1400 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1401 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1402 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00001403 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001404 Values[ 7] =
Chris Lattner271d4c22008-11-24 05:29:24 +00001405 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00001406 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar12996f52008-08-26 21:51:14 +00001407 Methods);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001408 // cache is always NULL.
1409 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1410 Values[ 9] = Protocols;
1411 // ivar_layout for metaclass is always NULL.
1412 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1413 // The class extension is always unused for metaclasses.
1414 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1415 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1416 Values);
1417
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001418 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattnerd120b9e2008-11-24 03:54:41 +00001419 Name += ID->getNameAsCString();
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001420
1421 // Check for a forward reference.
1422 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1423 if (GV) {
1424 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1425 "Forward metaclass reference has incorrect type.");
1426 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1427 GV->setInitializer(Init);
1428 } else {
1429 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1430 llvm::GlobalValue::InternalLinkage,
1431 Init, Name,
1432 &CGM.getModule());
1433 }
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001434 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1435 UsedGlobals.push_back(GV);
1436 // FIXME: Why?
1437 GV->setAlignment(32);
1438
1439 return GV;
1440}
1441
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001442llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattner271d4c22008-11-24 05:29:24 +00001443 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001444
1445 // FIXME: Should we look these up somewhere other than the
1446 // module. Its a bit silly since we only generate these while
1447 // processing an implementation, so exactly one pointer would work
1448 // if know when we entered/exitted an implementation block.
1449
1450 // Check for an existing forward reference.
Fariborz Jahanian5fe09f72009-01-07 20:11:22 +00001451 // Previously, metaclass with internal linkage may have been defined.
1452 // pass 'true' as 2nd argument so it is returned.
1453 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +00001454 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1455 "Forward metaclass reference has incorrect type.");
1456 return GV;
1457 } else {
1458 // Generate as an external reference to keep a consistent
1459 // module. This will be patched up when we emit the metaclass.
1460 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1461 llvm::GlobalValue::ExternalLinkage,
1462 0,
1463 Name,
1464 &CGM.getModule());
1465 }
1466}
1467
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001468/*
1469 struct objc_class_ext {
1470 uint32_t size;
1471 const char *weak_ivar_layout;
1472 struct _objc_property_list *properties;
1473 };
1474*/
1475llvm::Constant *
1476CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1477 uint64_t Size =
Daniel Dunbard8439f22009-01-12 21:08:18 +00001478 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001479
1480 std::vector<llvm::Constant*> Values(3);
1481 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001482 // FIXME: Output weak_ivar_layout string.
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001483 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Chris Lattner271d4c22008-11-24 05:29:24 +00001484 Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +00001485 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001486
1487 // Return null if no extension bits are used.
1488 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1489 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1490
1491 llvm::Constant *Init =
1492 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1493 llvm::GlobalVariable *GV =
1494 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1495 llvm::GlobalValue::InternalLinkage,
1496 Init,
Chris Lattner271d4c22008-11-24 05:29:24 +00001497 "\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001498 &CGM.getModule());
1499 // No special section, but goes in llvm.used
1500 UsedGlobals.push_back(GV);
1501
1502 return GV;
1503}
1504
Fariborz Jahanianc314a492009-01-17 19:36:33 +00001505/// countInheritedIvars - count number of ivars in class and its super class(s)
1506///
1507static int countInheritedIvars(const ObjCInterfaceDecl *OI) {
1508 int count = 0;
1509 if (!OI)
1510 return 0;
1511 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
1512 if (SuperClass)
1513 count += countInheritedIvars(SuperClass);
1514 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1515 E = OI->ivar_end(); I != E; ++I)
1516 ++count;
1517 return count;
1518}
1519
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001520/*
1521 struct objc_ivar {
1522 char *ivar_name;
1523 char *ivar_type;
1524 int ivar_offset;
1525 };
1526
1527 struct objc_ivar_list {
1528 int ivar_count;
1529 struct objc_ivar list[count];
1530 };
1531 */
1532llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00001533 bool ForClass) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001534 std::vector<llvm::Constant*> Ivars, Ivar(3);
1535
1536 // When emitting the root class GCC emits ivar entries for the
1537 // actual class structure. It is not clear if we need to follow this
1538 // behavior; for now lets try and get away with not doing it. If so,
1539 // the cleanest solution would be to make up an ObjCInterfaceDecl
1540 // for the class.
1541 if (ForClass)
1542 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00001543
1544 ObjCInterfaceDecl *OID =
1545 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
1546 const llvm::Type *InterfaceTy =
1547 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001548 const llvm::StructLayout *Layout =
1549 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00001550
1551 RecordDecl::field_iterator ifield, pfield;
1552 const RecordDecl *RD = GetFirstIvarInRecord(OID, ifield, pfield);
Fariborz Jahanianc314a492009-01-17 19:36:33 +00001553 for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) {
1554 FieldDecl *Field = *ifield;
1555 unsigned Offset = Layout->getElementOffset(CGM.getTypes().
1556 getLLVMFieldNo(Field));
1557 if (Field->getIdentifier())
1558 Ivar[0] = GetMethodVarName(Field->getIdentifier());
1559 else
1560 Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001561 std::string TypeStr;
Fariborz Jahanianc314a492009-01-17 19:36:33 +00001562 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001563 Ivar[1] = GetMethodVarType(TypeStr);
1564 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001565 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001566 }
1567
1568 // Return null for empty list.
1569 if (Ivars.empty())
1570 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1571
1572 std::vector<llvm::Constant*> Values(2);
1573 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1574 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1575 Ivars.size());
1576 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1577 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1578
1579 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1580 "\01L_OBJC_INSTANCE_VARIABLES_");
1581 llvm::GlobalVariable *GV =
1582 new llvm::GlobalVariable(Init->getType(), false,
1583 llvm::GlobalValue::InternalLinkage,
1584 Init,
Chris Lattner271d4c22008-11-24 05:29:24 +00001585 Prefix + ID->getNameAsString(),
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001586 &CGM.getModule());
1587 if (ForClass) {
1588 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1589 // FIXME: Why is this only here?
1590 GV->setAlignment(32);
1591 } else {
1592 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1593 }
1594 UsedGlobals.push_back(GV);
1595 return llvm::ConstantExpr::getBitCast(GV,
1596 ObjCTypes.IvarListPtrTy);
1597}
1598
1599/*
1600 struct objc_method {
1601 SEL method_name;
1602 char *method_types;
1603 void *method;
1604 };
1605
1606 struct objc_method_list {
1607 struct objc_method_list *obsolete;
1608 int count;
1609 struct objc_method methods_list[count];
1610 };
1611*/
Daniel Dunbar12996f52008-08-26 21:51:14 +00001612
1613/// GetMethodConstant - Return a struct objc_method constant for the
1614/// given method if it has been defined. The result is null if the
1615/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001616llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbar12996f52008-08-26 21:51:14 +00001617 // FIXME: Use DenseMap::lookup
1618 llvm::Function *Fn = MethodDefinitions[MD];
1619 if (!Fn)
1620 return 0;
1621
1622 std::vector<llvm::Constant*> Method(3);
1623 Method[0] =
1624 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1625 ObjCTypes.SelectorPtrTy);
1626 Method[1] = GetMethodVarType(MD);
1627 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1628 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1629}
1630
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001631llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1632 const char *Section,
Daniel Dunbarfe131f02008-08-27 02:31:56 +00001633 const ConstantVector &Methods) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001634 // Return null for empty list.
1635 if (Methods.empty())
1636 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1637
1638 std::vector<llvm::Constant*> Values(3);
1639 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1640 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1641 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1642 Methods.size());
1643 Values[2] = llvm::ConstantArray::get(AT, Methods);
1644 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1645
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001646 llvm::GlobalVariable *GV =
1647 new llvm::GlobalVariable(Init->getType(), false,
1648 llvm::GlobalValue::InternalLinkage,
1649 Init,
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001650 Name,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001651 &CGM.getModule());
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00001652 GV->setSection(Section);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00001653 UsedGlobals.push_back(GV);
1654 return llvm::ConstantExpr::getBitCast(GV,
1655 ObjCTypes.MethodListPtrTy);
Daniel Dunbarace33292008-08-16 03:19:19 +00001656}
1657
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00001658llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Fariborz Jahanian0adaa8a2009-01-10 21:06:09 +00001659 const ObjCContainerDecl *CD) {
Daniel Dunbarace33292008-08-16 03:19:19 +00001660 std::string Name;
Fariborz Jahanian0adaa8a2009-01-10 21:06:09 +00001661 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarace33292008-08-16 03:19:19 +00001662
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001663 const llvm::FunctionType *MethodTy =
1664 CGM.getTypes().GetFunctionType(CGFunctionInfo(OMD, CGM.getContext()));
Daniel Dunbarace33292008-08-16 03:19:19 +00001665 llvm::Function *Method =
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001666 llvm::Function::Create(MethodTy,
Daniel Dunbarace33292008-08-16 03:19:19 +00001667 llvm::GlobalValue::InternalLinkage,
1668 Name,
1669 &CGM.getModule());
Daniel Dunbar12996f52008-08-26 21:51:14 +00001670 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarace33292008-08-16 03:19:19 +00001671
Daniel Dunbarace33292008-08-16 03:19:19 +00001672 return Method;
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001673}
1674
1675llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbar1be1df32008-08-11 21:35:06 +00001676 // Abuse this interface function as a place to finalize.
1677 FinishModule();
1678
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001679 return NULL;
1680}
1681
Daniel Dunbarf7103722008-09-24 03:38:44 +00001682llvm::Function *CGObjCMac::GetPropertyGetFunction() {
1683 return ObjCTypes.GetPropertyFn;
1684}
1685
1686llvm::Function *CGObjCMac::GetPropertySetFunction() {
1687 return ObjCTypes.SetPropertyFn;
1688}
1689
Anders Carlsson58d16242008-08-31 04:05:03 +00001690llvm::Function *CGObjCMac::EnumerationMutationFunction()
1691{
1692 return ObjCTypes.EnumerationMutationFn;
1693}
1694
Daniel Dunbar83544842008-09-28 01:03:14 +00001695/*
1696
1697Objective-C setjmp-longjmp (sjlj) Exception Handling
1698--
1699
1700The basic framework for a @try-catch-finally is as follows:
1701{
1702 objc_exception_data d;
1703 id _rethrow = null;
1704
1705 objc_exception_try_enter(&d);
1706 if (!setjmp(d.jmp_buf)) {
1707 ... try body ...
1708 } else {
1709 // exception path
1710 id _caught = objc_exception_extract(&d);
1711
1712 // enter new try scope for handlers
1713 if (!setjmp(d.jmp_buf)) {
1714 ... match exception and execute catch blocks ...
1715
1716 // fell off end, rethrow.
1717 _rethrow = _caught;
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001718 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar83544842008-09-28 01:03:14 +00001719 } else {
1720 // exception in catch block
1721 _rethrow = objc_exception_extract(&d);
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001722 ... jump-through-finally_no_exit to finally_rethrow ...
Daniel Dunbar83544842008-09-28 01:03:14 +00001723 }
1724 }
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001725 ... jump-through-finally to finally_end ...
Daniel Dunbar83544842008-09-28 01:03:14 +00001726
1727finally:
1728 // match either the initial try_enter or the catch try_enter,
1729 // depending on the path followed.
1730 objc_exception_try_exit(&d);
1731finally_no_exit:
1732 ... finally block ....
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001733 ... dispatch to finally destination ...
1734
1735finally_rethrow:
1736 objc_exception_throw(_rethrow);
1737
1738finally_end:
Daniel Dunbar83544842008-09-28 01:03:14 +00001739}
1740
1741This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001742uses _rethrow to determine if objc_exception_try_exit should be called
1743and if the object should be rethrown. This breaks in the face of
1744throwing nil and introduces unnecessary branches.
Daniel Dunbar83544842008-09-28 01:03:14 +00001745
1746We specialize this framework for a few particular circumstances:
1747
1748 - If there are no catch blocks, then we avoid emitting the second
1749 exception handling context.
1750
1751 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
1752 e)) we avoid emitting the code to rethrow an uncaught exception.
1753
1754 - FIXME: If there is no @finally block we can do a few more
1755 simplifications.
1756
1757Rethrows and Jumps-Through-Finally
1758--
1759
1760Support for implicit rethrows and jumping through the finally block is
1761handled by storing the current exception-handling context in
1762ObjCEHStack.
1763
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001764In order to implement proper @finally semantics, we support one basic
1765mechanism for jumping through the finally block to an arbitrary
1766destination. Constructs which generate exits from a @try or @catch
1767block use this mechanism to implement the proper semantics by chaining
1768jumps, as necessary.
1769
1770This mechanism works like the one used for indirect goto: we
1771arbitrarily assign an ID to each destination and store the ID for the
1772destination in a variable prior to entering the finally block. At the
1773end of the finally block we simply create a switch to the proper
1774destination.
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00001775
1776Code gen for @synchronized(expr) stmt;
1777Effectively generating code for:
1778objc_sync_enter(expr);
1779@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar83544842008-09-28 01:03:14 +00001780*/
1781
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00001782void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1783 const Stmt &S) {
1784 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001785 // Create various blocks we refer to for handling @finally.
Daniel Dunbar72f96552008-11-11 02:29:29 +00001786 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
1787 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
1788 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1789 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001790 llvm::Value *DestCode =
1791 CGF.CreateTempAlloca(llvm::Type::Int32Ty, "finally.dst");
1792
1793 // Generate jump code. Done here so we can directly add things to
1794 // the switch instruction.
Daniel Dunbar72f96552008-11-11 02:29:29 +00001795 llvm::BasicBlock *FinallyJump = CGF.createBasicBlock("finally.jump");
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001796 llvm::SwitchInst *FinallySwitch =
1797 llvm::SwitchInst::Create(new llvm::LoadInst(DestCode, "", FinallyJump),
1798 FinallyEnd, 10, FinallyJump);
1799
1800 // Push an EH context entry, used for handling rethrows and jumps
1801 // through finally.
1802 CodeGenFunction::ObjCEHEntry EHEntry(FinallyBlock, FinallyNoExit,
1803 FinallySwitch, DestCode);
1804 CGF.ObjCEHStack.push_back(&EHEntry);
1805
1806 // Allocate memory for the exception data and rethrow pointer.
Anders Carlssonfca6c292008-09-09 17:59:25 +00001807 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
1808 "exceptiondata.ptr");
Daniel Dunbar35b777f2008-10-29 22:36:39 +00001809 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
1810 "_rethrow");
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00001811 if (!isTry) {
1812 // For @synchronized, call objc_sync_enter(sync.expr)
Fariborz Jahanian3895d922008-11-21 19:21:53 +00001813 llvm::Value *Arg = CGF.EmitScalarExpr(
1814 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1815 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1816 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, Arg);
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00001817 }
Anders Carlssonfca6c292008-09-09 17:59:25 +00001818
1819 // Enter a new try block and call setjmp.
1820 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
1821 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
1822 "jmpbufarray");
1823 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
1824 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1825 JmpBufPtr, "result");
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001826
Daniel Dunbar72f96552008-11-11 02:29:29 +00001827 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1828 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbarbe56f012008-10-02 17:05:36 +00001829 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbar0ac66f72008-09-27 23:30:04 +00001830 TryHandler, TryBlock);
Anders Carlssonfca6c292008-09-09 17:59:25 +00001831
1832 // Emit the @try block.
1833 CGF.EmitBlock(TryBlock);
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00001834 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
1835 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001836 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlssonfca6c292008-09-09 17:59:25 +00001837
1838 // Emit the "exception in @try" block.
Daniel Dunbar0ac66f72008-09-27 23:30:04 +00001839 CGF.EmitBlock(TryHandler);
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00001840
1841 // Retrieve the exception object. We may emit multiple blocks but
1842 // nothing can cross this so the value is already in SSA form.
1843 llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1844 ExceptionData,
1845 "caught");
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001846 EHEntry.Exception = Caught;
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00001847 if (!isTry)
1848 {
1849 CGF.Builder.CreateStore(Caught, RethrowPtr);
1850 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
1851 }
1852 else if (const ObjCAtCatchStmt* CatchStmt =
1853 cast<ObjCAtTryStmt>(S).getCatchStmts())
1854 {
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00001855 // Enter a new exception try block (in case a @catch block throws
1856 // an exception).
Anders Carlssonfca6c292008-09-09 17:59:25 +00001857 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00001858
Anders Carlssonfca6c292008-09-09 17:59:25 +00001859 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1860 JmpBufPtr, "result");
Daniel Dunbarbe56f012008-10-02 17:05:36 +00001861 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlssonfca6c292008-09-09 17:59:25 +00001862
Daniel Dunbar72f96552008-11-11 02:29:29 +00001863 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
1864 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbar0ac66f72008-09-27 23:30:04 +00001865 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlssonfca6c292008-09-09 17:59:25 +00001866
1867 CGF.EmitBlock(CatchBlock);
1868
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00001869 // Handle catch list. As a special case we check if everything is
1870 // matched and avoid generating code for falling off the end if
1871 // so.
1872 bool AllMatched = false;
Anders Carlssonfca6c292008-09-09 17:59:25 +00001873 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar72f96552008-11-11 02:29:29 +00001874 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlssonfca6c292008-09-09 17:59:25 +00001875
Anders Carlsson75d86732008-09-11 09:15:33 +00001876 const DeclStmt *CatchParam =
1877 cast_or_null<DeclStmt>(CatchStmt->getCatchParamStmt());
Daniel Dunbar7a68b452008-09-27 07:36:24 +00001878 const VarDecl *VD = 0;
1879 const PointerType *PT = 0;
1880
Anders Carlssonfca6c292008-09-09 17:59:25 +00001881 // catch(...) always matches.
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00001882 if (!CatchParam) {
1883 AllMatched = true;
1884 } else {
Ted Kremenekf41e9f72008-10-06 20:58:56 +00001885 VD = cast<VarDecl>(CatchParam->getSolitaryDecl());
Daniel Dunbar7a68b452008-09-27 07:36:24 +00001886 PT = VD->getType()->getAsPointerType();
Anders Carlssonfca6c292008-09-09 17:59:25 +00001887
Daniel Dunbard04c9352008-09-27 22:21:14 +00001888 // catch(id e) always matches.
1889 // FIXME: For the time being we also match id<X>; this should
1890 // be rejected by Sema instead.
1891 if ((PT && CGF.getContext().isObjCIdType(PT->getPointeeType())) ||
1892 VD->getType()->isObjCQualifiedIdType())
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00001893 AllMatched = true;
Anders Carlssonfca6c292008-09-09 17:59:25 +00001894 }
1895
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00001896 if (AllMatched) {
Anders Carlsson75d86732008-09-11 09:15:33 +00001897 if (CatchParam) {
1898 CGF.EmitStmt(CatchParam);
Daniel Dunbar5aa22bc2008-11-11 23:11:34 +00001899 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar7a68b452008-09-27 07:36:24 +00001900 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(VD));
Anders Carlsson75d86732008-09-11 09:15:33 +00001901 }
Anders Carlsson1f4acc32008-09-11 08:21:54 +00001902
Anders Carlsson75d86732008-09-11 09:15:33 +00001903 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001904 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlssonfca6c292008-09-09 17:59:25 +00001905 break;
1906 }
1907
Daniel Dunbar7a68b452008-09-27 07:36:24 +00001908 assert(PT && "Unexpected non-pointer type in @catch");
1909 QualType T = PT->getPointeeType();
Anders Carlssona4519172008-09-11 06:35:14 +00001910 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlssonfca6c292008-09-09 17:59:25 +00001911 assert(ObjCType && "Catch parameter must have Objective-C type!");
1912
1913 // Check if the @catch block matches the exception object.
1914 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
1915
Anders Carlssonfca6c292008-09-09 17:59:25 +00001916 llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn,
1917 Class, Caught, "match");
Anders Carlssonfca6c292008-09-09 17:59:25 +00001918
Daniel Dunbar72f96552008-11-11 02:29:29 +00001919 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlssonfca6c292008-09-09 17:59:25 +00001920
Daniel Dunbarbe56f012008-10-02 17:05:36 +00001921 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbar0ac66f72008-09-27 23:30:04 +00001922 MatchedBlock, NextCatchBlock);
Anders Carlssonfca6c292008-09-09 17:59:25 +00001923
1924 // Emit the @catch block.
1925 CGF.EmitBlock(MatchedBlock);
Daniel Dunbar83544842008-09-28 01:03:14 +00001926 CGF.EmitStmt(CatchParam);
Daniel Dunbar5aa22bc2008-11-11 23:11:34 +00001927 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar83544842008-09-28 01:03:14 +00001928
1929 llvm::Value *Tmp =
1930 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(VD->getType()),
1931 "tmp");
1932 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(VD));
Anders Carlsson75d86732008-09-11 09:15:33 +00001933
1934 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001935 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlssonfca6c292008-09-09 17:59:25 +00001936
1937 CGF.EmitBlock(NextCatchBlock);
1938 }
1939
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00001940 if (!AllMatched) {
1941 // None of the handlers caught the exception, so store it to be
1942 // rethrown at the end of the @finally block.
1943 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001944 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow);
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00001945 }
1946
1947 // Emit the exception handler for the @catch blocks.
Daniel Dunbar0ac66f72008-09-27 23:30:04 +00001948 CGF.EmitBlock(CatchHandler);
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00001949 CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1950 ExceptionData),
1951 RethrowPtr);
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001952 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Daniel Dunbar4655e2e2008-09-27 07:03:52 +00001953 } else {
Anders Carlssonfca6c292008-09-09 17:59:25 +00001954 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001955 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Anders Carlssonfca6c292008-09-09 17:59:25 +00001956 }
1957
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001958 // Pop the exception-handling stack entry. It is important to do
1959 // this now, because the code in the @finally block is not in this
1960 // context.
1961 CGF.ObjCEHStack.pop_back();
1962
Anders Carlssonfca6c292008-09-09 17:59:25 +00001963 // Emit the @finally block.
1964 CGF.EmitBlock(FinallyBlock);
Anders Carlssonfca6c292008-09-09 17:59:25 +00001965 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData);
Daniel Dunbar7a68b452008-09-27 07:36:24 +00001966
1967 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00001968 if (isTry) {
1969 if (const ObjCAtFinallyStmt* FinallyStmt =
1970 cast<ObjCAtTryStmt>(S).getFinallyStmt())
1971 CGF.EmitStmt(FinallyStmt->getFinallyBody());
1972 }
Fariborz Jahanian3895d922008-11-21 19:21:53 +00001973 else {
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00001974 // For @synchronized objc_sync_exit(expr); As finally's sole statement.
Fariborz Jahanian3895d922008-11-21 19:21:53 +00001975 // For @synchronized, call objc_sync_enter(sync.expr)
1976 llvm::Value *Arg = CGF.EmitScalarExpr(
1977 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1978 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1979 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, Arg);
1980 }
Anders Carlssonfca6c292008-09-09 17:59:25 +00001981
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001982 CGF.EmitBlock(FinallyJump);
1983
1984 CGF.EmitBlock(FinallyRethrow);
1985 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn,
1986 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbar0ac66f72008-09-27 23:30:04 +00001987 CGF.Builder.CreateUnreachable();
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001988
1989 CGF.EmitBlock(FinallyEnd);
Anders Carlssonb01a2112008-09-09 10:04:29 +00001990}
1991
1992void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbare9900eb2008-09-30 01:06:03 +00001993 const ObjCAtThrowStmt &S) {
Anders Carlsson05d7be72008-09-09 16:16:55 +00001994 llvm::Value *ExceptionAsObject;
1995
1996 if (const Expr *ThrowExpr = S.getThrowExpr()) {
1997 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
1998 ExceptionAsObject =
1999 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2000 } else {
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002001 assert((!CGF.ObjCEHStack.empty() && CGF.ObjCEHStack.back()->Exception) &&
Daniel Dunbar83544842008-09-28 01:03:14 +00002002 "Unexpected rethrow outside @catch block.");
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002003 ExceptionAsObject = CGF.ObjCEHStack.back()->Exception;
Anders Carlsson05d7be72008-09-09 16:16:55 +00002004 }
2005
2006 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
Anders Carlssonfca6c292008-09-09 17:59:25 +00002007 CGF.Builder.CreateUnreachable();
Daniel Dunbar5aa22bc2008-11-11 23:11:34 +00002008
2009 // Clear the insertion point to indicate we are in unreachable code.
2010 CGF.Builder.ClearInsertionPoint();
Anders Carlssonb01a2112008-09-09 10:04:29 +00002011}
2012
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002013void CodeGenFunction::EmitJumpThroughFinally(ObjCEHEntry *E,
2014 llvm::BasicBlock *Dst,
2015 bool ExecuteTryExit) {
Daniel Dunbar5aa22bc2008-11-11 23:11:34 +00002016 if (!HaveInsertPoint())
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002017 return;
2018
2019 // Find the destination code for this block. We always use 0 for the
2020 // fallthrough block (default destination).
2021 llvm::SwitchInst *SI = E->FinallySwitch;
2022 llvm::ConstantInt *ID;
2023 if (Dst == SI->getDefaultDest()) {
2024 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
2025 } else {
2026 ID = SI->findCaseDest(Dst);
2027 if (!ID) {
2028 // No code found, get a new unique one by just using the number
2029 // of switch successors.
2030 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, SI->getNumSuccessors());
2031 SI->addCase(ID, Dst);
2032 }
2033 }
2034
2035 // Set the destination code and branch.
2036 Builder.CreateStore(ID, E->DestCode);
Daniel Dunbar5aa22bc2008-11-11 23:11:34 +00002037 EmitBranch(ExecuteTryExit ? E->FinallyBlock : E->FinallyNoExit);
Daniel Dunbare9900eb2008-09-30 01:06:03 +00002038}
2039
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00002040/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian3305ad32008-11-18 21:45:40 +00002041/// object: objc_read_weak (id *src)
2042///
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00002043llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian3305ad32008-11-18 21:45:40 +00002044 llvm::Value *AddrWeakObj)
2045{
Fariborz Jahaniand2f661a2008-11-19 17:34:06 +00002046 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian3305ad32008-11-18 21:45:40 +00002047 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00002048 AddrWeakObj, "weakread");
Fariborz Jahanian3305ad32008-11-18 21:45:40 +00002049 return read_weak;
2050}
2051
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00002052/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2053/// objc_assign_weak (id src, id *dst)
2054///
2055void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2056 llvm::Value *src, llvm::Value *dst)
2057{
Fariborz Jahaniand2f661a2008-11-19 17:34:06 +00002058 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2059 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00002060 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
2061 src, dst, "weakassign");
2062 return;
2063}
2064
Fariborz Jahanian17958902008-11-19 00:59:10 +00002065/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2066/// objc_assign_global (id src, id *dst)
2067///
2068void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2069 llvm::Value *src, llvm::Value *dst)
2070{
Fariborz Jahaniand2f661a2008-11-19 17:34:06 +00002071 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2072 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian17958902008-11-19 00:59:10 +00002073 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
2074 src, dst, "globalassign");
2075 return;
2076}
2077
Fariborz Jahanianf310b592008-11-20 19:23:36 +00002078/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2079/// objc_assign_ivar (id src, id *dst)
2080///
2081void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2082 llvm::Value *src, llvm::Value *dst)
2083{
2084 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2085 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
2086 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
2087 src, dst, "assignivar");
2088 return;
2089}
2090
Fariborz Jahanian17958902008-11-19 00:59:10 +00002091/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2092/// objc_assign_strongCast (id src, id *dst)
2093///
2094void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2095 llvm::Value *src, llvm::Value *dst)
2096{
Fariborz Jahaniand2f661a2008-11-19 17:34:06 +00002097 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2098 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian17958902008-11-19 00:59:10 +00002099 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
2100 src, dst, "weakassign");
2101 return;
2102}
2103
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002104/* *** Private Interface *** */
2105
2106/// EmitImageInfo - Emit the image info marker used to encode some module
2107/// level information.
2108///
2109/// See: <rdr://4810609&4810587&4810587>
2110/// struct IMAGE_INFO {
2111/// unsigned version;
2112/// unsigned flags;
2113/// };
2114enum ImageInfoFlags {
2115 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
2116 eImageInfo_GarbageCollected = (1 << 1),
2117 eImageInfo_GCOnly = (1 << 2)
2118};
2119
2120void CGObjCMac::EmitImageInfo() {
2121 unsigned version = 0; // Version is unused?
2122 unsigned flags = 0;
2123
2124 // FIXME: Fix and continue?
2125 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2126 flags |= eImageInfo_GarbageCollected;
2127 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2128 flags |= eImageInfo_GCOnly;
2129
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002130 // Emitted as int[2];
2131 llvm::Constant *values[2] = {
2132 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2133 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2134 };
2135 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002136 llvm::GlobalVariable *GV =
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002137 new llvm::GlobalVariable(AT, true,
2138 llvm::GlobalValue::InternalLinkage,
2139 llvm::ConstantArray::get(AT, values, 2),
2140 "\01L_OBJC_IMAGE_INFO",
2141 &CGM.getModule());
2142
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00002143 if (ObjCABI == 1) {
2144 GV->setSection("__OBJC, __image_info,regular");
2145 } else {
2146 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
2147 }
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002148
2149 UsedGlobals.push_back(GV);
2150}
2151
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002152
2153// struct objc_module {
2154// unsigned long version;
2155// unsigned long size;
2156// const char *name;
2157// Symtab symtab;
2158// };
2159
2160// FIXME: Get from somewhere
2161static const int ModuleVersion = 7;
2162
2163void CGObjCMac::EmitModuleInfo() {
Daniel Dunbard8439f22009-01-12 21:08:18 +00002164 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002165
2166 std::vector<llvm::Constant*> Values(4);
2167 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2168 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbarac93e472008-08-15 22:20:32 +00002169 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002170 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002171 Values[3] = EmitModuleSymbols();
2172
2173 llvm::GlobalVariable *GV =
2174 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
2175 llvm::GlobalValue::InternalLinkage,
2176 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
2177 Values),
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002178 "\01L_OBJC_MODULES",
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002179 &CGM.getModule());
2180 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
2181 UsedGlobals.push_back(GV);
2182}
2183
2184llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002185 unsigned NumClasses = DefinedClasses.size();
2186 unsigned NumCategories = DefinedCategories.size();
2187
Daniel Dunbar8ede0052008-08-25 06:02:07 +00002188 // Return null if no symbols were defined.
2189 if (!NumClasses && !NumCategories)
2190 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2191
2192 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002193 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2194 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2195 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2196 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2197
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00002198 // The runtime expects exactly the list of defined classes followed
2199 // by the list of defined categories, in a single array.
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002200 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00002201 for (unsigned i=0; i<NumClasses; i++)
2202 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2203 ObjCTypes.Int8PtrTy);
2204 for (unsigned i=0; i<NumCategories; i++)
2205 Symbols[NumClasses + i] =
2206 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2207 ObjCTypes.Int8PtrTy);
2208
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002209 Values[4] =
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00002210 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002211 NumClasses + NumCategories),
2212 Symbols);
2213
2214 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2215
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002216 llvm::GlobalVariable *GV =
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002217 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002218 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002219 Init,
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002220 "\01L_OBJC_SYMBOLS",
2221 &CGM.getModule());
2222 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
2223 UsedGlobals.push_back(GV);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002224 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2225}
2226
Daniel Dunbard916e6e2008-11-01 01:53:16 +00002227llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002228 const ObjCInterfaceDecl *ID) {
Daniel Dunbar8ede0052008-08-25 06:02:07 +00002229 LazySymbols.insert(ID->getIdentifier());
2230
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002231 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2232
2233 if (!Entry) {
2234 llvm::Constant *Casted =
2235 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2236 ObjCTypes.ClassPtrTy);
2237 Entry =
2238 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
2239 llvm::GlobalValue::InternalLinkage,
2240 Casted, "\01L_OBJC_CLASS_REFERENCES_",
2241 &CGM.getModule());
2242 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
2243 UsedGlobals.push_back(Entry);
2244 }
2245
2246 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002247}
2248
Daniel Dunbard916e6e2008-11-01 01:53:16 +00002249llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar5eec6142008-08-12 03:39:23 +00002250 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2251
2252 if (!Entry) {
2253 llvm::Constant *Casted =
2254 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2255 ObjCTypes.SelectorPtrTy);
2256 Entry =
2257 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
2258 llvm::GlobalValue::InternalLinkage,
2259 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
2260 &CGM.getModule());
2261 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
2262 UsedGlobals.push_back(Entry);
2263 }
2264
2265 return Builder.CreateLoad(Entry, false, "tmp");
2266}
2267
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00002268llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002269 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002270
2271 if (!Entry) {
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002272 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002273 Entry =
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002274 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002275 llvm::GlobalValue::InternalLinkage,
2276 C, "\01L_OBJC_CLASS_NAME_",
2277 &CGM.getModule());
2278 Entry->setSection("__TEXT,__cstring,cstring_literals");
2279 UsedGlobals.push_back(Entry);
2280 }
2281
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002282 return getConstantGEP(Entry, 0, 0);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002283}
2284
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00002285llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar5eec6142008-08-12 03:39:23 +00002286 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
2287
2288 if (!Entry) {
Chris Lattner3a8f2942008-11-24 03:33:13 +00002289 // FIXME: Avoid std::string copying.
2290 llvm::Constant *C = llvm::ConstantArray::get(Sel.getAsString());
Daniel Dunbar5eec6142008-08-12 03:39:23 +00002291 Entry =
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002292 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar5eec6142008-08-12 03:39:23 +00002293 llvm::GlobalValue::InternalLinkage,
2294 C, "\01L_OBJC_METH_VAR_NAME_",
2295 &CGM.getModule());
2296 Entry->setSection("__TEXT,__cstring,cstring_literals");
2297 UsedGlobals.push_back(Entry);
2298 }
2299
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002300 return getConstantGEP(Entry, 0, 0);
2301}
2302
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002303// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00002304llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002305 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
2306}
2307
2308// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00002309llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002310 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
2311}
2312
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00002313llvm::Constant *CGObjCCommonMac::GetMethodVarType(const std::string &Name) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002314 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002315
2316 if (!Entry) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002317 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002318 Entry =
2319 new llvm::GlobalVariable(C->getType(), false,
2320 llvm::GlobalValue::InternalLinkage,
2321 C, "\01L_OBJC_METH_VAR_TYPE_",
2322 &CGM.getModule());
2323 Entry->setSection("__TEXT,__cstring,cstring_literals");
2324 UsedGlobals.push_back(Entry);
2325 }
2326
2327 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar5eec6142008-08-12 03:39:23 +00002328}
2329
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002330// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00002331llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002332 std::string TypeStr;
Daniel Dunbar12996f52008-08-26 21:51:14 +00002333 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
2334 TypeStr);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002335 return GetMethodVarType(TypeStr);
2336}
2337
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00002338// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00002339llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00002340 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
2341
2342 if (!Entry) {
2343 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
2344 Entry =
2345 new llvm::GlobalVariable(C->getType(), false,
2346 llvm::GlobalValue::InternalLinkage,
2347 C, "\01L_OBJC_PROP_NAME_ATTR_",
2348 &CGM.getModule());
2349 Entry->setSection("__TEXT,__cstring,cstring_literals");
2350 UsedGlobals.push_back(Entry);
2351 }
2352
2353 return getConstantGEP(Entry, 0, 0);
2354}
2355
2356// FIXME: Merge into a single cstring creation function.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002357// FIXME: This Decl should be more precise.
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00002358llvm::Constant *CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002359 const Decl *Container) {
2360 std::string TypeStr;
2361 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbara6eb6b72008-08-23 00:19:03 +00002362 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
2363}
2364
Fariborz Jahanian32b5ea22009-01-21 23:34:32 +00002365void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
2366 const ObjCContainerDecl *CD,
2367 std::string &NameOut) {
Daniel Dunbarace33292008-08-16 03:19:19 +00002368 // FIXME: Find the mangling GCC uses.
Douglas Gregor5d764842009-01-09 17:18:27 +00002369 NameOut = (D->isInstanceMethod() ? "-" : "+");
Chris Lattner3a8f2942008-11-24 03:33:13 +00002370 NameOut += '[';
Fariborz Jahanian0adaa8a2009-01-10 21:06:09 +00002371 assert (CD && "Missing container decl in GetNameForMethod");
2372 NameOut += CD->getNameAsString();
Fariborz Jahanianc6f0c212009-01-26 23:49:05 +00002373 // FIXME. For a method in a category, (CAT_NAME) is inserted here.
2374 // Right now! there is not enough info. to do this.
Chris Lattner3a8f2942008-11-24 03:33:13 +00002375 NameOut += ' ';
2376 NameOut += D->getSelector().getAsString();
2377 NameOut += ']';
Daniel Dunbarace33292008-08-16 03:19:19 +00002378}
2379
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00002380/// GetFirstIvarInRecord - This routine returns the record for the
2381/// implementation of the fiven class OID. It also returns field
2382/// corresponding to the first ivar in the class in FIV. It also
2383/// returns the one before the first ivar.
2384///
2385const RecordDecl *CGObjCCommonMac::GetFirstIvarInRecord(
2386 const ObjCInterfaceDecl *OID,
2387 RecordDecl::field_iterator &FIV,
2388 RecordDecl::field_iterator &PIV) {
2389 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass());
2390 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
2391 RecordDecl::field_iterator ifield = RD->field_begin();
2392 RecordDecl::field_iterator pfield = RD->field_end();
2393 while (countSuperClassIvars-- > 0) {
2394 pfield = ifield;
2395 ++ifield;
2396 }
2397 FIV = ifield;
2398 PIV = pfield;
2399 return RD;
2400}
2401
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002402void CGObjCMac::FinishModule() {
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002403 EmitModuleInfo();
2404
Daniel Dunbar35b777f2008-10-29 22:36:39 +00002405 // Emit the dummy bodies for any protocols which were referenced but
2406 // never defined.
2407 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
2408 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
2409 if (i->second->hasInitializer())
2410 continue;
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002411
Daniel Dunbar35b777f2008-10-29 22:36:39 +00002412 std::vector<llvm::Constant*> Values(5);
2413 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
2414 Values[1] = GetClassName(i->first);
2415 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
2416 Values[3] = Values[4] =
2417 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
2418 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
2419 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
2420 Values));
2421 }
2422
2423 std::vector<llvm::Constant*> Used;
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002424 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002425 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002426 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002427 }
2428
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002429 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002430 llvm::GlobalValue *GV =
2431 new llvm::GlobalVariable(AT, false,
2432 llvm::GlobalValue::AppendingLinkage,
2433 llvm::ConstantArray::get(AT, Used),
2434 "llvm.used",
2435 &CGM.getModule());
2436
2437 GV->setSection("llvm.metadata");
Daniel Dunbar8ede0052008-08-25 06:02:07 +00002438
2439 // Add assembler directives to add lazy undefined symbol references
2440 // for classes which are referenced but not defined. This is
2441 // important for correct linker interaction.
2442
2443 // FIXME: Uh, this isn't particularly portable.
2444 std::stringstream s;
Anders Carlsson63f98352008-12-10 02:21:04 +00002445
2446 if (!CGM.getModule().getModuleInlineAsm().empty())
2447 s << "\n";
2448
Daniel Dunbar8ede0052008-08-25 06:02:07 +00002449 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
2450 e = LazySymbols.end(); i != e; ++i) {
2451 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
2452 }
2453 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
2454 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002455 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar8ede0052008-08-25 06:02:07 +00002456 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
2457 }
Anders Carlsson63f98352008-12-10 02:21:04 +00002458
Daniel Dunbar8ede0052008-08-25 06:02:07 +00002459 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002460}
2461
Fariborz Jahaniand0374812009-01-22 23:02:58 +00002462CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian48543f52009-01-21 22:04:16 +00002463 : CGObjCCommonMac(cgm),
2464 ObjCTypes(cgm)
2465{
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00002466 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanian48543f52009-01-21 22:04:16 +00002467 ObjCABI = 2;
2468}
2469
Daniel Dunbar1be1df32008-08-11 21:35:06 +00002470/* *** */
2471
Fariborz Jahanian48543f52009-01-21 22:04:16 +00002472ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
2473: CGM(cgm)
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00002474{
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002475 CodeGen::CodeGenTypes &Types = CGM.getTypes();
2476 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian48543f52009-01-21 22:04:16 +00002477
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002478 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002479 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002480 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002481 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
2482
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002483 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanianc192d4d2008-11-18 20:18:11 +00002484 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002485 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002486
2487 // FIXME: It would be nice to unify this with the opaque type, so
2488 // that the IR comes out a bit cleaner.
2489 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
2490 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanian48543f52009-01-21 22:04:16 +00002491
2492 // I'm not sure I like this. The implicit coordination is a bit
2493 // gross. We should solve this in a reasonable fashion because this
2494 // is a pretty common task (match some runtime data structure with
2495 // an LLVM data structure).
2496
2497 // FIXME: This is leaked.
2498 // FIXME: Merge with rewriter code?
2499
2500 // struct _objc_super {
2501 // id self;
2502 // Class cls;
2503 // }
2504 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
2505 SourceLocation(),
2506 &Ctx.Idents.get("_objc_super"));
2507 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2508 Ctx.getObjCIdType(), 0, false));
2509 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2510 Ctx.getObjCClassType(), 0, false));
2511 RD->completeDefinition(Ctx);
2512
2513 SuperCTy = Ctx.getTagDeclType(RD);
2514 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
2515
2516 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahanian4b161702009-01-22 00:37:21 +00002517 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
2518
Fariborz Jahaniand0374812009-01-22 23:02:58 +00002519 // struct _prop_t {
2520 // char *name;
2521 // char *attributes;
2522 // }
2523 PropertyTy = llvm::StructType::get(Int8PtrTy,
2524 Int8PtrTy,
2525 NULL);
2526 CGM.getModule().addTypeName("struct._prop_t",
2527 PropertyTy);
2528
2529 // struct _prop_list_t {
Fariborz Jahanian781f2732009-01-23 01:46:23 +00002530 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahaniand0374812009-01-22 23:02:58 +00002531 // uint32_t count_of_properties;
Fariborz Jahanian781f2732009-01-23 01:46:23 +00002532 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahaniand0374812009-01-22 23:02:58 +00002533 // }
2534 PropertyListTy = llvm::StructType::get(IntTy,
2535 IntTy,
2536 llvm::ArrayType::get(PropertyTy, 0),
2537 NULL);
2538 CGM.getModule().addTypeName("struct._prop_list_t",
2539 PropertyListTy);
2540 // struct _prop_list_t *
2541 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
2542
2543 // struct _objc_method {
2544 // SEL _cmd;
2545 // char *method_type;
2546 // char *_imp;
2547 // }
2548 MethodTy = llvm::StructType::get(SelectorPtrTy,
2549 Int8PtrTy,
2550 Int8PtrTy,
2551 NULL);
2552 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahanian781f2732009-01-23 01:46:23 +00002553
2554 // struct _objc_cache *
2555 CacheTy = llvm::OpaqueType::get();
2556 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
2557 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahaniand0374812009-01-22 23:02:58 +00002558
Fariborz Jahanian4b161702009-01-22 00:37:21 +00002559 // Property manipulation functions.
2560
2561 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
2562 std::vector<const llvm::Type*> Params;
2563 Params.push_back(ObjectPtrTy);
2564 Params.push_back(SelectorPtrTy);
2565 Params.push_back(LongTy);
2566 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2567 GetPropertyFn =
2568 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2569 Params,
2570 false),
2571 "objc_getProperty");
2572
2573 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
2574 Params.clear();
2575 Params.push_back(ObjectPtrTy);
2576 Params.push_back(SelectorPtrTy);
2577 Params.push_back(LongTy);
2578 Params.push_back(ObjectPtrTy);
2579 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2580 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2581 SetPropertyFn =
2582 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2583 Params,
2584 false),
2585 "objc_setProperty");
2586 // Enumeration mutation.
2587
2588 Params.clear();
2589 Params.push_back(ObjectPtrTy);
2590 EnumerationMutationFn =
2591 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2592 Params,
2593 false),
2594 "objc_enumerationMutation");
2595
2596 // gc's API
2597 // id objc_read_weak (id *)
2598 Params.clear();
2599 Params.push_back(PtrObjectPtrTy);
2600 GcReadWeakFn =
2601 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2602 Params,
2603 false),
2604 "objc_read_weak");
2605 // id objc_assign_weak (id, id *)
2606 Params.clear();
2607 Params.push_back(ObjectPtrTy);
2608 Params.push_back(PtrObjectPtrTy);
2609 GcAssignWeakFn =
2610 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2611 Params,
2612 false),
2613 "objc_assign_weak");
2614 GcAssignGlobalFn =
2615 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2616 Params,
2617 false),
2618 "objc_assign_global");
2619 GcAssignIvarFn =
2620 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2621 Params,
2622 false),
2623 "objc_assign_ivar");
2624 GcAssignStrongCastFn =
2625 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2626 Params,
2627 false),
2628 "objc_assign_strongCast");
Fariborz Jahanian48543f52009-01-21 22:04:16 +00002629}
Daniel Dunbarb8fe21b2008-08-12 06:48:42 +00002630
Fariborz Jahanian48543f52009-01-21 22:04:16 +00002631ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
2632 : ObjCCommonTypesHelper(cgm)
2633{
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002634 // struct _objc_method_description {
2635 // SEL name;
2636 // char *types;
2637 // }
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002638 MethodDescriptionTy =
2639 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002640 Int8PtrTy,
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002641 NULL);
2642 CGM.getModule().addTypeName("struct._objc_method_description",
2643 MethodDescriptionTy);
2644
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002645 // struct _objc_method_description_list {
2646 // int count;
2647 // struct _objc_method_description[1];
2648 // }
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002649 MethodDescriptionListTy =
2650 llvm::StructType::get(IntTy,
2651 llvm::ArrayType::get(MethodDescriptionTy, 0),
2652 NULL);
2653 CGM.getModule().addTypeName("struct._objc_method_description_list",
2654 MethodDescriptionListTy);
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002655
2656 // struct _objc_method_description_list *
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002657 MethodDescriptionListPtrTy =
2658 llvm::PointerType::getUnqual(MethodDescriptionListTy);
2659
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002660 // Protocol description structures
2661
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002662 // struct _objc_protocol_extension {
2663 // uint32_t size; // sizeof(struct _objc_protocol_extension)
2664 // struct _objc_method_description_list *optional_instance_methods;
2665 // struct _objc_method_description_list *optional_class_methods;
2666 // struct _objc_property_list *instance_properties;
2667 // }
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002668 ProtocolExtensionTy =
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002669 llvm::StructType::get(IntTy,
2670 MethodDescriptionListPtrTy,
2671 MethodDescriptionListPtrTy,
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002672 PropertyListPtrTy,
2673 NULL);
2674 CGM.getModule().addTypeName("struct._objc_protocol_extension",
2675 ProtocolExtensionTy);
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002676
2677 // struct _objc_protocol_extension *
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002678 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
2679
Daniel Dunbar35b777f2008-10-29 22:36:39 +00002680 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002681
2682 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
2683 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2684
Fariborz Jahanian48543f52009-01-21 22:04:16 +00002685 const llvm::Type *T =
2686 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
2687 LongTy,
2688 llvm::ArrayType::get(ProtocolTyHolder, 0),
2689 NULL);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002690 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
2691
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002692 // struct _objc_protocol {
2693 // struct _objc_protocol_extension *isa;
2694 // char *protocol_name;
2695 // struct _objc_protocol **_objc_protocol_list;
2696 // struct _objc_method_description_list *instance_methods;
2697 // struct _objc_method_description_list *class_methods;
2698 // }
2699 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002700 Int8PtrTy,
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002701 llvm::PointerType::getUnqual(ProtocolListTyHolder),
2702 MethodDescriptionListPtrTy,
2703 MethodDescriptionListPtrTy,
2704 NULL);
2705 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
2706
2707 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
2708 CGM.getModule().addTypeName("struct._objc_protocol_list",
2709 ProtocolListTy);
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002710 // struct _objc_protocol_list *
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002711 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
2712
2713 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002714 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00002715 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002716
2717 // Class description structures
2718
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002719 // struct _objc_ivar {
2720 // char *ivar_name;
2721 // char *ivar_type;
2722 // int ivar_offset;
2723 // }
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002724 IvarTy = llvm::StructType::get(Int8PtrTy,
2725 Int8PtrTy,
2726 IntTy,
2727 NULL);
2728 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
2729
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002730 // struct _objc_ivar_list *
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002731 IvarListTy = llvm::OpaqueType::get();
2732 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
2733 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
2734
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002735 // struct _objc_method_list *
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002736 MethodListTy = llvm::OpaqueType::get();
2737 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
2738 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
2739
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002740 // struct _objc_class_extension *
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002741 ClassExtensionTy =
2742 llvm::StructType::get(IntTy,
2743 Int8PtrTy,
2744 PropertyListPtrTy,
2745 NULL);
2746 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
2747 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
2748
2749 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
2750
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002751 // struct _objc_class {
2752 // Class isa;
2753 // Class super_class;
2754 // char *name;
2755 // long version;
2756 // long info;
2757 // long instance_size;
2758 // struct _objc_ivar_list *ivars;
2759 // struct _objc_method_list *methods;
2760 // struct _objc_cache *cache;
2761 // struct _objc_protocol_list *protocols;
2762 // char *ivar_layout;
2763 // struct _objc_class_ext *ext;
2764 // };
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002765 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
2766 llvm::PointerType::getUnqual(ClassTyHolder),
2767 Int8PtrTy,
2768 LongTy,
2769 LongTy,
2770 LongTy,
2771 IvarListPtrTy,
2772 MethodListPtrTy,
2773 CachePtrTy,
2774 ProtocolListPtrTy,
2775 Int8PtrTy,
2776 ClassExtensionPtrTy,
2777 NULL);
2778 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
2779
2780 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
2781 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
2782 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
2783
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002784 // struct _objc_category {
2785 // char *category_name;
2786 // char *class_name;
2787 // struct _objc_method_list *instance_method;
2788 // struct _objc_method_list *class_method;
2789 // uint32_t size; // sizeof(struct _objc_category)
2790 // struct _objc_property_list *instance_properties;// category's @property
2791 // }
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00002792 CategoryTy = llvm::StructType::get(Int8PtrTy,
2793 Int8PtrTy,
2794 MethodListPtrTy,
2795 MethodListPtrTy,
2796 ProtocolListPtrTy,
2797 IntTy,
2798 PropertyListPtrTy,
2799 NULL);
2800 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
2801
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002802 // Global metadata structures
2803
Fariborz Jahanianb5048aa2009-01-21 00:39:53 +00002804 // struct _objc_symtab {
2805 // long sel_ref_cnt;
2806 // SEL *refs;
2807 // short cls_def_cnt;
2808 // short cat_def_cnt;
2809 // char *defs[cls_def_cnt + cat_def_cnt];
2810 // }
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002811 SymtabTy = llvm::StructType::get(LongTy,
2812 SelectorPtrTy,
2813 ShortTy,
2814 ShortTy,
Daniel Dunbar4246a8b2008-08-22 20:34:54 +00002815 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002816 NULL);
2817 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
2818 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
2819
Fariborz Jahanian4b161702009-01-22 00:37:21 +00002820 // struct _objc_module {
2821 // long version;
2822 // long size; // sizeof(struct _objc_module)
2823 // char *name;
2824 // struct _objc_symtab* symtab;
2825 // }
Daniel Dunbarb050fa62008-08-21 04:36:09 +00002826 ModuleTy =
2827 llvm::StructType::get(LongTy,
2828 LongTy,
2829 Int8PtrTy,
2830 SymtabPtrTy,
2831 NULL);
2832 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar87062ff2008-08-23 09:25:55 +00002833
Daniel Dunbarf7103722008-09-24 03:38:44 +00002834 // Message send functions.
Daniel Dunbar87062ff2008-08-23 09:25:55 +00002835
Fariborz Jahanian4b161702009-01-22 00:37:21 +00002836 // id objc_msgSend (id, SEL, ...)
Daniel Dunbar87062ff2008-08-23 09:25:55 +00002837 std::vector<const llvm::Type*> Params;
2838 Params.push_back(ObjectPtrTy);
2839 Params.push_back(SelectorPtrTy);
Daniel Dunbar4e19f4e2008-10-01 01:06:06 +00002840 MessageSendFn =
2841 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2842 Params,
2843 true),
2844 "objc_msgSend");
Daniel Dunbar87062ff2008-08-23 09:25:55 +00002845
Fariborz Jahanian4b161702009-01-22 00:37:21 +00002846 // id objc_msgSend_stret (id, SEL, ...)
Daniel Dunbar87062ff2008-08-23 09:25:55 +00002847 Params.clear();
Daniel Dunbar87062ff2008-08-23 09:25:55 +00002848 Params.push_back(ObjectPtrTy);
2849 Params.push_back(SelectorPtrTy);
2850 MessageSendStretFn =
Daniel Dunbar4e19f4e2008-10-01 01:06:06 +00002851 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2852 Params,
2853 true),
2854 "objc_msgSend_stret");
Daniel Dunbaraecef4c2008-10-17 03:24:53 +00002855
Fariborz Jahanian4b161702009-01-22 00:37:21 +00002856 //
Daniel Dunbaraecef4c2008-10-17 03:24:53 +00002857 Params.clear();
2858 Params.push_back(ObjectPtrTy);
2859 Params.push_back(SelectorPtrTy);
2860 // FIXME: This should be long double on x86_64?
Fariborz Jahanian4b161702009-01-22 00:37:21 +00002861 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
Daniel Dunbaraecef4c2008-10-17 03:24:53 +00002862 MessageSendFpretFn =
2863 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
2864 Params,
2865 true),
2866 "objc_msgSend_fpret");
Daniel Dunbar87062ff2008-08-23 09:25:55 +00002867
Fariborz Jahanian4b161702009-01-22 00:37:21 +00002868 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
Daniel Dunbar87062ff2008-08-23 09:25:55 +00002869 Params.clear();
2870 Params.push_back(SuperPtrTy);
2871 Params.push_back(SelectorPtrTy);
2872 MessageSendSuperFn =
Daniel Dunbar4e19f4e2008-10-01 01:06:06 +00002873 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2874 Params,
2875 true),
2876 "objc_msgSendSuper");
Daniel Dunbar87062ff2008-08-23 09:25:55 +00002877
Fariborz Jahanian4b161702009-01-22 00:37:21 +00002878 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
2879 // SEL op, ...)
Daniel Dunbar87062ff2008-08-23 09:25:55 +00002880 Params.clear();
2881 Params.push_back(Int8PtrTy);
2882 Params.push_back(SuperPtrTy);
2883 Params.push_back(SelectorPtrTy);
2884 MessageSendSuperStretFn =
Daniel Dunbar4e19f4e2008-10-01 01:06:06 +00002885 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2886 Params,
2887 true),
2888 "objc_msgSendSuper_stret");
Daniel Dunbaraecef4c2008-10-17 03:24:53 +00002889
2890 // There is no objc_msgSendSuper_fpret? How can that work?
2891 MessageSendSuperFpretFn = MessageSendSuperFn;
Anders Carlsson58d16242008-08-31 04:05:03 +00002892
Anders Carlsson9acb0a42008-09-09 10:10:21 +00002893 // FIXME: This is the size of the setjmp buffer and should be
2894 // target specific. 18 is what's used on 32-bit X86.
2895 uint64_t SetJmpBufferSize = 18;
2896
2897 // Exceptions
2898 const llvm::Type *StackPtrTy =
Daniel Dunbar1c5e4632008-09-27 06:32:25 +00002899 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson9acb0a42008-09-09 10:10:21 +00002900
2901 ExceptionDataTy =
2902 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
2903 SetJmpBufferSize),
2904 StackPtrTy, NULL);
2905 CGM.getModule().addTypeName("struct._objc_exception_data",
2906 ExceptionDataTy);
2907
2908 Params.clear();
2909 Params.push_back(ObjectPtrTy);
2910 ExceptionThrowFn =
Daniel Dunbar4e19f4e2008-10-01 01:06:06 +00002911 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2912 Params,
2913 false),
2914 "objc_exception_throw");
Anders Carlsson9acb0a42008-09-09 10:10:21 +00002915
2916 Params.clear();
2917 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
2918 ExceptionTryEnterFn =
Daniel Dunbar4e19f4e2008-10-01 01:06:06 +00002919 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2920 Params,
2921 false),
2922 "objc_exception_try_enter");
Anders Carlsson9acb0a42008-09-09 10:10:21 +00002923 ExceptionTryExitFn =
Daniel Dunbar4e19f4e2008-10-01 01:06:06 +00002924 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2925 Params,
2926 false),
2927 "objc_exception_try_exit");
Anders Carlsson9acb0a42008-09-09 10:10:21 +00002928 ExceptionExtractFn =
Daniel Dunbar4e19f4e2008-10-01 01:06:06 +00002929 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2930 Params,
2931 false),
2932 "objc_exception_extract");
Anders Carlsson9acb0a42008-09-09 10:10:21 +00002933
2934 Params.clear();
2935 Params.push_back(ClassPtrTy);
2936 Params.push_back(ObjectPtrTy);
2937 ExceptionMatchFn =
Daniel Dunbar4e19f4e2008-10-01 01:06:06 +00002938 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
2939 Params,
2940 false),
2941 "objc_exception_match");
Chris Lattnerdd978702008-11-15 21:26:17 +00002942
2943 // synchronized APIs
2944 // void objc_sync_enter (id)
2945 Params.clear();
2946 Params.push_back(ObjectPtrTy);
2947 SyncEnterFn =
2948 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2949 Params,
2950 false),
2951 "objc_sync_enter");
2952 // void objc_sync_exit (id)
2953 SyncExitFn =
2954 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2955 Params,
2956 false),
2957 "objc_sync_exit");
2958
Anders Carlsson9acb0a42008-09-09 10:10:21 +00002959
2960 Params.clear();
2961 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
2962 SetJmpFn =
Daniel Dunbar4e19f4e2008-10-01 01:06:06 +00002963 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
2964 Params,
2965 false),
2966 "_setjmp");
Fariborz Jahanianc192d4d2008-11-18 20:18:11 +00002967
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00002968}
2969
Fariborz Jahaniand0374812009-01-22 23:02:58 +00002970ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian48543f52009-01-21 22:04:16 +00002971: ObjCCommonTypesHelper(cgm)
2972{
Fariborz Jahaniand0374812009-01-22 23:02:58 +00002973 // struct _method_list_t {
2974 // uint32_t entsize; // sizeof(struct _objc_method)
2975 // uint32_t method_count;
2976 // struct _objc_method method_list[method_count];
2977 // }
Fariborz Jahanian781f2732009-01-23 01:46:23 +00002978 MethodListnfABITy = llvm::StructType::get(IntTy,
2979 IntTy,
2980 llvm::ArrayType::get(MethodTy, 0),
2981 NULL);
2982 CGM.getModule().addTypeName("struct.__method_list_t",
2983 MethodListnfABITy);
2984 // struct method_list_t *
2985 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahaniand0374812009-01-22 23:02:58 +00002986
2987 // struct _protocol_t {
2988 // id isa; // NULL
2989 // const char * const protocol_name;
Fariborz Jahanian781f2732009-01-23 01:46:23 +00002990 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahaniand0374812009-01-22 23:02:58 +00002991 // const struct method_list_t * const instance_methods;
2992 // const struct method_list_t * const class_methods;
2993 // const struct method_list_t *optionalInstanceMethods;
2994 // const struct method_list_t *optionalClassMethods;
Fariborz Jahanian781f2732009-01-23 01:46:23 +00002995 // const struct _prop_list_t * properties;
Fariborz Jahaniand0374812009-01-22 23:02:58 +00002996 // const uint32_t size; // sizeof(struct _protocol_t)
2997 // const uint32_t flags; // = 0
2998 // }
2999
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003000 // Holder for struct _protocol_list_t *
3001 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3002
3003 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
3004 Int8PtrTy,
3005 llvm::PointerType::getUnqual(
3006 ProtocolListTyHolder),
3007 MethodListnfABIPtrTy,
3008 MethodListnfABIPtrTy,
3009 MethodListnfABIPtrTy,
3010 MethodListnfABIPtrTy,
3011 PropertyListPtrTy,
3012 IntTy,
3013 IntTy,
3014 NULL);
3015 CGM.getModule().addTypeName("struct._protocol_t",
3016 ProtocolnfABITy);
3017
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00003018 // struct _protocol_list_t {
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003019 // long protocol_count; // Note, this is 32/64 bit
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003020 // struct _protocol_t[protocol_count];
3021 // }
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003022 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3023 llvm::ArrayType::get(
3024 ProtocolnfABITy, 0),
3025 NULL);
3026 CGM.getModule().addTypeName("struct._objc_protocol_list",
3027 ProtocolListnfABITy);
3028
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003029 // struct _objc_protocol_list*
3030 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003031
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003032 // FIXME! Is this doing the right thing?
3033 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3034 ProtocolListnfABIPtrTy);
3035
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003036 // struct _ivar_t {
3037 // unsigned long int *offset; // pointer to ivar offset location
3038 // char *name;
3039 // char *type;
3040 // uint32_t alignment;
3041 // uint32_t size;
3042 // }
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003043 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3044 Int8PtrTy,
3045 Int8PtrTy,
3046 IntTy,
3047 IntTy,
3048 NULL);
3049 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3050
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003051 // struct _ivar_list_t {
3052 // uint32 entsize; // sizeof(struct _ivar_t)
3053 // uint32 count;
3054 // struct _iver_t list[count];
3055 // }
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003056 IvarListnfABITy = llvm::StructType::get(IntTy,
3057 IntTy,
3058 llvm::ArrayType::get(
3059 IvarnfABITy, 0),
3060 NULL);
3061 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3062
3063 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003064
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003065 // struct _class_ro_t {
Fariborz Jahaniand0374812009-01-22 23:02:58 +00003066 // uint32_t const flags;
3067 // uint32_t const instanceStart;
3068 // uint32_t const instanceSize;
3069 // uint32_t const reserved; // only when building for 64bit targets
3070 // const uint8_t * const ivarLayout;
3071 // const char *const name;
3072 // const struct _method_list_t * const baseMethods;
3073 // const struct _objc_protocol_list *const baseProtocols;
3074 // const struct _ivar_list_t *const ivars;
3075 // const uint8_t * const weakIvarLayout;
3076 // const struct _prop_list_t * const properties;
3077 // }
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003078
3079 // FIXME. Add 'reserved' field in 64bit abi mode!
3080 ClassRonfABITy = llvm::StructType::get(IntTy,
3081 IntTy,
3082 IntTy,
3083 Int8PtrTy,
3084 Int8PtrTy,
3085 MethodListnfABIPtrTy,
3086 ProtocolListnfABIPtrTy,
3087 IvarListnfABIPtrTy,
3088 Int8PtrTy,
3089 PropertyListPtrTy,
3090 NULL);
3091 CGM.getModule().addTypeName("struct._class_ro_t",
3092 ClassRonfABITy);
3093
3094 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3095 std::vector<const llvm::Type*> Params;
3096 Params.push_back(ObjectPtrTy);
3097 Params.push_back(SelectorPtrTy);
3098 ImpnfABITy = llvm::PointerType::getUnqual(
3099 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3100
3101 // struct _class_t {
3102 // struct _class_t *isa;
3103 // struct _class_t * const superclass;
3104 // void *cache;
3105 // IMP *vtable;
3106 // struct class_ro_t *ro;
3107 // }
3108
3109 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3110 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3111 llvm::PointerType::getUnqual(ClassTyHolder),
3112 CachePtrTy,
3113 llvm::PointerType::getUnqual(ImpnfABITy),
3114 llvm::PointerType::getUnqual(
3115 ClassRonfABITy),
3116 NULL);
3117 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3118
3119 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3120 ClassnfABITy);
3121
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003122 // LLVM for struct _class_t *
3123 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3124
Fariborz Jahanian781f2732009-01-23 01:46:23 +00003125 // struct _category_t {
3126 // const char * const name;
3127 // struct _class_t *const cls;
3128 // const struct _method_list_t * const instance_methods;
3129 // const struct _method_list_t * const class_methods;
3130 // const struct _protocol_list_t * const protocols;
3131 // const struct _prop_list_t * const properties;
Fariborz Jahanianb9459b72009-01-23 17:41:22 +00003132 // }
3133 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003134 ClassnfABIPtrTy,
Fariborz Jahanianb9459b72009-01-23 17:41:22 +00003135 MethodListnfABIPtrTy,
3136 MethodListnfABIPtrTy,
3137 ProtocolListnfABIPtrTy,
3138 PropertyListPtrTy,
3139 NULL);
3140 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
3141
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00003142}
3143
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003144llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
3145 FinishNonFragileABIModule();
3146
3147 return NULL;
3148}
3149
3150void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
3151 // nonfragile abi has no module definition.
Fariborz Jahanian11c93dd2009-01-30 20:55:31 +00003152
3153 // Build list of all implemented classe addresses in array
3154 // L_OBJC_LABEL_CLASS_$.
3155 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$
3156 // list of 'nonlazy' implementations (defined as those with a +load{}
3157 // method!!).
3158 unsigned NumClasses = DefinedClasses.size();
3159 if (NumClasses) {
3160 std::vector<llvm::Constant*> Symbols(NumClasses);
3161 for (unsigned i=0; i<NumClasses; i++)
3162 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
3163 ObjCTypes.Int8PtrTy);
3164 llvm::Constant* Init =
3165 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3166 NumClasses),
3167 Symbols);
3168
3169 llvm::GlobalVariable *GV =
3170 new llvm::GlobalVariable(Init->getType(), false,
3171 llvm::GlobalValue::InternalLinkage,
3172 Init,
3173 "\01L_OBJC_LABEL_CLASS_$",
3174 &CGM.getModule());
3175 GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip");
3176 UsedGlobals.push_back(GV);
3177 }
3178
3179 // Build list of all implemented category addresses in array
3180 // L_OBJC_LABEL_CATEGORY_$.
3181 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$
3182 // list of 'nonlazy' category implementations (defined as those with a +load{}
3183 // method!!).
3184 unsigned NumCategory = DefinedCategories.size();
3185 if (NumCategory) {
3186 std::vector<llvm::Constant*> Symbols(NumCategory);
3187 for (unsigned i=0; i<NumCategory; i++)
3188 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i],
3189 ObjCTypes.Int8PtrTy);
3190 llvm::Constant* Init =
3191 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3192 NumCategory),
3193 Symbols);
3194
3195 llvm::GlobalVariable *GV =
3196 new llvm::GlobalVariable(Init->getType(), false,
3197 llvm::GlobalValue::InternalLinkage,
3198 Init,
3199 "\01L_OBJC_LABEL_CATEGORY_$",
3200 &CGM.getModule());
3201 GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip");
3202 UsedGlobals.push_back(GV);
3203 }
3204
Fariborz Jahanian5b2f5502009-01-30 22:07:48 +00003205 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
3206 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
3207 std::vector<llvm::Constant*> Values(2);
3208 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3209 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3210 llvm::Constant* Init = llvm::ConstantArray::get(
3211 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
3212 Values);
3213 llvm::GlobalVariable *IMGV =
3214 new llvm::GlobalVariable(Init->getType(), false,
3215 llvm::GlobalValue::InternalLinkage,
3216 Init,
3217 "\01L_OBJC_IMAGE_INFO",
3218 &CGM.getModule());
3219 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
3220 UsedGlobals.push_back(IMGV);
3221
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003222 std::vector<llvm::Constant*> Used;
3223 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
3224 e = UsedGlobals.end(); i != e; ++i) {
3225 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
3226 }
3227
3228 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
3229 llvm::GlobalValue *GV =
3230 new llvm::GlobalVariable(AT, false,
3231 llvm::GlobalValue::AppendingLinkage,
3232 llvm::ConstantArray::get(AT, Used),
3233 "llvm.used",
3234 &CGM.getModule());
3235
3236 GV->setSection("llvm.metadata");
3237
3238}
3239
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003240// Metadata flags
3241enum MetaDataDlags {
3242 CLS = 0x0,
3243 CLS_META = 0x1,
3244 CLS_ROOT = 0x2,
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00003245 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003246 CLS_EXCEPTION = 0x20
3247};
3248/// BuildClassRoTInitializer - generate meta-data for:
3249/// struct _class_ro_t {
3250/// uint32_t const flags;
3251/// uint32_t const instanceStart;
3252/// uint32_t const instanceSize;
3253/// uint32_t const reserved; // only when building for 64bit targets
3254/// const uint8_t * const ivarLayout;
3255/// const char *const name;
3256/// const struct _method_list_t * const baseMethods;
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00003257/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003258/// const struct _ivar_list_t *const ivars;
3259/// const uint8_t * const weakIvarLayout;
3260/// const struct _prop_list_t * const properties;
3261/// }
3262///
3263llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
3264 unsigned flags,
3265 unsigned InstanceStart,
3266 unsigned InstanceSize,
3267 const ObjCImplementationDecl *ID) {
3268 std::string ClassName = ID->getNameAsString();
3269 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
3270 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
3271 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
3272 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
3273 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00003274 // FIXME. ivarLayout is currently null!
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003275 Values[ 3] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3276 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00003277 // const struct _method_list_t * const baseMethods;
3278 std::vector<llvm::Constant*> Methods;
3279 std::string MethodListName("\01l_OBJC_$_");
3280 if (flags & CLS_META) {
3281 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
3282 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
3283 e = ID->classmeth_end(); i != e; ++i) {
3284 // Class methods should always be defined.
3285 Methods.push_back(GetMethodConstant(*i));
3286 }
3287 } else {
3288 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
3289 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
3290 e = ID->instmeth_end(); i != e; ++i) {
3291 // Instance methods should always be defined.
3292 Methods.push_back(GetMethodConstant(*i));
3293 }
Fariborz Jahanian78355ec2009-01-28 22:46:49 +00003294 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
3295 e = ID->propimpl_end(); i != e; ++i) {
3296 ObjCPropertyImplDecl *PID = *i;
3297
3298 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
3299 ObjCPropertyDecl *PD = PID->getPropertyDecl();
3300
3301 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
3302 if (llvm::Constant *C = GetMethodConstant(MD))
3303 Methods.push_back(C);
3304 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
3305 if (llvm::Constant *C = GetMethodConstant(MD))
3306 Methods.push_back(C);
3307 }
3308 }
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00003309 }
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00003310 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00003311 "__DATA, __objc_const", Methods);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00003312
3313 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3314 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
3315 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
3316 + OID->getNameAsString(),
3317 OID->protocol_begin(),
3318 OID->protocol_end());
3319
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00003320 if (flags & CLS_META)
3321 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3322 else
3323 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00003324 // FIXME. weakIvarLayout is currently null.
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003325 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +00003326 if (flags & CLS_META)
3327 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3328 else
3329 Values[ 9] =
3330 EmitPropertyList(
3331 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
3332 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003333 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
3334 Values);
3335 llvm::GlobalVariable *CLASS_RO_GV =
3336 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
3337 llvm::GlobalValue::InternalLinkage,
3338 Init,
3339 (flags & CLS_META) ?
3340 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
3341 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
3342 &CGM.getModule());
Fariborz Jahanianf00a1fa2009-01-30 23:15:42 +00003343 CLASS_RO_GV->setAlignment(GetPointerAlign());
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00003344 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003345 UsedGlobals.push_back(CLASS_RO_GV);
3346 return CLASS_RO_GV;
Fariborz Jahanianc98c87b2009-01-26 22:58:07 +00003347
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003348}
3349
3350/// BuildClassMetaData - This routine defines that to-level meta-data
3351/// for the given ClassName for:
3352/// struct _class_t {
3353/// struct _class_t *isa;
3354/// struct _class_t * const superclass;
3355/// void *cache;
3356/// IMP *vtable;
3357/// struct class_ro_t *ro;
3358/// }
3359///
Fariborz Jahanian06726462009-01-24 21:21:53 +00003360llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
3361 std::string &ClassName,
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003362 llvm::Constant *IsAGV,
3363 llvm::Constant *SuperClassGV,
3364 llvm::Constant *ClassRoGV) {
3365 std::vector<llvm::Constant*> Values(5);
3366 Values[0] = IsAGV;
Fariborz Jahanian06726462009-01-24 21:21:53 +00003367 Values[1] = SuperClassGV
3368 ? SuperClassGV
3369 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003370 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
3371 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
3372 Values[4] = ClassRoGV; // &CLASS_RO_GV
3373 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
3374 Values);
3375 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(ClassName);
3376 if (GV)
3377 GV->setInitializer(Init);
3378 else
3379 GV =
3380 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3381 llvm::GlobalValue::ExternalLinkage,
3382 Init,
3383 ClassName,
3384 &CGM.getModule());
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00003385 GV->setSection("__DATA, __objc_const");
Fariborz Jahanianf00a1fa2009-01-30 23:15:42 +00003386 GV->setAlignment(GetPointerAlign());
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003387 UsedGlobals.push_back(GV);
Fariborz Jahanian06726462009-01-24 21:21:53 +00003388 return GV;
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003389}
3390
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003391void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
3392 std::string ClassName = ID->getNameAsString();
3393 if (!ObjCEmptyCacheVar) {
3394 ObjCEmptyCacheVar = new llvm::GlobalVariable(
3395 ObjCTypes.CachePtrTy,
3396 false,
3397 llvm::GlobalValue::ExternalLinkage,
3398 0,
3399 "\01_objc_empty_cache",
3400 &CGM.getModule());
3401 UsedGlobals.push_back(ObjCEmptyCacheVar);
3402
3403 ObjCEmptyVtableVar = new llvm::GlobalVariable(
3404 llvm::PointerType::getUnqual(
3405 ObjCTypes.ImpnfABITy),
3406 false,
3407 llvm::GlobalValue::ExternalLinkage,
3408 0,
3409 "\01_objc_empty_vtable",
3410 &CGM.getModule());
3411 UsedGlobals.push_back(ObjCEmptyVtableVar);
3412 }
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00003413 assert(ID->getClassInterface() &&
3414 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003415 uint32_t InstanceStart =
3416 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
3417 uint32_t InstanceSize = InstanceStart;
3418 uint32_t flags = CLS_META;
3419 std::string ObjCMetaClassName("\01_OBJC_METACLASS_$_");
3420 std::string ObjCClassName("\01_OBJC_CLASS_$_");
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003421
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003422 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003423
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00003424 if (IsClassHidden(ID->getClassInterface()))
3425 flags |= OBJC2_CLS_HIDDEN;
3426 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003427 // class is root
3428 flags |= CLS_ROOT;
3429 std::string SuperClassName = ObjCClassName + ClassName;
3430 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3431 if (!SuperClassGV)
3432 SuperClassGV =
3433 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3434 llvm::GlobalValue::ExternalLinkage,
3435 0,
3436 SuperClassName,
3437 &CGM.getModule());
3438 UsedGlobals.push_back(SuperClassGV);
3439 std::string IsAClassName = ObjCMetaClassName + ClassName;
3440 IsAGV = CGM.getModule().getGlobalVariable(IsAClassName);
3441 if (!IsAGV)
3442 IsAGV =
3443 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3444 llvm::GlobalValue::ExternalLinkage,
3445 0,
3446 IsAClassName,
3447 &CGM.getModule());
3448 UsedGlobals.push_back(IsAGV);
3449 } else {
Fariborz Jahanian06726462009-01-24 21:21:53 +00003450 // Has a root. Current class is not a root.
Fariborz Jahanian4c1e4612009-01-24 20:21:50 +00003451 std::string RootClassName =
3452 ID->getClassInterface()->getSuperClass()->getNameAsString();
3453 std::string SuperClassName = ObjCMetaClassName + RootClassName;
3454 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3455 if (!SuperClassGV)
3456 SuperClassGV =
3457 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3458 llvm::GlobalValue::ExternalLinkage,
3459 0,
3460 SuperClassName,
3461 &CGM.getModule());
3462 UsedGlobals.push_back(SuperClassGV);
3463 IsAGV = SuperClassGV;
3464 }
3465 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
3466 InstanceStart,
3467 InstanceSize,ID);
Fariborz Jahanian06726462009-01-24 21:21:53 +00003468 std::string TClassName = ObjCMetaClassName + ClassName;
3469 llvm::GlobalVariable *MetaTClass =
3470 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV);
3471
3472 // Metadata for the class
3473 flags = CLS;
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00003474 if (IsClassHidden(ID->getClassInterface()))
3475 flags |= OBJC2_CLS_HIDDEN;
3476 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian06726462009-01-24 21:21:53 +00003477 flags |= CLS_ROOT;
3478 SuperClassGV = 0;
3479 }
3480 else {
3481 // Has a root. Current class is not a root.
3482 std::string RootClassName =
3483 ID->getClassInterface()->getSuperClass()->getNameAsString();
3484 std::string SuperClassName = ObjCClassName + RootClassName;
3485 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3486 if (!SuperClassGV)
3487 SuperClassGV =
3488 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3489 llvm::GlobalValue::ExternalLinkage,
3490 0,
3491 SuperClassName,
3492 &CGM.getModule());
3493 UsedGlobals.push_back(SuperClassGV);
3494
3495 }
3496
Fariborz Jahanianddd2fdd2009-01-24 23:43:01 +00003497 InstanceStart = InstanceSize = 0;
3498 if (ObjCInterfaceDecl *OID =
3499 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface())) {
3500 // FIXME. Share this with the one in EmitIvarList.
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00003501 const llvm::Type *InterfaceTy =
3502 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
3503 const llvm::StructLayout *Layout =
3504 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanianddd2fdd2009-01-24 23:43:01 +00003505
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00003506 RecordDecl::field_iterator firstField, lastField;
3507 const RecordDecl *RD = GetFirstIvarInRecord(OID, firstField, lastField);
Fariborz Jahanianddd2fdd2009-01-24 23:43:01 +00003508
3509 for (RecordDecl::field_iterator e = RD->field_end(),
3510 ifield = firstField; ifield != e; ++ifield)
3511 lastField = ifield;
3512
Fariborz Jahanianddd2fdd2009-01-24 23:43:01 +00003513 if (lastField != RD->field_end()) {
3514 FieldDecl *Field = *lastField;
3515 const llvm::Type *FieldTy =
3516 CGM.getTypes().ConvertTypeForMem(Field->getType());
3517 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3518 InstanceSize = Layout->getElementOffset(
3519 CGM.getTypes().getLLVMFieldNo(Field)) +
3520 Size;
3521 if (firstField == RD->field_end())
3522 InstanceStart = InstanceSize;
3523 else
3524 InstanceStart = Layout->getElementOffset(CGM.getTypes().
3525 getLLVMFieldNo(*firstField));
3526 }
3527 }
Fariborz Jahanian06726462009-01-24 21:21:53 +00003528 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianddd2fdd2009-01-24 23:43:01 +00003529 InstanceStart,
3530 InstanceSize,
3531 ID);
Fariborz Jahanian06726462009-01-24 21:21:53 +00003532
3533 TClassName = ObjCClassName + ClassName;
Fariborz Jahanian11c93dd2009-01-30 20:55:31 +00003534 llvm::GlobalVariable *ClassMD =
3535 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV);
3536 DefinedClasses.push_back(ClassMD);
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00003537}
3538
Fariborz Jahanian5d13ab12009-01-30 18:58:59 +00003539/// GenerateProtocolRef - This routine is called to generate code for
3540/// a protocol reference expression; as in:
3541/// @code
3542/// @protocol(Proto1);
3543/// @endcode
3544/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
3545/// which will hold address of the protocol meta-data.
3546///
3547llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
3548 const ObjCProtocolDecl *PD) {
3549
3550 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
3551 ObjCTypes.ExternalProtocolPtrTy);
3552
3553 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
3554 ProtocolName += PD->getNameAsCString();
3555
3556 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
3557 if (PTGV)
3558 return Builder.CreateLoad(PTGV, false, "tmp");
3559 PTGV = new llvm::GlobalVariable(
3560 Init->getType(), false,
3561 llvm::GlobalValue::WeakLinkage,
3562 Init,
3563 ProtocolName,
3564 &CGM.getModule());
3565 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
3566 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
3567 UsedGlobals.push_back(PTGV);
3568 return Builder.CreateLoad(PTGV, false, "tmp");
3569}
3570
Fariborz Jahanianfe49a092009-01-26 18:32:24 +00003571/// GenerateCategory - Build metadata for a category implementation.
3572/// struct _category_t {
3573/// const char * const name;
3574/// struct _class_t *const cls;
3575/// const struct _method_list_t * const instance_methods;
3576/// const struct _method_list_t * const class_methods;
3577/// const struct _protocol_list_t * const protocols;
3578/// const struct _prop_list_t * const properties;
3579/// }
3580///
3581void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD)
3582{
3583 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianc98c87b2009-01-26 22:58:07 +00003584 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
3585 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianfe49a092009-01-26 18:32:24 +00003586 "_$_" + OCD->getNameAsString());
3587 std::string ExtClassName("\01_OBJC_CLASS_$_" + Interface->getNameAsString());
3588
3589 std::vector<llvm::Constant*> Values(6);
3590 Values[0] = GetClassName(OCD->getIdentifier());
3591 // meta-class entry symbol
3592 llvm::GlobalVariable *ClassGV =
3593 CGM.getModule().getGlobalVariable(ExtClassName);
3594 if (!ClassGV)
3595 ClassGV =
3596 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3597 llvm::GlobalValue::ExternalLinkage,
3598 0,
3599 ExtClassName,
3600 &CGM.getModule());
3601 UsedGlobals.push_back(ClassGV);
3602 Values[1] = ClassGV;
Fariborz Jahanianc98c87b2009-01-26 22:58:07 +00003603 std::vector<llvm::Constant*> Methods;
3604 std::string MethodListName(Prefix);
3605 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
3606 "_$_" + OCD->getNameAsString();
3607
3608 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
3609 e = OCD->instmeth_end(); i != e; ++i) {
3610 // Instance methods should always be defined.
3611 Methods.push_back(GetMethodConstant(*i));
3612 }
3613
3614 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00003615 "__DATA, __objc_const",
Fariborz Jahanianc98c87b2009-01-26 22:58:07 +00003616 Methods);
3617
3618 MethodListName = Prefix;
3619 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
3620 OCD->getNameAsString();
3621 Methods.clear();
3622 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
3623 e = OCD->classmeth_end(); i != e; ++i) {
3624 // Class methods should always be defined.
3625 Methods.push_back(GetMethodConstant(*i));
3626 }
3627
3628 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00003629 "__DATA, __objc_const",
Fariborz Jahanianc98c87b2009-01-26 22:58:07 +00003630 Methods);
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +00003631 const ObjCCategoryDecl *Category =
3632 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian7531da62009-01-29 23:23:06 +00003633 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
3634 + Interface->getNameAsString() + "_$_"
3635 + Category->getNameAsString(),
3636 Category->protocol_begin(),
3637 Category->protocol_end());
3638
Fariborz Jahanian7b709bb2009-01-28 22:18:42 +00003639 std::string ExtName(Interface->getNameAsString() + "_$_" +
3640 OCD->getNameAsString());
3641 Values[5] =
3642 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
3643 OCD, Category, ObjCTypes);
Fariborz Jahanianfe49a092009-01-26 18:32:24 +00003644 llvm::Constant *Init =
3645 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
3646 Values);
3647 llvm::GlobalVariable *GCATV
3648 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
3649 false,
3650 llvm::GlobalValue::InternalLinkage,
3651 Init,
3652 ExtCatName,
3653 &CGM.getModule());
Fariborz Jahanianf00a1fa2009-01-30 23:15:42 +00003654 GCATV->setAlignment(GetPointerAlign());
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00003655 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianfe49a092009-01-26 18:32:24 +00003656 UsedGlobals.push_back(GCATV);
3657 DefinedCategories.push_back(GCATV);
3658}
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00003659
3660/// GetMethodConstant - Return a struct objc_method constant for the
3661/// given method if it has been defined. The result is null if the
3662/// method has not been defined. The return value has type MethodPtrTy.
3663llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
3664 const ObjCMethodDecl *MD) {
3665 // FIXME: Use DenseMap::lookup
3666 llvm::Function *Fn = MethodDefinitions[MD];
3667 if (!Fn)
3668 return 0;
3669
3670 std::vector<llvm::Constant*> Method(3);
3671 Method[0] =
3672 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
3673 ObjCTypes.SelectorPtrTy);
3674 Method[1] = GetMethodVarType(MD);
3675 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
3676 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
3677}
3678
3679/// EmitMethodList - Build meta-data for method declarations
3680/// struct _method_list_t {
3681/// uint32_t entsize; // sizeof(struct _objc_method)
3682/// uint32_t method_count;
3683/// struct _objc_method method_list[method_count];
3684/// }
3685///
3686llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
3687 const std::string &Name,
3688 const char *Section,
3689 const ConstantVector &Methods) {
3690 // Return null for empty list.
3691 if (Methods.empty())
3692 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
3693
3694 std::vector<llvm::Constant*> Values(3);
3695 // sizeof(struct _objc_method)
3696 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy);
3697 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3698 // method_count
3699 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
3700 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
3701 Methods.size());
3702 Values[2] = llvm::ConstantArray::get(AT, Methods);
3703 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
3704
3705 llvm::GlobalVariable *GV =
3706 new llvm::GlobalVariable(Init->getType(), false,
3707 llvm::GlobalValue::InternalLinkage,
3708 Init,
3709 Name,
3710 &CGM.getModule());
Fariborz Jahanianf00a1fa2009-01-30 23:15:42 +00003711 GV->setAlignment(GetPointerAlign());
Fariborz Jahaniana9502ca2009-01-26 21:38:32 +00003712 GV->setSection(Section);
3713 UsedGlobals.push_back(GV);
3714 return llvm::ConstantExpr::getBitCast(GV,
3715 ObjCTypes.MethodListnfABIPtrTy);
3716}
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00003717
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00003718llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
3719 const ObjCImplementationDecl *ID,
Fariborz Jahanian150f7732009-01-28 01:36:42 +00003720 const ObjCIvarDecl *Ivar,
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00003721 unsigned long int Offset) {
3722
3723 std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.'
Fariborz Jahanian150f7732009-01-28 01:36:42 +00003724 + Ivar->getNameAsString());
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00003725 llvm::Constant *Init = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
3726
3727 llvm::GlobalVariable *IvarOffsetGV =
3728 CGM.getModule().getGlobalVariable(ExternalName);
3729 if (IvarOffsetGV) {
3730 // ivar offset symbol already built due to user code referencing it.
3731 IvarOffsetGV->setInitializer(Init);
Fariborz Jahanian150f7732009-01-28 01:36:42 +00003732 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00003733 return IvarOffsetGV;
3734 }
3735
3736 IvarOffsetGV =
3737 new llvm::GlobalVariable(Init->getType(),
3738 false,
3739 llvm::GlobalValue::ExternalLinkage,
3740 Init,
3741 ExternalName,
3742 &CGM.getModule());
Fariborz Jahanian150f7732009-01-28 01:36:42 +00003743 // @private and @package have hidden visibility.
3744 bool globalVisibility = (Ivar->getAccessControl() == ObjCIvarDecl::Public ||
3745 Ivar->getAccessControl() == ObjCIvarDecl::Protected);
3746 if (!globalVisibility)
3747 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00003748 IvarOffsetGV->setSection("__DATA, __objc_const");
3749 UsedGlobals.push_back(IvarOffsetGV);
3750
3751 return llvm::ConstantExpr::getBitCast(
3752 IvarOffsetGV,
3753 llvm::PointerType::getUnqual(ObjCTypes.LongTy));
3754}
3755
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00003756/// EmitIvarList - Emit the ivar list for the given
3757/// implementation. If ForClass is true the list of class ivars
3758/// (i.e. metaclass ivars) is emitted, otherwise the list of
3759/// interface ivars will be emitted. The return value has type
3760/// IvarListnfABIPtrTy.
3761/// struct _ivar_t {
3762/// unsigned long int *offset; // pointer to ivar offset location
3763/// char *name;
3764/// char *type;
3765/// uint32_t alignment;
3766/// uint32_t size;
3767/// }
3768/// struct _ivar_list_t {
3769/// uint32 entsize; // sizeof(struct _ivar_t)
3770/// uint32 count;
3771/// struct _iver_t list[count];
3772/// }
3773///
3774llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
3775 const ObjCImplementationDecl *ID) {
3776
3777 std::vector<llvm::Constant*> Ivars, Ivar(5);
3778
3779 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3780 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
3781
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00003782 // FIXME. Consolidate this with similar code in GenerateClass.
3783 const llvm::Type *InterfaceTy =
3784 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(
3785 const_cast<ObjCInterfaceDecl*>(OID)));
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00003786 const llvm::StructLayout *Layout =
3787 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanianf2a94cd2009-01-28 19:12:34 +00003788
3789 RecordDecl::field_iterator i,p;
3790 const RecordDecl *RD = GetFirstIvarInRecord(OID, i,p);
Fariborz Jahanian150f7732009-01-28 01:36:42 +00003791 ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin();
3792
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00003793 for (RecordDecl::field_iterator e = RD->field_end(); i != e; ++i) {
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00003794 FieldDecl *Field = *i;
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00003795 unsigned long offset = Layout->getElementOffset(CGM.getTypes().
3796 getLLVMFieldNo(Field));
Fariborz Jahanian150f7732009-01-28 01:36:42 +00003797 const ObjCIvarDecl *ivarDecl = *I++;
3798 Ivar[0] = EmitIvarOffsetVar(ID, ivarDecl, offset);
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00003799 if (Field->getIdentifier())
3800 Ivar[1] = GetMethodVarName(Field->getIdentifier());
3801 else
3802 Ivar[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3803 std::string TypeStr;
3804 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3805 Ivar[2] = GetMethodVarType(TypeStr);
3806 const llvm::Type *FieldTy =
3807 CGM.getTypes().ConvertTypeForMem(Field->getType());
3808 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3809 unsigned Align = CGM.getContext().getPreferredTypeAlign(
3810 Field->getType().getTypePtr()) >> 3;
3811 Align = llvm::Log2_32(Align);
3812 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Fariborz Jahanian71bcf672009-01-27 22:27:56 +00003813 // NOTE. Size of a bitfield does not match gcc's, because of the way
3814 // bitfields are treated special in each. But I am told that 'size'
3815 // for bitfield ivars is ignored by the runtime so it does not matter.
3816 // (even if it matters, some day, there is enough info. to get the bitfield
3817 // right!
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00003818 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3819 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
3820 }
3821 // Return null for empty list.
3822 if (Ivars.empty())
3823 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3824 std::vector<llvm::Constant*> Values(3);
3825 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy);
3826 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3827 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
3828 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
3829 Ivars.size());
3830 Values[2] = llvm::ConstantArray::get(AT, Ivars);
3831 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
3832 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
3833 llvm::GlobalVariable *GV =
3834 new llvm::GlobalVariable(Init->getType(), false,
3835 llvm::GlobalValue::InternalLinkage,
3836 Init,
3837 Prefix + OID->getNameAsString(),
3838 &CGM.getModule());
Fariborz Jahanian28617a02009-01-30 23:51:52 +00003839 GV->setAlignment(GetPointerAlign());
Fariborz Jahanian3175ddd2009-01-28 01:05:23 +00003840 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian3f1cc562009-01-27 19:38:51 +00003841
3842 UsedGlobals.push_back(GV);
3843 return llvm::ConstantExpr::getBitCast(GV,
3844 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00003845}
3846
3847llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
3848 const ObjCProtocolDecl *PD) {
3849 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
3850
3851 if (!Entry) {
3852 // We use the initializer as a marker of whether this is a forward
3853 // reference or not. At module finalization we add the empty
3854 // contents for protocols which were referenced but never defined.
3855 Entry =
3856 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
3857 llvm::GlobalValue::ExternalLinkage,
3858 0,
3859 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(),
3860 &CGM.getModule());
3861 Entry->setSection("__DATA,__datacoal_nt,coalesced");
3862 UsedGlobals.push_back(Entry);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00003863 }
3864
3865 return Entry;
3866}
3867
3868/// GetOrEmitProtocol - Generate the protocol meta-data:
3869/// @code
3870/// struct _protocol_t {
3871/// id isa; // NULL
3872/// const char * const protocol_name;
3873/// const struct _protocol_list_t * protocol_list; // super protocols
3874/// const struct method_list_t * const instance_methods;
3875/// const struct method_list_t * const class_methods;
3876/// const struct method_list_t *optionalInstanceMethods;
3877/// const struct method_list_t *optionalClassMethods;
3878/// const struct _prop_list_t * properties;
3879/// const uint32_t size; // sizeof(struct _protocol_t)
3880/// const uint32_t flags; // = 0
3881/// }
3882/// @endcode
3883///
3884
3885llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
3886 const ObjCProtocolDecl *PD) {
3887 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
3888
3889 // Early exit if a defining object has already been generated.
3890 if (Entry && Entry->hasInitializer())
3891 return Entry;
3892
3893 const char *ProtocolName = PD->getNameAsCString();
3894
3895 // Construct method lists.
3896 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
3897 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
3898 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
3899 e = PD->instmeth_end(); i != e; ++i) {
3900 ObjCMethodDecl *MD = *i;
Fariborz Jahanian151747b2009-01-30 00:46:37 +00003901 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00003902 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
3903 OptInstanceMethods.push_back(C);
3904 } else {
3905 InstanceMethods.push_back(C);
3906 }
3907 }
3908
3909 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
3910 e = PD->classmeth_end(); i != e; ++i) {
3911 ObjCMethodDecl *MD = *i;
Fariborz Jahanian151747b2009-01-30 00:46:37 +00003912 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00003913 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
3914 OptClassMethods.push_back(C);
3915 } else {
3916 ClassMethods.push_back(C);
3917 }
3918 }
3919
3920 std::vector<llvm::Constant*> Values(10);
3921 // isa is NULL
3922 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
3923 Values[1] = GetClassName(PD->getIdentifier());
3924 Values[2] = EmitProtocolList(
3925 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
3926 PD->protocol_begin(),
3927 PD->protocol_end());
3928
Fariborz Jahanian151747b2009-01-30 00:46:37 +00003929 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00003930 + PD->getNameAsString(),
3931 "__DATA, __objc_const",
3932 InstanceMethods);
Fariborz Jahanian151747b2009-01-30 00:46:37 +00003933 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00003934 + PD->getNameAsString(),
3935 "__DATA, __objc_const",
3936 ClassMethods);
Fariborz Jahanian151747b2009-01-30 00:46:37 +00003937 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00003938 + PD->getNameAsString(),
3939 "__DATA, __objc_const",
3940 OptInstanceMethods);
Fariborz Jahanian151747b2009-01-30 00:46:37 +00003941 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00003942 + PD->getNameAsString(),
3943 "__DATA, __objc_const",
3944 OptClassMethods);
3945 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
3946 0, PD, ObjCTypes);
3947 uint32_t Size =
3948 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy);
3949 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3950 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
3951 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
3952 Values);
3953
3954 if (Entry) {
3955 // Already created, fix the linkage and update the initializer.
Fariborz Jahanianfd02a662009-01-29 20:10:59 +00003956 Entry->setLinkage(llvm::GlobalValue::WeakLinkage);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00003957 Entry->setInitializer(Init);
3958 } else {
3959 Entry =
3960 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
Fariborz Jahanianfd02a662009-01-29 20:10:59 +00003961 llvm::GlobalValue::WeakLinkage,
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00003962 Init,
3963 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName,
3964 &CGM.getModule());
Fariborz Jahanianf00a1fa2009-01-30 23:15:42 +00003965 Entry->setAlignment(GetPointerAlign());
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00003966 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00003967 }
Fariborz Jahanianfd02a662009-01-29 20:10:59 +00003968 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
3969
3970 // Use this protocol meta-data to build protocol list table in section
3971 // __DATA, __objc_protolist
3972 llvm::Type *ptype = llvm::PointerType::getUnqual(ObjCTypes.ProtocolnfABITy);
3973 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
3974 ptype, false,
3975 llvm::GlobalValue::WeakLinkage,
3976 Entry,
3977 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
3978 +ProtocolName,
3979 &CGM.getModule());
Fariborz Jahanianf00a1fa2009-01-30 23:15:42 +00003980 PTGV->setAlignment(GetPointerAlign());
Fariborz Jahanianfd02a662009-01-29 20:10:59 +00003981 PTGV->setSection("__DATA, __objc_protolist");
3982 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
3983 UsedGlobals.push_back(PTGV);
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00003984 return Entry;
3985}
3986
3987/// EmitProtocolList - Generate protocol list meta-data:
3988/// @code
3989/// struct _protocol_list_t {
3990/// long protocol_count; // Note, this is 32/64 bit
3991/// struct _protocol_t[protocol_count];
3992/// }
3993/// @endcode
3994///
3995llvm::Constant *
3996CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
3997 ObjCProtocolDecl::protocol_iterator begin,
3998 ObjCProtocolDecl::protocol_iterator end) {
3999 std::vector<llvm::Constant*> ProtocolRefs;
4000
4001 for (; begin != end; ++begin)
4002 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4003
4004 // Just return null for empty protocol lists
4005 if (ProtocolRefs.empty())
4006 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4007
4008 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4009 if (GV)
4010 return GV;
4011 // This list is null terminated.
4012 ProtocolRefs.push_back(llvm::Constant::getNullValue(
4013 ObjCTypes.ProtocolListnfABIPtrTy));
4014
4015 std::vector<llvm::Constant*> Values(2);
4016 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
4017 Values[1] =
4018 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolListnfABIPtrTy,
4019 ProtocolRefs.size()),
4020 ProtocolRefs);
4021
4022 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4023 GV = new llvm::GlobalVariable(Init->getType(), false,
4024 llvm::GlobalValue::InternalLinkage,
4025 Init,
4026 Name,
4027 &CGM.getModule());
4028 GV->setSection("__DATA, __objc_const");
Fariborz Jahanianf00a1fa2009-01-30 23:15:42 +00004029 GV->setAlignment(GetPointerAlign());
Fariborz Jahanian5fedf4f2009-01-29 19:24:30 +00004030 UsedGlobals.push_back(GV);
4031 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
4032}
4033
Fariborz Jahanian151747b2009-01-30 00:46:37 +00004034/// GetMethodDescriptionConstant - This routine build following meta-data:
4035/// struct _objc_method {
4036/// SEL _cmd;
4037/// char *method_type;
4038/// char *_imp;
4039/// }
4040
4041llvm::Constant *
4042CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4043 std::vector<llvm::Constant*> Desc(3);
4044 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4045 ObjCTypes.SelectorPtrTy);
4046 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian5d13ab12009-01-30 18:58:59 +00004047 // Protocol methods have no implementation. So, this entry is always NULL.
Fariborz Jahanian151747b2009-01-30 00:46:37 +00004048 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4049 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
4050}
Daniel Dunbardaf4ad42008-08-12 00:12:39 +00004051/* *** */
4052
Daniel Dunbarcffcdac2008-08-13 03:21:16 +00004053CodeGen::CGObjCRuntime *
4054CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00004055 return new CGObjCMac(CGM);
4056}
Fariborz Jahanian48543f52009-01-21 22:04:16 +00004057
4058CodeGen::CGObjCRuntime *
Fariborz Jahaniand0374812009-01-22 23:02:58 +00004059CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianc2a1c3e2009-01-23 23:53:38 +00004060 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanian48543f52009-01-21 22:04:16 +00004061}