blob: 360eac0535adecd2dd649d4f1707a85fd4ac198e [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 Jahanian83a8a752009-02-04 20:42:28 +0000218public:
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000219 llvm::Function *MessageSendFixupFn, *MessageSendFpretFixupFn,
220 *MessageSendStretFixupFn, *MessageSendIdFixupFn,
221 *MessageSendIdStretFixupFn, *MessageSendSuper2FixupFn,
222 *MessageSendSuper2StretFixupFn;
223
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000224 // MethodListnfABITy - LLVM for struct _method_list_t
225 const llvm::StructType *MethodListnfABITy;
226
227 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
228 const llvm::Type *MethodListnfABIPtrTy;
229
230 // ProtocolnfABITy = LLVM for struct _protocol_t
231 const llvm::StructType *ProtocolnfABITy;
232
233 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
234 const llvm::StructType *ProtocolListnfABITy;
235
236 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
237 const llvm::Type *ProtocolListnfABIPtrTy;
238
239 // ClassnfABITy - LLVM for struct _class_t
240 const llvm::StructType *ClassnfABITy;
241
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000242 // ClassnfABIPtrTy - LLVM for struct _class_t*
243 const llvm::Type *ClassnfABIPtrTy;
244
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000245 // IvarnfABITy - LLVM for struct _ivar_t
246 const llvm::StructType *IvarnfABITy;
247
248 // IvarListnfABITy - LLVM for struct _ivar_list_t
249 const llvm::StructType *IvarListnfABITy;
250
251 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
252 const llvm::Type *IvarListnfABIPtrTy;
253
254 // ClassRonfABITy - LLVM for struct _class_ro_t
255 const llvm::StructType *ClassRonfABITy;
256
257 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
258 const llvm::Type *ImpnfABITy;
259
260 // CategorynfABITy - LLVM for struct _category_t
261 const llvm::StructType *CategorynfABITy;
262
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000263 // New types for nonfragile abi messaging.
264
265 // MessageRefTy - LLVM for:
266 // struct _message_ref_t {
267 // IMP messenger;
268 // SEL name;
269 // };
270 const llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000271 // MessageRefCTy - clang type for struct _message_ref_t
272 QualType MessageRefCTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000273
274 // MessageRefPtrTy - LLVM for struct _message_ref_t*
275 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000276 // MessageRefCPtrTy - clang type for struct _message_ref_t*
277 QualType MessageRefCPtrTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000278
Fariborz Jahanianef163782009-02-05 01:13:09 +0000279 // MessengerTy - Type of the messenger (shown as IMP above)
280 const llvm::FunctionType *MessengerTy;
281
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000282 // SuperMessageRefTy - LLVM for:
283 // struct _super_message_ref_t {
284 // SUPER_IMP messenger;
285 // SEL name;
286 // };
287 const llvm::StructType *SuperMessageRefTy;
288
289 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
290 const llvm::Type *SuperMessageRefPtrTy;
291
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000292 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
293 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000294};
295
296class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
297protected:
298 CodeGen::CodeGenModule &CGM;
299 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000300 unsigned ObjCABI;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000301
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000302 /// LazySymbols - Symbols to generate a lazy reference for. See
303 /// DefinedSymbols and FinishModule().
304 std::set<IdentifierInfo*> LazySymbols;
305
306 /// DefinedSymbols - External symbols which are defined by this
307 /// module. The symbols in this list and LazySymbols are used to add
308 /// special linker symbols which ensure that Objective-C modules are
309 /// linked properly.
310 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000311
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000312 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000313 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000314
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000315 /// MethodVarNames - uniqued method variable names.
316 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000317
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000318 /// MethodVarTypes - uniqued method type signatures. We have to use
319 /// a StringMap here because have no other unique reference.
320 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000321
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000322 /// MethodDefinitions - map of methods which have been defined in
323 /// this translation unit.
324 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000325
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000326 /// PropertyNames - uniqued method variable names.
327 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000328
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000329 /// ClassReferences - uniqued class references.
330 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000331
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000332 /// SelectorReferences - uniqued selector references.
333 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000334
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000335 /// Protocols - Protocols for which an objc_protocol structure has
336 /// been emitted. Forward declarations are handled by creating an
337 /// empty structure whose initializer is filled in when/if defined.
338 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000339
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000340 /// DefinedProtocols - Protocols which have actually been
341 /// defined. We should not need this, see FIXME in GenerateProtocol.
342 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000343
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000344 /// DefinedClasses - List of defined classes.
345 std::vector<llvm::GlobalValue*> DefinedClasses;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000346
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000347 /// DefinedCategories - List of defined categories.
348 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000349
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000350 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000351 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000352 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000353
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000354 /// GetNameForMethod - Return a name for the given method.
355 /// \param[out] NameOut - The return value.
356 void GetNameForMethod(const ObjCMethodDecl *OMD,
357 const ObjCContainerDecl *CD,
358 std::string &NameOut);
359
360 /// GetMethodVarName - Return a unique constant for the given
361 /// selector's name. The return value has type char *.
362 llvm::Constant *GetMethodVarName(Selector Sel);
363 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
364 llvm::Constant *GetMethodVarName(const std::string &Name);
365
366 /// GetMethodVarType - Return a unique constant for the given
367 /// selector's name. The return value has type char *.
368
369 // FIXME: This is a horrible name.
370 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
371 llvm::Constant *GetMethodVarType(const std::string &Name);
372
373 /// GetPropertyName - Return a unique constant for the given
374 /// name. The return value has type char *.
375 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
376
377 // FIXME: This can be dropped once string functions are unified.
378 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
379 const Decl *Container);
380
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000381 /// GetClassName - Return a unique constant for the given selector's
382 /// name. The return value has type char *.
383 llvm::Constant *GetClassName(IdentifierInfo *Ident);
384
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000385 const RecordDecl *GetFirstIvarInRecord(const ObjCInterfaceDecl *OID,
386 RecordDecl::field_iterator &FIV,
387 RecordDecl::field_iterator &PIV);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000388 /// EmitPropertyList - Emit the given property list. The return
389 /// value has type PropertyListPtrTy.
390 llvm::Constant *EmitPropertyList(const std::string &Name,
391 const Decl *Container,
392 const ObjCContainerDecl *OCD,
393 const ObjCCommonTypesHelper &ObjCTypes);
394
Fariborz Jahanianda320092009-01-29 19:24:30 +0000395 /// GetProtocolRef - Return a reference to the internal protocol
396 /// description, creating an empty one if it has not been
397 /// defined. The return value has type ProtocolPtrTy.
398 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
399
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000400public:
401 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
402 { }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000403
404 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000405
406 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
407 const ObjCContainerDecl *CD=0);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000408
409 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
410
411 /// GetOrEmitProtocol - Get the protocol object for the given
412 /// declaration, emitting it if necessary. The return value has type
413 /// ProtocolPtrTy.
414 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
415
416 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
417 /// object for the given declaration, emitting it if needed. These
418 /// forward references will be filled in with empty bodies if no
419 /// definition is seen. The return value has type ProtocolPtrTy.
420 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000421};
422
423class CGObjCMac : public CGObjCCommonMac {
424private:
425 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000426 /// EmitImageInfo - Emit the image info marker used to encode some module
427 /// level information.
428 void EmitImageInfo();
429
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000430 /// EmitModuleInfo - Another marker encoding module level
431 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000432 void EmitModuleInfo();
433
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000434 /// EmitModuleSymols - Emit module symbols, the list of defined
435 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000436 llvm::Constant *EmitModuleSymbols();
437
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000438 /// FinishModule - Write out global data structures at the end of
439 /// processing a translation unit.
440 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000441
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000442 /// EmitClassExtension - Generate the class extension structure used
443 /// to store the weak ivar layout and properties. The return value
444 /// has type ClassExtensionPtrTy.
445 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
446
447 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
448 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000449 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000450 const ObjCInterfaceDecl *ID);
451
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000452 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000453 QualType ResultType,
454 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000455 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000456 QualType Arg0Ty,
457 bool IsSuper,
458 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000459
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000460 /// EmitIvarList - Emit the ivar list for the given
461 /// implementation. If ForClass is true the list of class ivars
462 /// (i.e. metaclass ivars) is emitted, otherwise the list of
463 /// interface ivars will be emitted. The return value has type
464 /// IvarListPtrTy.
465 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000466 bool ForClass);
467
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000468 /// EmitMetaClass - Emit a forward reference to the class structure
469 /// for the metaclass of the given interface. The return value has
470 /// type ClassPtrTy.
471 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
472
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000473 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000474 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000475 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
476 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000477 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000478 const ConstantVector &Methods);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000479
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000480 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000481
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000482 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000483
484 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000485 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000486 llvm::Constant *EmitMethodList(const std::string &Name,
487 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000488 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000489
490 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000491 /// method declarations.
492 /// - TypeName: The name for the type containing the methods.
493 /// - IsProtocol: True iff these methods are for a protocol.
494 /// - ClassMethds: True iff these are class methods.
495 /// - Required: When true, only "required" methods are
496 /// listed. Similarly, when false only "optional" methods are
497 /// listed. For classes this should always be true.
498 /// - begin, end: The method list to output.
499 ///
500 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000501 llvm::Constant *EmitMethodDescList(const std::string &Name,
502 const char *Section,
503 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000504
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000505 /// GetOrEmitProtocol - Get the protocol object for the given
506 /// declaration, emitting it if necessary. The return value has type
507 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000508 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000509
510 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
511 /// object for the given declaration, emitting it if needed. These
512 /// forward references will be filled in with empty bodies if no
513 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000514 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000515
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000516 /// EmitProtocolExtension - Generate the protocol extension
517 /// structure used to store optional instance and class methods, and
518 /// protocol properties. The return value has type
519 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000520 llvm::Constant *
521 EmitProtocolExtension(const ObjCProtocolDecl *PD,
522 const ConstantVector &OptInstanceMethods,
523 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000524
525 /// EmitProtocolList - Generate the list of referenced
526 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc93372008-08-21 21:57:41 +0000527 llvm::Constant *EmitProtocolList(const std::string &Name,
528 ObjCProtocolDecl::protocol_iterator begin,
529 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000530
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000531 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
532 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000533 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000534
Fariborz Jahanianda320092009-01-29 19:24:30 +0000535 public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000536 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000537
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000538 virtual llvm::Function *ModuleInitFunction();
539
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000540 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000541 QualType ResultType,
542 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000543 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000544 bool IsClassMessage,
545 const CallArgList &CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000546
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000547 virtual CodeGen::RValue
548 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000549 QualType ResultType,
550 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000551 const ObjCInterfaceDecl *Class,
552 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000553 bool IsClassMessage,
554 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000555
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000556 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000557 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000558
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000559 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000560
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000561 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000562
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000563 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000564
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000565 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000566 const ObjCProtocolDecl *PD);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000567
Daniel Dunbar49f66022008-09-24 03:38:44 +0000568 virtual llvm::Function *GetPropertyGetFunction();
569 virtual llvm::Function *GetPropertySetFunction();
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000570 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000571
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000572 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
573 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000574 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
575 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000576 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000577 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000578 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
579 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000580 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
581 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000582 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
583 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000584 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
585 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +0000586
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000587 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
588 QualType ObjectTy,
589 llvm::Value *BaseValue,
590 const ObjCIvarDecl *Ivar,
591 const FieldDecl *Field,
592 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000593 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
594 ObjCInterfaceDecl *Interface,
595 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000596};
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000597
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000598class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000599private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000600 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000601 llvm::GlobalVariable* ObjCEmptyCacheVar;
602 llvm::GlobalVariable* ObjCEmptyVtableVar;
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +0000603 /// MetaClassReferences - uniqued meta class references.
604 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000605
606 /// FinishNonFragileABIModule - Write out global data structures at the end of
607 /// processing a translation unit.
608 void FinishNonFragileABIModule();
609
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000610 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
611 unsigned InstanceStart,
612 unsigned InstanceSize,
613 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +0000614 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
615 llvm::Constant *IsAGV,
616 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +0000617 llvm::Constant *ClassRoGV,
618 bool HiddenVisibility);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000619
620 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
621
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +0000622 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
623
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000624 /// EmitMethodList - Emit the method list for the given
625 /// implementation. The return value has type MethodListnfABITy.
626 llvm::Constant *EmitMethodList(const std::string &Name,
627 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +0000628 const ConstantVector &Methods);
629 /// EmitIvarList - Emit the ivar list for the given
630 /// implementation. If ForClass is true the list of class ivars
631 /// (i.e. metaclass ivars) is emitted, otherwise the list of
632 /// interface ivars will be emitted. The return value has type
633 /// IvarListnfABIPtrTy.
634 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000635
Fariborz Jahanianed157d32009-02-10 20:21:06 +0000636 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +0000637 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +0000638 unsigned long int offset);
639
Fariborz Jahanianda320092009-01-29 19:24:30 +0000640 /// GetOrEmitProtocol - Get the protocol object for the given
641 /// declaration, emitting it if necessary. The return value has type
642 /// ProtocolPtrTy.
643 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
644
645 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
646 /// object for the given declaration, emitting it if needed. These
647 /// forward references will be filled in with empty bodies if no
648 /// definition is seen. The return value has type ProtocolPtrTy.
649 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
650
651 /// EmitProtocolList - Generate the list of referenced
652 /// protocols. The return value has type ProtocolListPtrTy.
653 llvm::Constant *EmitProtocolList(const std::string &Name,
654 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000655 ObjCProtocolDecl::protocol_iterator end);
656
657 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
658 QualType ResultType,
659 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000660 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000661 QualType Arg0Ty,
662 bool IsSuper,
663 const CallArgList &CallArgs);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000664
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +0000665 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
666 /// for the given class.
667 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +0000668 const ObjCInterfaceDecl *ID,
669 bool IsSuper = false);
670
671 /// EmitMetaClassRef - Return a Value * of the address of _class_t
672 /// meta-data
673 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
674 const ObjCInterfaceDecl *ID);
675
Fariborz Jahanianed157d32009-02-10 20:21:06 +0000676 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
677 /// the given ivar.
678 ///
679 llvm::GlobalVariable * ObjCIvarOffsetVariable(std::string &Name,
Fariborz Jahanian01a0c362009-02-12 18:51:23 +0000680 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +0000681 const ObjCIvarDecl *Ivar);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +0000682
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +0000683 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
684 /// for the given selector.
685 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
686
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000687public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000688 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000689 // FIXME. All stubs for now!
690 virtual llvm::Function *ModuleInitFunction();
691
692 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
693 QualType ResultType,
694 Selector Sel,
695 llvm::Value *Receiver,
696 bool IsClassMessage,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000697 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000698
699 virtual CodeGen::RValue
700 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
701 QualType ResultType,
702 Selector Sel,
703 const ObjCInterfaceDecl *Class,
704 llvm::Value *Receiver,
705 bool IsClassMessage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +0000706 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000707
708 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +0000709 const ObjCInterfaceDecl *ID);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000710
711 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +0000712 { return EmitSelector(Builder, Sel); }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000713
Fariborz Jahanianeb062d92009-01-26 18:32:24 +0000714 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000715
716 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000717 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +0000718 const ObjCProtocolDecl *PD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000719
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000720 virtual llvm::Function *GetPropertyGetFunction(){
721 return ObjCTypes.GetPropertyFn;
722 }
723 virtual llvm::Function *GetPropertySetFunction(){
724 return ObjCTypes.SetPropertyFn;
725 }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000726 virtual llvm::Function *EnumerationMutationFunction()
Fariborz Jahanian3afdfd72009-02-10 22:46:12 +0000727 { return ObjCTypes.EnumerationMutationFn; }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000728
729 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
730 const Stmt &S)
731 { return; }
732 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
733 const ObjCAtThrowStmt &S)
734 { return; }
735 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
736 llvm::Value *AddrWeakObj)
737 { return 0; }
738 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
739 llvm::Value *src, llvm::Value *dst)
740 { return; }
741 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
742 llvm::Value *src, llvm::Value *dest)
743 { return; }
744 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
745 llvm::Value *src, llvm::Value *dest)
746 { return; }
747 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
748 llvm::Value *src, llvm::Value *dest)
749 { return; }
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000750 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
751 QualType ObjectTy,
752 llvm::Value *BaseValue,
753 const ObjCIvarDecl *Ivar,
754 const FieldDecl *Field,
755 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000756 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
757 ObjCInterfaceDecl *Interface,
758 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000759};
760
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000761} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000762
763/* *** Helper Functions *** */
764
765/// getConstantGEP() - Help routine to construct simple GEPs.
766static llvm::Constant *getConstantGEP(llvm::Constant *C,
767 unsigned idx0,
768 unsigned idx1) {
769 llvm::Value *Idxs[] = {
770 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
771 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
772 };
773 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
774}
775
776/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000777
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000778CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
779 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000780{
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000781 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000782 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000783}
784
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000785/// GetClass - Return a reference to the class for the given interface
786/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000787llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000788 const ObjCInterfaceDecl *ID) {
789 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000790}
791
792/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000793llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000794 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000795}
796
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000797/// Generate a constant CFString object.
798/*
799 struct __builtin_CFString {
800 const int *isa; // point to __CFConstantStringClassReference
801 int flags;
802 const char *str;
803 long length;
804 };
805*/
806
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000807llvm::Constant *CGObjCCommonMac::GenerateConstantString(
808 const std::string &String) {
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000809 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000810}
811
812/// Generates a message send where the super is the receiver. This is
813/// a message send to self with special delivery semantics indicating
814/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000815CodeGen::RValue
816CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000817 QualType ResultType,
818 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000819 const ObjCInterfaceDecl *Class,
820 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000821 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000822 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000823 // Create and init a super structure; this is a (receiver, class)
824 // pair we will pass to objc_msgSendSuper.
825 llvm::Value *ObjCSuper =
826 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
827 llvm::Value *ReceiverAsObject =
828 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
829 CGF.Builder.CreateStore(ReceiverAsObject,
830 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000831
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000832 // If this is a class message the metaclass is passed as the target.
833 llvm::Value *Target;
834 if (IsClassMessage) {
835 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
836 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
837 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
838 Target = Super;
839 } else {
840 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
841 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000842 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
843 // and ObjCTypes types.
844 const llvm::Type *ClassTy =
845 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000846 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000847 CGF.Builder.CreateStore(Target,
848 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
849
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000850 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000851 ObjCSuper, ObjCTypes.SuperPtrCTy,
852 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000853}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000854
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000855/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000856CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000857 QualType ResultType,
858 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000859 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000860 bool IsClassMessage,
861 const CallArgList &CallArgs) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000862 llvm::Value *Arg0 =
863 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000864 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000865 Arg0, CGF.getContext().getObjCIdType(),
866 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000867}
868
869CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000870 QualType ResultType,
871 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000872 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000873 QualType Arg0Ty,
874 bool IsSuper,
875 const CallArgList &CallArgs) {
876 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000877 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
878 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
879 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000880 CGF.getContext().getObjCSelType()));
881 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000882
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000883 CodeGenTypes &Types = CGM.getTypes();
884 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
885 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false);
Daniel Dunbar5669e572008-10-17 03:24:53 +0000886
887 llvm::Constant *Fn;
Daniel Dunbar88b53962009-02-02 22:03:45 +0000888 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Daniel Dunbar5669e572008-10-17 03:24:53 +0000889 Fn = ObjCTypes.getSendStretFn(IsSuper);
890 } else if (ResultType->isFloatingType()) {
891 // FIXME: Sadly, this is wrong. This actually depends on the
892 // architecture. This happens to be right for x86-32 though.
893 Fn = ObjCTypes.getSendFpretFn(IsSuper);
894 } else {
895 Fn = ObjCTypes.getSendFn(IsSuper);
896 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000897 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar88b53962009-02-02 22:03:45 +0000898 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000899}
900
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000901llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000902 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +0000903 // FIXME: I don't understand why gcc generates this, or where it is
904 // resolved. Investigate. Its also wasteful to look this up over and
905 // over.
906 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
907
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000908 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
909 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000910}
911
Fariborz Jahanianda320092009-01-29 19:24:30 +0000912void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000913 // FIXME: We shouldn't need this, the protocol decl should contain
914 // enough information to tell us whether this was a declaration or a
915 // definition.
916 DefinedProtocols.insert(PD->getIdentifier());
917
918 // If we have generated a forward reference to this protocol, emit
919 // it now. Otherwise do nothing, the protocol objects are lazily
920 // emitted.
921 if (Protocols.count(PD->getIdentifier()))
922 GetOrEmitProtocol(PD);
923}
924
Fariborz Jahanianda320092009-01-29 19:24:30 +0000925llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000926 if (DefinedProtocols.count(PD->getIdentifier()))
927 return GetOrEmitProtocol(PD);
928 return GetOrEmitProtocolRef(PD);
929}
930
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000931/*
932 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
933 struct _objc_protocol {
934 struct _objc_protocol_extension *isa;
935 char *protocol_name;
936 struct _objc_protocol_list *protocol_list;
937 struct _objc__method_prototype_list *instance_methods;
938 struct _objc__method_prototype_list *class_methods
939 };
940
941 See EmitProtocolExtension().
942*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000943llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
944 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
945
946 // Early exit if a defining object has already been generated.
947 if (Entry && Entry->hasInitializer())
948 return Entry;
949
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000950 // FIXME: I don't understand why gcc generates this, or where it is
951 // resolved. Investigate. Its also wasteful to look this up over and
952 // over.
953 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
954
Chris Lattner8ec03f52008-11-24 03:54:41 +0000955 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000956
957 // Construct method lists.
958 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
959 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
960 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
961 e = PD->instmeth_end(); i != e; ++i) {
962 ObjCMethodDecl *MD = *i;
963 llvm::Constant *C = GetMethodDescriptionConstant(MD);
964 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
965 OptInstanceMethods.push_back(C);
966 } else {
967 InstanceMethods.push_back(C);
968 }
969 }
970
971 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
972 e = PD->classmeth_end(); i != e; ++i) {
973 ObjCMethodDecl *MD = *i;
974 llvm::Constant *C = GetMethodDescriptionConstant(MD);
975 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
976 OptClassMethods.push_back(C);
977 } else {
978 ClassMethods.push_back(C);
979 }
980 }
981
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000982 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000983 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000984 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc93372008-08-21 21:57:41 +0000985 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000986 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc93372008-08-21 21:57:41 +0000987 PD->protocol_begin(),
988 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000989 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000990 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
991 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000992 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
993 InstanceMethods);
994 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000995 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
996 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000997 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
998 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000999 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
1000 Values);
1001
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001002 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001003 // Already created, fix the linkage and update the initializer.
1004 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001005 Entry->setInitializer(Init);
1006 } else {
1007 Entry =
1008 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
1009 llvm::GlobalValue::InternalLinkage,
1010 Init,
1011 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
1012 &CGM.getModule());
1013 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
1014 UsedGlobals.push_back(Entry);
1015 // FIXME: Is this necessary? Why only for protocol?
1016 Entry->setAlignment(4);
1017 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001018
1019 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001020}
1021
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001022llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001023 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1024
1025 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001026 // We use the initializer as a marker of whether this is a forward
1027 // reference or not. At module finalization we add the empty
1028 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001029 Entry =
1030 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001031 llvm::GlobalValue::ExternalLinkage,
1032 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001033 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001034 &CGM.getModule());
1035 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
1036 UsedGlobals.push_back(Entry);
1037 // FIXME: Is this necessary? Why only for protocol?
1038 Entry->setAlignment(4);
1039 }
1040
1041 return Entry;
1042}
1043
1044/*
1045 struct _objc_protocol_extension {
1046 uint32_t size;
1047 struct objc_method_description_list *optional_instance_methods;
1048 struct objc_method_description_list *optional_class_methods;
1049 struct objc_property_list *instance_properties;
1050 };
1051*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001052llvm::Constant *
1053CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1054 const ConstantVector &OptInstanceMethods,
1055 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001056 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001057 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001058 std::vector<llvm::Constant*> Values(4);
1059 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001060 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001061 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1062 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001063 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1064 OptInstanceMethods);
1065 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001066 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1067 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001068 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1069 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001070 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1071 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001072 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001073
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001074 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001075 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1076 Values[3]->isNullValue())
1077 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
1078
1079 llvm::Constant *Init =
1080 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
1081 llvm::GlobalVariable *GV =
1082 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
1083 llvm::GlobalValue::InternalLinkage,
1084 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001085 "\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001086 &CGM.getModule());
1087 // No special section, but goes in llvm.used
1088 UsedGlobals.push_back(GV);
1089
1090 return GV;
1091}
1092
1093/*
1094 struct objc_protocol_list {
1095 struct objc_protocol_list *next;
1096 long count;
1097 Protocol *list[];
1098 };
1099*/
Daniel Dunbardbc93372008-08-21 21:57:41 +00001100llvm::Constant *
1101CGObjCMac::EmitProtocolList(const std::string &Name,
1102 ObjCProtocolDecl::protocol_iterator begin,
1103 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001104 std::vector<llvm::Constant*> ProtocolRefs;
1105
Daniel Dunbardbc93372008-08-21 21:57:41 +00001106 for (; begin != end; ++begin)
1107 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001108
1109 // Just return null for empty protocol lists
1110 if (ProtocolRefs.empty())
1111 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1112
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001113 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001114 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1115
1116 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001117 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001118 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1119 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1120 Values[2] =
1121 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1122 ProtocolRefs.size()),
1123 ProtocolRefs);
1124
1125 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1126 llvm::GlobalVariable *GV =
1127 new llvm::GlobalVariable(Init->getType(), false,
1128 llvm::GlobalValue::InternalLinkage,
1129 Init,
Daniel Dunbardbc93372008-08-21 21:57:41 +00001130 Name,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001131 &CGM.getModule());
1132 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
1133 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1134}
1135
1136/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001137 struct _objc_property {
1138 const char * const name;
1139 const char * const attributes;
1140 };
1141
1142 struct _objc_property_list {
1143 uint32_t entsize; // sizeof (struct _objc_property)
1144 uint32_t prop_count;
1145 struct _objc_property[prop_count];
1146 };
1147*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001148llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1149 const Decl *Container,
1150 const ObjCContainerDecl *OCD,
1151 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001152 std::vector<llvm::Constant*> Properties, Prop(2);
Steve Naroff93983f82009-01-11 12:47:58 +00001153 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1154 E = OCD->prop_end(); I != E; ++I) {
1155 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001156 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001157 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001158 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1159 Prop));
1160 }
1161
1162 // Return null for empty list.
1163 if (Properties.empty())
1164 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1165
1166 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001167 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001168 std::vector<llvm::Constant*> Values(3);
1169 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1170 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1171 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1172 Properties.size());
1173 Values[2] = llvm::ConstantArray::get(AT, Properties);
1174 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1175
1176 llvm::GlobalVariable *GV =
1177 new llvm::GlobalVariable(Init->getType(), false,
1178 llvm::GlobalValue::InternalLinkage,
1179 Init,
1180 Name,
1181 &CGM.getModule());
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001182 if (ObjCABI == 2)
1183 GV->setSection("__DATA, __objc_const");
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001184 // No special section on property lists?
1185 UsedGlobals.push_back(GV);
1186 return llvm::ConstantExpr::getBitCast(GV,
1187 ObjCTypes.PropertyListPtrTy);
1188
1189}
1190
1191/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001192 struct objc_method_description_list {
1193 int count;
1194 struct objc_method_description list[];
1195 };
1196*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001197llvm::Constant *
1198CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1199 std::vector<llvm::Constant*> Desc(2);
1200 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1201 ObjCTypes.SelectorPtrTy);
1202 Desc[1] = GetMethodVarType(MD);
1203 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1204 Desc);
1205}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001206
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001207llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1208 const char *Section,
1209 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001210 // Return null for empty list.
1211 if (Methods.empty())
1212 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1213
1214 std::vector<llvm::Constant*> Values(2);
1215 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1216 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1217 Methods.size());
1218 Values[1] = llvm::ConstantArray::get(AT, Methods);
1219 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1220
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001221 llvm::GlobalVariable *GV =
1222 new llvm::GlobalVariable(Init->getType(), false,
1223 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001224 Init, Name, &CGM.getModule());
1225 GV->setSection(Section);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001226 UsedGlobals.push_back(GV);
1227 return llvm::ConstantExpr::getBitCast(GV,
1228 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001229}
1230
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001231/*
1232 struct _objc_category {
1233 char *category_name;
1234 char *class_name;
1235 struct _objc_method_list *instance_methods;
1236 struct _objc_method_list *class_methods;
1237 struct _objc_protocol_list *protocols;
1238 uint32_t size; // <rdar://4585769>
1239 struct _objc_property_list *instance_properties;
1240 };
1241 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001242void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001243 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001244
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001245 // FIXME: This is poor design, the OCD should have a pointer to the
1246 // category decl. Additionally, note that Category can be null for
1247 // the @implementation w/o an @interface case. Sema should just
1248 // create one for us as it does for @implementation so everyone else
1249 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001250 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001251 const ObjCCategoryDecl *Category =
1252 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001253 std::string ExtName(Interface->getNameAsString() + "_" +
1254 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001255
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001256 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1257 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
1258 e = OCD->instmeth_end(); i != e; ++i) {
1259 // Instance methods should always be defined.
1260 InstanceMethods.push_back(GetMethodConstant(*i));
1261 }
1262 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
1263 e = OCD->classmeth_end(); i != e; ++i) {
1264 // Class methods should always be defined.
1265 ClassMethods.push_back(GetMethodConstant(*i));
1266 }
1267
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001268 std::vector<llvm::Constant*> Values(7);
1269 Values[0] = GetClassName(OCD->getIdentifier());
1270 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001271 Values[2] =
1272 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1273 ExtName,
1274 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001275 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001276 Values[3] =
1277 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
1278 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001279 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001280 if (Category) {
1281 Values[4] =
1282 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1283 Category->protocol_begin(),
1284 Category->protocol_end());
1285 } else {
1286 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1287 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001288 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001289
1290 // If there is no category @interface then there can be no properties.
1291 if (Category) {
1292 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001293 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001294 } else {
1295 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1296 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001297
1298 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1299 Values);
1300
1301 llvm::GlobalVariable *GV =
1302 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
1303 llvm::GlobalValue::InternalLinkage,
1304 Init,
1305 std::string("\01L_OBJC_CATEGORY_")+ExtName,
1306 &CGM.getModule());
1307 GV->setSection("__OBJC,__category,regular,no_dead_strip");
1308 UsedGlobals.push_back(GV);
1309 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001310}
1311
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001312// FIXME: Get from somewhere?
1313enum ClassFlags {
1314 eClassFlags_Factory = 0x00001,
1315 eClassFlags_Meta = 0x00002,
1316 // <rdr://5142207>
1317 eClassFlags_HasCXXStructors = 0x02000,
1318 eClassFlags_Hidden = 0x20000,
1319 eClassFlags_ABI2_Hidden = 0x00010,
1320 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1321};
1322
1323// <rdr://5142207&4705298&4843145>
1324static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
1325 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
1326 // FIXME: Support -fvisibility
1327 switch (attr->getVisibility()) {
1328 default:
1329 assert(0 && "Unknown visibility");
1330 return false;
1331 case VisibilityAttr::DefaultVisibility:
1332 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
1333 return false;
1334 case VisibilityAttr::HiddenVisibility:
1335 return true;
1336 }
1337 } else {
1338 return false; // FIXME: Support -fvisibility
1339 }
1340}
1341
1342/*
1343 struct _objc_class {
1344 Class isa;
1345 Class super_class;
1346 const char *name;
1347 long version;
1348 long info;
1349 long instance_size;
1350 struct _objc_ivar_list *ivars;
1351 struct _objc_method_list *methods;
1352 struct _objc_cache *cache;
1353 struct _objc_protocol_list *protocols;
1354 // Objective-C 1.0 extensions (<rdr://4585769>)
1355 const char *ivar_layout;
1356 struct _objc_class_ext *ext;
1357 };
1358
1359 See EmitClassExtension();
1360 */
1361void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001362 DefinedSymbols.insert(ID->getIdentifier());
1363
Chris Lattner8ec03f52008-11-24 03:54:41 +00001364 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001365 // FIXME: Gross
1366 ObjCInterfaceDecl *Interface =
1367 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc93372008-08-21 21:57:41 +00001368 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001369 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00001370 Interface->protocol_begin(),
1371 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001372 const llvm::Type *InterfaceTy =
1373 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
1374 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001375 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001376
1377 // FIXME: Set CXX-structors flag.
1378 if (IsClassHidden(ID->getClassInterface()))
1379 Flags |= eClassFlags_Hidden;
1380
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001381 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1382 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
1383 e = ID->instmeth_end(); i != e; ++i) {
1384 // Instance methods should always be defined.
1385 InstanceMethods.push_back(GetMethodConstant(*i));
1386 }
1387 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
1388 e = ID->classmeth_end(); i != e; ++i) {
1389 // Class methods should always be defined.
1390 ClassMethods.push_back(GetMethodConstant(*i));
1391 }
1392
1393 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
1394 e = ID->propimpl_end(); i != e; ++i) {
1395 ObjCPropertyImplDecl *PID = *i;
1396
1397 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1398 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1399
1400 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1401 if (llvm::Constant *C = GetMethodConstant(MD))
1402 InstanceMethods.push_back(C);
1403 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1404 if (llvm::Constant *C = GetMethodConstant(MD))
1405 InstanceMethods.push_back(C);
1406 }
1407 }
1408
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001409 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001410 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001411 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001412 // Record a reference to the super class.
1413 LazySymbols.insert(Super->getIdentifier());
1414
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001415 Values[ 1] =
1416 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1417 ObjCTypes.ClassPtrTy);
1418 } else {
1419 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1420 }
1421 Values[ 2] = GetClassName(ID->getIdentifier());
1422 // Version is always 0.
1423 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1424 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1425 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001426 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001427 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001428 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001429 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001430 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001431 // cache is always NULL.
1432 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1433 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001434 // FIXME: Set ivar_layout
1435 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001436 Values[11] = EmitClassExtension(ID);
1437 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1438 Values);
1439
1440 llvm::GlobalVariable *GV =
1441 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1442 llvm::GlobalValue::InternalLinkage,
1443 Init,
1444 std::string("\01L_OBJC_CLASS_")+ClassName,
1445 &CGM.getModule());
1446 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1447 UsedGlobals.push_back(GV);
1448 // FIXME: Why?
1449 GV->setAlignment(32);
1450 DefinedClasses.push_back(GV);
1451}
1452
1453llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1454 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001455 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001456 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001457 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001458 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001459
1460 if (IsClassHidden(ID->getClassInterface()))
1461 Flags |= eClassFlags_Hidden;
1462
1463 std::vector<llvm::Constant*> Values(12);
1464 // The isa for the metaclass is the root of the hierarchy.
1465 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1466 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1467 Root = Super;
1468 Values[ 0] =
1469 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1470 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001471 // The super class for the metaclass is emitted as the name of the
1472 // super class. The runtime fixes this up to point to the
1473 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001474 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1475 Values[ 1] =
1476 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1477 ObjCTypes.ClassPtrTy);
1478 } else {
1479 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1480 }
1481 Values[ 2] = GetClassName(ID->getIdentifier());
1482 // Version is always 0.
1483 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1484 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1485 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001486 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001487 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001488 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001489 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001490 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001491 // cache is always NULL.
1492 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1493 Values[ 9] = Protocols;
1494 // ivar_layout for metaclass is always NULL.
1495 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1496 // The class extension is always unused for metaclasses.
1497 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1498 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1499 Values);
1500
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001501 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00001502 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001503
1504 // Check for a forward reference.
1505 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1506 if (GV) {
1507 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1508 "Forward metaclass reference has incorrect type.");
1509 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1510 GV->setInitializer(Init);
1511 } else {
1512 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1513 llvm::GlobalValue::InternalLinkage,
1514 Init, Name,
1515 &CGM.getModule());
1516 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001517 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1518 UsedGlobals.push_back(GV);
1519 // FIXME: Why?
1520 GV->setAlignment(32);
1521
1522 return GV;
1523}
1524
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001525llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001526 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001527
1528 // FIXME: Should we look these up somewhere other than the
1529 // module. Its a bit silly since we only generate these while
1530 // processing an implementation, so exactly one pointer would work
1531 // if know when we entered/exitted an implementation block.
1532
1533 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00001534 // Previously, metaclass with internal linkage may have been defined.
1535 // pass 'true' as 2nd argument so it is returned.
1536 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001537 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1538 "Forward metaclass reference has incorrect type.");
1539 return GV;
1540 } else {
1541 // Generate as an external reference to keep a consistent
1542 // module. This will be patched up when we emit the metaclass.
1543 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1544 llvm::GlobalValue::ExternalLinkage,
1545 0,
1546 Name,
1547 &CGM.getModule());
1548 }
1549}
1550
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001551/*
1552 struct objc_class_ext {
1553 uint32_t size;
1554 const char *weak_ivar_layout;
1555 struct _objc_property_list *properties;
1556 };
1557*/
1558llvm::Constant *
1559CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1560 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001561 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001562
1563 std::vector<llvm::Constant*> Values(3);
1564 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001565 // FIXME: Output weak_ivar_layout string.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001566 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001567 Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001568 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001569
1570 // Return null if no extension bits are used.
1571 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1572 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1573
1574 llvm::Constant *Init =
1575 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1576 llvm::GlobalVariable *GV =
1577 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1578 llvm::GlobalValue::InternalLinkage,
1579 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001580 "\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001581 &CGM.getModule());
1582 // No special section, but goes in llvm.used
1583 UsedGlobals.push_back(GV);
1584
1585 return GV;
1586}
1587
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001588/// countInheritedIvars - count number of ivars in class and its super class(s)
1589///
1590static int countInheritedIvars(const ObjCInterfaceDecl *OI) {
1591 int count = 0;
1592 if (!OI)
1593 return 0;
1594 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
1595 if (SuperClass)
1596 count += countInheritedIvars(SuperClass);
1597 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1598 E = OI->ivar_end(); I != E; ++I)
1599 ++count;
1600 return count;
1601}
1602
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00001603/// getInterfaceDeclForIvar - Get the interface declaration node where
1604/// this ivar is declared in.
1605/// FIXME. Ideally, this info should be in the ivar node. But currently
1606/// it is not and prevailing wisdom is that ASTs should not have more
1607/// info than is absolutely needed, even though this info reflects the
1608/// source language.
1609///
1610static const ObjCInterfaceDecl *getInterfaceDeclForIvar(
1611 const ObjCInterfaceDecl *OI,
1612 const ObjCIvarDecl *IVD) {
1613 if (!OI)
1614 return 0;
1615 assert(isa<ObjCInterfaceDecl>(OI) && "OI is not an interface");
1616 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1617 E = OI->ivar_end(); I != E; ++I)
1618 if ((*I)->getIdentifier() == IVD->getIdentifier())
1619 return OI;
1620 return getInterfaceDeclForIvar(OI->getSuperClass(), IVD);
1621}
1622
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001623/*
1624 struct objc_ivar {
1625 char *ivar_name;
1626 char *ivar_type;
1627 int ivar_offset;
1628 };
1629
1630 struct objc_ivar_list {
1631 int ivar_count;
1632 struct objc_ivar list[count];
1633 };
1634 */
1635llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001636 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001637 std::vector<llvm::Constant*> Ivars, Ivar(3);
1638
1639 // When emitting the root class GCC emits ivar entries for the
1640 // actual class structure. It is not clear if we need to follow this
1641 // behavior; for now lets try and get away with not doing it. If so,
1642 // the cleanest solution would be to make up an ObjCInterfaceDecl
1643 // for the class.
1644 if (ForClass)
1645 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001646
1647 ObjCInterfaceDecl *OID =
1648 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
1649 const llvm::Type *InterfaceTy =
1650 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001651 const llvm::StructLayout *Layout =
1652 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001653
1654 RecordDecl::field_iterator ifield, pfield;
1655 const RecordDecl *RD = GetFirstIvarInRecord(OID, ifield, pfield);
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001656 for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) {
1657 FieldDecl *Field = *ifield;
1658 unsigned Offset = Layout->getElementOffset(CGM.getTypes().
1659 getLLVMFieldNo(Field));
1660 if (Field->getIdentifier())
1661 Ivar[0] = GetMethodVarName(Field->getIdentifier());
1662 else
1663 Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001664 std::string TypeStr;
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001665 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001666 Ivar[1] = GetMethodVarType(TypeStr);
1667 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001668 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001669 }
1670
1671 // Return null for empty list.
1672 if (Ivars.empty())
1673 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1674
1675 std::vector<llvm::Constant*> Values(2);
1676 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1677 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1678 Ivars.size());
1679 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1680 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1681
1682 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1683 "\01L_OBJC_INSTANCE_VARIABLES_");
1684 llvm::GlobalVariable *GV =
1685 new llvm::GlobalVariable(Init->getType(), false,
1686 llvm::GlobalValue::InternalLinkage,
1687 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001688 Prefix + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001689 &CGM.getModule());
1690 if (ForClass) {
1691 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1692 // FIXME: Why is this only here?
1693 GV->setAlignment(32);
1694 } else {
1695 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1696 }
1697 UsedGlobals.push_back(GV);
1698 return llvm::ConstantExpr::getBitCast(GV,
1699 ObjCTypes.IvarListPtrTy);
1700}
1701
1702/*
1703 struct objc_method {
1704 SEL method_name;
1705 char *method_types;
1706 void *method;
1707 };
1708
1709 struct objc_method_list {
1710 struct objc_method_list *obsolete;
1711 int count;
1712 struct objc_method methods_list[count];
1713 };
1714*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001715
1716/// GetMethodConstant - Return a struct objc_method constant for the
1717/// given method if it has been defined. The result is null if the
1718/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001719llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001720 // FIXME: Use DenseMap::lookup
1721 llvm::Function *Fn = MethodDefinitions[MD];
1722 if (!Fn)
1723 return 0;
1724
1725 std::vector<llvm::Constant*> Method(3);
1726 Method[0] =
1727 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1728 ObjCTypes.SelectorPtrTy);
1729 Method[1] = GetMethodVarType(MD);
1730 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1731 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1732}
1733
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001734llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1735 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001736 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001737 // Return null for empty list.
1738 if (Methods.empty())
1739 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1740
1741 std::vector<llvm::Constant*> Values(3);
1742 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1743 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1744 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1745 Methods.size());
1746 Values[2] = llvm::ConstantArray::get(AT, Methods);
1747 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1748
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001749 llvm::GlobalVariable *GV =
1750 new llvm::GlobalVariable(Init->getType(), false,
1751 llvm::GlobalValue::InternalLinkage,
1752 Init,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001753 Name,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001754 &CGM.getModule());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001755 GV->setSection(Section);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001756 UsedGlobals.push_back(GV);
1757 return llvm::ConstantExpr::getBitCast(GV,
1758 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001759}
1760
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001761llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001762 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001763 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001764 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001765
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001766 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001767 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001768 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001769 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001770 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001771 llvm::GlobalValue::InternalLinkage,
1772 Name,
1773 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001774 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001775
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001776 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001777}
1778
1779llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001780 // Abuse this interface function as a place to finalize.
1781 FinishModule();
1782
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001783 return NULL;
1784}
1785
Daniel Dunbar49f66022008-09-24 03:38:44 +00001786llvm::Function *CGObjCMac::GetPropertyGetFunction() {
1787 return ObjCTypes.GetPropertyFn;
1788}
1789
1790llvm::Function *CGObjCMac::GetPropertySetFunction() {
1791 return ObjCTypes.SetPropertyFn;
1792}
1793
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001794llvm::Function *CGObjCMac::EnumerationMutationFunction()
1795{
1796 return ObjCTypes.EnumerationMutationFn;
1797}
1798
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001799/*
1800
1801Objective-C setjmp-longjmp (sjlj) Exception Handling
1802--
1803
1804The basic framework for a @try-catch-finally is as follows:
1805{
1806 objc_exception_data d;
1807 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00001808 bool _call_try_exit = true;
1809
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001810 objc_exception_try_enter(&d);
1811 if (!setjmp(d.jmp_buf)) {
1812 ... try body ...
1813 } else {
1814 // exception path
1815 id _caught = objc_exception_extract(&d);
1816
1817 // enter new try scope for handlers
1818 if (!setjmp(d.jmp_buf)) {
1819 ... match exception and execute catch blocks ...
1820
1821 // fell off end, rethrow.
1822 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00001823 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001824 } else {
1825 // exception in catch block
1826 _rethrow = objc_exception_extract(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00001827 _call_try_exit = false;
1828 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001829 }
1830 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00001831 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001832
1833finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00001834 if (_call_try_exit)
1835 objc_exception_try_exit(&d);
1836
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001837 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00001838 ... dispatch to finally destination ...
1839
1840finally_rethrow:
1841 objc_exception_throw(_rethrow);
1842
1843finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001844}
1845
1846This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00001847uses _rethrow to determine if objc_exception_try_exit should be called
1848and if the object should be rethrown. This breaks in the face of
1849throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001850
1851We specialize this framework for a few particular circumstances:
1852
1853 - If there are no catch blocks, then we avoid emitting the second
1854 exception handling context.
1855
1856 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
1857 e)) we avoid emitting the code to rethrow an uncaught exception.
1858
1859 - FIXME: If there is no @finally block we can do a few more
1860 simplifications.
1861
1862Rethrows and Jumps-Through-Finally
1863--
1864
1865Support for implicit rethrows and jumping through the finally block is
1866handled by storing the current exception-handling context in
1867ObjCEHStack.
1868
Daniel Dunbar898d5082008-09-30 01:06:03 +00001869In order to implement proper @finally semantics, we support one basic
1870mechanism for jumping through the finally block to an arbitrary
1871destination. Constructs which generate exits from a @try or @catch
1872block use this mechanism to implement the proper semantics by chaining
1873jumps, as necessary.
1874
1875This mechanism works like the one used for indirect goto: we
1876arbitrarily assign an ID to each destination and store the ID for the
1877destination in a variable prior to entering the finally block. At the
1878end of the finally block we simply create a switch to the proper
1879destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001880
1881Code gen for @synchronized(expr) stmt;
1882Effectively generating code for:
1883objc_sync_enter(expr);
1884@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001885*/
1886
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001887void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1888 const Stmt &S) {
1889 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001890 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001891 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Anders Carlsson190d00e2009-02-07 21:26:04 +00001892 llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit");
Daniel Dunbar55e87422008-11-11 02:29:29 +00001893 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
1894 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1895 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001896
1897 // Push an EH context entry, used for handling rethrows and jumps
1898 // through finally.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00001899 CGF.PushCleanupBlock(FinallyBlock);
1900
Anders Carlsson273558f2009-02-07 21:37:21 +00001901 CGF.ObjCEHValueStack.push_back(0);
1902
Daniel Dunbar898d5082008-09-30 01:06:03 +00001903 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00001904 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
1905 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001906 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
1907 "_rethrow");
Anders Carlsson190d00e2009-02-07 21:26:04 +00001908 llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty,
1909 "_call_try_exit");
1910 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr);
1911
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001912 if (!isTry) {
1913 // For @synchronized, call objc_sync_enter(sync.expr)
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001914 llvm::Value *Arg = CGF.EmitScalarExpr(
1915 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1916 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1917 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, Arg);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001918 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001919
1920 // Enter a new try block and call setjmp.
1921 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
1922 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
1923 "jmpbufarray");
1924 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
1925 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1926 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001927
Daniel Dunbar55e87422008-11-11 02:29:29 +00001928 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1929 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001930 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001931 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001932
1933 // Emit the @try block.
1934 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001935 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
1936 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00001937 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001938
1939 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001940 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001941
1942 // Retrieve the exception object. We may emit multiple blocks but
1943 // nothing can cross this so the value is already in SSA form.
1944 llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1945 ExceptionData,
1946 "caught");
Anders Carlsson273558f2009-02-07 21:37:21 +00001947 CGF.ObjCEHValueStack.back() = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001948 if (!isTry)
1949 {
1950 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00001951 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00001952 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001953 }
1954 else if (const ObjCAtCatchStmt* CatchStmt =
1955 cast<ObjCAtTryStmt>(S).getCatchStmts())
1956 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00001957 // Enter a new exception try block (in case a @catch block throws
1958 // an exception).
Anders Carlsson80f25672008-09-09 17:59:25 +00001959 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001960
Anders Carlsson80f25672008-09-09 17:59:25 +00001961 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1962 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001963 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00001964
Daniel Dunbar55e87422008-11-11 02:29:29 +00001965 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
1966 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001967 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001968
1969 CGF.EmitBlock(CatchBlock);
1970
Daniel Dunbar55e40722008-09-27 07:03:52 +00001971 // Handle catch list. As a special case we check if everything is
1972 // matched and avoid generating code for falling off the end if
1973 // so.
1974 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00001975 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00001976 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00001977
Anders Carlssondde0a942008-09-11 09:15:33 +00001978 const DeclStmt *CatchParam =
1979 cast_or_null<DeclStmt>(CatchStmt->getCatchParamStmt());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001980 const VarDecl *VD = 0;
1981 const PointerType *PT = 0;
1982
Anders Carlsson80f25672008-09-09 17:59:25 +00001983 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00001984 if (!CatchParam) {
1985 AllMatched = true;
1986 } else {
Ted Kremenekde3b8fb2008-10-06 20:58:56 +00001987 VD = cast<VarDecl>(CatchParam->getSolitaryDecl());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001988 PT = VD->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001989
Daniel Dunbar97f61d12008-09-27 22:21:14 +00001990 // catch(id e) always matches.
1991 // FIXME: For the time being we also match id<X>; this should
1992 // be rejected by Sema instead.
Steve Naroff389bf462009-02-12 17:52:19 +00001993 if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) ||
Daniel Dunbar97f61d12008-09-27 22:21:14 +00001994 VD->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00001995 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00001996 }
1997
Daniel Dunbar55e40722008-09-27 07:03:52 +00001998 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00001999 if (CatchParam) {
2000 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002001 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar129271a2008-09-27 07:36:24 +00002002 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00002003 }
Anders Carlsson1452f552008-09-11 08:21:54 +00002004
Anders Carlssondde0a942008-09-11 09:15:33 +00002005 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002006 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002007 break;
2008 }
2009
Daniel Dunbar129271a2008-09-27 07:36:24 +00002010 assert(PT && "Unexpected non-pointer type in @catch");
2011 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00002012 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002013 assert(ObjCType && "Catch parameter must have Objective-C type!");
2014
2015 // Check if the @catch block matches the exception object.
2016 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
2017
Anders Carlsson80f25672008-09-09 17:59:25 +00002018 llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn,
2019 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00002020
Daniel Dunbar55e87422008-11-11 02:29:29 +00002021 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00002022
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002023 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002024 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002025
2026 // Emit the @catch block.
2027 CGF.EmitBlock(MatchedBlock);
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002028 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002029 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002030
2031 llvm::Value *Tmp =
2032 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(VD->getType()),
2033 "tmp");
2034 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00002035
2036 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002037 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002038
2039 CGF.EmitBlock(NextCatchBlock);
2040 }
2041
Daniel Dunbar55e40722008-09-27 07:03:52 +00002042 if (!AllMatched) {
2043 // None of the handlers caught the exception, so store it to be
2044 // rethrown at the end of the @finally block.
2045 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002046 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002047 }
2048
2049 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002050 CGF.EmitBlock(CatchHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002051 CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
2052 ExceptionData),
2053 RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002054 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002055 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002056 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00002057 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002058 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002059 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlsson80f25672008-09-09 17:59:25 +00002060 }
2061
Daniel Dunbar898d5082008-09-30 01:06:03 +00002062 // Pop the exception-handling stack entry. It is important to do
2063 // this now, because the code in the @finally block is not in this
2064 // context.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002065 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
2066
Anders Carlsson273558f2009-02-07 21:37:21 +00002067 CGF.ObjCEHValueStack.pop_back();
2068
Anders Carlsson80f25672008-09-09 17:59:25 +00002069 // Emit the @finally block.
2070 CGF.EmitBlock(FinallyBlock);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002071 llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp");
2072
2073 CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit);
2074
2075 CGF.EmitBlock(FinallyExit);
Anders Carlsson80f25672008-09-09 17:59:25 +00002076 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00002077
2078 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002079 if (isTry) {
2080 if (const ObjCAtFinallyStmt* FinallyStmt =
2081 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2082 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2083 }
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002084 else {
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002085 // For @synchronized objc_sync_exit(expr); As finally's sole statement.
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002086 // For @synchronized, call objc_sync_enter(sync.expr)
2087 llvm::Value *Arg = CGF.EmitScalarExpr(
2088 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2089 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
2090 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, Arg);
2091 }
Anders Carlsson80f25672008-09-09 17:59:25 +00002092
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002093 // Emit the switch block
2094 if (Info.SwitchBlock)
2095 CGF.EmitBlock(Info.SwitchBlock);
2096 if (Info.EndBlock)
2097 CGF.EmitBlock(Info.EndBlock);
2098
Daniel Dunbar898d5082008-09-30 01:06:03 +00002099 CGF.EmitBlock(FinallyRethrow);
2100 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn,
2101 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002102 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00002103
2104 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002105}
2106
2107void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002108 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002109 llvm::Value *ExceptionAsObject;
2110
2111 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2112 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2113 ExceptionAsObject =
2114 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2115 } else {
Anders Carlsson273558f2009-02-07 21:37:21 +00002116 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002117 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00002118 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002119 }
2120
2121 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002122 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002123
2124 // Clear the insertion point to indicate we are in unreachable code.
2125 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002126}
2127
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002128/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002129/// object: objc_read_weak (id *src)
2130///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002131llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002132 llvm::Value *AddrWeakObj)
2133{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002134 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002135 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002136 AddrWeakObj, "weakread");
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002137 return read_weak;
2138}
2139
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002140/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2141/// objc_assign_weak (id src, id *dst)
2142///
2143void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2144 llvm::Value *src, llvm::Value *dst)
2145{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002146 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2147 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002148 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
2149 src, dst, "weakassign");
2150 return;
2151}
2152
Fariborz Jahanian58626502008-11-19 00:59:10 +00002153/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2154/// objc_assign_global (id src, id *dst)
2155///
2156void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2157 llvm::Value *src, llvm::Value *dst)
2158{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002159 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2160 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002161 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
2162 src, dst, "globalassign");
2163 return;
2164}
2165
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002166/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2167/// objc_assign_ivar (id src, id *dst)
2168///
2169void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2170 llvm::Value *src, llvm::Value *dst)
2171{
2172 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2173 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
2174 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
2175 src, dst, "assignivar");
2176 return;
2177}
2178
Fariborz Jahanian58626502008-11-19 00:59:10 +00002179/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2180/// objc_assign_strongCast (id src, id *dst)
2181///
2182void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2183 llvm::Value *src, llvm::Value *dst)
2184{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002185 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2186 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002187 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
2188 src, dst, "weakassign");
2189 return;
2190}
2191
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002192/// EmitObjCValueForIvar - Code Gen for ivar reference.
2193///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002194LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2195 QualType ObjectTy,
2196 llvm::Value *BaseValue,
2197 const ObjCIvarDecl *Ivar,
2198 const FieldDecl *Field,
2199 unsigned CVRQualifiers) {
2200 if (Ivar->isBitField())
2201 return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field),
2202 CVRQualifiers);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002203 // TODO: Add a special case for isa (index 0)
2204 unsigned Index = CGM.getTypes().getLLVMFieldNo(Field);
2205 llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp");
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002206 LValue LV = LValue::MakeAddr(V,
2207 Ivar->getType().getCVRQualifiers()|CVRQualifiers);
2208 LValue::SetObjCIvar(LV, true);
2209 return LV;
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002210}
2211
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002212llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
2213 ObjCInterfaceDecl *Interface,
2214 const ObjCIvarDecl *Ivar) {
2215 const llvm::Type *InterfaceLTy =
2216 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
2217 const llvm::StructLayout *Layout =
2218 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceLTy));
2219 FieldDecl *Field = Interface->lookupFieldDeclForIvar(CGM.getContext(), Ivar);
2220 uint64_t Offset =
2221 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
2222
2223 return llvm::ConstantInt::get(
2224 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
2225 Offset);
2226}
2227
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002228/* *** Private Interface *** */
2229
2230/// EmitImageInfo - Emit the image info marker used to encode some module
2231/// level information.
2232///
2233/// See: <rdr://4810609&4810587&4810587>
2234/// struct IMAGE_INFO {
2235/// unsigned version;
2236/// unsigned flags;
2237/// };
2238enum ImageInfoFlags {
2239 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
2240 eImageInfo_GarbageCollected = (1 << 1),
2241 eImageInfo_GCOnly = (1 << 2)
2242};
2243
2244void CGObjCMac::EmitImageInfo() {
2245 unsigned version = 0; // Version is unused?
2246 unsigned flags = 0;
2247
2248 // FIXME: Fix and continue?
2249 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2250 flags |= eImageInfo_GarbageCollected;
2251 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2252 flags |= eImageInfo_GCOnly;
2253
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002254 // Emitted as int[2];
2255 llvm::Constant *values[2] = {
2256 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2257 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2258 };
2259 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002260 llvm::GlobalVariable *GV =
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002261 new llvm::GlobalVariable(AT, true,
2262 llvm::GlobalValue::InternalLinkage,
2263 llvm::ConstantArray::get(AT, values, 2),
2264 "\01L_OBJC_IMAGE_INFO",
2265 &CGM.getModule());
2266
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002267 if (ObjCABI == 1) {
2268 GV->setSection("__OBJC, __image_info,regular");
2269 } else {
2270 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
2271 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002272
2273 UsedGlobals.push_back(GV);
2274}
2275
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002276
2277// struct objc_module {
2278// unsigned long version;
2279// unsigned long size;
2280// const char *name;
2281// Symtab symtab;
2282// };
2283
2284// FIXME: Get from somewhere
2285static const int ModuleVersion = 7;
2286
2287void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002288 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002289
2290 std::vector<llvm::Constant*> Values(4);
2291 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2292 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002293 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002294 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002295 Values[3] = EmitModuleSymbols();
2296
2297 llvm::GlobalVariable *GV =
2298 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
2299 llvm::GlobalValue::InternalLinkage,
2300 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
2301 Values),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002302 "\01L_OBJC_MODULES",
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002303 &CGM.getModule());
2304 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
2305 UsedGlobals.push_back(GV);
2306}
2307
2308llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002309 unsigned NumClasses = DefinedClasses.size();
2310 unsigned NumCategories = DefinedCategories.size();
2311
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002312 // Return null if no symbols were defined.
2313 if (!NumClasses && !NumCategories)
2314 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2315
2316 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002317 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2318 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2319 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2320 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2321
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002322 // The runtime expects exactly the list of defined classes followed
2323 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002324 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002325 for (unsigned i=0; i<NumClasses; i++)
2326 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2327 ObjCTypes.Int8PtrTy);
2328 for (unsigned i=0; i<NumCategories; i++)
2329 Symbols[NumClasses + i] =
2330 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2331 ObjCTypes.Int8PtrTy);
2332
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002333 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002334 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002335 NumClasses + NumCategories),
2336 Symbols);
2337
2338 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2339
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002340 llvm::GlobalVariable *GV =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002341 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002342 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002343 Init,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002344 "\01L_OBJC_SYMBOLS",
2345 &CGM.getModule());
2346 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
2347 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002348 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2349}
2350
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002351llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002352 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002353 LazySymbols.insert(ID->getIdentifier());
2354
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002355 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2356
2357 if (!Entry) {
2358 llvm::Constant *Casted =
2359 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2360 ObjCTypes.ClassPtrTy);
2361 Entry =
2362 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
2363 llvm::GlobalValue::InternalLinkage,
2364 Casted, "\01L_OBJC_CLASS_REFERENCES_",
2365 &CGM.getModule());
2366 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
2367 UsedGlobals.push_back(Entry);
2368 }
2369
2370 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002371}
2372
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002373llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002374 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2375
2376 if (!Entry) {
2377 llvm::Constant *Casted =
2378 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2379 ObjCTypes.SelectorPtrTy);
2380 Entry =
2381 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
2382 llvm::GlobalValue::InternalLinkage,
2383 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
2384 &CGM.getModule());
2385 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
2386 UsedGlobals.push_back(Entry);
2387 }
2388
2389 return Builder.CreateLoad(Entry, false, "tmp");
2390}
2391
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002392llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002393 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002394
2395 if (!Entry) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002396 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002397 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002398 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002399 llvm::GlobalValue::InternalLinkage,
2400 C, "\01L_OBJC_CLASS_NAME_",
2401 &CGM.getModule());
2402 Entry->setSection("__TEXT,__cstring,cstring_literals");
2403 UsedGlobals.push_back(Entry);
2404 }
2405
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002406 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002407}
2408
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002409llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002410 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
2411
2412 if (!Entry) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00002413 // FIXME: Avoid std::string copying.
2414 llvm::Constant *C = llvm::ConstantArray::get(Sel.getAsString());
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002415 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002416 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002417 llvm::GlobalValue::InternalLinkage,
2418 C, "\01L_OBJC_METH_VAR_NAME_",
2419 &CGM.getModule());
2420 Entry->setSection("__TEXT,__cstring,cstring_literals");
2421 UsedGlobals.push_back(Entry);
2422 }
2423
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002424 return getConstantGEP(Entry, 0, 0);
2425}
2426
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002427// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002428llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002429 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
2430}
2431
2432// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002433llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002434 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
2435}
2436
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002437llvm::Constant *CGObjCCommonMac::GetMethodVarType(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002438 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002439
2440 if (!Entry) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002441 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002442 Entry =
2443 new llvm::GlobalVariable(C->getType(), false,
2444 llvm::GlobalValue::InternalLinkage,
2445 C, "\01L_OBJC_METH_VAR_TYPE_",
2446 &CGM.getModule());
2447 Entry->setSection("__TEXT,__cstring,cstring_literals");
2448 UsedGlobals.push_back(Entry);
2449 }
2450
2451 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002452}
2453
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002454// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002455llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002456 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002457 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
2458 TypeStr);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002459 return GetMethodVarType(TypeStr);
2460}
2461
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002462// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002463llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002464 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
2465
2466 if (!Entry) {
2467 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
2468 Entry =
2469 new llvm::GlobalVariable(C->getType(), false,
2470 llvm::GlobalValue::InternalLinkage,
2471 C, "\01L_OBJC_PROP_NAME_ATTR_",
2472 &CGM.getModule());
2473 Entry->setSection("__TEXT,__cstring,cstring_literals");
2474 UsedGlobals.push_back(Entry);
2475 }
2476
2477 return getConstantGEP(Entry, 0, 0);
2478}
2479
2480// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002481// FIXME: This Decl should be more precise.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002482llvm::Constant *CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002483 const Decl *Container) {
2484 std::string TypeStr;
2485 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002486 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
2487}
2488
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002489void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
2490 const ObjCContainerDecl *CD,
2491 std::string &NameOut) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002492 // FIXME: Find the mangling GCC uses.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002493 NameOut = (D->isInstanceMethod() ? "-" : "+");
Chris Lattner077bf5e2008-11-24 03:33:13 +00002494 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002495 assert (CD && "Missing container decl in GetNameForMethod");
2496 NameOut += CD->getNameAsString();
Fariborz Jahanian52847332009-01-26 23:49:05 +00002497 // FIXME. For a method in a category, (CAT_NAME) is inserted here.
2498 // Right now! there is not enough info. to do this.
Chris Lattner077bf5e2008-11-24 03:33:13 +00002499 NameOut += ' ';
2500 NameOut += D->getSelector().getAsString();
2501 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002502}
2503
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002504/// GetFirstIvarInRecord - This routine returns the record for the
2505/// implementation of the fiven class OID. It also returns field
2506/// corresponding to the first ivar in the class in FIV. It also
2507/// returns the one before the first ivar.
2508///
2509const RecordDecl *CGObjCCommonMac::GetFirstIvarInRecord(
2510 const ObjCInterfaceDecl *OID,
2511 RecordDecl::field_iterator &FIV,
2512 RecordDecl::field_iterator &PIV) {
2513 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass());
2514 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
2515 RecordDecl::field_iterator ifield = RD->field_begin();
2516 RecordDecl::field_iterator pfield = RD->field_end();
2517 while (countSuperClassIvars-- > 0) {
2518 pfield = ifield;
2519 ++ifield;
2520 }
2521 FIV = ifield;
2522 PIV = pfield;
2523 return RD;
2524}
2525
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002526void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002527 EmitModuleInfo();
2528
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002529 // Emit the dummy bodies for any protocols which were referenced but
2530 // never defined.
2531 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
2532 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
2533 if (i->second->hasInitializer())
2534 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002535
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002536 std::vector<llvm::Constant*> Values(5);
2537 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
2538 Values[1] = GetClassName(i->first);
2539 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
2540 Values[3] = Values[4] =
2541 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
2542 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
2543 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
2544 Values));
2545 }
2546
2547 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002548 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002549 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002550 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002551 }
2552
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002553 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002554 llvm::GlobalValue *GV =
2555 new llvm::GlobalVariable(AT, false,
2556 llvm::GlobalValue::AppendingLinkage,
2557 llvm::ConstantArray::get(AT, Used),
2558 "llvm.used",
2559 &CGM.getModule());
2560
2561 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002562
2563 // Add assembler directives to add lazy undefined symbol references
2564 // for classes which are referenced but not defined. This is
2565 // important for correct linker interaction.
2566
2567 // FIXME: Uh, this isn't particularly portable.
2568 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00002569
2570 if (!CGM.getModule().getModuleInlineAsm().empty())
2571 s << "\n";
2572
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002573 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
2574 e = LazySymbols.end(); i != e; ++i) {
2575 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
2576 }
2577 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
2578 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002579 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002580 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
2581 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00002582
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002583 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002584}
2585
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002586CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002587 : CGObjCCommonMac(cgm),
2588 ObjCTypes(cgm)
2589{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00002590 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002591 ObjCABI = 2;
2592}
2593
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002594/* *** */
2595
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002596ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
2597: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002598{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002599 CodeGen::CodeGenTypes &Types = CGM.getTypes();
2600 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002601
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002602 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002603 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002604 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002605 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
2606
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002607 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002608 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002609 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002610
2611 // FIXME: It would be nice to unify this with the opaque type, so
2612 // that the IR comes out a bit cleaner.
2613 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
2614 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002615
2616 // I'm not sure I like this. The implicit coordination is a bit
2617 // gross. We should solve this in a reasonable fashion because this
2618 // is a pretty common task (match some runtime data structure with
2619 // an LLVM data structure).
2620
2621 // FIXME: This is leaked.
2622 // FIXME: Merge with rewriter code?
2623
2624 // struct _objc_super {
2625 // id self;
2626 // Class cls;
2627 // }
2628 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
2629 SourceLocation(),
2630 &Ctx.Idents.get("_objc_super"));
2631 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2632 Ctx.getObjCIdType(), 0, false));
2633 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2634 Ctx.getObjCClassType(), 0, false));
2635 RD->completeDefinition(Ctx);
2636
2637 SuperCTy = Ctx.getTagDeclType(RD);
2638 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
2639
2640 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002641 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
2642
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002643 // struct _prop_t {
2644 // char *name;
2645 // char *attributes;
2646 // }
2647 PropertyTy = llvm::StructType::get(Int8PtrTy,
2648 Int8PtrTy,
2649 NULL);
2650 CGM.getModule().addTypeName("struct._prop_t",
2651 PropertyTy);
2652
2653 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002654 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002655 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002656 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002657 // }
2658 PropertyListTy = llvm::StructType::get(IntTy,
2659 IntTy,
2660 llvm::ArrayType::get(PropertyTy, 0),
2661 NULL);
2662 CGM.getModule().addTypeName("struct._prop_list_t",
2663 PropertyListTy);
2664 // struct _prop_list_t *
2665 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
2666
2667 // struct _objc_method {
2668 // SEL _cmd;
2669 // char *method_type;
2670 // char *_imp;
2671 // }
2672 MethodTy = llvm::StructType::get(SelectorPtrTy,
2673 Int8PtrTy,
2674 Int8PtrTy,
2675 NULL);
2676 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002677
2678 // struct _objc_cache *
2679 CacheTy = llvm::OpaqueType::get();
2680 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
2681 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002682
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002683 // Property manipulation functions.
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002684
2685 QualType IdType = Ctx.getObjCIdType();
2686 QualType SelType = Ctx.getObjCSelType();
2687 llvm::SmallVector<QualType,16> Params;
2688 const llvm::FunctionType *FTy;
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002689
2690 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002691 Params.push_back(IdType);
2692 Params.push_back(SelType);
2693 Params.push_back(Ctx.LongTy);
2694 Params.push_back(Ctx.BoolTy);
2695 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params),
2696 false);
2697 GetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002698
2699 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
2700 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002701 Params.push_back(IdType);
2702 Params.push_back(SelType);
2703 Params.push_back(Ctx.LongTy);
2704 Params.push_back(IdType);
2705 Params.push_back(Ctx.BoolTy);
2706 Params.push_back(Ctx.BoolTy);
2707 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
2708 SetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
2709
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002710 // Enumeration mutation.
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002711
2712 // void objc_enumerationMutation (id)
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002713 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002714 Params.push_back(IdType);
2715 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
2716 EnumerationMutationFn = CGM.CreateRuntimeFunction(FTy,
2717 "objc_enumerationMutation");
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002718
2719 // gc's API
2720 // id objc_read_weak (id *)
2721 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002722 Params.push_back(Ctx.getPointerType(IdType));
2723 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
2724 GcReadWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
2725
2726 // id objc_assign_weak (id, id *)
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002727 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002728 Params.push_back(IdType);
2729 Params.push_back(Ctx.getPointerType(IdType));
2730
2731 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
2732 GcAssignWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
2733 GcAssignGlobalFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
2734 GcAssignIvarFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
2735 GcAssignStrongCastFn =
2736 CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002737}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002738
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002739ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
2740 : ObjCCommonTypesHelper(cgm)
2741{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002742 // struct _objc_method_description {
2743 // SEL name;
2744 // char *types;
2745 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002746 MethodDescriptionTy =
2747 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002748 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002749 NULL);
2750 CGM.getModule().addTypeName("struct._objc_method_description",
2751 MethodDescriptionTy);
2752
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002753 // struct _objc_method_description_list {
2754 // int count;
2755 // struct _objc_method_description[1];
2756 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002757 MethodDescriptionListTy =
2758 llvm::StructType::get(IntTy,
2759 llvm::ArrayType::get(MethodDescriptionTy, 0),
2760 NULL);
2761 CGM.getModule().addTypeName("struct._objc_method_description_list",
2762 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002763
2764 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002765 MethodDescriptionListPtrTy =
2766 llvm::PointerType::getUnqual(MethodDescriptionListTy);
2767
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002768 // Protocol description structures
2769
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002770 // struct _objc_protocol_extension {
2771 // uint32_t size; // sizeof(struct _objc_protocol_extension)
2772 // struct _objc_method_description_list *optional_instance_methods;
2773 // struct _objc_method_description_list *optional_class_methods;
2774 // struct _objc_property_list *instance_properties;
2775 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002776 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002777 llvm::StructType::get(IntTy,
2778 MethodDescriptionListPtrTy,
2779 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002780 PropertyListPtrTy,
2781 NULL);
2782 CGM.getModule().addTypeName("struct._objc_protocol_extension",
2783 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002784
2785 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002786 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
2787
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002788 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002789
2790 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
2791 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2792
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002793 const llvm::Type *T =
2794 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
2795 LongTy,
2796 llvm::ArrayType::get(ProtocolTyHolder, 0),
2797 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002798 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
2799
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002800 // struct _objc_protocol {
2801 // struct _objc_protocol_extension *isa;
2802 // char *protocol_name;
2803 // struct _objc_protocol **_objc_protocol_list;
2804 // struct _objc_method_description_list *instance_methods;
2805 // struct _objc_method_description_list *class_methods;
2806 // }
2807 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002808 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002809 llvm::PointerType::getUnqual(ProtocolListTyHolder),
2810 MethodDescriptionListPtrTy,
2811 MethodDescriptionListPtrTy,
2812 NULL);
2813 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
2814
2815 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
2816 CGM.getModule().addTypeName("struct._objc_protocol_list",
2817 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002818 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002819 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
2820
2821 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002822 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002823 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002824
2825 // Class description structures
2826
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002827 // struct _objc_ivar {
2828 // char *ivar_name;
2829 // char *ivar_type;
2830 // int ivar_offset;
2831 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002832 IvarTy = llvm::StructType::get(Int8PtrTy,
2833 Int8PtrTy,
2834 IntTy,
2835 NULL);
2836 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
2837
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002838 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002839 IvarListTy = llvm::OpaqueType::get();
2840 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
2841 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
2842
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002843 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002844 MethodListTy = llvm::OpaqueType::get();
2845 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
2846 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
2847
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002848 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002849 ClassExtensionTy =
2850 llvm::StructType::get(IntTy,
2851 Int8PtrTy,
2852 PropertyListPtrTy,
2853 NULL);
2854 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
2855 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
2856
2857 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
2858
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002859 // struct _objc_class {
2860 // Class isa;
2861 // Class super_class;
2862 // char *name;
2863 // long version;
2864 // long info;
2865 // long instance_size;
2866 // struct _objc_ivar_list *ivars;
2867 // struct _objc_method_list *methods;
2868 // struct _objc_cache *cache;
2869 // struct _objc_protocol_list *protocols;
2870 // char *ivar_layout;
2871 // struct _objc_class_ext *ext;
2872 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002873 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
2874 llvm::PointerType::getUnqual(ClassTyHolder),
2875 Int8PtrTy,
2876 LongTy,
2877 LongTy,
2878 LongTy,
2879 IvarListPtrTy,
2880 MethodListPtrTy,
2881 CachePtrTy,
2882 ProtocolListPtrTy,
2883 Int8PtrTy,
2884 ClassExtensionPtrTy,
2885 NULL);
2886 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
2887
2888 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
2889 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
2890 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
2891
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002892 // struct _objc_category {
2893 // char *category_name;
2894 // char *class_name;
2895 // struct _objc_method_list *instance_method;
2896 // struct _objc_method_list *class_method;
2897 // uint32_t size; // sizeof(struct _objc_category)
2898 // struct _objc_property_list *instance_properties;// category's @property
2899 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002900 CategoryTy = llvm::StructType::get(Int8PtrTy,
2901 Int8PtrTy,
2902 MethodListPtrTy,
2903 MethodListPtrTy,
2904 ProtocolListPtrTy,
2905 IntTy,
2906 PropertyListPtrTy,
2907 NULL);
2908 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
2909
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002910 // Global metadata structures
2911
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002912 // struct _objc_symtab {
2913 // long sel_ref_cnt;
2914 // SEL *refs;
2915 // short cls_def_cnt;
2916 // short cat_def_cnt;
2917 // char *defs[cls_def_cnt + cat_def_cnt];
2918 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002919 SymtabTy = llvm::StructType::get(LongTy,
2920 SelectorPtrTy,
2921 ShortTy,
2922 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002923 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002924 NULL);
2925 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
2926 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
2927
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002928 // struct _objc_module {
2929 // long version;
2930 // long size; // sizeof(struct _objc_module)
2931 // char *name;
2932 // struct _objc_symtab* symtab;
2933 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002934 ModuleTy =
2935 llvm::StructType::get(LongTy,
2936 LongTy,
2937 Int8PtrTy,
2938 SymtabPtrTy,
2939 NULL);
2940 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002941
Daniel Dunbar49f66022008-09-24 03:38:44 +00002942 // Message send functions.
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002943
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002944 // id objc_msgSend (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002945 std::vector<const llvm::Type*> Params;
2946 Params.push_back(ObjectPtrTy);
2947 Params.push_back(SelectorPtrTy);
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002948 MessageSendFn =
2949 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2950 Params,
2951 true),
2952 "objc_msgSend");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002953
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002954 // id objc_msgSend_stret (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002955 Params.clear();
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002956 Params.push_back(ObjectPtrTy);
2957 Params.push_back(SelectorPtrTy);
2958 MessageSendStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002959 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2960 Params,
2961 true),
2962 "objc_msgSend_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002963
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002964 //
Daniel Dunbar5669e572008-10-17 03:24:53 +00002965 Params.clear();
2966 Params.push_back(ObjectPtrTy);
2967 Params.push_back(SelectorPtrTy);
2968 // FIXME: This should be long double on x86_64?
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002969 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
Daniel Dunbar5669e572008-10-17 03:24:53 +00002970 MessageSendFpretFn =
2971 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
2972 Params,
2973 true),
2974 "objc_msgSend_fpret");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002975
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002976 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002977 Params.clear();
2978 Params.push_back(SuperPtrTy);
2979 Params.push_back(SelectorPtrTy);
2980 MessageSendSuperFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002981 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2982 Params,
2983 true),
2984 "objc_msgSendSuper");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002985
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002986 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
2987 // SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002988 Params.clear();
2989 Params.push_back(Int8PtrTy);
2990 Params.push_back(SuperPtrTy);
2991 Params.push_back(SelectorPtrTy);
2992 MessageSendSuperStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002993 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2994 Params,
2995 true),
2996 "objc_msgSendSuper_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002997
2998 // There is no objc_msgSendSuper_fpret? How can that work?
2999 MessageSendSuperFpretFn = MessageSendSuperFn;
Anders Carlsson2abd89c2008-08-31 04:05:03 +00003000
Anders Carlsson124526b2008-09-09 10:10:21 +00003001 // FIXME: This is the size of the setjmp buffer and should be
3002 // target specific. 18 is what's used on 32-bit X86.
3003 uint64_t SetJmpBufferSize = 18;
3004
3005 // Exceptions
3006 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00003007 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00003008
3009 ExceptionDataTy =
3010 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
3011 SetJmpBufferSize),
3012 StackPtrTy, NULL);
3013 CGM.getModule().addTypeName("struct._objc_exception_data",
3014 ExceptionDataTy);
3015
3016 Params.clear();
3017 Params.push_back(ObjectPtrTy);
3018 ExceptionThrowFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003019 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3020 Params,
3021 false),
3022 "objc_exception_throw");
Anders Carlsson124526b2008-09-09 10:10:21 +00003023
3024 Params.clear();
3025 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
3026 ExceptionTryEnterFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003027 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3028 Params,
3029 false),
3030 "objc_exception_try_enter");
Anders Carlsson124526b2008-09-09 10:10:21 +00003031 ExceptionTryExitFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003032 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3033 Params,
3034 false),
3035 "objc_exception_try_exit");
Anders Carlsson124526b2008-09-09 10:10:21 +00003036 ExceptionExtractFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003037 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3038 Params,
3039 false),
3040 "objc_exception_extract");
Anders Carlsson124526b2008-09-09 10:10:21 +00003041
3042 Params.clear();
3043 Params.push_back(ClassPtrTy);
3044 Params.push_back(ObjectPtrTy);
3045 ExceptionMatchFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003046 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3047 Params,
3048 false),
3049 "objc_exception_match");
Chris Lattner10cac6f2008-11-15 21:26:17 +00003050
3051 // synchronized APIs
3052 // void objc_sync_enter (id)
3053 Params.clear();
3054 Params.push_back(ObjectPtrTy);
3055 SyncEnterFn =
3056 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3057 Params,
3058 false),
3059 "objc_sync_enter");
3060 // void objc_sync_exit (id)
3061 SyncExitFn =
3062 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3063 Params,
3064 false),
3065 "objc_sync_exit");
3066
Anders Carlsson124526b2008-09-09 10:10:21 +00003067
3068 Params.clear();
3069 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
3070 SetJmpFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003071 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3072 Params,
3073 false),
3074 "_setjmp");
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00003075
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003076}
3077
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003078ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003079: ObjCCommonTypesHelper(cgm)
3080{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003081 // struct _method_list_t {
3082 // uint32_t entsize; // sizeof(struct _objc_method)
3083 // uint32_t method_count;
3084 // struct _objc_method method_list[method_count];
3085 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003086 MethodListnfABITy = llvm::StructType::get(IntTy,
3087 IntTy,
3088 llvm::ArrayType::get(MethodTy, 0),
3089 NULL);
3090 CGM.getModule().addTypeName("struct.__method_list_t",
3091 MethodListnfABITy);
3092 // struct method_list_t *
3093 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003094
3095 // struct _protocol_t {
3096 // id isa; // NULL
3097 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003098 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003099 // const struct method_list_t * const instance_methods;
3100 // const struct method_list_t * const class_methods;
3101 // const struct method_list_t *optionalInstanceMethods;
3102 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003103 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003104 // const uint32_t size; // sizeof(struct _protocol_t)
3105 // const uint32_t flags; // = 0
3106 // }
3107
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003108 // Holder for struct _protocol_list_t *
3109 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3110
3111 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
3112 Int8PtrTy,
3113 llvm::PointerType::getUnqual(
3114 ProtocolListTyHolder),
3115 MethodListnfABIPtrTy,
3116 MethodListnfABIPtrTy,
3117 MethodListnfABIPtrTy,
3118 MethodListnfABIPtrTy,
3119 PropertyListPtrTy,
3120 IntTy,
3121 IntTy,
3122 NULL);
3123 CGM.getModule().addTypeName("struct._protocol_t",
3124 ProtocolnfABITy);
3125
Fariborz Jahanianda320092009-01-29 19:24:30 +00003126 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003127 // long protocol_count; // Note, this is 32/64 bit
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003128 // struct _protocol_t[protocol_count];
3129 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003130 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3131 llvm::ArrayType::get(
3132 ProtocolnfABITy, 0),
3133 NULL);
3134 CGM.getModule().addTypeName("struct._objc_protocol_list",
3135 ProtocolListnfABITy);
3136
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003137 // struct _objc_protocol_list*
3138 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003139
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003140 // FIXME! Is this doing the right thing?
3141 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3142 ProtocolListnfABIPtrTy);
3143
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003144 // struct _ivar_t {
3145 // unsigned long int *offset; // pointer to ivar offset location
3146 // char *name;
3147 // char *type;
3148 // uint32_t alignment;
3149 // uint32_t size;
3150 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003151 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3152 Int8PtrTy,
3153 Int8PtrTy,
3154 IntTy,
3155 IntTy,
3156 NULL);
3157 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3158
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003159 // struct _ivar_list_t {
3160 // uint32 entsize; // sizeof(struct _ivar_t)
3161 // uint32 count;
3162 // struct _iver_t list[count];
3163 // }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003164 IvarListnfABITy = llvm::StructType::get(IntTy,
3165 IntTy,
3166 llvm::ArrayType::get(
3167 IvarnfABITy, 0),
3168 NULL);
3169 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3170
3171 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003172
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003173 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003174 // uint32_t const flags;
3175 // uint32_t const instanceStart;
3176 // uint32_t const instanceSize;
3177 // uint32_t const reserved; // only when building for 64bit targets
3178 // const uint8_t * const ivarLayout;
3179 // const char *const name;
3180 // const struct _method_list_t * const baseMethods;
3181 // const struct _objc_protocol_list *const baseProtocols;
3182 // const struct _ivar_list_t *const ivars;
3183 // const uint8_t * const weakIvarLayout;
3184 // const struct _prop_list_t * const properties;
3185 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003186
3187 // FIXME. Add 'reserved' field in 64bit abi mode!
3188 ClassRonfABITy = llvm::StructType::get(IntTy,
3189 IntTy,
3190 IntTy,
3191 Int8PtrTy,
3192 Int8PtrTy,
3193 MethodListnfABIPtrTy,
3194 ProtocolListnfABIPtrTy,
3195 IvarListnfABIPtrTy,
3196 Int8PtrTy,
3197 PropertyListPtrTy,
3198 NULL);
3199 CGM.getModule().addTypeName("struct._class_ro_t",
3200 ClassRonfABITy);
3201
3202 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3203 std::vector<const llvm::Type*> Params;
3204 Params.push_back(ObjectPtrTy);
3205 Params.push_back(SelectorPtrTy);
3206 ImpnfABITy = llvm::PointerType::getUnqual(
3207 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3208
3209 // struct _class_t {
3210 // struct _class_t *isa;
3211 // struct _class_t * const superclass;
3212 // void *cache;
3213 // IMP *vtable;
3214 // struct class_ro_t *ro;
3215 // }
3216
3217 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3218 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3219 llvm::PointerType::getUnqual(ClassTyHolder),
3220 CachePtrTy,
3221 llvm::PointerType::getUnqual(ImpnfABITy),
3222 llvm::PointerType::getUnqual(
3223 ClassRonfABITy),
3224 NULL);
3225 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3226
3227 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3228 ClassnfABITy);
3229
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003230 // LLVM for struct _class_t *
3231 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3232
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003233 // struct _category_t {
3234 // const char * const name;
3235 // struct _class_t *const cls;
3236 // const struct _method_list_t * const instance_methods;
3237 // const struct _method_list_t * const class_methods;
3238 // const struct _protocol_list_t * const protocols;
3239 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003240 // }
3241 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003242 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003243 MethodListnfABIPtrTy,
3244 MethodListnfABIPtrTy,
3245 ProtocolListnfABIPtrTy,
3246 PropertyListPtrTy,
3247 NULL);
3248 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003249
3250 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003251 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3252 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003253
3254 // MessageRefTy - LLVM for:
3255 // struct _message_ref_t {
3256 // IMP messenger;
3257 // SEL name;
3258 // };
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003259
3260 // First the clang type for struct _message_ref_t
3261 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3262 SourceLocation(),
3263 &Ctx.Idents.get("_message_ref_t"));
3264 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3265 Ctx.VoidPtrTy, 0, false));
3266 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3267 Ctx.getObjCSelType(), 0, false));
3268 RD->completeDefinition(Ctx);
3269
3270 MessageRefCTy = Ctx.getTagDeclType(RD);
3271 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
3272 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003273
3274 // MessageRefPtrTy - LLVM for struct _message_ref_t*
3275 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
3276
3277 // SuperMessageRefTy - LLVM for:
3278 // struct _super_message_ref_t {
3279 // SUPER_IMP messenger;
3280 // SEL name;
3281 // };
3282 SuperMessageRefTy = llvm::StructType::get(ImpnfABITy,
3283 SelectorPtrTy,
3284 NULL);
3285 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
3286
3287 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
3288 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
3289
3290 // id objc_msgSend_fixup (id, struct message_ref_t*, ...)
3291 Params.clear();
3292 Params.push_back(ObjectPtrTy);
3293 Params.push_back(MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00003294 MessengerTy = llvm::FunctionType::get(ObjectPtrTy,
3295 Params,
3296 true);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003297 MessageSendFixupFn =
Fariborz Jahanianef163782009-02-05 01:13:09 +00003298 CGM.CreateRuntimeFunction(MessengerTy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003299 "objc_msgSend_fixup");
3300
3301 // id objc_msgSend_fpret_fixup (id, struct message_ref_t*, ...)
3302 MessageSendFpretFixupFn =
3303 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3304 Params,
3305 true),
3306 "objc_msgSend_fpret_fixup");
3307
3308 // id objc_msgSend_stret_fixup (id, struct message_ref_t*, ...)
3309 MessageSendStretFixupFn =
3310 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3311 Params,
3312 true),
3313 "objc_msgSend_stret_fixup");
3314
3315 // id objc_msgSendId_fixup (id, struct message_ref_t*, ...)
3316 MessageSendIdFixupFn =
3317 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3318 Params,
3319 true),
3320 "objc_msgSendId_fixup");
3321
3322
3323 // id objc_msgSendId_stret_fixup (id, struct message_ref_t*, ...)
3324 MessageSendIdStretFixupFn =
3325 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3326 Params,
3327 true),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003328 "objc_msgSendId_stret_fixup");
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003329
3330 // id objc_msgSendSuper2_fixup (struct objc_super *,
3331 // struct _super_message_ref_t*, ...)
3332 Params.clear();
3333 Params.push_back(SuperPtrTy);
3334 Params.push_back(SuperMessageRefPtrTy);
3335 MessageSendSuper2FixupFn =
3336 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3337 Params,
3338 true),
3339 "objc_msgSendSuper2_fixup");
3340
3341
3342 // id objc_msgSendSuper2_stret_fixup (struct objc_super *,
3343 // struct _super_message_ref_t*, ...)
3344 MessageSendSuper2StretFixupFn =
3345 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3346 Params,
3347 true),
3348 "objc_msgSendSuper2_stret_fixup");
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003349
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003350}
3351
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003352llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
3353 FinishNonFragileABIModule();
3354
3355 return NULL;
3356}
3357
3358void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
3359 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003360
3361 // Build list of all implemented classe addresses in array
3362 // L_OBJC_LABEL_CLASS_$.
3363 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$
3364 // list of 'nonlazy' implementations (defined as those with a +load{}
3365 // method!!).
3366 unsigned NumClasses = DefinedClasses.size();
3367 if (NumClasses) {
3368 std::vector<llvm::Constant*> Symbols(NumClasses);
3369 for (unsigned i=0; i<NumClasses; i++)
3370 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
3371 ObjCTypes.Int8PtrTy);
3372 llvm::Constant* Init =
3373 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3374 NumClasses),
3375 Symbols);
3376
3377 llvm::GlobalVariable *GV =
3378 new llvm::GlobalVariable(Init->getType(), false,
3379 llvm::GlobalValue::InternalLinkage,
3380 Init,
3381 "\01L_OBJC_LABEL_CLASS_$",
3382 &CGM.getModule());
3383 GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip");
3384 UsedGlobals.push_back(GV);
3385 }
3386
3387 // Build list of all implemented category addresses in array
3388 // L_OBJC_LABEL_CATEGORY_$.
3389 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$
3390 // list of 'nonlazy' category implementations (defined as those with a +load{}
3391 // method!!).
3392 unsigned NumCategory = DefinedCategories.size();
3393 if (NumCategory) {
3394 std::vector<llvm::Constant*> Symbols(NumCategory);
3395 for (unsigned i=0; i<NumCategory; i++)
3396 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i],
3397 ObjCTypes.Int8PtrTy);
3398 llvm::Constant* Init =
3399 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3400 NumCategory),
3401 Symbols);
3402
3403 llvm::GlobalVariable *GV =
3404 new llvm::GlobalVariable(Init->getType(), false,
3405 llvm::GlobalValue::InternalLinkage,
3406 Init,
3407 "\01L_OBJC_LABEL_CATEGORY_$",
3408 &CGM.getModule());
3409 GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip");
3410 UsedGlobals.push_back(GV);
3411 }
3412
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00003413 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
3414 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
3415 std::vector<llvm::Constant*> Values(2);
3416 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3417 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3418 llvm::Constant* Init = llvm::ConstantArray::get(
3419 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
3420 Values);
3421 llvm::GlobalVariable *IMGV =
3422 new llvm::GlobalVariable(Init->getType(), false,
3423 llvm::GlobalValue::InternalLinkage,
3424 Init,
3425 "\01L_OBJC_IMAGE_INFO",
3426 &CGM.getModule());
3427 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
3428 UsedGlobals.push_back(IMGV);
3429
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003430 std::vector<llvm::Constant*> Used;
3431 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
3432 e = UsedGlobals.end(); i != e; ++i) {
3433 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
3434 }
3435
3436 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
3437 llvm::GlobalValue *GV =
3438 new llvm::GlobalVariable(AT, false,
3439 llvm::GlobalValue::AppendingLinkage,
3440 llvm::ConstantArray::get(AT, Used),
3441 "llvm.used",
3442 &CGM.getModule());
3443
3444 GV->setSection("llvm.metadata");
3445
3446}
3447
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003448// Metadata flags
3449enum MetaDataDlags {
3450 CLS = 0x0,
3451 CLS_META = 0x1,
3452 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003453 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003454 CLS_EXCEPTION = 0x20
3455};
3456/// BuildClassRoTInitializer - generate meta-data for:
3457/// struct _class_ro_t {
3458/// uint32_t const flags;
3459/// uint32_t const instanceStart;
3460/// uint32_t const instanceSize;
3461/// uint32_t const reserved; // only when building for 64bit targets
3462/// const uint8_t * const ivarLayout;
3463/// const char *const name;
3464/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00003465/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003466/// const struct _ivar_list_t *const ivars;
3467/// const uint8_t * const weakIvarLayout;
3468/// const struct _prop_list_t * const properties;
3469/// }
3470///
3471llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
3472 unsigned flags,
3473 unsigned InstanceStart,
3474 unsigned InstanceSize,
3475 const ObjCImplementationDecl *ID) {
3476 std::string ClassName = ID->getNameAsString();
3477 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
3478 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
3479 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
3480 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
3481 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianda320092009-01-29 19:24:30 +00003482 // FIXME. ivarLayout is currently null!
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003483 Values[ 3] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3484 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003485 // const struct _method_list_t * const baseMethods;
3486 std::vector<llvm::Constant*> Methods;
3487 std::string MethodListName("\01l_OBJC_$_");
3488 if (flags & CLS_META) {
3489 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
3490 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
3491 e = ID->classmeth_end(); i != e; ++i) {
3492 // Class methods should always be defined.
3493 Methods.push_back(GetMethodConstant(*i));
3494 }
3495 } else {
3496 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
3497 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
3498 e = ID->instmeth_end(); i != e; ++i) {
3499 // Instance methods should always be defined.
3500 Methods.push_back(GetMethodConstant(*i));
3501 }
Fariborz Jahanian939abce2009-01-28 22:46:49 +00003502 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
3503 e = ID->propimpl_end(); i != e; ++i) {
3504 ObjCPropertyImplDecl *PID = *i;
3505
3506 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
3507 ObjCPropertyDecl *PD = PID->getPropertyDecl();
3508
3509 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
3510 if (llvm::Constant *C = GetMethodConstant(MD))
3511 Methods.push_back(C);
3512 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
3513 if (llvm::Constant *C = GetMethodConstant(MD))
3514 Methods.push_back(C);
3515 }
3516 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003517 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003518 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003519 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003520
3521 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3522 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
3523 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
3524 + OID->getNameAsString(),
3525 OID->protocol_begin(),
3526 OID->protocol_end());
3527
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003528 if (flags & CLS_META)
3529 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3530 else
3531 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003532 // FIXME. weakIvarLayout is currently null.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003533 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003534 if (flags & CLS_META)
3535 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3536 else
3537 Values[ 9] =
3538 EmitPropertyList(
3539 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
3540 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003541 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
3542 Values);
3543 llvm::GlobalVariable *CLASS_RO_GV =
3544 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
3545 llvm::GlobalValue::InternalLinkage,
3546 Init,
3547 (flags & CLS_META) ?
3548 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
3549 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
3550 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003551 CLASS_RO_GV->setAlignment(
3552 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003553 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003554 UsedGlobals.push_back(CLASS_RO_GV);
3555 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003556
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003557}
3558
3559/// BuildClassMetaData - This routine defines that to-level meta-data
3560/// for the given ClassName for:
3561/// struct _class_t {
3562/// struct _class_t *isa;
3563/// struct _class_t * const superclass;
3564/// void *cache;
3565/// IMP *vtable;
3566/// struct class_ro_t *ro;
3567/// }
3568///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003569llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
3570 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003571 llvm::Constant *IsAGV,
3572 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003573 llvm::Constant *ClassRoGV,
3574 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003575 std::vector<llvm::Constant*> Values(5);
3576 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003577 Values[1] = SuperClassGV
3578 ? SuperClassGV
3579 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003580 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
3581 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
3582 Values[4] = ClassRoGV; // &CLASS_RO_GV
3583 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
3584 Values);
3585 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(ClassName);
3586 if (GV)
3587 GV->setInitializer(Init);
3588 else
3589 GV =
3590 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3591 llvm::GlobalValue::ExternalLinkage,
3592 Init,
3593 ClassName,
3594 &CGM.getModule());
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00003595 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003596 GV->setAlignment(
3597 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003598 if (HiddenVisibility)
3599 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003600 UsedGlobals.push_back(GV);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003601 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003602}
3603
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003604void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
3605 std::string ClassName = ID->getNameAsString();
3606 if (!ObjCEmptyCacheVar) {
3607 ObjCEmptyCacheVar = new llvm::GlobalVariable(
3608 ObjCTypes.CachePtrTy,
3609 false,
3610 llvm::GlobalValue::ExternalLinkage,
3611 0,
Fariborz Jahanian07da3672009-02-03 17:34:34 +00003612 "\01__objc_empty_cache",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003613 &CGM.getModule());
3614 UsedGlobals.push_back(ObjCEmptyCacheVar);
3615
3616 ObjCEmptyVtableVar = new llvm::GlobalVariable(
3617 llvm::PointerType::getUnqual(
3618 ObjCTypes.ImpnfABITy),
3619 false,
3620 llvm::GlobalValue::ExternalLinkage,
3621 0,
Fariborz Jahanian07da3672009-02-03 17:34:34 +00003622 "\01__objc_empty_vtable",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003623 &CGM.getModule());
3624 UsedGlobals.push_back(ObjCEmptyVtableVar);
3625 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003626 assert(ID->getClassInterface() &&
3627 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003628 uint32_t InstanceStart =
3629 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
3630 uint32_t InstanceSize = InstanceStart;
3631 uint32_t flags = CLS_META;
3632 std::string ObjCMetaClassName("\01_OBJC_METACLASS_$_");
3633 std::string ObjCClassName("\01_OBJC_CLASS_$_");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003634
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003635 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003636
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003637 bool classIsHidden = IsClassHidden(ID->getClassInterface());
3638 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003639 flags |= OBJC2_CLS_HIDDEN;
3640 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003641 // class is root
3642 flags |= CLS_ROOT;
3643 std::string SuperClassName = ObjCClassName + ClassName;
3644 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003645 if (!SuperClassGV) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003646 SuperClassGV =
3647 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3648 llvm::GlobalValue::ExternalLinkage,
3649 0,
3650 SuperClassName,
3651 &CGM.getModule());
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003652 UsedGlobals.push_back(SuperClassGV);
3653 }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003654 std::string IsAClassName = ObjCMetaClassName + ClassName;
3655 IsAGV = CGM.getModule().getGlobalVariable(IsAClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003656 if (!IsAGV) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003657 IsAGV =
3658 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3659 llvm::GlobalValue::ExternalLinkage,
3660 0,
3661 IsAClassName,
3662 &CGM.getModule());
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003663 UsedGlobals.push_back(IsAGV);
3664 }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003665 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003666 // Has a root. Current class is not a root.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003667 std::string RootClassName =
3668 ID->getClassInterface()->getSuperClass()->getNameAsString();
3669 std::string SuperClassName = ObjCMetaClassName + RootClassName;
3670 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003671 if (!SuperClassGV) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003672 SuperClassGV =
3673 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3674 llvm::GlobalValue::ExternalLinkage,
3675 0,
3676 SuperClassName,
3677 &CGM.getModule());
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003678 UsedGlobals.push_back(SuperClassGV);
3679 }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003680 IsAGV = SuperClassGV;
3681 }
3682 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
3683 InstanceStart,
3684 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003685 std::string TClassName = ObjCMetaClassName + ClassName;
3686 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003687 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
3688 classIsHidden);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003689
3690 // Metadata for the class
3691 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003692 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003693 flags |= OBJC2_CLS_HIDDEN;
3694 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003695 flags |= CLS_ROOT;
3696 SuperClassGV = 0;
3697 }
3698 else {
3699 // Has a root. Current class is not a root.
3700 std::string RootClassName =
3701 ID->getClassInterface()->getSuperClass()->getNameAsString();
3702 std::string SuperClassName = ObjCClassName + RootClassName;
3703 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003704 if (!SuperClassGV) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003705 SuperClassGV =
3706 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3707 llvm::GlobalValue::ExternalLinkage,
3708 0,
3709 SuperClassName,
3710 &CGM.getModule());
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003711 UsedGlobals.push_back(SuperClassGV);
3712 }
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003713 }
3714
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003715 InstanceStart = InstanceSize = 0;
3716 if (ObjCInterfaceDecl *OID =
3717 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface())) {
3718 // FIXME. Share this with the one in EmitIvarList.
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003719 const llvm::Type *InterfaceTy =
3720 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
3721 const llvm::StructLayout *Layout =
3722 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003723
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003724 RecordDecl::field_iterator firstField, lastField;
3725 const RecordDecl *RD = GetFirstIvarInRecord(OID, firstField, lastField);
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003726
3727 for (RecordDecl::field_iterator e = RD->field_end(),
3728 ifield = firstField; ifield != e; ++ifield)
3729 lastField = ifield;
3730
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003731 if (lastField != RD->field_end()) {
3732 FieldDecl *Field = *lastField;
3733 const llvm::Type *FieldTy =
3734 CGM.getTypes().ConvertTypeForMem(Field->getType());
3735 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3736 InstanceSize = Layout->getElementOffset(
3737 CGM.getTypes().getLLVMFieldNo(Field)) +
3738 Size;
3739 if (firstField == RD->field_end())
3740 InstanceStart = InstanceSize;
3741 else
3742 InstanceStart = Layout->getElementOffset(CGM.getTypes().
3743 getLLVMFieldNo(*firstField));
3744 }
3745 }
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003746 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003747 InstanceStart,
3748 InstanceSize,
3749 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003750
3751 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003752 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003753 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
3754 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003755 DefinedClasses.push_back(ClassMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003756}
3757
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00003758/// GenerateProtocolRef - This routine is called to generate code for
3759/// a protocol reference expression; as in:
3760/// @code
3761/// @protocol(Proto1);
3762/// @endcode
3763/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
3764/// which will hold address of the protocol meta-data.
3765///
3766llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
3767 const ObjCProtocolDecl *PD) {
3768
3769 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
3770 ObjCTypes.ExternalProtocolPtrTy);
3771
3772 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
3773 ProtocolName += PD->getNameAsCString();
3774
3775 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
3776 if (PTGV)
3777 return Builder.CreateLoad(PTGV, false, "tmp");
3778 PTGV = new llvm::GlobalVariable(
3779 Init->getType(), false,
3780 llvm::GlobalValue::WeakLinkage,
3781 Init,
3782 ProtocolName,
3783 &CGM.getModule());
3784 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
3785 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
3786 UsedGlobals.push_back(PTGV);
3787 return Builder.CreateLoad(PTGV, false, "tmp");
3788}
3789
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003790/// GenerateCategory - Build metadata for a category implementation.
3791/// struct _category_t {
3792/// const char * const name;
3793/// struct _class_t *const cls;
3794/// const struct _method_list_t * const instance_methods;
3795/// const struct _method_list_t * const class_methods;
3796/// const struct _protocol_list_t * const protocols;
3797/// const struct _prop_list_t * const properties;
3798/// }
3799///
3800void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD)
3801{
3802 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003803 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
3804 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003805 "_$_" + OCD->getNameAsString());
3806 std::string ExtClassName("\01_OBJC_CLASS_$_" + Interface->getNameAsString());
3807
3808 std::vector<llvm::Constant*> Values(6);
3809 Values[0] = GetClassName(OCD->getIdentifier());
3810 // meta-class entry symbol
3811 llvm::GlobalVariable *ClassGV =
3812 CGM.getModule().getGlobalVariable(ExtClassName);
3813 if (!ClassGV)
3814 ClassGV =
3815 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3816 llvm::GlobalValue::ExternalLinkage,
3817 0,
3818 ExtClassName,
3819 &CGM.getModule());
3820 UsedGlobals.push_back(ClassGV);
3821 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003822 std::vector<llvm::Constant*> Methods;
3823 std::string MethodListName(Prefix);
3824 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
3825 "_$_" + OCD->getNameAsString();
3826
3827 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
3828 e = OCD->instmeth_end(); i != e; ++i) {
3829 // Instance methods should always be defined.
3830 Methods.push_back(GetMethodConstant(*i));
3831 }
3832
3833 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003834 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003835 Methods);
3836
3837 MethodListName = Prefix;
3838 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
3839 OCD->getNameAsString();
3840 Methods.clear();
3841 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
3842 e = OCD->classmeth_end(); i != e; ++i) {
3843 // Class methods should always be defined.
3844 Methods.push_back(GetMethodConstant(*i));
3845 }
3846
3847 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003848 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003849 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003850 const ObjCCategoryDecl *Category =
3851 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00003852 if (Category) {
3853 std::string ExtName(Interface->getNameAsString() + "_$_" +
3854 OCD->getNameAsString());
3855 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
3856 + Interface->getNameAsString() + "_$_"
3857 + Category->getNameAsString(),
3858 Category->protocol_begin(),
3859 Category->protocol_end());
3860 Values[5] =
3861 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
3862 OCD, Category, ObjCTypes);
3863 }
3864 else {
3865 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
3866 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3867 }
3868
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003869 llvm::Constant *Init =
3870 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
3871 Values);
3872 llvm::GlobalVariable *GCATV
3873 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
3874 false,
3875 llvm::GlobalValue::InternalLinkage,
3876 Init,
3877 ExtCatName,
3878 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003879 GCATV->setAlignment(
3880 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003881 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003882 UsedGlobals.push_back(GCATV);
3883 DefinedCategories.push_back(GCATV);
3884}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003885
3886/// GetMethodConstant - Return a struct objc_method constant for the
3887/// given method if it has been defined. The result is null if the
3888/// method has not been defined. The return value has type MethodPtrTy.
3889llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
3890 const ObjCMethodDecl *MD) {
3891 // FIXME: Use DenseMap::lookup
3892 llvm::Function *Fn = MethodDefinitions[MD];
3893 if (!Fn)
3894 return 0;
3895
3896 std::vector<llvm::Constant*> Method(3);
3897 Method[0] =
3898 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
3899 ObjCTypes.SelectorPtrTy);
3900 Method[1] = GetMethodVarType(MD);
3901 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
3902 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
3903}
3904
3905/// EmitMethodList - Build meta-data for method declarations
3906/// struct _method_list_t {
3907/// uint32_t entsize; // sizeof(struct _objc_method)
3908/// uint32_t method_count;
3909/// struct _objc_method method_list[method_count];
3910/// }
3911///
3912llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
3913 const std::string &Name,
3914 const char *Section,
3915 const ConstantVector &Methods) {
3916 // Return null for empty list.
3917 if (Methods.empty())
3918 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
3919
3920 std::vector<llvm::Constant*> Values(3);
3921 // sizeof(struct _objc_method)
3922 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy);
3923 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3924 // method_count
3925 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
3926 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
3927 Methods.size());
3928 Values[2] = llvm::ConstantArray::get(AT, Methods);
3929 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
3930
3931 llvm::GlobalVariable *GV =
3932 new llvm::GlobalVariable(Init->getType(), false,
3933 llvm::GlobalValue::InternalLinkage,
3934 Init,
3935 Name,
3936 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003937 GV->setAlignment(
3938 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003939 GV->setSection(Section);
3940 UsedGlobals.push_back(GV);
3941 return llvm::ConstantExpr::getBitCast(GV,
3942 ObjCTypes.MethodListnfABIPtrTy);
3943}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003944
Fariborz Jahanianed157d32009-02-10 20:21:06 +00003945/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
3946/// the given ivar.
3947///
3948llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(
3949 std::string &Name,
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00003950 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00003951 const ObjCIvarDecl *Ivar) {
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00003952 Name += "\01_OBJC_IVAR_$_" +
3953 getInterfaceDeclForIvar(ID, Ivar)->getNameAsString() + '.'
Fariborz Jahanianed157d32009-02-10 20:21:06 +00003954 + Ivar->getNameAsString();
3955 llvm::GlobalVariable *IvarOffsetGV =
3956 CGM.getModule().getGlobalVariable(Name);
3957 if (!IvarOffsetGV)
3958 IvarOffsetGV =
3959 new llvm::GlobalVariable(ObjCTypes.LongTy,
3960 false,
3961 llvm::GlobalValue::ExternalLinkage,
3962 0,
3963 Name,
3964 &CGM.getModule());
3965 return IvarOffsetGV;
3966}
3967
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003968llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
Fariborz Jahanianed157d32009-02-10 20:21:06 +00003969 const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003970 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003971 unsigned long int Offset) {
3972
Fariborz Jahanianed157d32009-02-10 20:21:06 +00003973 assert(ID && "EmitIvarOffsetVar - null interface decl.");
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003974 std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.'
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003975 + Ivar->getNameAsString());
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003976 llvm::Constant *Init = llvm::ConstantInt::get(ObjCTypes.LongTy, Offset);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003977
3978 llvm::GlobalVariable *IvarOffsetGV =
3979 CGM.getModule().getGlobalVariable(ExternalName);
3980 if (IvarOffsetGV) {
3981 // ivar offset symbol already built due to user code referencing it.
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003982 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003983 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003984 IvarOffsetGV->setInitializer(Init);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003985 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003986 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003987 return IvarOffsetGV;
3988 }
3989
3990 IvarOffsetGV =
3991 new llvm::GlobalVariable(Init->getType(),
3992 false,
3993 llvm::GlobalValue::ExternalLinkage,
3994 Init,
3995 ExternalName,
3996 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003997 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003998 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003999 // @private and @package have hidden visibility.
4000 bool globalVisibility = (Ivar->getAccessControl() == ObjCIvarDecl::Public ||
4001 Ivar->getAccessControl() == ObjCIvarDecl::Protected);
4002 if (!globalVisibility)
4003 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004004 else
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004005 if (IsClassHidden(ID))
4006 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004007
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004008 IvarOffsetGV->setSection("__DATA, __objc_const");
4009 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004010 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004011}
4012
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004013/// EmitIvarList - Emit the ivar list for the given
4014/// implementation. If ForClass is true the list of class ivars
4015/// (i.e. metaclass ivars) is emitted, otherwise the list of
4016/// interface ivars will be emitted. The return value has type
4017/// IvarListnfABIPtrTy.
4018/// struct _ivar_t {
4019/// unsigned long int *offset; // pointer to ivar offset location
4020/// char *name;
4021/// char *type;
4022/// uint32_t alignment;
4023/// uint32_t size;
4024/// }
4025/// struct _ivar_list_t {
4026/// uint32 entsize; // sizeof(struct _ivar_t)
4027/// uint32 count;
4028/// struct _iver_t list[count];
4029/// }
4030///
4031llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
4032 const ObjCImplementationDecl *ID) {
4033
4034 std::vector<llvm::Constant*> Ivars, Ivar(5);
4035
4036 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4037 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
4038
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004039 // FIXME. Consolidate this with similar code in GenerateClass.
4040 const llvm::Type *InterfaceTy =
4041 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(
4042 const_cast<ObjCInterfaceDecl*>(OID)));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004043 const llvm::StructLayout *Layout =
4044 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00004045
4046 RecordDecl::field_iterator i,p;
4047 const RecordDecl *RD = GetFirstIvarInRecord(OID, i,p);
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004048 ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin();
4049
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004050 for (RecordDecl::field_iterator e = RD->field_end(); i != e; ++i) {
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004051 FieldDecl *Field = *i;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004052 unsigned long offset = Layout->getElementOffset(CGM.getTypes().
4053 getLLVMFieldNo(Field));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004054 const ObjCIvarDecl *ivarDecl = *I++;
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004055 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), ivarDecl, offset);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004056 if (Field->getIdentifier())
4057 Ivar[1] = GetMethodVarName(Field->getIdentifier());
4058 else
4059 Ivar[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4060 std::string TypeStr;
4061 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4062 Ivar[2] = GetMethodVarType(TypeStr);
4063 const llvm::Type *FieldTy =
4064 CGM.getTypes().ConvertTypeForMem(Field->getType());
4065 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
4066 unsigned Align = CGM.getContext().getPreferredTypeAlign(
4067 Field->getType().getTypePtr()) >> 3;
4068 Align = llvm::Log2_32(Align);
4069 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Fariborz Jahanian07236ba2009-01-27 22:27:56 +00004070 // NOTE. Size of a bitfield does not match gcc's, because of the way
4071 // bitfields are treated special in each. But I am told that 'size'
4072 // for bitfield ivars is ignored by the runtime so it does not matter.
4073 // (even if it matters, some day, there is enough info. to get the bitfield
4074 // right!
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004075 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4076 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
4077 }
4078 // Return null for empty list.
4079 if (Ivars.empty())
4080 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4081 std::vector<llvm::Constant*> Values(3);
4082 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy);
4083 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4084 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
4085 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
4086 Ivars.size());
4087 Values[2] = llvm::ConstantArray::get(AT, Ivars);
4088 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4089 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
4090 llvm::GlobalVariable *GV =
4091 new llvm::GlobalVariable(Init->getType(), false,
4092 llvm::GlobalValue::InternalLinkage,
4093 Init,
4094 Prefix + OID->getNameAsString(),
4095 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004096 GV->setAlignment(
4097 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004098 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004099
4100 UsedGlobals.push_back(GV);
4101 return llvm::ConstantExpr::getBitCast(GV,
4102 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004103}
4104
4105llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
4106 const ObjCProtocolDecl *PD) {
4107 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4108
4109 if (!Entry) {
4110 // We use the initializer as a marker of whether this is a forward
4111 // reference or not. At module finalization we add the empty
4112 // contents for protocols which were referenced but never defined.
4113 Entry =
4114 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
4115 llvm::GlobalValue::ExternalLinkage,
4116 0,
4117 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(),
4118 &CGM.getModule());
4119 Entry->setSection("__DATA,__datacoal_nt,coalesced");
4120 UsedGlobals.push_back(Entry);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004121 }
4122
4123 return Entry;
4124}
4125
4126/// GetOrEmitProtocol - Generate the protocol meta-data:
4127/// @code
4128/// struct _protocol_t {
4129/// id isa; // NULL
4130/// const char * const protocol_name;
4131/// const struct _protocol_list_t * protocol_list; // super protocols
4132/// const struct method_list_t * const instance_methods;
4133/// const struct method_list_t * const class_methods;
4134/// const struct method_list_t *optionalInstanceMethods;
4135/// const struct method_list_t *optionalClassMethods;
4136/// const struct _prop_list_t * properties;
4137/// const uint32_t size; // sizeof(struct _protocol_t)
4138/// const uint32_t flags; // = 0
4139/// }
4140/// @endcode
4141///
4142
4143llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
4144 const ObjCProtocolDecl *PD) {
4145 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4146
4147 // Early exit if a defining object has already been generated.
4148 if (Entry && Entry->hasInitializer())
4149 return Entry;
4150
4151 const char *ProtocolName = PD->getNameAsCString();
4152
4153 // Construct method lists.
4154 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
4155 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
4156 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
4157 e = PD->instmeth_end(); i != e; ++i) {
4158 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004159 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004160 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4161 OptInstanceMethods.push_back(C);
4162 } else {
4163 InstanceMethods.push_back(C);
4164 }
4165 }
4166
4167 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
4168 e = PD->classmeth_end(); i != e; ++i) {
4169 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004170 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004171 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4172 OptClassMethods.push_back(C);
4173 } else {
4174 ClassMethods.push_back(C);
4175 }
4176 }
4177
4178 std::vector<llvm::Constant*> Values(10);
4179 // isa is NULL
4180 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
4181 Values[1] = GetClassName(PD->getIdentifier());
4182 Values[2] = EmitProtocolList(
4183 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
4184 PD->protocol_begin(),
4185 PD->protocol_end());
4186
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004187 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004188 + PD->getNameAsString(),
4189 "__DATA, __objc_const",
4190 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004191 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004192 + PD->getNameAsString(),
4193 "__DATA, __objc_const",
4194 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004195 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004196 + PD->getNameAsString(),
4197 "__DATA, __objc_const",
4198 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004199 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004200 + PD->getNameAsString(),
4201 "__DATA, __objc_const",
4202 OptClassMethods);
4203 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
4204 0, PD, ObjCTypes);
4205 uint32_t Size =
4206 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy);
4207 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4208 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
4209 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
4210 Values);
4211
4212 if (Entry) {
4213 // Already created, fix the linkage and update the initializer.
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004214 Entry->setLinkage(llvm::GlobalValue::WeakLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004215 Entry->setInitializer(Init);
4216 } else {
4217 Entry =
4218 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004219 llvm::GlobalValue::WeakLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004220 Init,
4221 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName,
4222 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004223 Entry->setAlignment(
4224 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004225 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004226 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004227 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
4228
4229 // Use this protocol meta-data to build protocol list table in section
4230 // __DATA, __objc_protolist
4231 llvm::Type *ptype = llvm::PointerType::getUnqual(ObjCTypes.ProtocolnfABITy);
4232 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
4233 ptype, false,
4234 llvm::GlobalValue::WeakLinkage,
4235 Entry,
4236 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
4237 +ProtocolName,
4238 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004239 PTGV->setAlignment(
4240 CGM.getTargetData().getPrefTypeAlignment(ptype));
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004241 PTGV->setSection("__DATA, __objc_protolist");
4242 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4243 UsedGlobals.push_back(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004244 return Entry;
4245}
4246
4247/// EmitProtocolList - Generate protocol list meta-data:
4248/// @code
4249/// struct _protocol_list_t {
4250/// long protocol_count; // Note, this is 32/64 bit
4251/// struct _protocol_t[protocol_count];
4252/// }
4253/// @endcode
4254///
4255llvm::Constant *
4256CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4257 ObjCProtocolDecl::protocol_iterator begin,
4258 ObjCProtocolDecl::protocol_iterator end) {
4259 std::vector<llvm::Constant*> ProtocolRefs;
4260
4261 for (; begin != end; ++begin)
4262 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4263
4264 // Just return null for empty protocol lists
4265 if (ProtocolRefs.empty())
4266 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4267
4268 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4269 if (GV)
4270 return GV;
4271 // This list is null terminated.
4272 ProtocolRefs.push_back(llvm::Constant::getNullValue(
4273 ObjCTypes.ProtocolListnfABIPtrTy));
4274
4275 std::vector<llvm::Constant*> Values(2);
4276 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
4277 Values[1] =
4278 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolListnfABIPtrTy,
4279 ProtocolRefs.size()),
4280 ProtocolRefs);
4281
4282 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4283 GV = new llvm::GlobalVariable(Init->getType(), false,
4284 llvm::GlobalValue::InternalLinkage,
4285 Init,
4286 Name,
4287 &CGM.getModule());
4288 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004289 GV->setAlignment(
4290 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004291 UsedGlobals.push_back(GV);
4292 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
4293}
4294
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004295/// GetMethodDescriptionConstant - This routine build following meta-data:
4296/// struct _objc_method {
4297/// SEL _cmd;
4298/// char *method_type;
4299/// char *_imp;
4300/// }
4301
4302llvm::Constant *
4303CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4304 std::vector<llvm::Constant*> Desc(3);
4305 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4306 ObjCTypes.SelectorPtrTy);
4307 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004308 // Protocol methods have no implementation. So, this entry is always NULL.
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004309 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4310 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
4311}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004312
4313/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
4314/// This code gen. amounts to generating code for:
4315/// @code
4316/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
4317/// @encode
4318///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004319LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004320 CodeGen::CodeGenFunction &CGF,
4321 QualType ObjectTy,
4322 llvm::Value *BaseValue,
4323 const ObjCIvarDecl *Ivar,
4324 const FieldDecl *Field,
4325 unsigned CVRQualifiers) {
4326 assert(ObjectTy->isObjCInterfaceType() &&
4327 "CGObjCNonFragileABIMac::EmitObjCValueForIvar");
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00004328 ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004329 std::string ExternalName;
4330 llvm::GlobalVariable *IvarOffsetGV =
4331 ObjCIvarOffsetVariable(ExternalName, ID, Ivar);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004332
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004333 // (char *) BaseValue
4334 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue,
4335 ObjCTypes.Int8PtrTy);
4336 llvm::Value *Offset = CGF.Builder.CreateLoad(IvarOffsetGV);
4337 // (char*)BaseValue + Offset_symbol
4338 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
4339 // (type *)((char*)BaseValue + Offset_symbol)
4340 const llvm::Type *IvarTy =
4341 CGM.getTypes().ConvertType(Ivar->getType());
4342 llvm::Type *ptrIvarTy = llvm::PointerType::getUnqual(IvarTy);
4343 V = CGF.Builder.CreateBitCast(V, ptrIvarTy);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004344
4345 if (Ivar->isBitField())
4346 return CGF.EmitLValueForBitfield(V, const_cast<FieldDecl *>(Field),
4347 CVRQualifiers);
4348
4349 LValue LV = LValue::MakeAddr(V,
4350 Ivar->getType().getCVRQualifiers()|CVRQualifiers);
4351 LValue::SetObjCIvar(LV, true);
4352 return LV;
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004353}
4354
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004355llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
4356 CodeGen::CodeGenFunction &CGF,
4357 ObjCInterfaceDecl *Interface,
4358 const ObjCIvarDecl *Ivar) {
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004359 std::string ExternalName;
4360 llvm::GlobalVariable *IvarOffsetGV =
4361 ObjCIvarOffsetVariable(ExternalName, Interface, Ivar);
4362
4363 return CGF.Builder.CreateLoad(IvarOffsetGV, false, "ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004364}
4365
Fariborz Jahanian46551122009-02-04 00:22:57 +00004366CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
4367 CodeGen::CodeGenFunction &CGF,
4368 QualType ResultType,
4369 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004370 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00004371 QualType Arg0Ty,
4372 bool IsSuper,
4373 const CallArgList &CallArgs) {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004374 // FIXME. Even though IsSuper is passes. This function doese not
4375 // handle calls to 'super' receivers.
4376 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004377 llvm::Value *Arg0 = Receiver;
4378 if (!IsSuper)
4379 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004380
4381 // Find the message function name.
Fariborz Jahanianef163782009-02-05 01:13:09 +00004382 // FIXME. This is too much work to get the ABI-specific result type
4383 // needed to find the message name.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004384 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType,
4385 llvm::SmallVector<QualType, 16>());
4386 llvm::Constant *Fn;
4387 std::string Name("\01l_");
4388 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004389#if 0
4390 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004391 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
4392 Fn = ObjCTypes.MessageSendIdStretFixupFn;
4393 // FIXME. Is there a better way of getting these names.
4394 // They are available in RuntimeFunctions vector pair.
4395 Name += "objc_msgSendId_stret_fixup";
4396 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004397 else
4398#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004399 if (IsSuper) {
4400 Fn = ObjCTypes.MessageSendSuper2StretFixupFn;
4401 Name += "objc_msgSendSuper2_stret_fixup";
4402 }
4403 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004404 {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004405 Fn = ObjCTypes.MessageSendStretFixupFn;
4406 Name += "objc_msgSend_stret_fixup";
4407 }
4408 }
Fariborz Jahanian1a6b3682009-02-05 19:35:43 +00004409 else if (ResultType->isFloatingType() &&
4410 // Selection of frret API only happens in 32bit nonfragile ABI.
4411 CGM.getTargetData().getTypePaddedSize(ObjCTypes.LongTy) == 4) {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004412 Fn = ObjCTypes.MessageSendFpretFixupFn;
4413 Name += "objc_msgSend_fpret_fixup";
4414 }
4415 else {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004416#if 0
4417// unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004418 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
4419 Fn = ObjCTypes.MessageSendIdFixupFn;
4420 Name += "objc_msgSendId_fixup";
4421 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004422 else
4423#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004424 if (IsSuper) {
4425 Fn = ObjCTypes.MessageSendSuper2FixupFn;
4426 Name += "objc_msgSendSuper2_fixup";
4427 }
4428 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004429 {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004430 Fn = ObjCTypes.MessageSendFixupFn;
4431 Name += "objc_msgSend_fixup";
4432 }
4433 }
4434 Name += '_';
4435 std::string SelName(Sel.getAsString());
4436 // Replace all ':' in selector name with '_' ouch!
4437 for(unsigned i = 0; i < SelName.size(); i++)
4438 if (SelName[i] == ':')
4439 SelName[i] = '_';
4440 Name += SelName;
4441 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
4442 if (!GV) {
4443 // Build messafe ref table entry.
4444 std::vector<llvm::Constant*> Values(2);
4445 Values[0] = Fn;
4446 Values[1] = GetMethodVarName(Sel);
4447 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4448 GV = new llvm::GlobalVariable(Init->getType(), false,
4449 llvm::GlobalValue::WeakLinkage,
4450 Init,
4451 Name,
4452 &CGM.getModule());
4453 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4454 GV->setAlignment(
4455 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.MessageRefTy));
4456 GV->setSection("__DATA, __objc_msgrefs, coalesced");
4457 UsedGlobals.push_back(GV);
4458 }
4459 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00004460
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004461 CallArgList ActualArgs;
4462 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
4463 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
4464 ObjCTypes.MessageRefCPtrTy));
4465 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahanianef163782009-02-05 01:13:09 +00004466 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs);
4467 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
4468 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanianef163782009-02-05 01:13:09 +00004469 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, false);
4470 Callee = CGF.Builder.CreateBitCast(Callee,
4471 llvm::PointerType::getUnqual(FTy));
4472 return CGF.EmitCall(FnInfo1, Callee, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00004473}
4474
4475/// Generate code for a message send expression in the nonfragile abi.
4476CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend(
4477 CodeGen::CodeGenFunction &CGF,
4478 QualType ResultType,
4479 Selector Sel,
4480 llvm::Value *Receiver,
4481 bool IsClassMessage,
4482 const CallArgList &CallArgs) {
Fariborz Jahanian46551122009-02-04 00:22:57 +00004483 return EmitMessageSend(CGF, ResultType, Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004484 Receiver, CGF.getContext().getObjCIdType(),
Fariborz Jahanian46551122009-02-04 00:22:57 +00004485 false, CallArgs);
4486}
4487
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004488llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004489 const ObjCInterfaceDecl *ID,
4490 bool IsSuper) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004491
4492 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
4493
4494 if (!Entry) {
4495 std::string ClassName("\01_OBJC_CLASS_$_" + ID->getNameAsString());
4496 llvm::GlobalVariable *ClassGV =
4497 CGM.getModule().getGlobalVariable(ClassName);
4498 if (!ClassGV) {
4499 ClassGV =
4500 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
4501 llvm::GlobalValue::ExternalLinkage,
4502 0,
4503 ClassName,
4504 &CGM.getModule());
4505 UsedGlobals.push_back(ClassGV);
4506 }
4507 Entry =
4508 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
4509 llvm::GlobalValue::InternalLinkage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004510 ClassGV,
4511 IsSuper ? "\01L_OBJC_CLASSLIST_SUP_REFS_$_"
4512 : "\01L_OBJC_CLASSLIST_REFERENCES_$_",
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004513 &CGM.getModule());
4514 Entry->setAlignment(
4515 CGM.getTargetData().getPrefTypeAlignment(
4516 ObjCTypes.ClassnfABIPtrTy));
4517
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004518 if (IsSuper)
4519 Entry->setSection("__OBJC,__objc_superrefs,regular,no_dead_strip");
4520 else
4521 Entry->setSection("__OBJC,__objc_classrefs,regular,no_dead_strip");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004522 UsedGlobals.push_back(Entry);
4523 }
4524
4525 return Builder.CreateLoad(Entry, false, "tmp");
4526}
4527
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004528/// EmitMetaClassRef - Return a Value * of the address of _class_t
4529/// meta-data
4530///
4531llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
4532 const ObjCInterfaceDecl *ID) {
4533 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
4534 if (Entry)
4535 return Builder.CreateLoad(Entry, false, "tmp");
4536
4537 std::string MetaClassName("\01_OBJC_METACLASS_$_" + ID->getNameAsString());
4538 llvm::GlobalVariable *MetaClassGV =
4539 CGM.getModule().getGlobalVariable(MetaClassName);
4540 if (!MetaClassGV) {
4541 MetaClassGV =
4542 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
4543 llvm::GlobalValue::ExternalLinkage,
4544 0,
4545 MetaClassName,
4546 &CGM.getModule());
4547 UsedGlobals.push_back(MetaClassGV);
4548 }
4549
4550 Entry =
4551 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
4552 llvm::GlobalValue::InternalLinkage,
4553 MetaClassGV,
4554 "\01L_OBJC_CLASSLIST_SUP_REFS_$_",
4555 &CGM.getModule());
4556 Entry->setAlignment(
4557 CGM.getTargetData().getPrefTypeAlignment(
4558 ObjCTypes.ClassnfABIPtrTy));
4559
4560 Entry->setSection("__OBJC,__objc_superrefs,regular,no_dead_strip");
4561 UsedGlobals.push_back(Entry);
4562
4563 return Builder.CreateLoad(Entry, false, "tmp");
4564}
4565
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004566/// GetClass - Return a reference to the class for the given interface
4567/// decl.
4568llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
4569 const ObjCInterfaceDecl *ID) {
4570 return EmitClassRef(Builder, ID);
4571}
4572
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004573/// Generates a message send where the super is the receiver. This is
4574/// a message send to self with special delivery semantics indicating
4575/// which class's method should be called.
4576CodeGen::RValue
4577CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
4578 QualType ResultType,
4579 Selector Sel,
4580 const ObjCInterfaceDecl *Class,
4581 llvm::Value *Receiver,
4582 bool IsClassMessage,
4583 const CodeGen::CallArgList &CallArgs) {
4584 // ...
4585 // Create and init a super structure; this is a (receiver, class)
4586 // pair we will pass to objc_msgSendSuper.
4587 llvm::Value *ObjCSuper =
4588 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
4589
4590 llvm::Value *ReceiverAsObject =
4591 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
4592 CGF.Builder.CreateStore(ReceiverAsObject,
4593 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
4594
4595 // If this is a class message the metaclass is passed as the target.
4596 llvm::Value *Target =
4597 IsClassMessage ? EmitMetaClassRef(CGF.Builder, Class)
4598 : EmitClassRef(CGF.Builder, Class, true);
4599
4600 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
4601 // and ObjCTypes types.
4602 const llvm::Type *ClassTy =
4603 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
4604 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
4605 CGF.Builder.CreateStore(Target,
4606 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
4607
4608 return EmitMessageSend(CGF, ResultType, Sel,
4609 ObjCSuper, ObjCTypes.SuperPtrCTy,
4610 true, CallArgs);
4611}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00004612
4613llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
4614 Selector Sel) {
4615 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
4616
4617 if (!Entry) {
4618 llvm::Constant *Casted =
4619 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
4620 ObjCTypes.SelectorPtrTy);
4621 Entry =
4622 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
4623 llvm::GlobalValue::InternalLinkage,
4624 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
4625 &CGM.getModule());
4626 Entry->setSection("__DATA,__objc_selrefs,literal_pointers,no_dead_strip");
4627 UsedGlobals.push_back(Entry);
4628 }
4629
4630 return Builder.CreateLoad(Entry, false, "tmp");
4631}
4632
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004633/* *** */
4634
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004635CodeGen::CGObjCRuntime *
4636CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00004637 return new CGObjCMac(CGM);
4638}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004639
4640CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004641CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004642 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004643}