blob: 1bea633589ca6e8836aa4b754e4b18439c100ddd [file] [log] [blame]
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001//===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides Objective-C code generation targetting the Apple runtime.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGObjCRuntime.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000015
16#include "CodeGenModule.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000017#include "CodeGenFunction.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000019#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000020#include "clang/AST/DeclObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000021#include "clang/Basic/LangOptions.h"
22
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000023#include "llvm/Module.h"
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +000024#include "llvm/ADT/DenseSet.h"
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000025#include "llvm/Target/TargetData.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000026#include <sstream>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000027
28using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000029using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000030
31namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000032
Daniel Dunbarae226fa2008-08-27 02:31:56 +000033 typedef std::vector<llvm::Constant*> ConstantVector;
34
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000035 // FIXME: We should find a nicer way to make the labels for
36 // metadata, string concatenation is lame.
37
Fariborz Jahanianee0af742009-01-21 22:04:16 +000038class ObjCCommonTypesHelper {
39protected:
40 CodeGen::CodeGenModule &CGM;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000041
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000042public:
Daniel Dunbar27f9d772008-08-21 04:36:09 +000043 const llvm::Type *ShortTy, *IntTy, *LongTy;
44 const llvm::Type *Int8PtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +000045
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000046 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
47 const llvm::Type *ObjectPtrTy;
Fariborz Jahanian6d657c42008-11-18 20:18:11 +000048
49 /// PtrObjectPtrTy - LLVM type for id *
50 const llvm::Type *PtrObjectPtrTy;
51
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000052 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000053 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000054 /// ProtocolPtrTy - LLVM type for external protocol handles
55 /// (typeof(Protocol))
56 const llvm::Type *ExternalProtocolPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +000057
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000058 // SuperCTy - clang type for struct objc_super.
59 QualType SuperCTy;
60 // SuperPtrCTy - clang type for struct objc_super *.
61 QualType SuperPtrCTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +000062
Daniel Dunbare8b470d2008-08-23 04:28:29 +000063 /// SuperTy - LLVM type for struct objc_super.
64 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +000065 /// SuperPtrTy - LLVM type for struct objc_super *.
66 const llvm::Type *SuperPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +000067
Fariborz Jahanian30bc5712009-01-22 23:02:58 +000068 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
69 /// in GCC parlance).
70 const llvm::StructType *PropertyTy;
71
72 /// PropertyListTy - LLVM type for struct objc_property_list
73 /// (_prop_list_t in GCC parlance).
74 const llvm::StructType *PropertyListTy;
75 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
76 const llvm::Type *PropertyListPtrTy;
77
78 // MethodTy - LLVM type for struct objc_method.
79 const llvm::StructType *MethodTy;
80
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +000081 /// CacheTy - LLVM type for struct objc_cache.
82 const llvm::Type *CacheTy;
83 /// CachePtrTy - LLVM type for struct objc_cache *.
84 const llvm::Type *CachePtrTy;
85
Fariborz Jahaniandb286862009-01-22 00:37:21 +000086 llvm::Function *GetPropertyFn, *SetPropertyFn;
87
88 llvm::Function *EnumerationMutationFn;
89
90 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
91 llvm::Function *GcReadWeakFn;
92
93 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
94 llvm::Function *GcAssignWeakFn;
95
96 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
97 llvm::Function *GcAssignGlobalFn;
98
99 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
100 llvm::Function *GcAssignIvarFn;
101
102 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
103 llvm::Function *GcAssignStrongCastFn;
104
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000105 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
106 ~ObjCCommonTypesHelper(){}
107};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000108
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000109/// ObjCTypesHelper - Helper class that encapsulates lazy
110/// construction of varies types used during ObjC generation.
111class ObjCTypesHelper : public ObjCCommonTypesHelper {
112private:
113
114 llvm::Function *MessageSendFn, *MessageSendStretFn, *MessageSendFpretFn;
115 llvm::Function *MessageSendSuperFn, *MessageSendSuperStretFn,
116 *MessageSendSuperFpretFn;
117
118public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000119 /// SymtabTy - LLVM type for struct objc_symtab.
120 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000121 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
122 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000123 /// ModuleTy - LLVM type for struct objc_module.
124 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000125
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000126 /// ProtocolTy - LLVM type for struct objc_protocol.
127 const llvm::StructType *ProtocolTy;
128 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
129 const llvm::Type *ProtocolPtrTy;
130 /// ProtocolExtensionTy - LLVM type for struct
131 /// objc_protocol_extension.
132 const llvm::StructType *ProtocolExtensionTy;
133 /// ProtocolExtensionTy - LLVM type for struct
134 /// objc_protocol_extension *.
135 const llvm::Type *ProtocolExtensionPtrTy;
136 /// MethodDescriptionTy - LLVM type for struct
137 /// objc_method_description.
138 const llvm::StructType *MethodDescriptionTy;
139 /// MethodDescriptionListTy - LLVM type for struct
140 /// objc_method_description_list.
141 const llvm::StructType *MethodDescriptionListTy;
142 /// MethodDescriptionListPtrTy - LLVM type for struct
143 /// objc_method_description_list *.
144 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000145 /// ProtocolListTy - LLVM type for struct objc_property_list.
146 const llvm::Type *ProtocolListTy;
147 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
148 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000149 /// CategoryTy - LLVM type for struct objc_category.
150 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000151 /// ClassTy - LLVM type for struct objc_class.
152 const llvm::StructType *ClassTy;
153 /// ClassPtrTy - LLVM type for struct objc_class *.
154 const llvm::Type *ClassPtrTy;
155 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
156 const llvm::StructType *ClassExtensionTy;
157 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
158 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000159 // IvarTy - LLVM type for struct objc_ivar.
160 const llvm::StructType *IvarTy;
161 /// IvarListTy - LLVM type for struct objc_ivar_list.
162 const llvm::Type *IvarListTy;
163 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
164 const llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000165 /// MethodListTy - LLVM type for struct objc_method_list.
166 const llvm::Type *MethodListTy;
167 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
168 const llvm::Type *MethodListPtrTy;
Anders Carlsson124526b2008-09-09 10:10:21 +0000169
170 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
171 const llvm::Type *ExceptionDataTy;
172
173 /// ExceptionThrowFn - LLVM objc_exception_throw function.
174 llvm::Function *ExceptionThrowFn;
175
176 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
177 llvm::Function *ExceptionTryEnterFn;
178
179 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
180 llvm::Function *ExceptionTryExitFn;
181
182 /// ExceptionExtractFn - LLVM objc_exception_extract function.
183 llvm::Function *ExceptionExtractFn;
184
185 /// ExceptionMatchFn - LLVM objc_exception_match function.
186 llvm::Function *ExceptionMatchFn;
187
188 /// SetJmpFn - LLVM _setjmp function.
189 llvm::Function *SetJmpFn;
Chris Lattner10cac6f2008-11-15 21:26:17 +0000190
191 /// SyncEnterFn - LLVM object_sync_enter function.
192 llvm::Function *SyncEnterFn;
193
194 /// SyncExitFn - LLVM object_sync_exit function.
195 llvm::Function *SyncExitFn;
196
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000197public:
198 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000199 ~ObjCTypesHelper() {}
Daniel Dunbar5669e572008-10-17 03:24:53 +0000200
201
202 llvm::Function *getSendFn(bool IsSuper) {
203 return IsSuper ? MessageSendSuperFn : MessageSendFn;
204 }
205
206 llvm::Function *getSendStretFn(bool IsSuper) {
207 return IsSuper ? MessageSendSuperStretFn : MessageSendStretFn;
208 }
209
210 llvm::Function *getSendFpretFn(bool IsSuper) {
211 return IsSuper ? MessageSendSuperFpretFn : MessageSendFpretFn;
212 }
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000213};
214
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000215/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000216/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000217class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000218public:
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000219 // MethodListnfABITy - LLVM for struct _method_list_t
220 const llvm::StructType *MethodListnfABITy;
221
222 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
223 const llvm::Type *MethodListnfABIPtrTy;
224
225 // ProtocolnfABITy = LLVM for struct _protocol_t
226 const llvm::StructType *ProtocolnfABITy;
227
228 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
229 const llvm::StructType *ProtocolListnfABITy;
230
231 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
232 const llvm::Type *ProtocolListnfABIPtrTy;
233
234 // ClassnfABITy - LLVM for struct _class_t
235 const llvm::StructType *ClassnfABITy;
236
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000237 // ClassnfABIPtrTy - LLVM for struct _class_t*
238 const llvm::Type *ClassnfABIPtrTy;
239
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000240 // IvarnfABITy - LLVM for struct _ivar_t
241 const llvm::StructType *IvarnfABITy;
242
243 // IvarListnfABITy - LLVM for struct _ivar_list_t
244 const llvm::StructType *IvarListnfABITy;
245
246 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
247 const llvm::Type *IvarListnfABIPtrTy;
248
249 // ClassRonfABITy - LLVM for struct _class_ro_t
250 const llvm::StructType *ClassRonfABITy;
251
252 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
253 const llvm::Type *ImpnfABITy;
254
255 // CategorynfABITy - LLVM for struct _category_t
256 const llvm::StructType *CategorynfABITy;
257
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000258 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
259 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000260};
261
262class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
263protected:
264 CodeGen::CodeGenModule &CGM;
265 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000266 unsigned ObjCABI;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000267
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000268 /// LazySymbols - Symbols to generate a lazy reference for. See
269 /// DefinedSymbols and FinishModule().
270 std::set<IdentifierInfo*> LazySymbols;
271
272 /// DefinedSymbols - External symbols which are defined by this
273 /// module. The symbols in this list and LazySymbols are used to add
274 /// special linker symbols which ensure that Objective-C modules are
275 /// linked properly.
276 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000277
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000278 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000279 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000280
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000281 /// MethodVarNames - uniqued method variable names.
282 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000283
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000284 /// MethodVarTypes - uniqued method type signatures. We have to use
285 /// a StringMap here because have no other unique reference.
286 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000287
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000288 /// MethodDefinitions - map of methods which have been defined in
289 /// this translation unit.
290 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000291
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000292 /// PropertyNames - uniqued method variable names.
293 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000294
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000295 /// ClassReferences - uniqued class references.
296 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000297
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000298 /// SelectorReferences - uniqued selector references.
299 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000300
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000301 /// Protocols - Protocols for which an objc_protocol structure has
302 /// been emitted. Forward declarations are handled by creating an
303 /// empty structure whose initializer is filled in when/if defined.
304 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000305
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000306 /// DefinedProtocols - Protocols which have actually been
307 /// defined. We should not need this, see FIXME in GenerateProtocol.
308 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000309
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000310 /// DefinedClasses - List of defined classes.
311 std::vector<llvm::GlobalValue*> DefinedClasses;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000312
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000313 /// DefinedCategories - List of defined categories.
314 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000315
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000316 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000317 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000318 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000319
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000320 /// GetNameForMethod - Return a name for the given method.
321 /// \param[out] NameOut - The return value.
322 void GetNameForMethod(const ObjCMethodDecl *OMD,
323 const ObjCContainerDecl *CD,
324 std::string &NameOut);
325
326 /// GetMethodVarName - Return a unique constant for the given
327 /// selector's name. The return value has type char *.
328 llvm::Constant *GetMethodVarName(Selector Sel);
329 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
330 llvm::Constant *GetMethodVarName(const std::string &Name);
331
332 /// GetMethodVarType - Return a unique constant for the given
333 /// selector's name. The return value has type char *.
334
335 // FIXME: This is a horrible name.
336 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
337 llvm::Constant *GetMethodVarType(const std::string &Name);
338
339 /// GetPropertyName - Return a unique constant for the given
340 /// name. The return value has type char *.
341 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
342
343 // FIXME: This can be dropped once string functions are unified.
344 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
345 const Decl *Container);
346
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000347 /// GetClassName - Return a unique constant for the given selector's
348 /// name. The return value has type char *.
349 llvm::Constant *GetClassName(IdentifierInfo *Ident);
350
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000351 const RecordDecl *GetFirstIvarInRecord(const ObjCInterfaceDecl *OID,
352 RecordDecl::field_iterator &FIV,
353 RecordDecl::field_iterator &PIV);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000354 /// EmitPropertyList - Emit the given property list. The return
355 /// value has type PropertyListPtrTy.
356 llvm::Constant *EmitPropertyList(const std::string &Name,
357 const Decl *Container,
358 const ObjCContainerDecl *OCD,
359 const ObjCCommonTypesHelper &ObjCTypes);
360
Fariborz Jahanianda320092009-01-29 19:24:30 +0000361 /// GetProtocolRef - Return a reference to the internal protocol
362 /// description, creating an empty one if it has not been
363 /// defined. The return value has type ProtocolPtrTy.
364 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
365
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000366public:
367 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
368 { }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000369
370 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000371
372 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
373 const ObjCContainerDecl *CD=0);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000374
375 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
376
377 /// GetOrEmitProtocol - Get the protocol object for the given
378 /// declaration, emitting it if necessary. The return value has type
379 /// ProtocolPtrTy.
380 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
381
382 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
383 /// object for the given declaration, emitting it if needed. These
384 /// forward references will be filled in with empty bodies if no
385 /// definition is seen. The return value has type ProtocolPtrTy.
386 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000387};
388
389class CGObjCMac : public CGObjCCommonMac {
390private:
391 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000392 /// EmitImageInfo - Emit the image info marker used to encode some module
393 /// level information.
394 void EmitImageInfo();
395
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000396 /// EmitModuleInfo - Another marker encoding module level
397 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000398 void EmitModuleInfo();
399
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000400 /// EmitModuleSymols - Emit module symbols, the list of defined
401 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000402 llvm::Constant *EmitModuleSymbols();
403
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000404 /// FinishModule - Write out global data structures at the end of
405 /// processing a translation unit.
406 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000407
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000408 /// EmitClassExtension - Generate the class extension structure used
409 /// to store the weak ivar layout and properties. The return value
410 /// has type ClassExtensionPtrTy.
411 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
412
413 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
414 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000415 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000416 const ObjCInterfaceDecl *ID);
417
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000418 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000419 QualType ResultType,
420 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000421 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000422 QualType Arg0Ty,
423 bool IsSuper,
424 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000425
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000426 /// EmitIvarList - Emit the ivar list for the given
427 /// implementation. If ForClass is true the list of class ivars
428 /// (i.e. metaclass ivars) is emitted, otherwise the list of
429 /// interface ivars will be emitted. The return value has type
430 /// IvarListPtrTy.
431 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000432 bool ForClass);
433
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000434 /// EmitMetaClass - Emit a forward reference to the class structure
435 /// for the metaclass of the given interface. The return value has
436 /// type ClassPtrTy.
437 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
438
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000439 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000440 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000441 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
442 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000443 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000444 const ConstantVector &Methods);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000445
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000446 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000447
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000448 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000449
450 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000451 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000452 llvm::Constant *EmitMethodList(const std::string &Name,
453 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000454 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000455
456 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000457 /// method declarations.
458 /// - TypeName: The name for the type containing the methods.
459 /// - IsProtocol: True iff these methods are for a protocol.
460 /// - ClassMethds: True iff these are class methods.
461 /// - Required: When true, only "required" methods are
462 /// listed. Similarly, when false only "optional" methods are
463 /// listed. For classes this should always be true.
464 /// - begin, end: The method list to output.
465 ///
466 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000467 llvm::Constant *EmitMethodDescList(const std::string &Name,
468 const char *Section,
469 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000470
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000471 /// GetOrEmitProtocol - Get the protocol object for the given
472 /// declaration, emitting it if necessary. The return value has type
473 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000474 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000475
476 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
477 /// object for the given declaration, emitting it if needed. These
478 /// forward references will be filled in with empty bodies if no
479 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000480 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000481
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000482 /// EmitProtocolExtension - Generate the protocol extension
483 /// structure used to store optional instance and class methods, and
484 /// protocol properties. The return value has type
485 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000486 llvm::Constant *
487 EmitProtocolExtension(const ObjCProtocolDecl *PD,
488 const ConstantVector &OptInstanceMethods,
489 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000490
491 /// EmitProtocolList - Generate the list of referenced
492 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc933702008-08-21 21:57:41 +0000493 llvm::Constant *EmitProtocolList(const std::string &Name,
494 ObjCProtocolDecl::protocol_iterator begin,
495 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000496
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000497 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
498 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000499 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000500
Fariborz Jahanianda320092009-01-29 19:24:30 +0000501 public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000502 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000503
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000504 virtual llvm::Function *ModuleInitFunction();
505
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000506 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000507 QualType ResultType,
508 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000509 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000510 bool IsClassMessage,
511 const CallArgList &CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000512
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000513 virtual CodeGen::RValue
514 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000515 QualType ResultType,
516 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000517 const ObjCInterfaceDecl *Class,
518 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000519 bool IsClassMessage,
520 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000521
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000522 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000523 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000524
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000525 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000526
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000527 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000528
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000529 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000530
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000531 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000532 const ObjCProtocolDecl *PD);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000533
Daniel Dunbar49f66022008-09-24 03:38:44 +0000534 virtual llvm::Function *GetPropertyGetFunction();
535 virtual llvm::Function *GetPropertySetFunction();
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000536 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000537
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000538 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
539 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000540 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
541 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000542 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000543 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000544 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
545 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000546 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
547 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000548 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
549 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000550 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
551 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +0000552
553 virtual llvm::Value *EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
554 llvm::Value *BaseValue,
555 const ObjCIvarDecl *Ivar,
556 const FieldDecl *Field,
557 unsigned CVRQualifiers);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000558};
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000559
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000560class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000561private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000562 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000563 llvm::GlobalVariable* ObjCEmptyCacheVar;
564 llvm::GlobalVariable* ObjCEmptyVtableVar;
565
566 /// FinishNonFragileABIModule - Write out global data structures at the end of
567 /// processing a translation unit.
568 void FinishNonFragileABIModule();
569
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000570 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
571 unsigned InstanceStart,
572 unsigned InstanceSize,
573 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +0000574 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
575 llvm::Constant *IsAGV,
576 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +0000577 llvm::Constant *ClassRoGV,
578 bool HiddenVisibility);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000579
580 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
581
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +0000582 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
583
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000584 /// EmitMethodList - Emit the method list for the given
585 /// implementation. The return value has type MethodListnfABITy.
586 llvm::Constant *EmitMethodList(const std::string &Name,
587 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +0000588 const ConstantVector &Methods);
589 /// EmitIvarList - Emit the ivar list for the given
590 /// implementation. If ForClass is true the list of class ivars
591 /// (i.e. metaclass ivars) is emitted, otherwise the list of
592 /// interface ivars will be emitted. The return value has type
593 /// IvarListnfABIPtrTy.
594 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000595
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +0000596 llvm::Constant *EmitIvarOffsetVar(const ObjCImplementationDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +0000597 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +0000598 unsigned long int offset);
599
Fariborz Jahanianda320092009-01-29 19:24:30 +0000600 /// GetOrEmitProtocol - Get the protocol object for the given
601 /// declaration, emitting it if necessary. The return value has type
602 /// ProtocolPtrTy.
603 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
604
605 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
606 /// object for the given declaration, emitting it if needed. These
607 /// forward references will be filled in with empty bodies if no
608 /// definition is seen. The return value has type ProtocolPtrTy.
609 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
610
611 /// EmitProtocolList - Generate the list of referenced
612 /// protocols. The return value has type ProtocolListPtrTy.
613 llvm::Constant *EmitProtocolList(const std::string &Name,
614 ObjCProtocolDecl::protocol_iterator begin,
615 ObjCProtocolDecl::protocol_iterator end);
616
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000617public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000618 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000619 // FIXME. All stubs for now!
620 virtual llvm::Function *ModuleInitFunction();
621
622 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
623 QualType ResultType,
624 Selector Sel,
625 llvm::Value *Receiver,
626 bool IsClassMessage,
627 const CallArgList &CallArgs)
628 {return RValue::get(0);}
629
630 virtual CodeGen::RValue
631 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
632 QualType ResultType,
633 Selector Sel,
634 const ObjCInterfaceDecl *Class,
635 llvm::Value *Receiver,
636 bool IsClassMessage,
637 const CallArgList &CallArgs){ return RValue::get(0);}
638
639 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
640 const ObjCInterfaceDecl *ID){ return 0; }
641
642 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
643 { return 0; }
644
Fariborz Jahanianeb062d92009-01-26 18:32:24 +0000645 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000646
647 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000648 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +0000649 const ObjCProtocolDecl *PD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000650
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000651 virtual llvm::Function *GetPropertyGetFunction(){ return 0; }
652 virtual llvm::Function *GetPropertySetFunction()
653 { return 0; }
654 virtual llvm::Function *EnumerationMutationFunction()
655 { return 0; }
656
657 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
658 const Stmt &S)
659 { return; }
660 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
661 const ObjCAtThrowStmt &S)
662 { return; }
663 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
664 llvm::Value *AddrWeakObj)
665 { return 0; }
666 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
667 llvm::Value *src, llvm::Value *dst)
668 { return; }
669 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
670 llvm::Value *src, llvm::Value *dest)
671 { return; }
672 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
673 llvm::Value *src, llvm::Value *dest)
674 { return; }
675 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
676 llvm::Value *src, llvm::Value *dest)
677 { return; }
Fariborz Jahanian0bb20362009-02-02 20:02:29 +0000678 virtual llvm::Value *EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
679 llvm::Value *BaseValue,
680 const ObjCIvarDecl *Ivar,
681 const FieldDecl *Field,
682 unsigned CVRQualifiers)
683 { return 0; }
684
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000685};
686
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000687} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000688
689/* *** Helper Functions *** */
690
691/// getConstantGEP() - Help routine to construct simple GEPs.
692static llvm::Constant *getConstantGEP(llvm::Constant *C,
693 unsigned idx0,
694 unsigned idx1) {
695 llvm::Value *Idxs[] = {
696 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
697 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
698 };
699 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
700}
701
702/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000703
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000704CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
705 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000706{
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000707 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000708 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000709}
710
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000711/// GetClass - Return a reference to the class for the given interface
712/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000713llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000714 const ObjCInterfaceDecl *ID) {
715 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000716}
717
718/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000719llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000720 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000721}
722
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000723/// Generate a constant CFString object.
724/*
725 struct __builtin_CFString {
726 const int *isa; // point to __CFConstantStringClassReference
727 int flags;
728 const char *str;
729 long length;
730 };
731*/
732
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000733llvm::Constant *CGObjCCommonMac::GenerateConstantString(
734 const std::string &String) {
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000735 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000736}
737
738/// Generates a message send where the super is the receiver. This is
739/// a message send to self with special delivery semantics indicating
740/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000741CodeGen::RValue
742CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000743 QualType ResultType,
744 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000745 const ObjCInterfaceDecl *Class,
746 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000747 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000748 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000749 // Create and init a super structure; this is a (receiver, class)
750 // pair we will pass to objc_msgSendSuper.
751 llvm::Value *ObjCSuper =
752 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
753 llvm::Value *ReceiverAsObject =
754 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
755 CGF.Builder.CreateStore(ReceiverAsObject,
756 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000757
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000758 // If this is a class message the metaclass is passed as the target.
759 llvm::Value *Target;
760 if (IsClassMessage) {
761 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
762 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
763 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
764 Target = Super;
765 } else {
766 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
767 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000768 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
769 // and ObjCTypes types.
770 const llvm::Type *ClassTy =
771 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000772 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000773 CGF.Builder.CreateStore(Target,
774 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
775
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000776 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000777 ObjCSuper, ObjCTypes.SuperPtrCTy,
778 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000779}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000780
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000781/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000782CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000783 QualType ResultType,
784 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000785 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000786 bool IsClassMessage,
787 const CallArgList &CallArgs) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000788 llvm::Value *Arg0 =
789 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000790 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000791 Arg0, CGF.getContext().getObjCIdType(),
792 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000793}
794
795CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000796 QualType ResultType,
797 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000798 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000799 QualType Arg0Ty,
800 bool IsSuper,
801 const CallArgList &CallArgs) {
802 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000803 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
804 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
805 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000806 CGF.getContext().getObjCSelType()));
807 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000808
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000809 const llvm::FunctionType *FTy =
Daniel Dunbar725ad312009-01-31 02:19:00 +0000810 CGM.getTypes().GetFunctionType(CGFunctionInfo(ResultType, ActualArgs,
811 false));
Daniel Dunbar5669e572008-10-17 03:24:53 +0000812
813 llvm::Constant *Fn;
814 if (CGM.ReturnTypeUsesSret(ResultType)) {
815 Fn = ObjCTypes.getSendStretFn(IsSuper);
816 } else if (ResultType->isFloatingType()) {
817 // FIXME: Sadly, this is wrong. This actually depends on the
818 // architecture. This happens to be right for x86-32 though.
819 Fn = ObjCTypes.getSendFpretFn(IsSuper);
820 } else {
821 Fn = ObjCTypes.getSendFn(IsSuper);
822 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000823 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar3913f182008-09-09 23:48:28 +0000824 return CGF.EmitCall(Fn, ResultType, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000825}
826
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000827llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000828 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +0000829 // FIXME: I don't understand why gcc generates this, or where it is
830 // resolved. Investigate. Its also wasteful to look this up over and
831 // over.
832 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
833
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000834 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
835 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000836}
837
Fariborz Jahanianda320092009-01-29 19:24:30 +0000838void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000839 // FIXME: We shouldn't need this, the protocol decl should contain
840 // enough information to tell us whether this was a declaration or a
841 // definition.
842 DefinedProtocols.insert(PD->getIdentifier());
843
844 // If we have generated a forward reference to this protocol, emit
845 // it now. Otherwise do nothing, the protocol objects are lazily
846 // emitted.
847 if (Protocols.count(PD->getIdentifier()))
848 GetOrEmitProtocol(PD);
849}
850
Fariborz Jahanianda320092009-01-29 19:24:30 +0000851llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000852 if (DefinedProtocols.count(PD->getIdentifier()))
853 return GetOrEmitProtocol(PD);
854 return GetOrEmitProtocolRef(PD);
855}
856
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000857/*
858 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
859 struct _objc_protocol {
860 struct _objc_protocol_extension *isa;
861 char *protocol_name;
862 struct _objc_protocol_list *protocol_list;
863 struct _objc__method_prototype_list *instance_methods;
864 struct _objc__method_prototype_list *class_methods
865 };
866
867 See EmitProtocolExtension().
868*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000869llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
870 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
871
872 // Early exit if a defining object has already been generated.
873 if (Entry && Entry->hasInitializer())
874 return Entry;
875
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000876 // FIXME: I don't understand why gcc generates this, or where it is
877 // resolved. Investigate. Its also wasteful to look this up over and
878 // over.
879 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
880
Chris Lattner8ec03f52008-11-24 03:54:41 +0000881 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000882
883 // Construct method lists.
884 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
885 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
886 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
887 e = PD->instmeth_end(); i != e; ++i) {
888 ObjCMethodDecl *MD = *i;
889 llvm::Constant *C = GetMethodDescriptionConstant(MD);
890 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
891 OptInstanceMethods.push_back(C);
892 } else {
893 InstanceMethods.push_back(C);
894 }
895 }
896
897 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
898 e = PD->classmeth_end(); i != e; ++i) {
899 ObjCMethodDecl *MD = *i;
900 llvm::Constant *C = GetMethodDescriptionConstant(MD);
901 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
902 OptClassMethods.push_back(C);
903 } else {
904 ClassMethods.push_back(C);
905 }
906 }
907
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000908 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000909 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000910 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +0000911 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000912 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +0000913 PD->protocol_begin(),
914 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000915 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000916 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
917 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000918 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
919 InstanceMethods);
920 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000921 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
922 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000923 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
924 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000925 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
926 Values);
927
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000928 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000929 // Already created, fix the linkage and update the initializer.
930 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000931 Entry->setInitializer(Init);
932 } else {
933 Entry =
934 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
935 llvm::GlobalValue::InternalLinkage,
936 Init,
937 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
938 &CGM.getModule());
939 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
940 UsedGlobals.push_back(Entry);
941 // FIXME: Is this necessary? Why only for protocol?
942 Entry->setAlignment(4);
943 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000944
945 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000946}
947
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000948llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000949 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
950
951 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000952 // We use the initializer as a marker of whether this is a forward
953 // reference or not. At module finalization we add the empty
954 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000955 Entry =
956 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000957 llvm::GlobalValue::ExternalLinkage,
958 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000959 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000960 &CGM.getModule());
961 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
962 UsedGlobals.push_back(Entry);
963 // FIXME: Is this necessary? Why only for protocol?
964 Entry->setAlignment(4);
965 }
966
967 return Entry;
968}
969
970/*
971 struct _objc_protocol_extension {
972 uint32_t size;
973 struct objc_method_description_list *optional_instance_methods;
974 struct objc_method_description_list *optional_class_methods;
975 struct objc_property_list *instance_properties;
976 };
977*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000978llvm::Constant *
979CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
980 const ConstantVector &OptInstanceMethods,
981 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000982 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +0000983 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000984 std::vector<llvm::Constant*> Values(4);
985 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000986 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000987 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
988 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000989 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
990 OptInstanceMethods);
991 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000992 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
993 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000994 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
995 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000996 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
997 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000998 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000999
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001000 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001001 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1002 Values[3]->isNullValue())
1003 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
1004
1005 llvm::Constant *Init =
1006 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
1007 llvm::GlobalVariable *GV =
1008 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
1009 llvm::GlobalValue::InternalLinkage,
1010 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001011 "\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001012 &CGM.getModule());
1013 // No special section, but goes in llvm.used
1014 UsedGlobals.push_back(GV);
1015
1016 return GV;
1017}
1018
1019/*
1020 struct objc_protocol_list {
1021 struct objc_protocol_list *next;
1022 long count;
1023 Protocol *list[];
1024 };
1025*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001026llvm::Constant *
1027CGObjCMac::EmitProtocolList(const std::string &Name,
1028 ObjCProtocolDecl::protocol_iterator begin,
1029 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001030 std::vector<llvm::Constant*> ProtocolRefs;
1031
Daniel Dunbardbc933702008-08-21 21:57:41 +00001032 for (; begin != end; ++begin)
1033 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001034
1035 // Just return null for empty protocol lists
1036 if (ProtocolRefs.empty())
1037 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1038
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001039 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001040 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1041
1042 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001043 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001044 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1045 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1046 Values[2] =
1047 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1048 ProtocolRefs.size()),
1049 ProtocolRefs);
1050
1051 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1052 llvm::GlobalVariable *GV =
1053 new llvm::GlobalVariable(Init->getType(), false,
1054 llvm::GlobalValue::InternalLinkage,
1055 Init,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001056 Name,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001057 &CGM.getModule());
1058 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
1059 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1060}
1061
1062/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001063 struct _objc_property {
1064 const char * const name;
1065 const char * const attributes;
1066 };
1067
1068 struct _objc_property_list {
1069 uint32_t entsize; // sizeof (struct _objc_property)
1070 uint32_t prop_count;
1071 struct _objc_property[prop_count];
1072 };
1073*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001074llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1075 const Decl *Container,
1076 const ObjCContainerDecl *OCD,
1077 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001078 std::vector<llvm::Constant*> Properties, Prop(2);
Steve Naroff93983f82009-01-11 12:47:58 +00001079 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1080 E = OCD->prop_end(); I != E; ++I) {
1081 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001082 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001083 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001084 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1085 Prop));
1086 }
1087
1088 // Return null for empty list.
1089 if (Properties.empty())
1090 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1091
1092 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001093 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001094 std::vector<llvm::Constant*> Values(3);
1095 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1096 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1097 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1098 Properties.size());
1099 Values[2] = llvm::ConstantArray::get(AT, Properties);
1100 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1101
1102 llvm::GlobalVariable *GV =
1103 new llvm::GlobalVariable(Init->getType(), false,
1104 llvm::GlobalValue::InternalLinkage,
1105 Init,
1106 Name,
1107 &CGM.getModule());
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001108 if (ObjCABI == 2)
1109 GV->setSection("__DATA, __objc_const");
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001110 // No special section on property lists?
1111 UsedGlobals.push_back(GV);
1112 return llvm::ConstantExpr::getBitCast(GV,
1113 ObjCTypes.PropertyListPtrTy);
1114
1115}
1116
1117/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001118 struct objc_method_description_list {
1119 int count;
1120 struct objc_method_description list[];
1121 };
1122*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001123llvm::Constant *
1124CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1125 std::vector<llvm::Constant*> Desc(2);
1126 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1127 ObjCTypes.SelectorPtrTy);
1128 Desc[1] = GetMethodVarType(MD);
1129 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1130 Desc);
1131}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001132
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001133llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1134 const char *Section,
1135 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001136 // Return null for empty list.
1137 if (Methods.empty())
1138 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1139
1140 std::vector<llvm::Constant*> Values(2);
1141 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1142 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1143 Methods.size());
1144 Values[1] = llvm::ConstantArray::get(AT, Methods);
1145 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1146
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001147 llvm::GlobalVariable *GV =
1148 new llvm::GlobalVariable(Init->getType(), false,
1149 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001150 Init, Name, &CGM.getModule());
1151 GV->setSection(Section);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001152 UsedGlobals.push_back(GV);
1153 return llvm::ConstantExpr::getBitCast(GV,
1154 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001155}
1156
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001157/*
1158 struct _objc_category {
1159 char *category_name;
1160 char *class_name;
1161 struct _objc_method_list *instance_methods;
1162 struct _objc_method_list *class_methods;
1163 struct _objc_protocol_list *protocols;
1164 uint32_t size; // <rdar://4585769>
1165 struct _objc_property_list *instance_properties;
1166 };
1167 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001168void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001169 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001170
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001171 // FIXME: This is poor design, the OCD should have a pointer to the
1172 // category decl. Additionally, note that Category can be null for
1173 // the @implementation w/o an @interface case. Sema should just
1174 // create one for us as it does for @implementation so everyone else
1175 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001176 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001177 const ObjCCategoryDecl *Category =
1178 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001179 std::string ExtName(Interface->getNameAsString() + "_" +
1180 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001181
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001182 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1183 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
1184 e = OCD->instmeth_end(); i != e; ++i) {
1185 // Instance methods should always be defined.
1186 InstanceMethods.push_back(GetMethodConstant(*i));
1187 }
1188 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
1189 e = OCD->classmeth_end(); i != e; ++i) {
1190 // Class methods should always be defined.
1191 ClassMethods.push_back(GetMethodConstant(*i));
1192 }
1193
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001194 std::vector<llvm::Constant*> Values(7);
1195 Values[0] = GetClassName(OCD->getIdentifier());
1196 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001197 Values[2] =
1198 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1199 ExtName,
1200 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001201 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001202 Values[3] =
1203 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
1204 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001205 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001206 if (Category) {
1207 Values[4] =
1208 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1209 Category->protocol_begin(),
1210 Category->protocol_end());
1211 } else {
1212 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1213 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001214 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001215
1216 // If there is no category @interface then there can be no properties.
1217 if (Category) {
1218 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001219 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001220 } else {
1221 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1222 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001223
1224 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1225 Values);
1226
1227 llvm::GlobalVariable *GV =
1228 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
1229 llvm::GlobalValue::InternalLinkage,
1230 Init,
1231 std::string("\01L_OBJC_CATEGORY_")+ExtName,
1232 &CGM.getModule());
1233 GV->setSection("__OBJC,__category,regular,no_dead_strip");
1234 UsedGlobals.push_back(GV);
1235 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001236}
1237
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001238// FIXME: Get from somewhere?
1239enum ClassFlags {
1240 eClassFlags_Factory = 0x00001,
1241 eClassFlags_Meta = 0x00002,
1242 // <rdr://5142207>
1243 eClassFlags_HasCXXStructors = 0x02000,
1244 eClassFlags_Hidden = 0x20000,
1245 eClassFlags_ABI2_Hidden = 0x00010,
1246 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1247};
1248
1249// <rdr://5142207&4705298&4843145>
1250static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
1251 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
1252 // FIXME: Support -fvisibility
1253 switch (attr->getVisibility()) {
1254 default:
1255 assert(0 && "Unknown visibility");
1256 return false;
1257 case VisibilityAttr::DefaultVisibility:
1258 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
1259 return false;
1260 case VisibilityAttr::HiddenVisibility:
1261 return true;
1262 }
1263 } else {
1264 return false; // FIXME: Support -fvisibility
1265 }
1266}
1267
1268/*
1269 struct _objc_class {
1270 Class isa;
1271 Class super_class;
1272 const char *name;
1273 long version;
1274 long info;
1275 long instance_size;
1276 struct _objc_ivar_list *ivars;
1277 struct _objc_method_list *methods;
1278 struct _objc_cache *cache;
1279 struct _objc_protocol_list *protocols;
1280 // Objective-C 1.0 extensions (<rdr://4585769>)
1281 const char *ivar_layout;
1282 struct _objc_class_ext *ext;
1283 };
1284
1285 See EmitClassExtension();
1286 */
1287void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001288 DefinedSymbols.insert(ID->getIdentifier());
1289
Chris Lattner8ec03f52008-11-24 03:54:41 +00001290 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001291 // FIXME: Gross
1292 ObjCInterfaceDecl *Interface =
1293 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001294 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001295 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001296 Interface->protocol_begin(),
1297 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001298 const llvm::Type *InterfaceTy =
1299 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
1300 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001301 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001302
1303 // FIXME: Set CXX-structors flag.
1304 if (IsClassHidden(ID->getClassInterface()))
1305 Flags |= eClassFlags_Hidden;
1306
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001307 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1308 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
1309 e = ID->instmeth_end(); i != e; ++i) {
1310 // Instance methods should always be defined.
1311 InstanceMethods.push_back(GetMethodConstant(*i));
1312 }
1313 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
1314 e = ID->classmeth_end(); i != e; ++i) {
1315 // Class methods should always be defined.
1316 ClassMethods.push_back(GetMethodConstant(*i));
1317 }
1318
1319 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
1320 e = ID->propimpl_end(); i != e; ++i) {
1321 ObjCPropertyImplDecl *PID = *i;
1322
1323 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1324 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1325
1326 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1327 if (llvm::Constant *C = GetMethodConstant(MD))
1328 InstanceMethods.push_back(C);
1329 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1330 if (llvm::Constant *C = GetMethodConstant(MD))
1331 InstanceMethods.push_back(C);
1332 }
1333 }
1334
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001335 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001336 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001337 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001338 // Record a reference to the super class.
1339 LazySymbols.insert(Super->getIdentifier());
1340
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001341 Values[ 1] =
1342 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1343 ObjCTypes.ClassPtrTy);
1344 } else {
1345 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1346 }
1347 Values[ 2] = GetClassName(ID->getIdentifier());
1348 // Version is always 0.
1349 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1350 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1351 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001352 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001353 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001354 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001355 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001356 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001357 // cache is always NULL.
1358 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1359 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001360 // FIXME: Set ivar_layout
1361 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001362 Values[11] = EmitClassExtension(ID);
1363 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1364 Values);
1365
1366 llvm::GlobalVariable *GV =
1367 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1368 llvm::GlobalValue::InternalLinkage,
1369 Init,
1370 std::string("\01L_OBJC_CLASS_")+ClassName,
1371 &CGM.getModule());
1372 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1373 UsedGlobals.push_back(GV);
1374 // FIXME: Why?
1375 GV->setAlignment(32);
1376 DefinedClasses.push_back(GV);
1377}
1378
1379llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1380 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001381 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001382 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001383 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001384 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001385
1386 if (IsClassHidden(ID->getClassInterface()))
1387 Flags |= eClassFlags_Hidden;
1388
1389 std::vector<llvm::Constant*> Values(12);
1390 // The isa for the metaclass is the root of the hierarchy.
1391 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1392 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1393 Root = Super;
1394 Values[ 0] =
1395 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1396 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001397 // The super class for the metaclass is emitted as the name of the
1398 // super class. The runtime fixes this up to point to the
1399 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001400 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1401 Values[ 1] =
1402 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1403 ObjCTypes.ClassPtrTy);
1404 } else {
1405 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1406 }
1407 Values[ 2] = GetClassName(ID->getIdentifier());
1408 // Version is always 0.
1409 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1410 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1411 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001412 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001413 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001414 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001415 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001416 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001417 // cache is always NULL.
1418 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1419 Values[ 9] = Protocols;
1420 // ivar_layout for metaclass is always NULL.
1421 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1422 // The class extension is always unused for metaclasses.
1423 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1424 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1425 Values);
1426
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001427 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00001428 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001429
1430 // Check for a forward reference.
1431 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1432 if (GV) {
1433 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1434 "Forward metaclass reference has incorrect type.");
1435 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1436 GV->setInitializer(Init);
1437 } else {
1438 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1439 llvm::GlobalValue::InternalLinkage,
1440 Init, Name,
1441 &CGM.getModule());
1442 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001443 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1444 UsedGlobals.push_back(GV);
1445 // FIXME: Why?
1446 GV->setAlignment(32);
1447
1448 return GV;
1449}
1450
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001451llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001452 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001453
1454 // FIXME: Should we look these up somewhere other than the
1455 // module. Its a bit silly since we only generate these while
1456 // processing an implementation, so exactly one pointer would work
1457 // if know when we entered/exitted an implementation block.
1458
1459 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00001460 // Previously, metaclass with internal linkage may have been defined.
1461 // pass 'true' as 2nd argument so it is returned.
1462 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001463 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1464 "Forward metaclass reference has incorrect type.");
1465 return GV;
1466 } else {
1467 // Generate as an external reference to keep a consistent
1468 // module. This will be patched up when we emit the metaclass.
1469 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1470 llvm::GlobalValue::ExternalLinkage,
1471 0,
1472 Name,
1473 &CGM.getModule());
1474 }
1475}
1476
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001477/*
1478 struct objc_class_ext {
1479 uint32_t size;
1480 const char *weak_ivar_layout;
1481 struct _objc_property_list *properties;
1482 };
1483*/
1484llvm::Constant *
1485CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1486 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001487 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001488
1489 std::vector<llvm::Constant*> Values(3);
1490 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001491 // FIXME: Output weak_ivar_layout string.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001492 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001493 Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001494 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001495
1496 // Return null if no extension bits are used.
1497 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1498 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1499
1500 llvm::Constant *Init =
1501 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1502 llvm::GlobalVariable *GV =
1503 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1504 llvm::GlobalValue::InternalLinkage,
1505 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001506 "\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001507 &CGM.getModule());
1508 // No special section, but goes in llvm.used
1509 UsedGlobals.push_back(GV);
1510
1511 return GV;
1512}
1513
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001514/// countInheritedIvars - count number of ivars in class and its super class(s)
1515///
1516static int countInheritedIvars(const ObjCInterfaceDecl *OI) {
1517 int count = 0;
1518 if (!OI)
1519 return 0;
1520 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
1521 if (SuperClass)
1522 count += countInheritedIvars(SuperClass);
1523 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1524 E = OI->ivar_end(); I != E; ++I)
1525 ++count;
1526 return count;
1527}
1528
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001529/*
1530 struct objc_ivar {
1531 char *ivar_name;
1532 char *ivar_type;
1533 int ivar_offset;
1534 };
1535
1536 struct objc_ivar_list {
1537 int ivar_count;
1538 struct objc_ivar list[count];
1539 };
1540 */
1541llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001542 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001543 std::vector<llvm::Constant*> Ivars, Ivar(3);
1544
1545 // When emitting the root class GCC emits ivar entries for the
1546 // actual class structure. It is not clear if we need to follow this
1547 // behavior; for now lets try and get away with not doing it. If so,
1548 // the cleanest solution would be to make up an ObjCInterfaceDecl
1549 // for the class.
1550 if (ForClass)
1551 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001552
1553 ObjCInterfaceDecl *OID =
1554 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
1555 const llvm::Type *InterfaceTy =
1556 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001557 const llvm::StructLayout *Layout =
1558 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001559
1560 RecordDecl::field_iterator ifield, pfield;
1561 const RecordDecl *RD = GetFirstIvarInRecord(OID, ifield, pfield);
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001562 for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) {
1563 FieldDecl *Field = *ifield;
1564 unsigned Offset = Layout->getElementOffset(CGM.getTypes().
1565 getLLVMFieldNo(Field));
1566 if (Field->getIdentifier())
1567 Ivar[0] = GetMethodVarName(Field->getIdentifier());
1568 else
1569 Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001570 std::string TypeStr;
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001571 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001572 Ivar[1] = GetMethodVarType(TypeStr);
1573 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001574 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001575 }
1576
1577 // Return null for empty list.
1578 if (Ivars.empty())
1579 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1580
1581 std::vector<llvm::Constant*> Values(2);
1582 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1583 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1584 Ivars.size());
1585 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1586 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1587
1588 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1589 "\01L_OBJC_INSTANCE_VARIABLES_");
1590 llvm::GlobalVariable *GV =
1591 new llvm::GlobalVariable(Init->getType(), false,
1592 llvm::GlobalValue::InternalLinkage,
1593 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001594 Prefix + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001595 &CGM.getModule());
1596 if (ForClass) {
1597 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1598 // FIXME: Why is this only here?
1599 GV->setAlignment(32);
1600 } else {
1601 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1602 }
1603 UsedGlobals.push_back(GV);
1604 return llvm::ConstantExpr::getBitCast(GV,
1605 ObjCTypes.IvarListPtrTy);
1606}
1607
1608/*
1609 struct objc_method {
1610 SEL method_name;
1611 char *method_types;
1612 void *method;
1613 };
1614
1615 struct objc_method_list {
1616 struct objc_method_list *obsolete;
1617 int count;
1618 struct objc_method methods_list[count];
1619 };
1620*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001621
1622/// GetMethodConstant - Return a struct objc_method constant for the
1623/// given method if it has been defined. The result is null if the
1624/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001625llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001626 // FIXME: Use DenseMap::lookup
1627 llvm::Function *Fn = MethodDefinitions[MD];
1628 if (!Fn)
1629 return 0;
1630
1631 std::vector<llvm::Constant*> Method(3);
1632 Method[0] =
1633 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1634 ObjCTypes.SelectorPtrTy);
1635 Method[1] = GetMethodVarType(MD);
1636 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1637 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1638}
1639
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001640llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1641 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001642 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001643 // Return null for empty list.
1644 if (Methods.empty())
1645 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1646
1647 std::vector<llvm::Constant*> Values(3);
1648 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1649 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1650 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1651 Methods.size());
1652 Values[2] = llvm::ConstantArray::get(AT, Methods);
1653 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1654
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001655 llvm::GlobalVariable *GV =
1656 new llvm::GlobalVariable(Init->getType(), false,
1657 llvm::GlobalValue::InternalLinkage,
1658 Init,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001659 Name,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001660 &CGM.getModule());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001661 GV->setSection(Section);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001662 UsedGlobals.push_back(GV);
1663 return llvm::ConstantExpr::getBitCast(GV,
1664 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001665}
1666
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001667llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001668 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001669 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001670 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001671
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001672 const llvm::FunctionType *MethodTy =
1673 CGM.getTypes().GetFunctionType(CGFunctionInfo(OMD, CGM.getContext()));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001674 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001675 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001676 llvm::GlobalValue::InternalLinkage,
1677 Name,
1678 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001679 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001680
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001681 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001682}
1683
1684llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001685 // Abuse this interface function as a place to finalize.
1686 FinishModule();
1687
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001688 return NULL;
1689}
1690
Daniel Dunbar49f66022008-09-24 03:38:44 +00001691llvm::Function *CGObjCMac::GetPropertyGetFunction() {
1692 return ObjCTypes.GetPropertyFn;
1693}
1694
1695llvm::Function *CGObjCMac::GetPropertySetFunction() {
1696 return ObjCTypes.SetPropertyFn;
1697}
1698
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001699llvm::Function *CGObjCMac::EnumerationMutationFunction()
1700{
1701 return ObjCTypes.EnumerationMutationFn;
1702}
1703
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001704/*
1705
1706Objective-C setjmp-longjmp (sjlj) Exception Handling
1707--
1708
1709The basic framework for a @try-catch-finally is as follows:
1710{
1711 objc_exception_data d;
1712 id _rethrow = null;
1713
1714 objc_exception_try_enter(&d);
1715 if (!setjmp(d.jmp_buf)) {
1716 ... try body ...
1717 } else {
1718 // exception path
1719 id _caught = objc_exception_extract(&d);
1720
1721 // enter new try scope for handlers
1722 if (!setjmp(d.jmp_buf)) {
1723 ... match exception and execute catch blocks ...
1724
1725 // fell off end, rethrow.
1726 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00001727 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001728 } else {
1729 // exception in catch block
1730 _rethrow = objc_exception_extract(&d);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001731 ... jump-through-finally_no_exit to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001732 }
1733 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00001734 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001735
1736finally:
1737 // match either the initial try_enter or the catch try_enter,
1738 // depending on the path followed.
1739 objc_exception_try_exit(&d);
1740finally_no_exit:
1741 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00001742 ... dispatch to finally destination ...
1743
1744finally_rethrow:
1745 objc_exception_throw(_rethrow);
1746
1747finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001748}
1749
1750This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00001751uses _rethrow to determine if objc_exception_try_exit should be called
1752and if the object should be rethrown. This breaks in the face of
1753throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001754
1755We specialize this framework for a few particular circumstances:
1756
1757 - If there are no catch blocks, then we avoid emitting the second
1758 exception handling context.
1759
1760 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
1761 e)) we avoid emitting the code to rethrow an uncaught exception.
1762
1763 - FIXME: If there is no @finally block we can do a few more
1764 simplifications.
1765
1766Rethrows and Jumps-Through-Finally
1767--
1768
1769Support for implicit rethrows and jumping through the finally block is
1770handled by storing the current exception-handling context in
1771ObjCEHStack.
1772
Daniel Dunbar898d5082008-09-30 01:06:03 +00001773In order to implement proper @finally semantics, we support one basic
1774mechanism for jumping through the finally block to an arbitrary
1775destination. Constructs which generate exits from a @try or @catch
1776block use this mechanism to implement the proper semantics by chaining
1777jumps, as necessary.
1778
1779This mechanism works like the one used for indirect goto: we
1780arbitrarily assign an ID to each destination and store the ID for the
1781destination in a variable prior to entering the finally block. At the
1782end of the finally block we simply create a switch to the proper
1783destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001784
1785Code gen for @synchronized(expr) stmt;
1786Effectively generating code for:
1787objc_sync_enter(expr);
1788@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001789*/
1790
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001791void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1792 const Stmt &S) {
1793 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001794 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001795 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
1796 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
1797 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1798 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001799 llvm::Value *DestCode =
1800 CGF.CreateTempAlloca(llvm::Type::Int32Ty, "finally.dst");
1801
1802 // Generate jump code. Done here so we can directly add things to
1803 // the switch instruction.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001804 llvm::BasicBlock *FinallyJump = CGF.createBasicBlock("finally.jump");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001805 llvm::SwitchInst *FinallySwitch =
1806 llvm::SwitchInst::Create(new llvm::LoadInst(DestCode, "", FinallyJump),
1807 FinallyEnd, 10, FinallyJump);
1808
1809 // Push an EH context entry, used for handling rethrows and jumps
1810 // through finally.
1811 CodeGenFunction::ObjCEHEntry EHEntry(FinallyBlock, FinallyNoExit,
1812 FinallySwitch, DestCode);
1813 CGF.ObjCEHStack.push_back(&EHEntry);
1814
1815 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00001816 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
1817 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001818 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
1819 "_rethrow");
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001820 if (!isTry) {
1821 // For @synchronized, call objc_sync_enter(sync.expr)
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001822 llvm::Value *Arg = CGF.EmitScalarExpr(
1823 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1824 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1825 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, Arg);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001826 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001827
1828 // Enter a new try block and call setjmp.
1829 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
1830 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
1831 "jmpbufarray");
1832 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
1833 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1834 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001835
Daniel Dunbar55e87422008-11-11 02:29:29 +00001836 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1837 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001838 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001839 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001840
1841 // Emit the @try block.
1842 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001843 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
1844 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001845 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001846
1847 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001848 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001849
1850 // Retrieve the exception object. We may emit multiple blocks but
1851 // nothing can cross this so the value is already in SSA form.
1852 llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1853 ExceptionData,
1854 "caught");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001855 EHEntry.Exception = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001856 if (!isTry)
1857 {
1858 CGF.Builder.CreateStore(Caught, RethrowPtr);
1859 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
1860 }
1861 else if (const ObjCAtCatchStmt* CatchStmt =
1862 cast<ObjCAtTryStmt>(S).getCatchStmts())
1863 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00001864 // Enter a new exception try block (in case a @catch block throws
1865 // an exception).
Anders Carlsson80f25672008-09-09 17:59:25 +00001866 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001867
Anders Carlsson80f25672008-09-09 17:59:25 +00001868 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1869 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001870 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00001871
Daniel Dunbar55e87422008-11-11 02:29:29 +00001872 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
1873 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001874 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001875
1876 CGF.EmitBlock(CatchBlock);
1877
Daniel Dunbar55e40722008-09-27 07:03:52 +00001878 // Handle catch list. As a special case we check if everything is
1879 // matched and avoid generating code for falling off the end if
1880 // so.
1881 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00001882 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00001883 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00001884
Anders Carlssondde0a942008-09-11 09:15:33 +00001885 const DeclStmt *CatchParam =
1886 cast_or_null<DeclStmt>(CatchStmt->getCatchParamStmt());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001887 const VarDecl *VD = 0;
1888 const PointerType *PT = 0;
1889
Anders Carlsson80f25672008-09-09 17:59:25 +00001890 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00001891 if (!CatchParam) {
1892 AllMatched = true;
1893 } else {
Ted Kremenekde3b8fb2008-10-06 20:58:56 +00001894 VD = cast<VarDecl>(CatchParam->getSolitaryDecl());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001895 PT = VD->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001896
Daniel Dunbar97f61d12008-09-27 22:21:14 +00001897 // catch(id e) always matches.
1898 // FIXME: For the time being we also match id<X>; this should
1899 // be rejected by Sema instead.
1900 if ((PT && CGF.getContext().isObjCIdType(PT->getPointeeType())) ||
1901 VD->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00001902 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00001903 }
1904
Daniel Dunbar55e40722008-09-27 07:03:52 +00001905 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00001906 if (CatchParam) {
1907 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001908 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar129271a2008-09-27 07:36:24 +00001909 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00001910 }
Anders Carlsson1452f552008-09-11 08:21:54 +00001911
Anders Carlssondde0a942008-09-11 09:15:33 +00001912 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001913 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001914 break;
1915 }
1916
Daniel Dunbar129271a2008-09-27 07:36:24 +00001917 assert(PT && "Unexpected non-pointer type in @catch");
1918 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00001919 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001920 assert(ObjCType && "Catch parameter must have Objective-C type!");
1921
1922 // Check if the @catch block matches the exception object.
1923 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
1924
Anders Carlsson80f25672008-09-09 17:59:25 +00001925 llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn,
1926 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00001927
Daniel Dunbar55e87422008-11-11 02:29:29 +00001928 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00001929
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001930 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001931 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001932
1933 // Emit the @catch block.
1934 CGF.EmitBlock(MatchedBlock);
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001935 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001936 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001937
1938 llvm::Value *Tmp =
1939 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(VD->getType()),
1940 "tmp");
1941 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00001942
1943 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001944 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001945
1946 CGF.EmitBlock(NextCatchBlock);
1947 }
1948
Daniel Dunbar55e40722008-09-27 07:03:52 +00001949 if (!AllMatched) {
1950 // None of the handlers caught the exception, so store it to be
1951 // rethrown at the end of the @finally block.
1952 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001953 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001954 }
1955
1956 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001957 CGF.EmitBlock(CatchHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001958 CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1959 ExceptionData),
1960 RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001961 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001962 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00001963 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001964 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Anders Carlsson80f25672008-09-09 17:59:25 +00001965 }
1966
Daniel Dunbar898d5082008-09-30 01:06:03 +00001967 // Pop the exception-handling stack entry. It is important to do
1968 // this now, because the code in the @finally block is not in this
1969 // context.
1970 CGF.ObjCEHStack.pop_back();
1971
Anders Carlsson80f25672008-09-09 17:59:25 +00001972 // Emit the @finally block.
1973 CGF.EmitBlock(FinallyBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001974 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00001975
1976 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001977 if (isTry) {
1978 if (const ObjCAtFinallyStmt* FinallyStmt =
1979 cast<ObjCAtTryStmt>(S).getFinallyStmt())
1980 CGF.EmitStmt(FinallyStmt->getFinallyBody());
1981 }
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001982 else {
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001983 // For @synchronized objc_sync_exit(expr); As finally's sole statement.
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001984 // For @synchronized, call objc_sync_enter(sync.expr)
1985 llvm::Value *Arg = CGF.EmitScalarExpr(
1986 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1987 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1988 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, Arg);
1989 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001990
Daniel Dunbar898d5082008-09-30 01:06:03 +00001991 CGF.EmitBlock(FinallyJump);
1992
1993 CGF.EmitBlock(FinallyRethrow);
1994 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn,
1995 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001996 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00001997
1998 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001999}
2000
2001void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002002 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002003 llvm::Value *ExceptionAsObject;
2004
2005 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2006 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2007 ExceptionAsObject =
2008 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2009 } else {
Daniel Dunbar898d5082008-09-30 01:06:03 +00002010 assert((!CGF.ObjCEHStack.empty() && CGF.ObjCEHStack.back()->Exception) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002011 "Unexpected rethrow outside @catch block.");
Daniel Dunbar898d5082008-09-30 01:06:03 +00002012 ExceptionAsObject = CGF.ObjCEHStack.back()->Exception;
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002013 }
2014
2015 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002016 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002017
2018 // Clear the insertion point to indicate we are in unreachable code.
2019 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002020}
2021
Daniel Dunbar898d5082008-09-30 01:06:03 +00002022void CodeGenFunction::EmitJumpThroughFinally(ObjCEHEntry *E,
2023 llvm::BasicBlock *Dst,
2024 bool ExecuteTryExit) {
Daniel Dunbara448fb22008-11-11 23:11:34 +00002025 if (!HaveInsertPoint())
Daniel Dunbar898d5082008-09-30 01:06:03 +00002026 return;
2027
2028 // Find the destination code for this block. We always use 0 for the
2029 // fallthrough block (default destination).
2030 llvm::SwitchInst *SI = E->FinallySwitch;
2031 llvm::ConstantInt *ID;
2032 if (Dst == SI->getDefaultDest()) {
2033 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
2034 } else {
2035 ID = SI->findCaseDest(Dst);
2036 if (!ID) {
2037 // No code found, get a new unique one by just using the number
2038 // of switch successors.
2039 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, SI->getNumSuccessors());
2040 SI->addCase(ID, Dst);
2041 }
2042 }
2043
2044 // Set the destination code and branch.
2045 Builder.CreateStore(ID, E->DestCode);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002046 EmitBranch(ExecuteTryExit ? E->FinallyBlock : E->FinallyNoExit);
Daniel Dunbar898d5082008-09-30 01:06:03 +00002047}
2048
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002049/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002050/// object: objc_read_weak (id *src)
2051///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002052llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002053 llvm::Value *AddrWeakObj)
2054{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002055 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002056 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002057 AddrWeakObj, "weakread");
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002058 return read_weak;
2059}
2060
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002061/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2062/// objc_assign_weak (id src, id *dst)
2063///
2064void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2065 llvm::Value *src, llvm::Value *dst)
2066{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002067 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2068 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002069 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
2070 src, dst, "weakassign");
2071 return;
2072}
2073
Fariborz Jahanian58626502008-11-19 00:59:10 +00002074/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2075/// objc_assign_global (id src, id *dst)
2076///
2077void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2078 llvm::Value *src, llvm::Value *dst)
2079{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002080 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2081 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002082 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
2083 src, dst, "globalassign");
2084 return;
2085}
2086
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002087/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2088/// objc_assign_ivar (id src, id *dst)
2089///
2090void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2091 llvm::Value *src, llvm::Value *dst)
2092{
2093 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2094 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
2095 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
2096 src, dst, "assignivar");
2097 return;
2098}
2099
Fariborz Jahanian58626502008-11-19 00:59:10 +00002100/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2101/// objc_assign_strongCast (id src, id *dst)
2102///
2103void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2104 llvm::Value *src, llvm::Value *dst)
2105{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002106 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2107 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002108 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
2109 src, dst, "weakassign");
2110 return;
2111}
2112
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002113/// EmitObjCValueForIvar - Code Gen for ivar reference.
2114///
2115llvm::Value *CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2116 llvm::Value *BaseValue,
2117 const ObjCIvarDecl *Ivar,
2118 const FieldDecl *Field,
2119 unsigned CVRQualifiers) {
2120 // TODO: Add a special case for isa (index 0)
2121 unsigned Index = CGM.getTypes().getLLVMFieldNo(Field);
2122 llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp");
2123 return V;
2124}
2125
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002126/* *** Private Interface *** */
2127
2128/// EmitImageInfo - Emit the image info marker used to encode some module
2129/// level information.
2130///
2131/// See: <rdr://4810609&4810587&4810587>
2132/// struct IMAGE_INFO {
2133/// unsigned version;
2134/// unsigned flags;
2135/// };
2136enum ImageInfoFlags {
2137 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
2138 eImageInfo_GarbageCollected = (1 << 1),
2139 eImageInfo_GCOnly = (1 << 2)
2140};
2141
2142void CGObjCMac::EmitImageInfo() {
2143 unsigned version = 0; // Version is unused?
2144 unsigned flags = 0;
2145
2146 // FIXME: Fix and continue?
2147 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2148 flags |= eImageInfo_GarbageCollected;
2149 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2150 flags |= eImageInfo_GCOnly;
2151
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002152 // Emitted as int[2];
2153 llvm::Constant *values[2] = {
2154 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2155 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2156 };
2157 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002158 llvm::GlobalVariable *GV =
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002159 new llvm::GlobalVariable(AT, true,
2160 llvm::GlobalValue::InternalLinkage,
2161 llvm::ConstantArray::get(AT, values, 2),
2162 "\01L_OBJC_IMAGE_INFO",
2163 &CGM.getModule());
2164
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002165 if (ObjCABI == 1) {
2166 GV->setSection("__OBJC, __image_info,regular");
2167 } else {
2168 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
2169 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002170
2171 UsedGlobals.push_back(GV);
2172}
2173
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002174
2175// struct objc_module {
2176// unsigned long version;
2177// unsigned long size;
2178// const char *name;
2179// Symtab symtab;
2180// };
2181
2182// FIXME: Get from somewhere
2183static const int ModuleVersion = 7;
2184
2185void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002186 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002187
2188 std::vector<llvm::Constant*> Values(4);
2189 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2190 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002191 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002192 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002193 Values[3] = EmitModuleSymbols();
2194
2195 llvm::GlobalVariable *GV =
2196 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
2197 llvm::GlobalValue::InternalLinkage,
2198 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
2199 Values),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002200 "\01L_OBJC_MODULES",
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002201 &CGM.getModule());
2202 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
2203 UsedGlobals.push_back(GV);
2204}
2205
2206llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002207 unsigned NumClasses = DefinedClasses.size();
2208 unsigned NumCategories = DefinedCategories.size();
2209
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002210 // Return null if no symbols were defined.
2211 if (!NumClasses && !NumCategories)
2212 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2213
2214 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002215 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2216 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2217 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2218 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2219
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002220 // The runtime expects exactly the list of defined classes followed
2221 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002222 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002223 for (unsigned i=0; i<NumClasses; i++)
2224 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2225 ObjCTypes.Int8PtrTy);
2226 for (unsigned i=0; i<NumCategories; i++)
2227 Symbols[NumClasses + i] =
2228 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2229 ObjCTypes.Int8PtrTy);
2230
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002231 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002232 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002233 NumClasses + NumCategories),
2234 Symbols);
2235
2236 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2237
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002238 llvm::GlobalVariable *GV =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002239 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002240 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002241 Init,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002242 "\01L_OBJC_SYMBOLS",
2243 &CGM.getModule());
2244 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
2245 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002246 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2247}
2248
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002249llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002250 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002251 LazySymbols.insert(ID->getIdentifier());
2252
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002253 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2254
2255 if (!Entry) {
2256 llvm::Constant *Casted =
2257 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2258 ObjCTypes.ClassPtrTy);
2259 Entry =
2260 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
2261 llvm::GlobalValue::InternalLinkage,
2262 Casted, "\01L_OBJC_CLASS_REFERENCES_",
2263 &CGM.getModule());
2264 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
2265 UsedGlobals.push_back(Entry);
2266 }
2267
2268 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002269}
2270
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002271llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002272 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2273
2274 if (!Entry) {
2275 llvm::Constant *Casted =
2276 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2277 ObjCTypes.SelectorPtrTy);
2278 Entry =
2279 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
2280 llvm::GlobalValue::InternalLinkage,
2281 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
2282 &CGM.getModule());
2283 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
2284 UsedGlobals.push_back(Entry);
2285 }
2286
2287 return Builder.CreateLoad(Entry, false, "tmp");
2288}
2289
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002290llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002291 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002292
2293 if (!Entry) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002294 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002295 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002296 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002297 llvm::GlobalValue::InternalLinkage,
2298 C, "\01L_OBJC_CLASS_NAME_",
2299 &CGM.getModule());
2300 Entry->setSection("__TEXT,__cstring,cstring_literals");
2301 UsedGlobals.push_back(Entry);
2302 }
2303
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002304 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002305}
2306
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002307llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002308 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
2309
2310 if (!Entry) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00002311 // FIXME: Avoid std::string copying.
2312 llvm::Constant *C = llvm::ConstantArray::get(Sel.getAsString());
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002313 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002314 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002315 llvm::GlobalValue::InternalLinkage,
2316 C, "\01L_OBJC_METH_VAR_NAME_",
2317 &CGM.getModule());
2318 Entry->setSection("__TEXT,__cstring,cstring_literals");
2319 UsedGlobals.push_back(Entry);
2320 }
2321
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002322 return getConstantGEP(Entry, 0, 0);
2323}
2324
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002325// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002326llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002327 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
2328}
2329
2330// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002331llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002332 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
2333}
2334
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002335llvm::Constant *CGObjCCommonMac::GetMethodVarType(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002336 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002337
2338 if (!Entry) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002339 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002340 Entry =
2341 new llvm::GlobalVariable(C->getType(), false,
2342 llvm::GlobalValue::InternalLinkage,
2343 C, "\01L_OBJC_METH_VAR_TYPE_",
2344 &CGM.getModule());
2345 Entry->setSection("__TEXT,__cstring,cstring_literals");
2346 UsedGlobals.push_back(Entry);
2347 }
2348
2349 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002350}
2351
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002352// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002353llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002354 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002355 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
2356 TypeStr);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002357 return GetMethodVarType(TypeStr);
2358}
2359
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002360// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002361llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002362 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
2363
2364 if (!Entry) {
2365 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
2366 Entry =
2367 new llvm::GlobalVariable(C->getType(), false,
2368 llvm::GlobalValue::InternalLinkage,
2369 C, "\01L_OBJC_PROP_NAME_ATTR_",
2370 &CGM.getModule());
2371 Entry->setSection("__TEXT,__cstring,cstring_literals");
2372 UsedGlobals.push_back(Entry);
2373 }
2374
2375 return getConstantGEP(Entry, 0, 0);
2376}
2377
2378// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002379// FIXME: This Decl should be more precise.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002380llvm::Constant *CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002381 const Decl *Container) {
2382 std::string TypeStr;
2383 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002384 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
2385}
2386
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002387void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
2388 const ObjCContainerDecl *CD,
2389 std::string &NameOut) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002390 // FIXME: Find the mangling GCC uses.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002391 NameOut = (D->isInstanceMethod() ? "-" : "+");
Chris Lattner077bf5e2008-11-24 03:33:13 +00002392 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002393 assert (CD && "Missing container decl in GetNameForMethod");
2394 NameOut += CD->getNameAsString();
Fariborz Jahanian52847332009-01-26 23:49:05 +00002395 // FIXME. For a method in a category, (CAT_NAME) is inserted here.
2396 // Right now! there is not enough info. to do this.
Chris Lattner077bf5e2008-11-24 03:33:13 +00002397 NameOut += ' ';
2398 NameOut += D->getSelector().getAsString();
2399 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002400}
2401
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002402/// GetFirstIvarInRecord - This routine returns the record for the
2403/// implementation of the fiven class OID. It also returns field
2404/// corresponding to the first ivar in the class in FIV. It also
2405/// returns the one before the first ivar.
2406///
2407const RecordDecl *CGObjCCommonMac::GetFirstIvarInRecord(
2408 const ObjCInterfaceDecl *OID,
2409 RecordDecl::field_iterator &FIV,
2410 RecordDecl::field_iterator &PIV) {
2411 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass());
2412 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
2413 RecordDecl::field_iterator ifield = RD->field_begin();
2414 RecordDecl::field_iterator pfield = RD->field_end();
2415 while (countSuperClassIvars-- > 0) {
2416 pfield = ifield;
2417 ++ifield;
2418 }
2419 FIV = ifield;
2420 PIV = pfield;
2421 return RD;
2422}
2423
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002424void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002425 EmitModuleInfo();
2426
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002427 // Emit the dummy bodies for any protocols which were referenced but
2428 // never defined.
2429 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
2430 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
2431 if (i->second->hasInitializer())
2432 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002433
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002434 std::vector<llvm::Constant*> Values(5);
2435 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
2436 Values[1] = GetClassName(i->first);
2437 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
2438 Values[3] = Values[4] =
2439 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
2440 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
2441 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
2442 Values));
2443 }
2444
2445 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002446 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002447 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002448 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002449 }
2450
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002451 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002452 llvm::GlobalValue *GV =
2453 new llvm::GlobalVariable(AT, false,
2454 llvm::GlobalValue::AppendingLinkage,
2455 llvm::ConstantArray::get(AT, Used),
2456 "llvm.used",
2457 &CGM.getModule());
2458
2459 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002460
2461 // Add assembler directives to add lazy undefined symbol references
2462 // for classes which are referenced but not defined. This is
2463 // important for correct linker interaction.
2464
2465 // FIXME: Uh, this isn't particularly portable.
2466 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00002467
2468 if (!CGM.getModule().getModuleInlineAsm().empty())
2469 s << "\n";
2470
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002471 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
2472 e = LazySymbols.end(); i != e; ++i) {
2473 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
2474 }
2475 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
2476 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002477 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002478 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
2479 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00002480
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002481 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002482}
2483
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002484CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002485 : CGObjCCommonMac(cgm),
2486 ObjCTypes(cgm)
2487{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00002488 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002489 ObjCABI = 2;
2490}
2491
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002492/* *** */
2493
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002494ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
2495: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002496{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002497 CodeGen::CodeGenTypes &Types = CGM.getTypes();
2498 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002499
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002500 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002501 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002502 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002503 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
2504
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002505 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002506 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002507 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002508
2509 // FIXME: It would be nice to unify this with the opaque type, so
2510 // that the IR comes out a bit cleaner.
2511 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
2512 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002513
2514 // I'm not sure I like this. The implicit coordination is a bit
2515 // gross. We should solve this in a reasonable fashion because this
2516 // is a pretty common task (match some runtime data structure with
2517 // an LLVM data structure).
2518
2519 // FIXME: This is leaked.
2520 // FIXME: Merge with rewriter code?
2521
2522 // struct _objc_super {
2523 // id self;
2524 // Class cls;
2525 // }
2526 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
2527 SourceLocation(),
2528 &Ctx.Idents.get("_objc_super"));
2529 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2530 Ctx.getObjCIdType(), 0, false));
2531 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2532 Ctx.getObjCClassType(), 0, false));
2533 RD->completeDefinition(Ctx);
2534
2535 SuperCTy = Ctx.getTagDeclType(RD);
2536 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
2537
2538 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002539 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
2540
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002541 // struct _prop_t {
2542 // char *name;
2543 // char *attributes;
2544 // }
2545 PropertyTy = llvm::StructType::get(Int8PtrTy,
2546 Int8PtrTy,
2547 NULL);
2548 CGM.getModule().addTypeName("struct._prop_t",
2549 PropertyTy);
2550
2551 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002552 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002553 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002554 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002555 // }
2556 PropertyListTy = llvm::StructType::get(IntTy,
2557 IntTy,
2558 llvm::ArrayType::get(PropertyTy, 0),
2559 NULL);
2560 CGM.getModule().addTypeName("struct._prop_list_t",
2561 PropertyListTy);
2562 // struct _prop_list_t *
2563 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
2564
2565 // struct _objc_method {
2566 // SEL _cmd;
2567 // char *method_type;
2568 // char *_imp;
2569 // }
2570 MethodTy = llvm::StructType::get(SelectorPtrTy,
2571 Int8PtrTy,
2572 Int8PtrTy,
2573 NULL);
2574 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002575
2576 // struct _objc_cache *
2577 CacheTy = llvm::OpaqueType::get();
2578 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
2579 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002580
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002581 // Property manipulation functions.
2582
2583 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
2584 std::vector<const llvm::Type*> Params;
2585 Params.push_back(ObjectPtrTy);
2586 Params.push_back(SelectorPtrTy);
2587 Params.push_back(LongTy);
2588 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2589 GetPropertyFn =
2590 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2591 Params,
2592 false),
2593 "objc_getProperty");
2594
2595 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
2596 Params.clear();
2597 Params.push_back(ObjectPtrTy);
2598 Params.push_back(SelectorPtrTy);
2599 Params.push_back(LongTy);
2600 Params.push_back(ObjectPtrTy);
2601 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2602 Params.push_back(Types.ConvertTypeForMem(Ctx.BoolTy));
2603 SetPropertyFn =
2604 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2605 Params,
2606 false),
2607 "objc_setProperty");
2608 // Enumeration mutation.
2609
2610 Params.clear();
2611 Params.push_back(ObjectPtrTy);
2612 EnumerationMutationFn =
2613 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2614 Params,
2615 false),
2616 "objc_enumerationMutation");
2617
2618 // gc's API
2619 // id objc_read_weak (id *)
2620 Params.clear();
2621 Params.push_back(PtrObjectPtrTy);
2622 GcReadWeakFn =
2623 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2624 Params,
2625 false),
2626 "objc_read_weak");
2627 // id objc_assign_weak (id, id *)
2628 Params.clear();
2629 Params.push_back(ObjectPtrTy);
2630 Params.push_back(PtrObjectPtrTy);
2631 GcAssignWeakFn =
2632 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2633 Params,
2634 false),
2635 "objc_assign_weak");
2636 GcAssignGlobalFn =
2637 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2638 Params,
2639 false),
2640 "objc_assign_global");
2641 GcAssignIvarFn =
2642 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2643 Params,
2644 false),
2645 "objc_assign_ivar");
2646 GcAssignStrongCastFn =
2647 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2648 Params,
2649 false),
2650 "objc_assign_strongCast");
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002651}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002652
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002653ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
2654 : ObjCCommonTypesHelper(cgm)
2655{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002656 // struct _objc_method_description {
2657 // SEL name;
2658 // char *types;
2659 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002660 MethodDescriptionTy =
2661 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002662 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002663 NULL);
2664 CGM.getModule().addTypeName("struct._objc_method_description",
2665 MethodDescriptionTy);
2666
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002667 // struct _objc_method_description_list {
2668 // int count;
2669 // struct _objc_method_description[1];
2670 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002671 MethodDescriptionListTy =
2672 llvm::StructType::get(IntTy,
2673 llvm::ArrayType::get(MethodDescriptionTy, 0),
2674 NULL);
2675 CGM.getModule().addTypeName("struct._objc_method_description_list",
2676 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002677
2678 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002679 MethodDescriptionListPtrTy =
2680 llvm::PointerType::getUnqual(MethodDescriptionListTy);
2681
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002682 // Protocol description structures
2683
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002684 // struct _objc_protocol_extension {
2685 // uint32_t size; // sizeof(struct _objc_protocol_extension)
2686 // struct _objc_method_description_list *optional_instance_methods;
2687 // struct _objc_method_description_list *optional_class_methods;
2688 // struct _objc_property_list *instance_properties;
2689 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002690 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002691 llvm::StructType::get(IntTy,
2692 MethodDescriptionListPtrTy,
2693 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002694 PropertyListPtrTy,
2695 NULL);
2696 CGM.getModule().addTypeName("struct._objc_protocol_extension",
2697 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002698
2699 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002700 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
2701
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002702 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002703
2704 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
2705 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2706
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002707 const llvm::Type *T =
2708 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
2709 LongTy,
2710 llvm::ArrayType::get(ProtocolTyHolder, 0),
2711 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002712 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
2713
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002714 // struct _objc_protocol {
2715 // struct _objc_protocol_extension *isa;
2716 // char *protocol_name;
2717 // struct _objc_protocol **_objc_protocol_list;
2718 // struct _objc_method_description_list *instance_methods;
2719 // struct _objc_method_description_list *class_methods;
2720 // }
2721 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002722 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002723 llvm::PointerType::getUnqual(ProtocolListTyHolder),
2724 MethodDescriptionListPtrTy,
2725 MethodDescriptionListPtrTy,
2726 NULL);
2727 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
2728
2729 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
2730 CGM.getModule().addTypeName("struct._objc_protocol_list",
2731 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002732 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002733 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
2734
2735 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002736 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002737 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002738
2739 // Class description structures
2740
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002741 // struct _objc_ivar {
2742 // char *ivar_name;
2743 // char *ivar_type;
2744 // int ivar_offset;
2745 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002746 IvarTy = llvm::StructType::get(Int8PtrTy,
2747 Int8PtrTy,
2748 IntTy,
2749 NULL);
2750 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
2751
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002752 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002753 IvarListTy = llvm::OpaqueType::get();
2754 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
2755 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
2756
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002757 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002758 MethodListTy = llvm::OpaqueType::get();
2759 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
2760 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
2761
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002762 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002763 ClassExtensionTy =
2764 llvm::StructType::get(IntTy,
2765 Int8PtrTy,
2766 PropertyListPtrTy,
2767 NULL);
2768 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
2769 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
2770
2771 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
2772
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002773 // struct _objc_class {
2774 // Class isa;
2775 // Class super_class;
2776 // char *name;
2777 // long version;
2778 // long info;
2779 // long instance_size;
2780 // struct _objc_ivar_list *ivars;
2781 // struct _objc_method_list *methods;
2782 // struct _objc_cache *cache;
2783 // struct _objc_protocol_list *protocols;
2784 // char *ivar_layout;
2785 // struct _objc_class_ext *ext;
2786 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002787 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
2788 llvm::PointerType::getUnqual(ClassTyHolder),
2789 Int8PtrTy,
2790 LongTy,
2791 LongTy,
2792 LongTy,
2793 IvarListPtrTy,
2794 MethodListPtrTy,
2795 CachePtrTy,
2796 ProtocolListPtrTy,
2797 Int8PtrTy,
2798 ClassExtensionPtrTy,
2799 NULL);
2800 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
2801
2802 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
2803 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
2804 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
2805
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002806 // struct _objc_category {
2807 // char *category_name;
2808 // char *class_name;
2809 // struct _objc_method_list *instance_method;
2810 // struct _objc_method_list *class_method;
2811 // uint32_t size; // sizeof(struct _objc_category)
2812 // struct _objc_property_list *instance_properties;// category's @property
2813 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002814 CategoryTy = llvm::StructType::get(Int8PtrTy,
2815 Int8PtrTy,
2816 MethodListPtrTy,
2817 MethodListPtrTy,
2818 ProtocolListPtrTy,
2819 IntTy,
2820 PropertyListPtrTy,
2821 NULL);
2822 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
2823
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002824 // Global metadata structures
2825
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002826 // struct _objc_symtab {
2827 // long sel_ref_cnt;
2828 // SEL *refs;
2829 // short cls_def_cnt;
2830 // short cat_def_cnt;
2831 // char *defs[cls_def_cnt + cat_def_cnt];
2832 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002833 SymtabTy = llvm::StructType::get(LongTy,
2834 SelectorPtrTy,
2835 ShortTy,
2836 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002837 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002838 NULL);
2839 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
2840 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
2841
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002842 // struct _objc_module {
2843 // long version;
2844 // long size; // sizeof(struct _objc_module)
2845 // char *name;
2846 // struct _objc_symtab* symtab;
2847 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002848 ModuleTy =
2849 llvm::StructType::get(LongTy,
2850 LongTy,
2851 Int8PtrTy,
2852 SymtabPtrTy,
2853 NULL);
2854 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002855
Daniel Dunbar49f66022008-09-24 03:38:44 +00002856 // Message send functions.
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002857
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002858 // id objc_msgSend (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002859 std::vector<const llvm::Type*> Params;
2860 Params.push_back(ObjectPtrTy);
2861 Params.push_back(SelectorPtrTy);
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002862 MessageSendFn =
2863 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2864 Params,
2865 true),
2866 "objc_msgSend");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002867
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002868 // id objc_msgSend_stret (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002869 Params.clear();
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002870 Params.push_back(ObjectPtrTy);
2871 Params.push_back(SelectorPtrTy);
2872 MessageSendStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002873 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2874 Params,
2875 true),
2876 "objc_msgSend_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002877
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002878 //
Daniel Dunbar5669e572008-10-17 03:24:53 +00002879 Params.clear();
2880 Params.push_back(ObjectPtrTy);
2881 Params.push_back(SelectorPtrTy);
2882 // FIXME: This should be long double on x86_64?
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002883 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
Daniel Dunbar5669e572008-10-17 03:24:53 +00002884 MessageSendFpretFn =
2885 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
2886 Params,
2887 true),
2888 "objc_msgSend_fpret");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002889
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002890 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002891 Params.clear();
2892 Params.push_back(SuperPtrTy);
2893 Params.push_back(SelectorPtrTy);
2894 MessageSendSuperFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002895 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2896 Params,
2897 true),
2898 "objc_msgSendSuper");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002899
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002900 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
2901 // SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002902 Params.clear();
2903 Params.push_back(Int8PtrTy);
2904 Params.push_back(SuperPtrTy);
2905 Params.push_back(SelectorPtrTy);
2906 MessageSendSuperStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002907 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2908 Params,
2909 true),
2910 "objc_msgSendSuper_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002911
2912 // There is no objc_msgSendSuper_fpret? How can that work?
2913 MessageSendSuperFpretFn = MessageSendSuperFn;
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002914
Anders Carlsson124526b2008-09-09 10:10:21 +00002915 // FIXME: This is the size of the setjmp buffer and should be
2916 // target specific. 18 is what's used on 32-bit X86.
2917 uint64_t SetJmpBufferSize = 18;
2918
2919 // Exceptions
2920 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00002921 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00002922
2923 ExceptionDataTy =
2924 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
2925 SetJmpBufferSize),
2926 StackPtrTy, NULL);
2927 CGM.getModule().addTypeName("struct._objc_exception_data",
2928 ExceptionDataTy);
2929
2930 Params.clear();
2931 Params.push_back(ObjectPtrTy);
2932 ExceptionThrowFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002933 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2934 Params,
2935 false),
2936 "objc_exception_throw");
Anders Carlsson124526b2008-09-09 10:10:21 +00002937
2938 Params.clear();
2939 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
2940 ExceptionTryEnterFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002941 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2942 Params,
2943 false),
2944 "objc_exception_try_enter");
Anders Carlsson124526b2008-09-09 10:10:21 +00002945 ExceptionTryExitFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002946 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2947 Params,
2948 false),
2949 "objc_exception_try_exit");
Anders Carlsson124526b2008-09-09 10:10:21 +00002950 ExceptionExtractFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002951 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2952 Params,
2953 false),
2954 "objc_exception_extract");
Anders Carlsson124526b2008-09-09 10:10:21 +00002955
2956 Params.clear();
2957 Params.push_back(ClassPtrTy);
2958 Params.push_back(ObjectPtrTy);
2959 ExceptionMatchFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002960 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
2961 Params,
2962 false),
2963 "objc_exception_match");
Chris Lattner10cac6f2008-11-15 21:26:17 +00002964
2965 // synchronized APIs
2966 // void objc_sync_enter (id)
2967 Params.clear();
2968 Params.push_back(ObjectPtrTy);
2969 SyncEnterFn =
2970 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2971 Params,
2972 false),
2973 "objc_sync_enter");
2974 // void objc_sync_exit (id)
2975 SyncExitFn =
2976 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2977 Params,
2978 false),
2979 "objc_sync_exit");
2980
Anders Carlsson124526b2008-09-09 10:10:21 +00002981
2982 Params.clear();
2983 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
2984 SetJmpFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002985 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
2986 Params,
2987 false),
2988 "_setjmp");
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002989
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002990}
2991
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002992ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002993: ObjCCommonTypesHelper(cgm)
2994{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002995 // struct _method_list_t {
2996 // uint32_t entsize; // sizeof(struct _objc_method)
2997 // uint32_t method_count;
2998 // struct _objc_method method_list[method_count];
2999 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003000 MethodListnfABITy = llvm::StructType::get(IntTy,
3001 IntTy,
3002 llvm::ArrayType::get(MethodTy, 0),
3003 NULL);
3004 CGM.getModule().addTypeName("struct.__method_list_t",
3005 MethodListnfABITy);
3006 // struct method_list_t *
3007 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003008
3009 // struct _protocol_t {
3010 // id isa; // NULL
3011 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003012 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003013 // const struct method_list_t * const instance_methods;
3014 // const struct method_list_t * const class_methods;
3015 // const struct method_list_t *optionalInstanceMethods;
3016 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003017 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003018 // const uint32_t size; // sizeof(struct _protocol_t)
3019 // const uint32_t flags; // = 0
3020 // }
3021
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003022 // Holder for struct _protocol_list_t *
3023 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3024
3025 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
3026 Int8PtrTy,
3027 llvm::PointerType::getUnqual(
3028 ProtocolListTyHolder),
3029 MethodListnfABIPtrTy,
3030 MethodListnfABIPtrTy,
3031 MethodListnfABIPtrTy,
3032 MethodListnfABIPtrTy,
3033 PropertyListPtrTy,
3034 IntTy,
3035 IntTy,
3036 NULL);
3037 CGM.getModule().addTypeName("struct._protocol_t",
3038 ProtocolnfABITy);
3039
Fariborz Jahanianda320092009-01-29 19:24:30 +00003040 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003041 // long protocol_count; // Note, this is 32/64 bit
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003042 // struct _protocol_t[protocol_count];
3043 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003044 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3045 llvm::ArrayType::get(
3046 ProtocolnfABITy, 0),
3047 NULL);
3048 CGM.getModule().addTypeName("struct._objc_protocol_list",
3049 ProtocolListnfABITy);
3050
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003051 // struct _objc_protocol_list*
3052 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003053
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003054 // FIXME! Is this doing the right thing?
3055 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3056 ProtocolListnfABIPtrTy);
3057
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003058 // struct _ivar_t {
3059 // unsigned long int *offset; // pointer to ivar offset location
3060 // char *name;
3061 // char *type;
3062 // uint32_t alignment;
3063 // uint32_t size;
3064 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003065 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3066 Int8PtrTy,
3067 Int8PtrTy,
3068 IntTy,
3069 IntTy,
3070 NULL);
3071 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3072
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003073 // struct _ivar_list_t {
3074 // uint32 entsize; // sizeof(struct _ivar_t)
3075 // uint32 count;
3076 // struct _iver_t list[count];
3077 // }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003078 IvarListnfABITy = llvm::StructType::get(IntTy,
3079 IntTy,
3080 llvm::ArrayType::get(
3081 IvarnfABITy, 0),
3082 NULL);
3083 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3084
3085 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003086
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003087 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003088 // uint32_t const flags;
3089 // uint32_t const instanceStart;
3090 // uint32_t const instanceSize;
3091 // uint32_t const reserved; // only when building for 64bit targets
3092 // const uint8_t * const ivarLayout;
3093 // const char *const name;
3094 // const struct _method_list_t * const baseMethods;
3095 // const struct _objc_protocol_list *const baseProtocols;
3096 // const struct _ivar_list_t *const ivars;
3097 // const uint8_t * const weakIvarLayout;
3098 // const struct _prop_list_t * const properties;
3099 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003100
3101 // FIXME. Add 'reserved' field in 64bit abi mode!
3102 ClassRonfABITy = llvm::StructType::get(IntTy,
3103 IntTy,
3104 IntTy,
3105 Int8PtrTy,
3106 Int8PtrTy,
3107 MethodListnfABIPtrTy,
3108 ProtocolListnfABIPtrTy,
3109 IvarListnfABIPtrTy,
3110 Int8PtrTy,
3111 PropertyListPtrTy,
3112 NULL);
3113 CGM.getModule().addTypeName("struct._class_ro_t",
3114 ClassRonfABITy);
3115
3116 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3117 std::vector<const llvm::Type*> Params;
3118 Params.push_back(ObjectPtrTy);
3119 Params.push_back(SelectorPtrTy);
3120 ImpnfABITy = llvm::PointerType::getUnqual(
3121 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3122
3123 // struct _class_t {
3124 // struct _class_t *isa;
3125 // struct _class_t * const superclass;
3126 // void *cache;
3127 // IMP *vtable;
3128 // struct class_ro_t *ro;
3129 // }
3130
3131 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3132 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3133 llvm::PointerType::getUnqual(ClassTyHolder),
3134 CachePtrTy,
3135 llvm::PointerType::getUnqual(ImpnfABITy),
3136 llvm::PointerType::getUnqual(
3137 ClassRonfABITy),
3138 NULL);
3139 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3140
3141 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3142 ClassnfABITy);
3143
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003144 // LLVM for struct _class_t *
3145 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3146
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003147 // struct _category_t {
3148 // const char * const name;
3149 // struct _class_t *const cls;
3150 // const struct _method_list_t * const instance_methods;
3151 // const struct _method_list_t * const class_methods;
3152 // const struct _protocol_list_t * const protocols;
3153 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003154 // }
3155 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003156 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003157 MethodListnfABIPtrTy,
3158 MethodListnfABIPtrTy,
3159 ProtocolListnfABIPtrTy,
3160 PropertyListPtrTy,
3161 NULL);
3162 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
3163
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003164}
3165
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003166llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
3167 FinishNonFragileABIModule();
3168
3169 return NULL;
3170}
3171
3172void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
3173 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003174
3175 // Build list of all implemented classe addresses in array
3176 // L_OBJC_LABEL_CLASS_$.
3177 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$
3178 // list of 'nonlazy' implementations (defined as those with a +load{}
3179 // method!!).
3180 unsigned NumClasses = DefinedClasses.size();
3181 if (NumClasses) {
3182 std::vector<llvm::Constant*> Symbols(NumClasses);
3183 for (unsigned i=0; i<NumClasses; i++)
3184 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
3185 ObjCTypes.Int8PtrTy);
3186 llvm::Constant* Init =
3187 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3188 NumClasses),
3189 Symbols);
3190
3191 llvm::GlobalVariable *GV =
3192 new llvm::GlobalVariable(Init->getType(), false,
3193 llvm::GlobalValue::InternalLinkage,
3194 Init,
3195 "\01L_OBJC_LABEL_CLASS_$",
3196 &CGM.getModule());
3197 GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip");
3198 UsedGlobals.push_back(GV);
3199 }
3200
3201 // Build list of all implemented category addresses in array
3202 // L_OBJC_LABEL_CATEGORY_$.
3203 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$
3204 // list of 'nonlazy' category implementations (defined as those with a +load{}
3205 // method!!).
3206 unsigned NumCategory = DefinedCategories.size();
3207 if (NumCategory) {
3208 std::vector<llvm::Constant*> Symbols(NumCategory);
3209 for (unsigned i=0; i<NumCategory; i++)
3210 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i],
3211 ObjCTypes.Int8PtrTy);
3212 llvm::Constant* Init =
3213 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3214 NumCategory),
3215 Symbols);
3216
3217 llvm::GlobalVariable *GV =
3218 new llvm::GlobalVariable(Init->getType(), false,
3219 llvm::GlobalValue::InternalLinkage,
3220 Init,
3221 "\01L_OBJC_LABEL_CATEGORY_$",
3222 &CGM.getModule());
3223 GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip");
3224 UsedGlobals.push_back(GV);
3225 }
3226
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00003227 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
3228 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
3229 std::vector<llvm::Constant*> Values(2);
3230 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3231 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3232 llvm::Constant* Init = llvm::ConstantArray::get(
3233 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
3234 Values);
3235 llvm::GlobalVariable *IMGV =
3236 new llvm::GlobalVariable(Init->getType(), false,
3237 llvm::GlobalValue::InternalLinkage,
3238 Init,
3239 "\01L_OBJC_IMAGE_INFO",
3240 &CGM.getModule());
3241 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
3242 UsedGlobals.push_back(IMGV);
3243
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003244 std::vector<llvm::Constant*> Used;
3245 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
3246 e = UsedGlobals.end(); i != e; ++i) {
3247 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
3248 }
3249
3250 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
3251 llvm::GlobalValue *GV =
3252 new llvm::GlobalVariable(AT, false,
3253 llvm::GlobalValue::AppendingLinkage,
3254 llvm::ConstantArray::get(AT, Used),
3255 "llvm.used",
3256 &CGM.getModule());
3257
3258 GV->setSection("llvm.metadata");
3259
3260}
3261
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003262// Metadata flags
3263enum MetaDataDlags {
3264 CLS = 0x0,
3265 CLS_META = 0x1,
3266 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003267 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003268 CLS_EXCEPTION = 0x20
3269};
3270/// BuildClassRoTInitializer - generate meta-data for:
3271/// struct _class_ro_t {
3272/// uint32_t const flags;
3273/// uint32_t const instanceStart;
3274/// uint32_t const instanceSize;
3275/// uint32_t const reserved; // only when building for 64bit targets
3276/// const uint8_t * const ivarLayout;
3277/// const char *const name;
3278/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00003279/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003280/// const struct _ivar_list_t *const ivars;
3281/// const uint8_t * const weakIvarLayout;
3282/// const struct _prop_list_t * const properties;
3283/// }
3284///
3285llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
3286 unsigned flags,
3287 unsigned InstanceStart,
3288 unsigned InstanceSize,
3289 const ObjCImplementationDecl *ID) {
3290 std::string ClassName = ID->getNameAsString();
3291 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
3292 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
3293 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
3294 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
3295 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianda320092009-01-29 19:24:30 +00003296 // FIXME. ivarLayout is currently null!
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003297 Values[ 3] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3298 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003299 // const struct _method_list_t * const baseMethods;
3300 std::vector<llvm::Constant*> Methods;
3301 std::string MethodListName("\01l_OBJC_$_");
3302 if (flags & CLS_META) {
3303 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
3304 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
3305 e = ID->classmeth_end(); i != e; ++i) {
3306 // Class methods should always be defined.
3307 Methods.push_back(GetMethodConstant(*i));
3308 }
3309 } else {
3310 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
3311 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
3312 e = ID->instmeth_end(); i != e; ++i) {
3313 // Instance methods should always be defined.
3314 Methods.push_back(GetMethodConstant(*i));
3315 }
Fariborz Jahanian939abce2009-01-28 22:46:49 +00003316 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
3317 e = ID->propimpl_end(); i != e; ++i) {
3318 ObjCPropertyImplDecl *PID = *i;
3319
3320 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
3321 ObjCPropertyDecl *PD = PID->getPropertyDecl();
3322
3323 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
3324 if (llvm::Constant *C = GetMethodConstant(MD))
3325 Methods.push_back(C);
3326 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
3327 if (llvm::Constant *C = GetMethodConstant(MD))
3328 Methods.push_back(C);
3329 }
3330 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003331 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003332 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003333 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003334
3335 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3336 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
3337 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
3338 + OID->getNameAsString(),
3339 OID->protocol_begin(),
3340 OID->protocol_end());
3341
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003342 if (flags & CLS_META)
3343 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3344 else
3345 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003346 // FIXME. weakIvarLayout is currently null.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003347 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003348 if (flags & CLS_META)
3349 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3350 else
3351 Values[ 9] =
3352 EmitPropertyList(
3353 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
3354 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003355 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
3356 Values);
3357 llvm::GlobalVariable *CLASS_RO_GV =
3358 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
3359 llvm::GlobalValue::InternalLinkage,
3360 Init,
3361 (flags & CLS_META) ?
3362 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
3363 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
3364 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003365 CLASS_RO_GV->setAlignment(
3366 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003367 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003368 UsedGlobals.push_back(CLASS_RO_GV);
3369 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003370
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003371}
3372
3373/// BuildClassMetaData - This routine defines that to-level meta-data
3374/// for the given ClassName for:
3375/// struct _class_t {
3376/// struct _class_t *isa;
3377/// struct _class_t * const superclass;
3378/// void *cache;
3379/// IMP *vtable;
3380/// struct class_ro_t *ro;
3381/// }
3382///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003383llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
3384 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003385 llvm::Constant *IsAGV,
3386 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003387 llvm::Constant *ClassRoGV,
3388 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003389 std::vector<llvm::Constant*> Values(5);
3390 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003391 Values[1] = SuperClassGV
3392 ? SuperClassGV
3393 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003394 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
3395 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
3396 Values[4] = ClassRoGV; // &CLASS_RO_GV
3397 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
3398 Values);
3399 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(ClassName);
3400 if (GV)
3401 GV->setInitializer(Init);
3402 else
3403 GV =
3404 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3405 llvm::GlobalValue::ExternalLinkage,
3406 Init,
3407 ClassName,
3408 &CGM.getModule());
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00003409 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003410 GV->setAlignment(
3411 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003412 if (HiddenVisibility)
3413 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003414 UsedGlobals.push_back(GV);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003415 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003416}
3417
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003418void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
3419 std::string ClassName = ID->getNameAsString();
3420 if (!ObjCEmptyCacheVar) {
3421 ObjCEmptyCacheVar = new llvm::GlobalVariable(
3422 ObjCTypes.CachePtrTy,
3423 false,
3424 llvm::GlobalValue::ExternalLinkage,
3425 0,
3426 "\01_objc_empty_cache",
3427 &CGM.getModule());
3428 UsedGlobals.push_back(ObjCEmptyCacheVar);
3429
3430 ObjCEmptyVtableVar = new llvm::GlobalVariable(
3431 llvm::PointerType::getUnqual(
3432 ObjCTypes.ImpnfABITy),
3433 false,
3434 llvm::GlobalValue::ExternalLinkage,
3435 0,
3436 "\01_objc_empty_vtable",
3437 &CGM.getModule());
3438 UsedGlobals.push_back(ObjCEmptyVtableVar);
3439 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003440 assert(ID->getClassInterface() &&
3441 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003442 uint32_t InstanceStart =
3443 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
3444 uint32_t InstanceSize = InstanceStart;
3445 uint32_t flags = CLS_META;
3446 std::string ObjCMetaClassName("\01_OBJC_METACLASS_$_");
3447 std::string ObjCClassName("\01_OBJC_CLASS_$_");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003448
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003449 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003450
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003451 bool classIsHidden = IsClassHidden(ID->getClassInterface());
3452 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003453 flags |= OBJC2_CLS_HIDDEN;
3454 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003455 // class is root
3456 flags |= CLS_ROOT;
3457 std::string SuperClassName = ObjCClassName + ClassName;
3458 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3459 if (!SuperClassGV)
3460 SuperClassGV =
3461 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3462 llvm::GlobalValue::ExternalLinkage,
3463 0,
3464 SuperClassName,
3465 &CGM.getModule());
3466 UsedGlobals.push_back(SuperClassGV);
3467 std::string IsAClassName = ObjCMetaClassName + ClassName;
3468 IsAGV = CGM.getModule().getGlobalVariable(IsAClassName);
3469 if (!IsAGV)
3470 IsAGV =
3471 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3472 llvm::GlobalValue::ExternalLinkage,
3473 0,
3474 IsAClassName,
3475 &CGM.getModule());
3476 UsedGlobals.push_back(IsAGV);
3477 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003478 // Has a root. Current class is not a root.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003479 std::string RootClassName =
3480 ID->getClassInterface()->getSuperClass()->getNameAsString();
3481 std::string SuperClassName = ObjCMetaClassName + RootClassName;
3482 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3483 if (!SuperClassGV)
3484 SuperClassGV =
3485 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3486 llvm::GlobalValue::ExternalLinkage,
3487 0,
3488 SuperClassName,
3489 &CGM.getModule());
3490 UsedGlobals.push_back(SuperClassGV);
3491 IsAGV = SuperClassGV;
3492 }
3493 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
3494 InstanceStart,
3495 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003496 std::string TClassName = ObjCMetaClassName + ClassName;
3497 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003498 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
3499 classIsHidden);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003500
3501 // Metadata for the class
3502 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003503 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003504 flags |= OBJC2_CLS_HIDDEN;
3505 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003506 flags |= CLS_ROOT;
3507 SuperClassGV = 0;
3508 }
3509 else {
3510 // Has a root. Current class is not a root.
3511 std::string RootClassName =
3512 ID->getClassInterface()->getSuperClass()->getNameAsString();
3513 std::string SuperClassName = ObjCClassName + RootClassName;
3514 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
3515 if (!SuperClassGV)
3516 SuperClassGV =
3517 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3518 llvm::GlobalValue::ExternalLinkage,
3519 0,
3520 SuperClassName,
3521 &CGM.getModule());
3522 UsedGlobals.push_back(SuperClassGV);
3523
3524 }
3525
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003526 InstanceStart = InstanceSize = 0;
3527 if (ObjCInterfaceDecl *OID =
3528 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface())) {
3529 // FIXME. Share this with the one in EmitIvarList.
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003530 const llvm::Type *InterfaceTy =
3531 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
3532 const llvm::StructLayout *Layout =
3533 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003534
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003535 RecordDecl::field_iterator firstField, lastField;
3536 const RecordDecl *RD = GetFirstIvarInRecord(OID, firstField, lastField);
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003537
3538 for (RecordDecl::field_iterator e = RD->field_end(),
3539 ifield = firstField; ifield != e; ++ifield)
3540 lastField = ifield;
3541
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003542 if (lastField != RD->field_end()) {
3543 FieldDecl *Field = *lastField;
3544 const llvm::Type *FieldTy =
3545 CGM.getTypes().ConvertTypeForMem(Field->getType());
3546 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3547 InstanceSize = Layout->getElementOffset(
3548 CGM.getTypes().getLLVMFieldNo(Field)) +
3549 Size;
3550 if (firstField == RD->field_end())
3551 InstanceStart = InstanceSize;
3552 else
3553 InstanceStart = Layout->getElementOffset(CGM.getTypes().
3554 getLLVMFieldNo(*firstField));
3555 }
3556 }
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003557 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003558 InstanceStart,
3559 InstanceSize,
3560 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003561
3562 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003563 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003564 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
3565 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003566 DefinedClasses.push_back(ClassMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003567}
3568
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00003569/// GenerateProtocolRef - This routine is called to generate code for
3570/// a protocol reference expression; as in:
3571/// @code
3572/// @protocol(Proto1);
3573/// @endcode
3574/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
3575/// which will hold address of the protocol meta-data.
3576///
3577llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
3578 const ObjCProtocolDecl *PD) {
3579
3580 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
3581 ObjCTypes.ExternalProtocolPtrTy);
3582
3583 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
3584 ProtocolName += PD->getNameAsCString();
3585
3586 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
3587 if (PTGV)
3588 return Builder.CreateLoad(PTGV, false, "tmp");
3589 PTGV = new llvm::GlobalVariable(
3590 Init->getType(), false,
3591 llvm::GlobalValue::WeakLinkage,
3592 Init,
3593 ProtocolName,
3594 &CGM.getModule());
3595 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
3596 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
3597 UsedGlobals.push_back(PTGV);
3598 return Builder.CreateLoad(PTGV, false, "tmp");
3599}
3600
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003601/// GenerateCategory - Build metadata for a category implementation.
3602/// struct _category_t {
3603/// const char * const name;
3604/// struct _class_t *const cls;
3605/// const struct _method_list_t * const instance_methods;
3606/// const struct _method_list_t * const class_methods;
3607/// const struct _protocol_list_t * const protocols;
3608/// const struct _prop_list_t * const properties;
3609/// }
3610///
3611void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD)
3612{
3613 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003614 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
3615 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003616 "_$_" + OCD->getNameAsString());
3617 std::string ExtClassName("\01_OBJC_CLASS_$_" + Interface->getNameAsString());
3618
3619 std::vector<llvm::Constant*> Values(6);
3620 Values[0] = GetClassName(OCD->getIdentifier());
3621 // meta-class entry symbol
3622 llvm::GlobalVariable *ClassGV =
3623 CGM.getModule().getGlobalVariable(ExtClassName);
3624 if (!ClassGV)
3625 ClassGV =
3626 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3627 llvm::GlobalValue::ExternalLinkage,
3628 0,
3629 ExtClassName,
3630 &CGM.getModule());
3631 UsedGlobals.push_back(ClassGV);
3632 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003633 std::vector<llvm::Constant*> Methods;
3634 std::string MethodListName(Prefix);
3635 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
3636 "_$_" + OCD->getNameAsString();
3637
3638 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
3639 e = OCD->instmeth_end(); i != e; ++i) {
3640 // Instance methods should always be defined.
3641 Methods.push_back(GetMethodConstant(*i));
3642 }
3643
3644 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003645 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003646 Methods);
3647
3648 MethodListName = Prefix;
3649 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
3650 OCD->getNameAsString();
3651 Methods.clear();
3652 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
3653 e = OCD->classmeth_end(); i != e; ++i) {
3654 // Class methods should always be defined.
3655 Methods.push_back(GetMethodConstant(*i));
3656 }
3657
3658 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003659 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003660 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003661 const ObjCCategoryDecl *Category =
3662 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanianeb732132009-01-29 23:23:06 +00003663 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
3664 + Interface->getNameAsString() + "_$_"
3665 + Category->getNameAsString(),
3666 Category->protocol_begin(),
3667 Category->protocol_end());
3668
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003669 std::string ExtName(Interface->getNameAsString() + "_$_" +
3670 OCD->getNameAsString());
3671 Values[5] =
3672 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
3673 OCD, Category, ObjCTypes);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003674 llvm::Constant *Init =
3675 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
3676 Values);
3677 llvm::GlobalVariable *GCATV
3678 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
3679 false,
3680 llvm::GlobalValue::InternalLinkage,
3681 Init,
3682 ExtCatName,
3683 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003684 GCATV->setAlignment(
3685 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003686 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003687 UsedGlobals.push_back(GCATV);
3688 DefinedCategories.push_back(GCATV);
3689}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003690
3691/// GetMethodConstant - Return a struct objc_method constant for the
3692/// given method if it has been defined. The result is null if the
3693/// method has not been defined. The return value has type MethodPtrTy.
3694llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
3695 const ObjCMethodDecl *MD) {
3696 // FIXME: Use DenseMap::lookup
3697 llvm::Function *Fn = MethodDefinitions[MD];
3698 if (!Fn)
3699 return 0;
3700
3701 std::vector<llvm::Constant*> Method(3);
3702 Method[0] =
3703 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
3704 ObjCTypes.SelectorPtrTy);
3705 Method[1] = GetMethodVarType(MD);
3706 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
3707 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
3708}
3709
3710/// EmitMethodList - Build meta-data for method declarations
3711/// struct _method_list_t {
3712/// uint32_t entsize; // sizeof(struct _objc_method)
3713/// uint32_t method_count;
3714/// struct _objc_method method_list[method_count];
3715/// }
3716///
3717llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
3718 const std::string &Name,
3719 const char *Section,
3720 const ConstantVector &Methods) {
3721 // Return null for empty list.
3722 if (Methods.empty())
3723 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
3724
3725 std::vector<llvm::Constant*> Values(3);
3726 // sizeof(struct _objc_method)
3727 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy);
3728 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3729 // method_count
3730 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
3731 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
3732 Methods.size());
3733 Values[2] = llvm::ConstantArray::get(AT, Methods);
3734 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
3735
3736 llvm::GlobalVariable *GV =
3737 new llvm::GlobalVariable(Init->getType(), false,
3738 llvm::GlobalValue::InternalLinkage,
3739 Init,
3740 Name,
3741 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003742 GV->setAlignment(
3743 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003744 GV->setSection(Section);
3745 UsedGlobals.push_back(GV);
3746 return llvm::ConstantExpr::getBitCast(GV,
3747 ObjCTypes.MethodListnfABIPtrTy);
3748}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003749
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003750llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
3751 const ObjCImplementationDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003752 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003753 unsigned long int Offset) {
3754
3755 std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.'
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003756 + Ivar->getNameAsString());
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003757 llvm::Constant *Init = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
3758
3759 llvm::GlobalVariable *IvarOffsetGV =
3760 CGM.getModule().getGlobalVariable(ExternalName);
3761 if (IvarOffsetGV) {
3762 // ivar offset symbol already built due to user code referencing it.
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003763 IvarOffsetGV->setAlignment(
3764 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.Int8PtrTy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003765 IvarOffsetGV->setInitializer(Init);
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003766 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003767 return IvarOffsetGV;
3768 }
3769
3770 IvarOffsetGV =
3771 new llvm::GlobalVariable(Init->getType(),
3772 false,
3773 llvm::GlobalValue::ExternalLinkage,
3774 Init,
3775 ExternalName,
3776 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003777 IvarOffsetGV->setAlignment(
3778 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.Int8PtrTy));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003779 // @private and @package have hidden visibility.
3780 bool globalVisibility = (Ivar->getAccessControl() == ObjCIvarDecl::Public ||
3781 Ivar->getAccessControl() == ObjCIvarDecl::Protected);
3782 if (!globalVisibility)
3783 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003784 else
3785 if (const ObjCInterfaceDecl *OID = ID->getClassInterface())
3786 if (IsClassHidden(OID))
3787 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
3788
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003789 IvarOffsetGV->setSection("__DATA, __objc_const");
3790 UsedGlobals.push_back(IvarOffsetGV);
3791
3792 return llvm::ConstantExpr::getBitCast(
3793 IvarOffsetGV,
3794 llvm::PointerType::getUnqual(ObjCTypes.LongTy));
3795}
3796
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003797/// EmitIvarList - Emit the ivar list for the given
3798/// implementation. If ForClass is true the list of class ivars
3799/// (i.e. metaclass ivars) is emitted, otherwise the list of
3800/// interface ivars will be emitted. The return value has type
3801/// IvarListnfABIPtrTy.
3802/// struct _ivar_t {
3803/// unsigned long int *offset; // pointer to ivar offset location
3804/// char *name;
3805/// char *type;
3806/// uint32_t alignment;
3807/// uint32_t size;
3808/// }
3809/// struct _ivar_list_t {
3810/// uint32 entsize; // sizeof(struct _ivar_t)
3811/// uint32 count;
3812/// struct _iver_t list[count];
3813/// }
3814///
3815llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
3816 const ObjCImplementationDecl *ID) {
3817
3818 std::vector<llvm::Constant*> Ivars, Ivar(5);
3819
3820 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3821 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
3822
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003823 // FIXME. Consolidate this with similar code in GenerateClass.
3824 const llvm::Type *InterfaceTy =
3825 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(
3826 const_cast<ObjCInterfaceDecl*>(OID)));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003827 const llvm::StructLayout *Layout =
3828 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003829
3830 RecordDecl::field_iterator i,p;
3831 const RecordDecl *RD = GetFirstIvarInRecord(OID, i,p);
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003832 ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin();
3833
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003834 for (RecordDecl::field_iterator e = RD->field_end(); i != e; ++i) {
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003835 FieldDecl *Field = *i;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003836 unsigned long offset = Layout->getElementOffset(CGM.getTypes().
3837 getLLVMFieldNo(Field));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003838 const ObjCIvarDecl *ivarDecl = *I++;
3839 Ivar[0] = EmitIvarOffsetVar(ID, ivarDecl, offset);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003840 if (Field->getIdentifier())
3841 Ivar[1] = GetMethodVarName(Field->getIdentifier());
3842 else
3843 Ivar[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3844 std::string TypeStr;
3845 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3846 Ivar[2] = GetMethodVarType(TypeStr);
3847 const llvm::Type *FieldTy =
3848 CGM.getTypes().ConvertTypeForMem(Field->getType());
3849 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3850 unsigned Align = CGM.getContext().getPreferredTypeAlign(
3851 Field->getType().getTypePtr()) >> 3;
3852 Align = llvm::Log2_32(Align);
3853 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Fariborz Jahanian07236ba2009-01-27 22:27:56 +00003854 // NOTE. Size of a bitfield does not match gcc's, because of the way
3855 // bitfields are treated special in each. But I am told that 'size'
3856 // for bitfield ivars is ignored by the runtime so it does not matter.
3857 // (even if it matters, some day, there is enough info. to get the bitfield
3858 // right!
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003859 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3860 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
3861 }
3862 // Return null for empty list.
3863 if (Ivars.empty())
3864 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3865 std::vector<llvm::Constant*> Values(3);
3866 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy);
3867 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3868 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
3869 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
3870 Ivars.size());
3871 Values[2] = llvm::ConstantArray::get(AT, Ivars);
3872 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
3873 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
3874 llvm::GlobalVariable *GV =
3875 new llvm::GlobalVariable(Init->getType(), false,
3876 llvm::GlobalValue::InternalLinkage,
3877 Init,
3878 Prefix + OID->getNameAsString(),
3879 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003880 GV->setAlignment(
3881 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003882 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003883
3884 UsedGlobals.push_back(GV);
3885 return llvm::ConstantExpr::getBitCast(GV,
3886 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003887}
3888
3889llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
3890 const ObjCProtocolDecl *PD) {
3891 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
3892
3893 if (!Entry) {
3894 // We use the initializer as a marker of whether this is a forward
3895 // reference or not. At module finalization we add the empty
3896 // contents for protocols which were referenced but never defined.
3897 Entry =
3898 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
3899 llvm::GlobalValue::ExternalLinkage,
3900 0,
3901 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(),
3902 &CGM.getModule());
3903 Entry->setSection("__DATA,__datacoal_nt,coalesced");
3904 UsedGlobals.push_back(Entry);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003905 }
3906
3907 return Entry;
3908}
3909
3910/// GetOrEmitProtocol - Generate the protocol meta-data:
3911/// @code
3912/// struct _protocol_t {
3913/// id isa; // NULL
3914/// const char * const protocol_name;
3915/// const struct _protocol_list_t * protocol_list; // super protocols
3916/// const struct method_list_t * const instance_methods;
3917/// const struct method_list_t * const class_methods;
3918/// const struct method_list_t *optionalInstanceMethods;
3919/// const struct method_list_t *optionalClassMethods;
3920/// const struct _prop_list_t * properties;
3921/// const uint32_t size; // sizeof(struct _protocol_t)
3922/// const uint32_t flags; // = 0
3923/// }
3924/// @endcode
3925///
3926
3927llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
3928 const ObjCProtocolDecl *PD) {
3929 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
3930
3931 // Early exit if a defining object has already been generated.
3932 if (Entry && Entry->hasInitializer())
3933 return Entry;
3934
3935 const char *ProtocolName = PD->getNameAsCString();
3936
3937 // Construct method lists.
3938 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
3939 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
3940 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
3941 e = PD->instmeth_end(); i != e; ++i) {
3942 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003943 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003944 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
3945 OptInstanceMethods.push_back(C);
3946 } else {
3947 InstanceMethods.push_back(C);
3948 }
3949 }
3950
3951 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
3952 e = PD->classmeth_end(); i != e; ++i) {
3953 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003954 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003955 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
3956 OptClassMethods.push_back(C);
3957 } else {
3958 ClassMethods.push_back(C);
3959 }
3960 }
3961
3962 std::vector<llvm::Constant*> Values(10);
3963 // isa is NULL
3964 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
3965 Values[1] = GetClassName(PD->getIdentifier());
3966 Values[2] = EmitProtocolList(
3967 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
3968 PD->protocol_begin(),
3969 PD->protocol_end());
3970
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003971 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00003972 + PD->getNameAsString(),
3973 "__DATA, __objc_const",
3974 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003975 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00003976 + PD->getNameAsString(),
3977 "__DATA, __objc_const",
3978 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003979 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00003980 + PD->getNameAsString(),
3981 "__DATA, __objc_const",
3982 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00003983 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00003984 + PD->getNameAsString(),
3985 "__DATA, __objc_const",
3986 OptClassMethods);
3987 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
3988 0, PD, ObjCTypes);
3989 uint32_t Size =
3990 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy);
3991 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3992 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
3993 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
3994 Values);
3995
3996 if (Entry) {
3997 // Already created, fix the linkage and update the initializer.
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00003998 Entry->setLinkage(llvm::GlobalValue::WeakLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003999 Entry->setInitializer(Init);
4000 } else {
4001 Entry =
4002 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004003 llvm::GlobalValue::WeakLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004004 Init,
4005 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName,
4006 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004007 Entry->setAlignment(
4008 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004009 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004010 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004011 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
4012
4013 // Use this protocol meta-data to build protocol list table in section
4014 // __DATA, __objc_protolist
4015 llvm::Type *ptype = llvm::PointerType::getUnqual(ObjCTypes.ProtocolnfABITy);
4016 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
4017 ptype, false,
4018 llvm::GlobalValue::WeakLinkage,
4019 Entry,
4020 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
4021 +ProtocolName,
4022 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004023 PTGV->setAlignment(
4024 CGM.getTargetData().getPrefTypeAlignment(ptype));
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004025 PTGV->setSection("__DATA, __objc_protolist");
4026 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4027 UsedGlobals.push_back(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004028 return Entry;
4029}
4030
4031/// EmitProtocolList - Generate protocol list meta-data:
4032/// @code
4033/// struct _protocol_list_t {
4034/// long protocol_count; // Note, this is 32/64 bit
4035/// struct _protocol_t[protocol_count];
4036/// }
4037/// @endcode
4038///
4039llvm::Constant *
4040CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4041 ObjCProtocolDecl::protocol_iterator begin,
4042 ObjCProtocolDecl::protocol_iterator end) {
4043 std::vector<llvm::Constant*> ProtocolRefs;
4044
4045 for (; begin != end; ++begin)
4046 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4047
4048 // Just return null for empty protocol lists
4049 if (ProtocolRefs.empty())
4050 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4051
4052 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4053 if (GV)
4054 return GV;
4055 // This list is null terminated.
4056 ProtocolRefs.push_back(llvm::Constant::getNullValue(
4057 ObjCTypes.ProtocolListnfABIPtrTy));
4058
4059 std::vector<llvm::Constant*> Values(2);
4060 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
4061 Values[1] =
4062 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolListnfABIPtrTy,
4063 ProtocolRefs.size()),
4064 ProtocolRefs);
4065
4066 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4067 GV = new llvm::GlobalVariable(Init->getType(), false,
4068 llvm::GlobalValue::InternalLinkage,
4069 Init,
4070 Name,
4071 &CGM.getModule());
4072 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004073 GV->setAlignment(
4074 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004075 UsedGlobals.push_back(GV);
4076 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
4077}
4078
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004079/// GetMethodDescriptionConstant - This routine build following meta-data:
4080/// struct _objc_method {
4081/// SEL _cmd;
4082/// char *method_type;
4083/// char *_imp;
4084/// }
4085
4086llvm::Constant *
4087CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4088 std::vector<llvm::Constant*> Desc(3);
4089 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4090 ObjCTypes.SelectorPtrTy);
4091 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004092 // Protocol methods have no implementation. So, this entry is always NULL.
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004093 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4094 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
4095}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004096/* *** */
4097
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004098CodeGen::CGObjCRuntime *
4099CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00004100 return new CGObjCMac(CGM);
4101}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004102
4103CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004104CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004105 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004106}