blob: 6c6419e4f17eabcba20f66ab0ca7c4a32c5b2ee9 [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 Dunbardbc933702008-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);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000593};
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000594
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000595class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000596private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000597 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000598 llvm::GlobalVariable* ObjCEmptyCacheVar;
599 llvm::GlobalVariable* ObjCEmptyVtableVar;
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +0000600 /// MetaClassReferences - uniqued meta class references.
601 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000602
603 /// FinishNonFragileABIModule - Write out global data structures at the end of
604 /// processing a translation unit.
605 void FinishNonFragileABIModule();
606
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000607 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
608 unsigned InstanceStart,
609 unsigned InstanceSize,
610 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +0000611 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
612 llvm::Constant *IsAGV,
613 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +0000614 llvm::Constant *ClassRoGV,
615 bool HiddenVisibility);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000616
617 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
618
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +0000619 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
620
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000621 /// EmitMethodList - Emit the method list for the given
622 /// implementation. The return value has type MethodListnfABITy.
623 llvm::Constant *EmitMethodList(const std::string &Name,
624 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +0000625 const ConstantVector &Methods);
626 /// EmitIvarList - Emit the ivar list for the given
627 /// implementation. If ForClass is true the list of class ivars
628 /// (i.e. metaclass ivars) is emitted, otherwise the list of
629 /// interface ivars will be emitted. The return value has type
630 /// IvarListnfABIPtrTy.
631 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000632
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +0000633 llvm::Constant *EmitIvarOffsetVar(const ObjCImplementationDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +0000634 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +0000635 unsigned long int offset);
636
Fariborz Jahanianda320092009-01-29 19:24:30 +0000637 /// GetOrEmitProtocol - Get the protocol object for the given
638 /// declaration, emitting it if necessary. The return value has type
639 /// ProtocolPtrTy.
640 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
641
642 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
643 /// object for the given declaration, emitting it if needed. These
644 /// forward references will be filled in with empty bodies if no
645 /// definition is seen. The return value has type ProtocolPtrTy.
646 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
647
648 /// EmitProtocolList - Generate the list of referenced
649 /// protocols. The return value has type ProtocolListPtrTy.
650 llvm::Constant *EmitProtocolList(const std::string &Name,
651 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000652 ObjCProtocolDecl::protocol_iterator end);
653
654 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
655 QualType ResultType,
656 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000657 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000658 QualType Arg0Ty,
659 bool IsSuper,
660 const CallArgList &CallArgs);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000661
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +0000662 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
663 /// for the given class.
664 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +0000665 const ObjCInterfaceDecl *ID,
666 bool IsSuper = false);
667
668 /// EmitMetaClassRef - Return a Value * of the address of _class_t
669 /// meta-data
670 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
671 const ObjCInterfaceDecl *ID);
672
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +0000673
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000674public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000675 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000676 // FIXME. All stubs for now!
677 virtual llvm::Function *ModuleInitFunction();
678
679 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
680 QualType ResultType,
681 Selector Sel,
682 llvm::Value *Receiver,
683 bool IsClassMessage,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000684 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000685
686 virtual CodeGen::RValue
687 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
688 QualType ResultType,
689 Selector Sel,
690 const ObjCInterfaceDecl *Class,
691 llvm::Value *Receiver,
692 bool IsClassMessage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +0000693 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000694
695 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +0000696 const ObjCInterfaceDecl *ID);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000697
698 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
699 { return 0; }
700
Fariborz Jahanianeb062d92009-01-26 18:32:24 +0000701 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000702
703 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000704 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +0000705 const ObjCProtocolDecl *PD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000706
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000707 virtual llvm::Function *GetPropertyGetFunction(){ return 0; }
708 virtual llvm::Function *GetPropertySetFunction()
709 { return 0; }
710 virtual llvm::Function *EnumerationMutationFunction()
711 { return 0; }
712
713 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
714 const Stmt &S)
715 { return; }
716 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
717 const ObjCAtThrowStmt &S)
718 { return; }
719 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
720 llvm::Value *AddrWeakObj)
721 { return 0; }
722 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
723 llvm::Value *src, llvm::Value *dst)
724 { return; }
725 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
726 llvm::Value *src, llvm::Value *dest)
727 { return; }
728 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
729 llvm::Value *src, llvm::Value *dest)
730 { return; }
731 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
732 llvm::Value *src, llvm::Value *dest)
733 { return; }
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000734 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
735 QualType ObjectTy,
736 llvm::Value *BaseValue,
737 const ObjCIvarDecl *Ivar,
738 const FieldDecl *Field,
739 unsigned CVRQualifiers);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000740};
741
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000742} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000743
744/* *** Helper Functions *** */
745
746/// getConstantGEP() - Help routine to construct simple GEPs.
747static llvm::Constant *getConstantGEP(llvm::Constant *C,
748 unsigned idx0,
749 unsigned idx1) {
750 llvm::Value *Idxs[] = {
751 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
752 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
753 };
754 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
755}
756
757/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000758
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000759CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
760 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000761{
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000762 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000763 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000764}
765
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000766/// GetClass - Return a reference to the class for the given interface
767/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000768llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000769 const ObjCInterfaceDecl *ID) {
770 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000771}
772
773/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000774llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000775 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000776}
777
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000778/// Generate a constant CFString object.
779/*
780 struct __builtin_CFString {
781 const int *isa; // point to __CFConstantStringClassReference
782 int flags;
783 const char *str;
784 long length;
785 };
786*/
787
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000788llvm::Constant *CGObjCCommonMac::GenerateConstantString(
789 const std::string &String) {
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000790 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000791}
792
793/// Generates a message send where the super is the receiver. This is
794/// a message send to self with special delivery semantics indicating
795/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000796CodeGen::RValue
797CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000798 QualType ResultType,
799 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000800 const ObjCInterfaceDecl *Class,
801 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000802 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000803 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000804 // Create and init a super structure; this is a (receiver, class)
805 // pair we will pass to objc_msgSendSuper.
806 llvm::Value *ObjCSuper =
807 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
808 llvm::Value *ReceiverAsObject =
809 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
810 CGF.Builder.CreateStore(ReceiverAsObject,
811 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000812
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000813 // If this is a class message the metaclass is passed as the target.
814 llvm::Value *Target;
815 if (IsClassMessage) {
816 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
817 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
818 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
819 Target = Super;
820 } else {
821 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
822 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000823 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
824 // and ObjCTypes types.
825 const llvm::Type *ClassTy =
826 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000827 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000828 CGF.Builder.CreateStore(Target,
829 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
830
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000831 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000832 ObjCSuper, ObjCTypes.SuperPtrCTy,
833 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000834}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000835
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000836/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000837CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000838 QualType ResultType,
839 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000840 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000841 bool IsClassMessage,
842 const CallArgList &CallArgs) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000843 llvm::Value *Arg0 =
844 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000845 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000846 Arg0, CGF.getContext().getObjCIdType(),
847 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000848}
849
850CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000851 QualType ResultType,
852 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000853 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000854 QualType Arg0Ty,
855 bool IsSuper,
856 const CallArgList &CallArgs) {
857 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000858 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
859 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
860 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000861 CGF.getContext().getObjCSelType()));
862 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000863
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000864 CodeGenTypes &Types = CGM.getTypes();
865 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
866 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false);
Daniel Dunbar5669e572008-10-17 03:24:53 +0000867
868 llvm::Constant *Fn;
Daniel Dunbar88b53962009-02-02 22:03:45 +0000869 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Daniel Dunbar5669e572008-10-17 03:24:53 +0000870 Fn = ObjCTypes.getSendStretFn(IsSuper);
871 } else if (ResultType->isFloatingType()) {
872 // FIXME: Sadly, this is wrong. This actually depends on the
873 // architecture. This happens to be right for x86-32 though.
874 Fn = ObjCTypes.getSendFpretFn(IsSuper);
875 } else {
876 Fn = ObjCTypes.getSendFn(IsSuper);
877 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000878 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar88b53962009-02-02 22:03:45 +0000879 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000880}
881
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000882llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000883 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +0000884 // FIXME: I don't understand why gcc generates this, or where it is
885 // resolved. Investigate. Its also wasteful to look this up over and
886 // over.
887 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
888
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000889 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
890 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000891}
892
Fariborz Jahanianda320092009-01-29 19:24:30 +0000893void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000894 // FIXME: We shouldn't need this, the protocol decl should contain
895 // enough information to tell us whether this was a declaration or a
896 // definition.
897 DefinedProtocols.insert(PD->getIdentifier());
898
899 // If we have generated a forward reference to this protocol, emit
900 // it now. Otherwise do nothing, the protocol objects are lazily
901 // emitted.
902 if (Protocols.count(PD->getIdentifier()))
903 GetOrEmitProtocol(PD);
904}
905
Fariborz Jahanianda320092009-01-29 19:24:30 +0000906llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000907 if (DefinedProtocols.count(PD->getIdentifier()))
908 return GetOrEmitProtocol(PD);
909 return GetOrEmitProtocolRef(PD);
910}
911
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000912/*
913 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
914 struct _objc_protocol {
915 struct _objc_protocol_extension *isa;
916 char *protocol_name;
917 struct _objc_protocol_list *protocol_list;
918 struct _objc__method_prototype_list *instance_methods;
919 struct _objc__method_prototype_list *class_methods
920 };
921
922 See EmitProtocolExtension().
923*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000924llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
925 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
926
927 // Early exit if a defining object has already been generated.
928 if (Entry && Entry->hasInitializer())
929 return Entry;
930
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000931 // FIXME: I don't understand why gcc generates this, or where it is
932 // resolved. Investigate. Its also wasteful to look this up over and
933 // over.
934 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
935
Chris Lattner8ec03f52008-11-24 03:54:41 +0000936 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000937
938 // Construct method lists.
939 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
940 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
941 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
942 e = PD->instmeth_end(); i != e; ++i) {
943 ObjCMethodDecl *MD = *i;
944 llvm::Constant *C = GetMethodDescriptionConstant(MD);
945 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
946 OptInstanceMethods.push_back(C);
947 } else {
948 InstanceMethods.push_back(C);
949 }
950 }
951
952 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
953 e = PD->classmeth_end(); i != e; ++i) {
954 ObjCMethodDecl *MD = *i;
955 llvm::Constant *C = GetMethodDescriptionConstant(MD);
956 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
957 OptClassMethods.push_back(C);
958 } else {
959 ClassMethods.push_back(C);
960 }
961 }
962
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000963 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000964 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000965 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +0000966 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000967 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +0000968 PD->protocol_begin(),
969 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000970 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000971 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
972 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000973 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
974 InstanceMethods);
975 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000976 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
977 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000978 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
979 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000980 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
981 Values);
982
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000983 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000984 // Already created, fix the linkage and update the initializer.
985 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000986 Entry->setInitializer(Init);
987 } else {
988 Entry =
989 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
990 llvm::GlobalValue::InternalLinkage,
991 Init,
992 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
993 &CGM.getModule());
994 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
995 UsedGlobals.push_back(Entry);
996 // FIXME: Is this necessary? Why only for protocol?
997 Entry->setAlignment(4);
998 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000999
1000 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001001}
1002
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001003llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001004 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1005
1006 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001007 // We use the initializer as a marker of whether this is a forward
1008 // reference or not. At module finalization we add the empty
1009 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001010 Entry =
1011 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001012 llvm::GlobalValue::ExternalLinkage,
1013 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001014 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001015 &CGM.getModule());
1016 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
1017 UsedGlobals.push_back(Entry);
1018 // FIXME: Is this necessary? Why only for protocol?
1019 Entry->setAlignment(4);
1020 }
1021
1022 return Entry;
1023}
1024
1025/*
1026 struct _objc_protocol_extension {
1027 uint32_t size;
1028 struct objc_method_description_list *optional_instance_methods;
1029 struct objc_method_description_list *optional_class_methods;
1030 struct objc_property_list *instance_properties;
1031 };
1032*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001033llvm::Constant *
1034CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1035 const ConstantVector &OptInstanceMethods,
1036 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001037 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001038 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001039 std::vector<llvm::Constant*> Values(4);
1040 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001041 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001042 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1043 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001044 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1045 OptInstanceMethods);
1046 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001047 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1048 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001049 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1050 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001051 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1052 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001053 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001054
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001055 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001056 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1057 Values[3]->isNullValue())
1058 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
1059
1060 llvm::Constant *Init =
1061 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
1062 llvm::GlobalVariable *GV =
1063 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
1064 llvm::GlobalValue::InternalLinkage,
1065 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001066 "\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001067 &CGM.getModule());
1068 // No special section, but goes in llvm.used
1069 UsedGlobals.push_back(GV);
1070
1071 return GV;
1072}
1073
1074/*
1075 struct objc_protocol_list {
1076 struct objc_protocol_list *next;
1077 long count;
1078 Protocol *list[];
1079 };
1080*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001081llvm::Constant *
1082CGObjCMac::EmitProtocolList(const std::string &Name,
1083 ObjCProtocolDecl::protocol_iterator begin,
1084 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001085 std::vector<llvm::Constant*> ProtocolRefs;
1086
Daniel Dunbardbc933702008-08-21 21:57:41 +00001087 for (; begin != end; ++begin)
1088 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001089
1090 // Just return null for empty protocol lists
1091 if (ProtocolRefs.empty())
1092 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1093
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001094 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001095 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1096
1097 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001098 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001099 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1100 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1101 Values[2] =
1102 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1103 ProtocolRefs.size()),
1104 ProtocolRefs);
1105
1106 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1107 llvm::GlobalVariable *GV =
1108 new llvm::GlobalVariable(Init->getType(), false,
1109 llvm::GlobalValue::InternalLinkage,
1110 Init,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001111 Name,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001112 &CGM.getModule());
1113 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
1114 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1115}
1116
1117/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001118 struct _objc_property {
1119 const char * const name;
1120 const char * const attributes;
1121 };
1122
1123 struct _objc_property_list {
1124 uint32_t entsize; // sizeof (struct _objc_property)
1125 uint32_t prop_count;
1126 struct _objc_property[prop_count];
1127 };
1128*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001129llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1130 const Decl *Container,
1131 const ObjCContainerDecl *OCD,
1132 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001133 std::vector<llvm::Constant*> Properties, Prop(2);
Steve Naroff93983f82009-01-11 12:47:58 +00001134 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1135 E = OCD->prop_end(); I != E; ++I) {
1136 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001137 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001138 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001139 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1140 Prop));
1141 }
1142
1143 // Return null for empty list.
1144 if (Properties.empty())
1145 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1146
1147 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001148 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001149 std::vector<llvm::Constant*> Values(3);
1150 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1151 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1152 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1153 Properties.size());
1154 Values[2] = llvm::ConstantArray::get(AT, Properties);
1155 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1156
1157 llvm::GlobalVariable *GV =
1158 new llvm::GlobalVariable(Init->getType(), false,
1159 llvm::GlobalValue::InternalLinkage,
1160 Init,
1161 Name,
1162 &CGM.getModule());
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001163 if (ObjCABI == 2)
1164 GV->setSection("__DATA, __objc_const");
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001165 // No special section on property lists?
1166 UsedGlobals.push_back(GV);
1167 return llvm::ConstantExpr::getBitCast(GV,
1168 ObjCTypes.PropertyListPtrTy);
1169
1170}
1171
1172/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001173 struct objc_method_description_list {
1174 int count;
1175 struct objc_method_description list[];
1176 };
1177*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001178llvm::Constant *
1179CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1180 std::vector<llvm::Constant*> Desc(2);
1181 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1182 ObjCTypes.SelectorPtrTy);
1183 Desc[1] = GetMethodVarType(MD);
1184 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1185 Desc);
1186}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001187
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001188llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1189 const char *Section,
1190 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001191 // Return null for empty list.
1192 if (Methods.empty())
1193 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1194
1195 std::vector<llvm::Constant*> Values(2);
1196 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1197 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1198 Methods.size());
1199 Values[1] = llvm::ConstantArray::get(AT, Methods);
1200 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1201
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001202 llvm::GlobalVariable *GV =
1203 new llvm::GlobalVariable(Init->getType(), false,
1204 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001205 Init, Name, &CGM.getModule());
1206 GV->setSection(Section);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001207 UsedGlobals.push_back(GV);
1208 return llvm::ConstantExpr::getBitCast(GV,
1209 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001210}
1211
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001212/*
1213 struct _objc_category {
1214 char *category_name;
1215 char *class_name;
1216 struct _objc_method_list *instance_methods;
1217 struct _objc_method_list *class_methods;
1218 struct _objc_protocol_list *protocols;
1219 uint32_t size; // <rdar://4585769>
1220 struct _objc_property_list *instance_properties;
1221 };
1222 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001223void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001224 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001225
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001226 // FIXME: This is poor design, the OCD should have a pointer to the
1227 // category decl. Additionally, note that Category can be null for
1228 // the @implementation w/o an @interface case. Sema should just
1229 // create one for us as it does for @implementation so everyone else
1230 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001231 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001232 const ObjCCategoryDecl *Category =
1233 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001234 std::string ExtName(Interface->getNameAsString() + "_" +
1235 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001236
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001237 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1238 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
1239 e = OCD->instmeth_end(); i != e; ++i) {
1240 // Instance methods should always be defined.
1241 InstanceMethods.push_back(GetMethodConstant(*i));
1242 }
1243 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
1244 e = OCD->classmeth_end(); i != e; ++i) {
1245 // Class methods should always be defined.
1246 ClassMethods.push_back(GetMethodConstant(*i));
1247 }
1248
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001249 std::vector<llvm::Constant*> Values(7);
1250 Values[0] = GetClassName(OCD->getIdentifier());
1251 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001252 Values[2] =
1253 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1254 ExtName,
1255 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001256 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001257 Values[3] =
1258 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
1259 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001260 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001261 if (Category) {
1262 Values[4] =
1263 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1264 Category->protocol_begin(),
1265 Category->protocol_end());
1266 } else {
1267 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1268 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001269 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001270
1271 // If there is no category @interface then there can be no properties.
1272 if (Category) {
1273 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001274 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001275 } else {
1276 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1277 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001278
1279 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1280 Values);
1281
1282 llvm::GlobalVariable *GV =
1283 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
1284 llvm::GlobalValue::InternalLinkage,
1285 Init,
1286 std::string("\01L_OBJC_CATEGORY_")+ExtName,
1287 &CGM.getModule());
1288 GV->setSection("__OBJC,__category,regular,no_dead_strip");
1289 UsedGlobals.push_back(GV);
1290 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001291}
1292
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001293// FIXME: Get from somewhere?
1294enum ClassFlags {
1295 eClassFlags_Factory = 0x00001,
1296 eClassFlags_Meta = 0x00002,
1297 // <rdr://5142207>
1298 eClassFlags_HasCXXStructors = 0x02000,
1299 eClassFlags_Hidden = 0x20000,
1300 eClassFlags_ABI2_Hidden = 0x00010,
1301 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1302};
1303
1304// <rdr://5142207&4705298&4843145>
1305static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
1306 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
1307 // FIXME: Support -fvisibility
1308 switch (attr->getVisibility()) {
1309 default:
1310 assert(0 && "Unknown visibility");
1311 return false;
1312 case VisibilityAttr::DefaultVisibility:
1313 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
1314 return false;
1315 case VisibilityAttr::HiddenVisibility:
1316 return true;
1317 }
1318 } else {
1319 return false; // FIXME: Support -fvisibility
1320 }
1321}
1322
1323/*
1324 struct _objc_class {
1325 Class isa;
1326 Class super_class;
1327 const char *name;
1328 long version;
1329 long info;
1330 long instance_size;
1331 struct _objc_ivar_list *ivars;
1332 struct _objc_method_list *methods;
1333 struct _objc_cache *cache;
1334 struct _objc_protocol_list *protocols;
1335 // Objective-C 1.0 extensions (<rdr://4585769>)
1336 const char *ivar_layout;
1337 struct _objc_class_ext *ext;
1338 };
1339
1340 See EmitClassExtension();
1341 */
1342void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001343 DefinedSymbols.insert(ID->getIdentifier());
1344
Chris Lattner8ec03f52008-11-24 03:54:41 +00001345 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001346 // FIXME: Gross
1347 ObjCInterfaceDecl *Interface =
1348 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001349 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001350 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001351 Interface->protocol_begin(),
1352 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001353 const llvm::Type *InterfaceTy =
1354 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
1355 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001356 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001357
1358 // FIXME: Set CXX-structors flag.
1359 if (IsClassHidden(ID->getClassInterface()))
1360 Flags |= eClassFlags_Hidden;
1361
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001362 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1363 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
1364 e = ID->instmeth_end(); i != e; ++i) {
1365 // Instance methods should always be defined.
1366 InstanceMethods.push_back(GetMethodConstant(*i));
1367 }
1368 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
1369 e = ID->classmeth_end(); i != e; ++i) {
1370 // Class methods should always be defined.
1371 ClassMethods.push_back(GetMethodConstant(*i));
1372 }
1373
1374 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
1375 e = ID->propimpl_end(); i != e; ++i) {
1376 ObjCPropertyImplDecl *PID = *i;
1377
1378 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1379 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1380
1381 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1382 if (llvm::Constant *C = GetMethodConstant(MD))
1383 InstanceMethods.push_back(C);
1384 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1385 if (llvm::Constant *C = GetMethodConstant(MD))
1386 InstanceMethods.push_back(C);
1387 }
1388 }
1389
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001390 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001391 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001392 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001393 // Record a reference to the super class.
1394 LazySymbols.insert(Super->getIdentifier());
1395
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001396 Values[ 1] =
1397 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1398 ObjCTypes.ClassPtrTy);
1399 } else {
1400 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1401 }
1402 Values[ 2] = GetClassName(ID->getIdentifier());
1403 // Version is always 0.
1404 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1405 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1406 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001407 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001408 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001409 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001410 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001411 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001412 // cache is always NULL.
1413 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1414 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001415 // FIXME: Set ivar_layout
1416 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001417 Values[11] = EmitClassExtension(ID);
1418 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1419 Values);
1420
1421 llvm::GlobalVariable *GV =
1422 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1423 llvm::GlobalValue::InternalLinkage,
1424 Init,
1425 std::string("\01L_OBJC_CLASS_")+ClassName,
1426 &CGM.getModule());
1427 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1428 UsedGlobals.push_back(GV);
1429 // FIXME: Why?
1430 GV->setAlignment(32);
1431 DefinedClasses.push_back(GV);
1432}
1433
1434llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1435 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001436 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001437 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001438 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001439 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001440
1441 if (IsClassHidden(ID->getClassInterface()))
1442 Flags |= eClassFlags_Hidden;
1443
1444 std::vector<llvm::Constant*> Values(12);
1445 // The isa for the metaclass is the root of the hierarchy.
1446 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1447 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1448 Root = Super;
1449 Values[ 0] =
1450 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1451 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001452 // The super class for the metaclass is emitted as the name of the
1453 // super class. The runtime fixes this up to point to the
1454 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001455 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1456 Values[ 1] =
1457 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1458 ObjCTypes.ClassPtrTy);
1459 } else {
1460 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1461 }
1462 Values[ 2] = GetClassName(ID->getIdentifier());
1463 // Version is always 0.
1464 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1465 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1466 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001467 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001468 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001469 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001470 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001471 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001472 // cache is always NULL.
1473 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1474 Values[ 9] = Protocols;
1475 // ivar_layout for metaclass is always NULL.
1476 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1477 // The class extension is always unused for metaclasses.
1478 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1479 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1480 Values);
1481
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001482 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00001483 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001484
1485 // Check for a forward reference.
1486 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1487 if (GV) {
1488 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1489 "Forward metaclass reference has incorrect type.");
1490 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1491 GV->setInitializer(Init);
1492 } else {
1493 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1494 llvm::GlobalValue::InternalLinkage,
1495 Init, Name,
1496 &CGM.getModule());
1497 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001498 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1499 UsedGlobals.push_back(GV);
1500 // FIXME: Why?
1501 GV->setAlignment(32);
1502
1503 return GV;
1504}
1505
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001506llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001507 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001508
1509 // FIXME: Should we look these up somewhere other than the
1510 // module. Its a bit silly since we only generate these while
1511 // processing an implementation, so exactly one pointer would work
1512 // if know when we entered/exitted an implementation block.
1513
1514 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00001515 // Previously, metaclass with internal linkage may have been defined.
1516 // pass 'true' as 2nd argument so it is returned.
1517 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001518 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1519 "Forward metaclass reference has incorrect type.");
1520 return GV;
1521 } else {
1522 // Generate as an external reference to keep a consistent
1523 // module. This will be patched up when we emit the metaclass.
1524 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1525 llvm::GlobalValue::ExternalLinkage,
1526 0,
1527 Name,
1528 &CGM.getModule());
1529 }
1530}
1531
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001532/*
1533 struct objc_class_ext {
1534 uint32_t size;
1535 const char *weak_ivar_layout;
1536 struct _objc_property_list *properties;
1537 };
1538*/
1539llvm::Constant *
1540CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1541 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001542 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001543
1544 std::vector<llvm::Constant*> Values(3);
1545 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001546 // FIXME: Output weak_ivar_layout string.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001547 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001548 Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001549 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001550
1551 // Return null if no extension bits are used.
1552 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1553 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1554
1555 llvm::Constant *Init =
1556 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1557 llvm::GlobalVariable *GV =
1558 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1559 llvm::GlobalValue::InternalLinkage,
1560 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001561 "\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001562 &CGM.getModule());
1563 // No special section, but goes in llvm.used
1564 UsedGlobals.push_back(GV);
1565
1566 return GV;
1567}
1568
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001569/// countInheritedIvars - count number of ivars in class and its super class(s)
1570///
1571static int countInheritedIvars(const ObjCInterfaceDecl *OI) {
1572 int count = 0;
1573 if (!OI)
1574 return 0;
1575 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
1576 if (SuperClass)
1577 count += countInheritedIvars(SuperClass);
1578 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1579 E = OI->ivar_end(); I != E; ++I)
1580 ++count;
1581 return count;
1582}
1583
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001584/*
1585 struct objc_ivar {
1586 char *ivar_name;
1587 char *ivar_type;
1588 int ivar_offset;
1589 };
1590
1591 struct objc_ivar_list {
1592 int ivar_count;
1593 struct objc_ivar list[count];
1594 };
1595 */
1596llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001597 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001598 std::vector<llvm::Constant*> Ivars, Ivar(3);
1599
1600 // When emitting the root class GCC emits ivar entries for the
1601 // actual class structure. It is not clear if we need to follow this
1602 // behavior; for now lets try and get away with not doing it. If so,
1603 // the cleanest solution would be to make up an ObjCInterfaceDecl
1604 // for the class.
1605 if (ForClass)
1606 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001607
1608 ObjCInterfaceDecl *OID =
1609 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
1610 const llvm::Type *InterfaceTy =
1611 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001612 const llvm::StructLayout *Layout =
1613 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001614
1615 RecordDecl::field_iterator ifield, pfield;
1616 const RecordDecl *RD = GetFirstIvarInRecord(OID, ifield, pfield);
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001617 for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) {
1618 FieldDecl *Field = *ifield;
1619 unsigned Offset = Layout->getElementOffset(CGM.getTypes().
1620 getLLVMFieldNo(Field));
1621 if (Field->getIdentifier())
1622 Ivar[0] = GetMethodVarName(Field->getIdentifier());
1623 else
1624 Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001625 std::string TypeStr;
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001626 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001627 Ivar[1] = GetMethodVarType(TypeStr);
1628 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001629 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001630 }
1631
1632 // Return null for empty list.
1633 if (Ivars.empty())
1634 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1635
1636 std::vector<llvm::Constant*> Values(2);
1637 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1638 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1639 Ivars.size());
1640 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1641 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1642
1643 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1644 "\01L_OBJC_INSTANCE_VARIABLES_");
1645 llvm::GlobalVariable *GV =
1646 new llvm::GlobalVariable(Init->getType(), false,
1647 llvm::GlobalValue::InternalLinkage,
1648 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001649 Prefix + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001650 &CGM.getModule());
1651 if (ForClass) {
1652 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1653 // FIXME: Why is this only here?
1654 GV->setAlignment(32);
1655 } else {
1656 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1657 }
1658 UsedGlobals.push_back(GV);
1659 return llvm::ConstantExpr::getBitCast(GV,
1660 ObjCTypes.IvarListPtrTy);
1661}
1662
1663/*
1664 struct objc_method {
1665 SEL method_name;
1666 char *method_types;
1667 void *method;
1668 };
1669
1670 struct objc_method_list {
1671 struct objc_method_list *obsolete;
1672 int count;
1673 struct objc_method methods_list[count];
1674 };
1675*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001676
1677/// GetMethodConstant - Return a struct objc_method constant for the
1678/// given method if it has been defined. The result is null if the
1679/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001680llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001681 // FIXME: Use DenseMap::lookup
1682 llvm::Function *Fn = MethodDefinitions[MD];
1683 if (!Fn)
1684 return 0;
1685
1686 std::vector<llvm::Constant*> Method(3);
1687 Method[0] =
1688 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1689 ObjCTypes.SelectorPtrTy);
1690 Method[1] = GetMethodVarType(MD);
1691 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1692 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1693}
1694
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001695llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1696 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001697 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001698 // Return null for empty list.
1699 if (Methods.empty())
1700 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1701
1702 std::vector<llvm::Constant*> Values(3);
1703 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1704 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1705 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1706 Methods.size());
1707 Values[2] = llvm::ConstantArray::get(AT, Methods);
1708 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1709
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001710 llvm::GlobalVariable *GV =
1711 new llvm::GlobalVariable(Init->getType(), false,
1712 llvm::GlobalValue::InternalLinkage,
1713 Init,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001714 Name,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001715 &CGM.getModule());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001716 GV->setSection(Section);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001717 UsedGlobals.push_back(GV);
1718 return llvm::ConstantExpr::getBitCast(GV,
1719 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001720}
1721
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001722llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001723 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001724 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001725 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001726
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001727 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001728 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001729 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001730 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001731 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001732 llvm::GlobalValue::InternalLinkage,
1733 Name,
1734 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001735 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001736
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001737 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001738}
1739
1740llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001741 // Abuse this interface function as a place to finalize.
1742 FinishModule();
1743
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001744 return NULL;
1745}
1746
Daniel Dunbar49f66022008-09-24 03:38:44 +00001747llvm::Function *CGObjCMac::GetPropertyGetFunction() {
1748 return ObjCTypes.GetPropertyFn;
1749}
1750
1751llvm::Function *CGObjCMac::GetPropertySetFunction() {
1752 return ObjCTypes.SetPropertyFn;
1753}
1754
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001755llvm::Function *CGObjCMac::EnumerationMutationFunction()
1756{
1757 return ObjCTypes.EnumerationMutationFn;
1758}
1759
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001760/*
1761
1762Objective-C setjmp-longjmp (sjlj) Exception Handling
1763--
1764
1765The basic framework for a @try-catch-finally is as follows:
1766{
1767 objc_exception_data d;
1768 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00001769 bool _call_try_exit = true;
1770
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001771 objc_exception_try_enter(&d);
1772 if (!setjmp(d.jmp_buf)) {
1773 ... try body ...
1774 } else {
1775 // exception path
1776 id _caught = objc_exception_extract(&d);
1777
1778 // enter new try scope for handlers
1779 if (!setjmp(d.jmp_buf)) {
1780 ... match exception and execute catch blocks ...
1781
1782 // fell off end, rethrow.
1783 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00001784 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001785 } else {
1786 // exception in catch block
1787 _rethrow = objc_exception_extract(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00001788 _call_try_exit = false;
1789 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001790 }
1791 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00001792 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001793
1794finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00001795 if (_call_try_exit)
1796 objc_exception_try_exit(&d);
1797
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001798 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00001799 ... dispatch to finally destination ...
1800
1801finally_rethrow:
1802 objc_exception_throw(_rethrow);
1803
1804finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001805}
1806
1807This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00001808uses _rethrow to determine if objc_exception_try_exit should be called
1809and if the object should be rethrown. This breaks in the face of
1810throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001811
1812We specialize this framework for a few particular circumstances:
1813
1814 - If there are no catch blocks, then we avoid emitting the second
1815 exception handling context.
1816
1817 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
1818 e)) we avoid emitting the code to rethrow an uncaught exception.
1819
1820 - FIXME: If there is no @finally block we can do a few more
1821 simplifications.
1822
1823Rethrows and Jumps-Through-Finally
1824--
1825
1826Support for implicit rethrows and jumping through the finally block is
1827handled by storing the current exception-handling context in
1828ObjCEHStack.
1829
Daniel Dunbar898d5082008-09-30 01:06:03 +00001830In order to implement proper @finally semantics, we support one basic
1831mechanism for jumping through the finally block to an arbitrary
1832destination. Constructs which generate exits from a @try or @catch
1833block use this mechanism to implement the proper semantics by chaining
1834jumps, as necessary.
1835
1836This mechanism works like the one used for indirect goto: we
1837arbitrarily assign an ID to each destination and store the ID for the
1838destination in a variable prior to entering the finally block. At the
1839end of the finally block we simply create a switch to the proper
1840destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001841
1842Code gen for @synchronized(expr) stmt;
1843Effectively generating code for:
1844objc_sync_enter(expr);
1845@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001846*/
1847
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001848void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1849 const Stmt &S) {
1850 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001851 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001852 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Anders Carlsson190d00e2009-02-07 21:26:04 +00001853 llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit");
Daniel Dunbar55e87422008-11-11 02:29:29 +00001854 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
1855 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1856 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001857 llvm::Value *DestCode =
1858 CGF.CreateTempAlloca(llvm::Type::Int32Ty, "finally.dst");
1859
1860 // Generate jump code. Done here so we can directly add things to
1861 // the switch instruction.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001862 llvm::BasicBlock *FinallyJump = CGF.createBasicBlock("finally.jump");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001863 llvm::SwitchInst *FinallySwitch =
1864 llvm::SwitchInst::Create(new llvm::LoadInst(DestCode, "", FinallyJump),
1865 FinallyEnd, 10, FinallyJump);
1866
1867 // Push an EH context entry, used for handling rethrows and jumps
1868 // through finally.
Anders Carlsson190d00e2009-02-07 21:26:04 +00001869 CodeGenFunction::ObjCEHEntry EHEntry(FinallyBlock, FinallySwitch, DestCode);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001870 CGF.ObjCEHStack.push_back(&EHEntry);
1871
1872 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00001873 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
1874 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001875 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
1876 "_rethrow");
Anders Carlsson190d00e2009-02-07 21:26:04 +00001877 llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty,
1878 "_call_try_exit");
1879 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr);
1880
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001881 if (!isTry) {
1882 // For @synchronized, call objc_sync_enter(sync.expr)
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001883 llvm::Value *Arg = CGF.EmitScalarExpr(
1884 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1885 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1886 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, Arg);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001887 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001888
1889 // Enter a new try block and call setjmp.
1890 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
1891 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
1892 "jmpbufarray");
1893 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
1894 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1895 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001896
Daniel Dunbar55e87422008-11-11 02:29:29 +00001897 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1898 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001899 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001900 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001901
1902 // Emit the @try block.
1903 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001904 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
1905 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001906 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001907
1908 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001909 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001910
1911 // Retrieve the exception object. We may emit multiple blocks but
1912 // nothing can cross this so the value is already in SSA form.
1913 llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1914 ExceptionData,
1915 "caught");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001916 EHEntry.Exception = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001917 if (!isTry)
1918 {
1919 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00001920 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001921 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
1922 }
1923 else if (const ObjCAtCatchStmt* CatchStmt =
1924 cast<ObjCAtTryStmt>(S).getCatchStmts())
1925 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00001926 // Enter a new exception try block (in case a @catch block throws
1927 // an exception).
Anders Carlsson80f25672008-09-09 17:59:25 +00001928 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001929
Anders Carlsson80f25672008-09-09 17:59:25 +00001930 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1931 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001932 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00001933
Daniel Dunbar55e87422008-11-11 02:29:29 +00001934 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
1935 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001936 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001937
1938 CGF.EmitBlock(CatchBlock);
1939
Daniel Dunbar55e40722008-09-27 07:03:52 +00001940 // Handle catch list. As a special case we check if everything is
1941 // matched and avoid generating code for falling off the end if
1942 // so.
1943 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00001944 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00001945 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00001946
Anders Carlssondde0a942008-09-11 09:15:33 +00001947 const DeclStmt *CatchParam =
1948 cast_or_null<DeclStmt>(CatchStmt->getCatchParamStmt());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001949 const VarDecl *VD = 0;
1950 const PointerType *PT = 0;
1951
Anders Carlsson80f25672008-09-09 17:59:25 +00001952 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00001953 if (!CatchParam) {
1954 AllMatched = true;
1955 } else {
Ted Kremenekde3b8fb2008-10-06 20:58:56 +00001956 VD = cast<VarDecl>(CatchParam->getSolitaryDecl());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001957 PT = VD->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001958
Daniel Dunbar97f61d12008-09-27 22:21:14 +00001959 // catch(id e) always matches.
1960 // FIXME: For the time being we also match id<X>; this should
1961 // be rejected by Sema instead.
1962 if ((PT && CGF.getContext().isObjCIdType(PT->getPointeeType())) ||
1963 VD->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00001964 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00001965 }
1966
Daniel Dunbar55e40722008-09-27 07:03:52 +00001967 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00001968 if (CatchParam) {
1969 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001970 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar129271a2008-09-27 07:36:24 +00001971 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00001972 }
Anders Carlsson1452f552008-09-11 08:21:54 +00001973
Anders Carlssondde0a942008-09-11 09:15:33 +00001974 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00001975 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001976 break;
1977 }
1978
Daniel Dunbar129271a2008-09-27 07:36:24 +00001979 assert(PT && "Unexpected non-pointer type in @catch");
1980 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00001981 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001982 assert(ObjCType && "Catch parameter must have Objective-C type!");
1983
1984 // Check if the @catch block matches the exception object.
1985 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
1986
Anders Carlsson80f25672008-09-09 17:59:25 +00001987 llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn,
1988 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00001989
Daniel Dunbar55e87422008-11-11 02:29:29 +00001990 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00001991
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001992 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001993 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001994
1995 // Emit the @catch block.
1996 CGF.EmitBlock(MatchedBlock);
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001997 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001998 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001999
2000 llvm::Value *Tmp =
2001 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(VD->getType()),
2002 "tmp");
2003 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00002004
2005 CGF.EmitStmt(CatchStmt->getCatchBody());
Daniel Dunbar898d5082008-09-30 01:06:03 +00002006 CGF.EmitJumpThroughFinally(&EHEntry, FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002007
2008 CGF.EmitBlock(NextCatchBlock);
2009 }
2010
Daniel Dunbar55e40722008-09-27 07:03:52 +00002011 if (!AllMatched) {
2012 // None of the handlers caught the exception, so store it to be
2013 // rethrown at the end of the @finally block.
2014 CGF.Builder.CreateStore(Caught, RethrowPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00002015 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002016 }
2017
2018 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002019 CGF.EmitBlock(CatchHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002020 CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
2021 ExceptionData),
2022 RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002023 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00002024 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002025 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00002026 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002027 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Daniel Dunbar898d5082008-09-30 01:06:03 +00002028 CGF.EmitJumpThroughFinally(&EHEntry, FinallyRethrow, false);
Anders Carlsson80f25672008-09-09 17:59:25 +00002029 }
2030
Daniel Dunbar898d5082008-09-30 01:06:03 +00002031 // Pop the exception-handling stack entry. It is important to do
2032 // this now, because the code in the @finally block is not in this
2033 // context.
2034 CGF.ObjCEHStack.pop_back();
2035
Anders Carlsson80f25672008-09-09 17:59:25 +00002036 // Emit the @finally block.
2037 CGF.EmitBlock(FinallyBlock);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002038 llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp");
2039
2040 CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit);
2041
2042 CGF.EmitBlock(FinallyExit);
Anders Carlsson80f25672008-09-09 17:59:25 +00002043 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00002044
2045 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002046 if (isTry) {
2047 if (const ObjCAtFinallyStmt* FinallyStmt =
2048 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2049 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2050 }
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002051 else {
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002052 // For @synchronized objc_sync_exit(expr); As finally's sole statement.
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002053 // For @synchronized, call objc_sync_enter(sync.expr)
2054 llvm::Value *Arg = CGF.EmitScalarExpr(
2055 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2056 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
2057 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, Arg);
2058 }
Anders Carlsson80f25672008-09-09 17:59:25 +00002059
Daniel Dunbar898d5082008-09-30 01:06:03 +00002060 CGF.EmitBlock(FinallyJump);
2061
2062 CGF.EmitBlock(FinallyRethrow);
2063 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn,
2064 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002065 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00002066
2067 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002068}
2069
2070void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002071 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002072 llvm::Value *ExceptionAsObject;
2073
2074 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2075 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2076 ExceptionAsObject =
2077 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2078 } else {
Daniel Dunbar898d5082008-09-30 01:06:03 +00002079 assert((!CGF.ObjCEHStack.empty() && CGF.ObjCEHStack.back()->Exception) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002080 "Unexpected rethrow outside @catch block.");
Daniel Dunbar898d5082008-09-30 01:06:03 +00002081 ExceptionAsObject = CGF.ObjCEHStack.back()->Exception;
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002082 }
2083
2084 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002085 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002086
2087 // Clear the insertion point to indicate we are in unreachable code.
2088 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002089}
2090
Daniel Dunbar898d5082008-09-30 01:06:03 +00002091void CodeGenFunction::EmitJumpThroughFinally(ObjCEHEntry *E,
2092 llvm::BasicBlock *Dst,
2093 bool ExecuteTryExit) {
Daniel Dunbara448fb22008-11-11 23:11:34 +00002094 if (!HaveInsertPoint())
Daniel Dunbar898d5082008-09-30 01:06:03 +00002095 return;
2096
2097 // Find the destination code for this block. We always use 0 for the
2098 // fallthrough block (default destination).
2099 llvm::SwitchInst *SI = E->FinallySwitch;
2100 llvm::ConstantInt *ID;
2101 if (Dst == SI->getDefaultDest()) {
2102 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
2103 } else {
2104 ID = SI->findCaseDest(Dst);
2105 if (!ID) {
2106 // No code found, get a new unique one by just using the number
2107 // of switch successors.
2108 ID = llvm::ConstantInt::get(llvm::Type::Int32Ty, SI->getNumSuccessors());
2109 SI->addCase(ID, Dst);
2110 }
2111 }
2112
2113 // Set the destination code and branch.
2114 Builder.CreateStore(ID, E->DestCode);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002115 EmitBranch(E->FinallyBlock);
Daniel Dunbar898d5082008-09-30 01:06:03 +00002116}
2117
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002118/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002119/// object: objc_read_weak (id *src)
2120///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002121llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002122 llvm::Value *AddrWeakObj)
2123{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002124 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002125 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002126 AddrWeakObj, "weakread");
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002127 return read_weak;
2128}
2129
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002130/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2131/// objc_assign_weak (id src, id *dst)
2132///
2133void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2134 llvm::Value *src, llvm::Value *dst)
2135{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002136 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2137 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002138 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
2139 src, dst, "weakassign");
2140 return;
2141}
2142
Fariborz Jahanian58626502008-11-19 00:59:10 +00002143/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2144/// objc_assign_global (id src, id *dst)
2145///
2146void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2147 llvm::Value *src, llvm::Value *dst)
2148{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002149 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2150 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002151 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
2152 src, dst, "globalassign");
2153 return;
2154}
2155
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002156/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2157/// objc_assign_ivar (id src, id *dst)
2158///
2159void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2160 llvm::Value *src, llvm::Value *dst)
2161{
2162 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2163 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
2164 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
2165 src, dst, "assignivar");
2166 return;
2167}
2168
Fariborz Jahanian58626502008-11-19 00:59:10 +00002169/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2170/// objc_assign_strongCast (id src, id *dst)
2171///
2172void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2173 llvm::Value *src, llvm::Value *dst)
2174{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002175 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2176 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002177 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
2178 src, dst, "weakassign");
2179 return;
2180}
2181
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002182/// EmitObjCValueForIvar - Code Gen for ivar reference.
2183///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002184LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2185 QualType ObjectTy,
2186 llvm::Value *BaseValue,
2187 const ObjCIvarDecl *Ivar,
2188 const FieldDecl *Field,
2189 unsigned CVRQualifiers) {
2190 if (Ivar->isBitField())
2191 return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field),
2192 CVRQualifiers);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002193 // TODO: Add a special case for isa (index 0)
2194 unsigned Index = CGM.getTypes().getLLVMFieldNo(Field);
2195 llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp");
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002196 LValue LV = LValue::MakeAddr(V,
2197 Ivar->getType().getCVRQualifiers()|CVRQualifiers);
2198 LValue::SetObjCIvar(LV, true);
2199 return LV;
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002200}
2201
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002202/* *** Private Interface *** */
2203
2204/// EmitImageInfo - Emit the image info marker used to encode some module
2205/// level information.
2206///
2207/// See: <rdr://4810609&4810587&4810587>
2208/// struct IMAGE_INFO {
2209/// unsigned version;
2210/// unsigned flags;
2211/// };
2212enum ImageInfoFlags {
2213 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
2214 eImageInfo_GarbageCollected = (1 << 1),
2215 eImageInfo_GCOnly = (1 << 2)
2216};
2217
2218void CGObjCMac::EmitImageInfo() {
2219 unsigned version = 0; // Version is unused?
2220 unsigned flags = 0;
2221
2222 // FIXME: Fix and continue?
2223 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2224 flags |= eImageInfo_GarbageCollected;
2225 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2226 flags |= eImageInfo_GCOnly;
2227
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002228 // Emitted as int[2];
2229 llvm::Constant *values[2] = {
2230 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2231 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2232 };
2233 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002234 llvm::GlobalVariable *GV =
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002235 new llvm::GlobalVariable(AT, true,
2236 llvm::GlobalValue::InternalLinkage,
2237 llvm::ConstantArray::get(AT, values, 2),
2238 "\01L_OBJC_IMAGE_INFO",
2239 &CGM.getModule());
2240
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002241 if (ObjCABI == 1) {
2242 GV->setSection("__OBJC, __image_info,regular");
2243 } else {
2244 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
2245 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002246
2247 UsedGlobals.push_back(GV);
2248}
2249
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002250
2251// struct objc_module {
2252// unsigned long version;
2253// unsigned long size;
2254// const char *name;
2255// Symtab symtab;
2256// };
2257
2258// FIXME: Get from somewhere
2259static const int ModuleVersion = 7;
2260
2261void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002262 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002263
2264 std::vector<llvm::Constant*> Values(4);
2265 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2266 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002267 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002268 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002269 Values[3] = EmitModuleSymbols();
2270
2271 llvm::GlobalVariable *GV =
2272 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
2273 llvm::GlobalValue::InternalLinkage,
2274 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
2275 Values),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002276 "\01L_OBJC_MODULES",
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002277 &CGM.getModule());
2278 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
2279 UsedGlobals.push_back(GV);
2280}
2281
2282llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002283 unsigned NumClasses = DefinedClasses.size();
2284 unsigned NumCategories = DefinedCategories.size();
2285
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002286 // Return null if no symbols were defined.
2287 if (!NumClasses && !NumCategories)
2288 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2289
2290 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002291 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2292 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2293 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2294 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2295
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002296 // The runtime expects exactly the list of defined classes followed
2297 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002298 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002299 for (unsigned i=0; i<NumClasses; i++)
2300 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2301 ObjCTypes.Int8PtrTy);
2302 for (unsigned i=0; i<NumCategories; i++)
2303 Symbols[NumClasses + i] =
2304 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2305 ObjCTypes.Int8PtrTy);
2306
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002307 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002308 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002309 NumClasses + NumCategories),
2310 Symbols);
2311
2312 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2313
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002314 llvm::GlobalVariable *GV =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002315 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002316 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002317 Init,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002318 "\01L_OBJC_SYMBOLS",
2319 &CGM.getModule());
2320 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
2321 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002322 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2323}
2324
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002325llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002326 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002327 LazySymbols.insert(ID->getIdentifier());
2328
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002329 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2330
2331 if (!Entry) {
2332 llvm::Constant *Casted =
2333 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2334 ObjCTypes.ClassPtrTy);
2335 Entry =
2336 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
2337 llvm::GlobalValue::InternalLinkage,
2338 Casted, "\01L_OBJC_CLASS_REFERENCES_",
2339 &CGM.getModule());
2340 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
2341 UsedGlobals.push_back(Entry);
2342 }
2343
2344 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002345}
2346
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002347llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002348 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2349
2350 if (!Entry) {
2351 llvm::Constant *Casted =
2352 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2353 ObjCTypes.SelectorPtrTy);
2354 Entry =
2355 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
2356 llvm::GlobalValue::InternalLinkage,
2357 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
2358 &CGM.getModule());
2359 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
2360 UsedGlobals.push_back(Entry);
2361 }
2362
2363 return Builder.CreateLoad(Entry, false, "tmp");
2364}
2365
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002366llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002367 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002368
2369 if (!Entry) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002370 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002371 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002372 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002373 llvm::GlobalValue::InternalLinkage,
2374 C, "\01L_OBJC_CLASS_NAME_",
2375 &CGM.getModule());
2376 Entry->setSection("__TEXT,__cstring,cstring_literals");
2377 UsedGlobals.push_back(Entry);
2378 }
2379
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002380 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002381}
2382
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002383llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002384 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
2385
2386 if (!Entry) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00002387 // FIXME: Avoid std::string copying.
2388 llvm::Constant *C = llvm::ConstantArray::get(Sel.getAsString());
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002389 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002390 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002391 llvm::GlobalValue::InternalLinkage,
2392 C, "\01L_OBJC_METH_VAR_NAME_",
2393 &CGM.getModule());
2394 Entry->setSection("__TEXT,__cstring,cstring_literals");
2395 UsedGlobals.push_back(Entry);
2396 }
2397
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002398 return getConstantGEP(Entry, 0, 0);
2399}
2400
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002401// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002402llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002403 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
2404}
2405
2406// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002407llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002408 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
2409}
2410
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002411llvm::Constant *CGObjCCommonMac::GetMethodVarType(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002412 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002413
2414 if (!Entry) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002415 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002416 Entry =
2417 new llvm::GlobalVariable(C->getType(), false,
2418 llvm::GlobalValue::InternalLinkage,
2419 C, "\01L_OBJC_METH_VAR_TYPE_",
2420 &CGM.getModule());
2421 Entry->setSection("__TEXT,__cstring,cstring_literals");
2422 UsedGlobals.push_back(Entry);
2423 }
2424
2425 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002426}
2427
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002428// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002429llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002430 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002431 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
2432 TypeStr);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002433 return GetMethodVarType(TypeStr);
2434}
2435
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002436// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002437llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002438 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
2439
2440 if (!Entry) {
2441 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
2442 Entry =
2443 new llvm::GlobalVariable(C->getType(), false,
2444 llvm::GlobalValue::InternalLinkage,
2445 C, "\01L_OBJC_PROP_NAME_ATTR_",
2446 &CGM.getModule());
2447 Entry->setSection("__TEXT,__cstring,cstring_literals");
2448 UsedGlobals.push_back(Entry);
2449 }
2450
2451 return getConstantGEP(Entry, 0, 0);
2452}
2453
2454// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002455// FIXME: This Decl should be more precise.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002456llvm::Constant *CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002457 const Decl *Container) {
2458 std::string TypeStr;
2459 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002460 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
2461}
2462
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002463void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
2464 const ObjCContainerDecl *CD,
2465 std::string &NameOut) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002466 // FIXME: Find the mangling GCC uses.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002467 NameOut = (D->isInstanceMethod() ? "-" : "+");
Chris Lattner077bf5e2008-11-24 03:33:13 +00002468 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002469 assert (CD && "Missing container decl in GetNameForMethod");
2470 NameOut += CD->getNameAsString();
Fariborz Jahanian52847332009-01-26 23:49:05 +00002471 // FIXME. For a method in a category, (CAT_NAME) is inserted here.
2472 // Right now! there is not enough info. to do this.
Chris Lattner077bf5e2008-11-24 03:33:13 +00002473 NameOut += ' ';
2474 NameOut += D->getSelector().getAsString();
2475 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002476}
2477
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002478/// GetFirstIvarInRecord - This routine returns the record for the
2479/// implementation of the fiven class OID. It also returns field
2480/// corresponding to the first ivar in the class in FIV. It also
2481/// returns the one before the first ivar.
2482///
2483const RecordDecl *CGObjCCommonMac::GetFirstIvarInRecord(
2484 const ObjCInterfaceDecl *OID,
2485 RecordDecl::field_iterator &FIV,
2486 RecordDecl::field_iterator &PIV) {
2487 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass());
2488 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
2489 RecordDecl::field_iterator ifield = RD->field_begin();
2490 RecordDecl::field_iterator pfield = RD->field_end();
2491 while (countSuperClassIvars-- > 0) {
2492 pfield = ifield;
2493 ++ifield;
2494 }
2495 FIV = ifield;
2496 PIV = pfield;
2497 return RD;
2498}
2499
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002500void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002501 EmitModuleInfo();
2502
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002503 // Emit the dummy bodies for any protocols which were referenced but
2504 // never defined.
2505 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
2506 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
2507 if (i->second->hasInitializer())
2508 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002509
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002510 std::vector<llvm::Constant*> Values(5);
2511 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
2512 Values[1] = GetClassName(i->first);
2513 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
2514 Values[3] = Values[4] =
2515 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
2516 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
2517 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
2518 Values));
2519 }
2520
2521 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002522 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002523 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002524 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002525 }
2526
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002527 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002528 llvm::GlobalValue *GV =
2529 new llvm::GlobalVariable(AT, false,
2530 llvm::GlobalValue::AppendingLinkage,
2531 llvm::ConstantArray::get(AT, Used),
2532 "llvm.used",
2533 &CGM.getModule());
2534
2535 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002536
2537 // Add assembler directives to add lazy undefined symbol references
2538 // for classes which are referenced but not defined. This is
2539 // important for correct linker interaction.
2540
2541 // FIXME: Uh, this isn't particularly portable.
2542 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00002543
2544 if (!CGM.getModule().getModuleInlineAsm().empty())
2545 s << "\n";
2546
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002547 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
2548 e = LazySymbols.end(); i != e; ++i) {
2549 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
2550 }
2551 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
2552 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002553 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002554 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
2555 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00002556
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002557 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002558}
2559
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002560CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002561 : CGObjCCommonMac(cgm),
2562 ObjCTypes(cgm)
2563{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00002564 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002565 ObjCABI = 2;
2566}
2567
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002568/* *** */
2569
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002570ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
2571: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002572{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002573 CodeGen::CodeGenTypes &Types = CGM.getTypes();
2574 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002575
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002576 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002577 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002578 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002579 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
2580
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002581 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002582 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002583 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002584
2585 // FIXME: It would be nice to unify this with the opaque type, so
2586 // that the IR comes out a bit cleaner.
2587 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
2588 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002589
2590 // I'm not sure I like this. The implicit coordination is a bit
2591 // gross. We should solve this in a reasonable fashion because this
2592 // is a pretty common task (match some runtime data structure with
2593 // an LLVM data structure).
2594
2595 // FIXME: This is leaked.
2596 // FIXME: Merge with rewriter code?
2597
2598 // struct _objc_super {
2599 // id self;
2600 // Class cls;
2601 // }
2602 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
2603 SourceLocation(),
2604 &Ctx.Idents.get("_objc_super"));
2605 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2606 Ctx.getObjCIdType(), 0, false));
2607 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2608 Ctx.getObjCClassType(), 0, false));
2609 RD->completeDefinition(Ctx);
2610
2611 SuperCTy = Ctx.getTagDeclType(RD);
2612 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
2613
2614 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002615 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
2616
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002617 // struct _prop_t {
2618 // char *name;
2619 // char *attributes;
2620 // }
2621 PropertyTy = llvm::StructType::get(Int8PtrTy,
2622 Int8PtrTy,
2623 NULL);
2624 CGM.getModule().addTypeName("struct._prop_t",
2625 PropertyTy);
2626
2627 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002628 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002629 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002630 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002631 // }
2632 PropertyListTy = llvm::StructType::get(IntTy,
2633 IntTy,
2634 llvm::ArrayType::get(PropertyTy, 0),
2635 NULL);
2636 CGM.getModule().addTypeName("struct._prop_list_t",
2637 PropertyListTy);
2638 // struct _prop_list_t *
2639 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
2640
2641 // struct _objc_method {
2642 // SEL _cmd;
2643 // char *method_type;
2644 // char *_imp;
2645 // }
2646 MethodTy = llvm::StructType::get(SelectorPtrTy,
2647 Int8PtrTy,
2648 Int8PtrTy,
2649 NULL);
2650 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002651
2652 // struct _objc_cache *
2653 CacheTy = llvm::OpaqueType::get();
2654 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
2655 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002656
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002657 // Property manipulation functions.
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002658
2659 QualType IdType = Ctx.getObjCIdType();
2660 QualType SelType = Ctx.getObjCSelType();
2661 llvm::SmallVector<QualType,16> Params;
2662 const llvm::FunctionType *FTy;
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002663
2664 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002665 Params.push_back(IdType);
2666 Params.push_back(SelType);
2667 Params.push_back(Ctx.LongTy);
2668 Params.push_back(Ctx.BoolTy);
2669 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params),
2670 false);
2671 GetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002672
2673 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
2674 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002675 Params.push_back(IdType);
2676 Params.push_back(SelType);
2677 Params.push_back(Ctx.LongTy);
2678 Params.push_back(IdType);
2679 Params.push_back(Ctx.BoolTy);
2680 Params.push_back(Ctx.BoolTy);
2681 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
2682 SetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
2683
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002684 // Enumeration mutation.
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002685
2686 // void objc_enumerationMutation (id)
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002687 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002688 Params.push_back(IdType);
2689 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
2690 EnumerationMutationFn = CGM.CreateRuntimeFunction(FTy,
2691 "objc_enumerationMutation");
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002692
2693 // gc's API
2694 // id objc_read_weak (id *)
2695 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002696 Params.push_back(Ctx.getPointerType(IdType));
2697 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
2698 GcReadWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
2699
2700 // id objc_assign_weak (id, id *)
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002701 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002702 Params.push_back(IdType);
2703 Params.push_back(Ctx.getPointerType(IdType));
2704
2705 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
2706 GcAssignWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
2707 GcAssignGlobalFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
2708 GcAssignIvarFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
2709 GcAssignStrongCastFn =
2710 CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002711}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002712
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002713ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
2714 : ObjCCommonTypesHelper(cgm)
2715{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002716 // struct _objc_method_description {
2717 // SEL name;
2718 // char *types;
2719 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002720 MethodDescriptionTy =
2721 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002722 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002723 NULL);
2724 CGM.getModule().addTypeName("struct._objc_method_description",
2725 MethodDescriptionTy);
2726
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002727 // struct _objc_method_description_list {
2728 // int count;
2729 // struct _objc_method_description[1];
2730 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002731 MethodDescriptionListTy =
2732 llvm::StructType::get(IntTy,
2733 llvm::ArrayType::get(MethodDescriptionTy, 0),
2734 NULL);
2735 CGM.getModule().addTypeName("struct._objc_method_description_list",
2736 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002737
2738 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002739 MethodDescriptionListPtrTy =
2740 llvm::PointerType::getUnqual(MethodDescriptionListTy);
2741
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002742 // Protocol description structures
2743
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002744 // struct _objc_protocol_extension {
2745 // uint32_t size; // sizeof(struct _objc_protocol_extension)
2746 // struct _objc_method_description_list *optional_instance_methods;
2747 // struct _objc_method_description_list *optional_class_methods;
2748 // struct _objc_property_list *instance_properties;
2749 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002750 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002751 llvm::StructType::get(IntTy,
2752 MethodDescriptionListPtrTy,
2753 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002754 PropertyListPtrTy,
2755 NULL);
2756 CGM.getModule().addTypeName("struct._objc_protocol_extension",
2757 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002758
2759 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002760 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
2761
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002762 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002763
2764 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
2765 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2766
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002767 const llvm::Type *T =
2768 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
2769 LongTy,
2770 llvm::ArrayType::get(ProtocolTyHolder, 0),
2771 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002772 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
2773
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002774 // struct _objc_protocol {
2775 // struct _objc_protocol_extension *isa;
2776 // char *protocol_name;
2777 // struct _objc_protocol **_objc_protocol_list;
2778 // struct _objc_method_description_list *instance_methods;
2779 // struct _objc_method_description_list *class_methods;
2780 // }
2781 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002782 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002783 llvm::PointerType::getUnqual(ProtocolListTyHolder),
2784 MethodDescriptionListPtrTy,
2785 MethodDescriptionListPtrTy,
2786 NULL);
2787 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
2788
2789 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
2790 CGM.getModule().addTypeName("struct._objc_protocol_list",
2791 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002792 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002793 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
2794
2795 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002796 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002797 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002798
2799 // Class description structures
2800
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002801 // struct _objc_ivar {
2802 // char *ivar_name;
2803 // char *ivar_type;
2804 // int ivar_offset;
2805 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002806 IvarTy = llvm::StructType::get(Int8PtrTy,
2807 Int8PtrTy,
2808 IntTy,
2809 NULL);
2810 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
2811
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002812 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002813 IvarListTy = llvm::OpaqueType::get();
2814 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
2815 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
2816
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002817 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002818 MethodListTy = llvm::OpaqueType::get();
2819 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
2820 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
2821
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002822 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002823 ClassExtensionTy =
2824 llvm::StructType::get(IntTy,
2825 Int8PtrTy,
2826 PropertyListPtrTy,
2827 NULL);
2828 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
2829 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
2830
2831 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
2832
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002833 // struct _objc_class {
2834 // Class isa;
2835 // Class super_class;
2836 // char *name;
2837 // long version;
2838 // long info;
2839 // long instance_size;
2840 // struct _objc_ivar_list *ivars;
2841 // struct _objc_method_list *methods;
2842 // struct _objc_cache *cache;
2843 // struct _objc_protocol_list *protocols;
2844 // char *ivar_layout;
2845 // struct _objc_class_ext *ext;
2846 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002847 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
2848 llvm::PointerType::getUnqual(ClassTyHolder),
2849 Int8PtrTy,
2850 LongTy,
2851 LongTy,
2852 LongTy,
2853 IvarListPtrTy,
2854 MethodListPtrTy,
2855 CachePtrTy,
2856 ProtocolListPtrTy,
2857 Int8PtrTy,
2858 ClassExtensionPtrTy,
2859 NULL);
2860 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
2861
2862 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
2863 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
2864 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
2865
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002866 // struct _objc_category {
2867 // char *category_name;
2868 // char *class_name;
2869 // struct _objc_method_list *instance_method;
2870 // struct _objc_method_list *class_method;
2871 // uint32_t size; // sizeof(struct _objc_category)
2872 // struct _objc_property_list *instance_properties;// category's @property
2873 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002874 CategoryTy = llvm::StructType::get(Int8PtrTy,
2875 Int8PtrTy,
2876 MethodListPtrTy,
2877 MethodListPtrTy,
2878 ProtocolListPtrTy,
2879 IntTy,
2880 PropertyListPtrTy,
2881 NULL);
2882 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
2883
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002884 // Global metadata structures
2885
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002886 // struct _objc_symtab {
2887 // long sel_ref_cnt;
2888 // SEL *refs;
2889 // short cls_def_cnt;
2890 // short cat_def_cnt;
2891 // char *defs[cls_def_cnt + cat_def_cnt];
2892 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002893 SymtabTy = llvm::StructType::get(LongTy,
2894 SelectorPtrTy,
2895 ShortTy,
2896 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002897 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002898 NULL);
2899 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
2900 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
2901
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002902 // struct _objc_module {
2903 // long version;
2904 // long size; // sizeof(struct _objc_module)
2905 // char *name;
2906 // struct _objc_symtab* symtab;
2907 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002908 ModuleTy =
2909 llvm::StructType::get(LongTy,
2910 LongTy,
2911 Int8PtrTy,
2912 SymtabPtrTy,
2913 NULL);
2914 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002915
Daniel Dunbar49f66022008-09-24 03:38:44 +00002916 // Message send functions.
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002917
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002918 // id objc_msgSend (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002919 std::vector<const llvm::Type*> Params;
2920 Params.push_back(ObjectPtrTy);
2921 Params.push_back(SelectorPtrTy);
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002922 MessageSendFn =
2923 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2924 Params,
2925 true),
2926 "objc_msgSend");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002927
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002928 // id objc_msgSend_stret (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002929 Params.clear();
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002930 Params.push_back(ObjectPtrTy);
2931 Params.push_back(SelectorPtrTy);
2932 MessageSendStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002933 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2934 Params,
2935 true),
2936 "objc_msgSend_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002937
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002938 //
Daniel Dunbar5669e572008-10-17 03:24:53 +00002939 Params.clear();
2940 Params.push_back(ObjectPtrTy);
2941 Params.push_back(SelectorPtrTy);
2942 // FIXME: This should be long double on x86_64?
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002943 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
Daniel Dunbar5669e572008-10-17 03:24:53 +00002944 MessageSendFpretFn =
2945 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
2946 Params,
2947 true),
2948 "objc_msgSend_fpret");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002949
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002950 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002951 Params.clear();
2952 Params.push_back(SuperPtrTy);
2953 Params.push_back(SelectorPtrTy);
2954 MessageSendSuperFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002955 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2956 Params,
2957 true),
2958 "objc_msgSendSuper");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002959
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002960 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
2961 // SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002962 Params.clear();
2963 Params.push_back(Int8PtrTy);
2964 Params.push_back(SuperPtrTy);
2965 Params.push_back(SelectorPtrTy);
2966 MessageSendSuperStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002967 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2968 Params,
2969 true),
2970 "objc_msgSendSuper_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002971
2972 // There is no objc_msgSendSuper_fpret? How can that work?
2973 MessageSendSuperFpretFn = MessageSendSuperFn;
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002974
Anders Carlsson124526b2008-09-09 10:10:21 +00002975 // FIXME: This is the size of the setjmp buffer and should be
2976 // target specific. 18 is what's used on 32-bit X86.
2977 uint64_t SetJmpBufferSize = 18;
2978
2979 // Exceptions
2980 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00002981 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00002982
2983 ExceptionDataTy =
2984 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
2985 SetJmpBufferSize),
2986 StackPtrTy, NULL);
2987 CGM.getModule().addTypeName("struct._objc_exception_data",
2988 ExceptionDataTy);
2989
2990 Params.clear();
2991 Params.push_back(ObjectPtrTy);
2992 ExceptionThrowFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002993 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2994 Params,
2995 false),
2996 "objc_exception_throw");
Anders Carlsson124526b2008-09-09 10:10:21 +00002997
2998 Params.clear();
2999 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
3000 ExceptionTryEnterFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003001 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3002 Params,
3003 false),
3004 "objc_exception_try_enter");
Anders Carlsson124526b2008-09-09 10:10:21 +00003005 ExceptionTryExitFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003006 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3007 Params,
3008 false),
3009 "objc_exception_try_exit");
Anders Carlsson124526b2008-09-09 10:10:21 +00003010 ExceptionExtractFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003011 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3012 Params,
3013 false),
3014 "objc_exception_extract");
Anders Carlsson124526b2008-09-09 10:10:21 +00003015
3016 Params.clear();
3017 Params.push_back(ClassPtrTy);
3018 Params.push_back(ObjectPtrTy);
3019 ExceptionMatchFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003020 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3021 Params,
3022 false),
3023 "objc_exception_match");
Chris Lattner10cac6f2008-11-15 21:26:17 +00003024
3025 // synchronized APIs
3026 // void objc_sync_enter (id)
3027 Params.clear();
3028 Params.push_back(ObjectPtrTy);
3029 SyncEnterFn =
3030 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3031 Params,
3032 false),
3033 "objc_sync_enter");
3034 // void objc_sync_exit (id)
3035 SyncExitFn =
3036 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3037 Params,
3038 false),
3039 "objc_sync_exit");
3040
Anders Carlsson124526b2008-09-09 10:10:21 +00003041
3042 Params.clear();
3043 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
3044 SetJmpFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003045 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3046 Params,
3047 false),
3048 "_setjmp");
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00003049
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003050}
3051
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003052ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003053: ObjCCommonTypesHelper(cgm)
3054{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003055 // struct _method_list_t {
3056 // uint32_t entsize; // sizeof(struct _objc_method)
3057 // uint32_t method_count;
3058 // struct _objc_method method_list[method_count];
3059 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003060 MethodListnfABITy = llvm::StructType::get(IntTy,
3061 IntTy,
3062 llvm::ArrayType::get(MethodTy, 0),
3063 NULL);
3064 CGM.getModule().addTypeName("struct.__method_list_t",
3065 MethodListnfABITy);
3066 // struct method_list_t *
3067 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003068
3069 // struct _protocol_t {
3070 // id isa; // NULL
3071 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003072 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003073 // const struct method_list_t * const instance_methods;
3074 // const struct method_list_t * const class_methods;
3075 // const struct method_list_t *optionalInstanceMethods;
3076 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003077 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003078 // const uint32_t size; // sizeof(struct _protocol_t)
3079 // const uint32_t flags; // = 0
3080 // }
3081
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003082 // Holder for struct _protocol_list_t *
3083 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3084
3085 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
3086 Int8PtrTy,
3087 llvm::PointerType::getUnqual(
3088 ProtocolListTyHolder),
3089 MethodListnfABIPtrTy,
3090 MethodListnfABIPtrTy,
3091 MethodListnfABIPtrTy,
3092 MethodListnfABIPtrTy,
3093 PropertyListPtrTy,
3094 IntTy,
3095 IntTy,
3096 NULL);
3097 CGM.getModule().addTypeName("struct._protocol_t",
3098 ProtocolnfABITy);
3099
Fariborz Jahanianda320092009-01-29 19:24:30 +00003100 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003101 // long protocol_count; // Note, this is 32/64 bit
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003102 // struct _protocol_t[protocol_count];
3103 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003104 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3105 llvm::ArrayType::get(
3106 ProtocolnfABITy, 0),
3107 NULL);
3108 CGM.getModule().addTypeName("struct._objc_protocol_list",
3109 ProtocolListnfABITy);
3110
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003111 // struct _objc_protocol_list*
3112 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003113
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003114 // FIXME! Is this doing the right thing?
3115 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3116 ProtocolListnfABIPtrTy);
3117
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003118 // struct _ivar_t {
3119 // unsigned long int *offset; // pointer to ivar offset location
3120 // char *name;
3121 // char *type;
3122 // uint32_t alignment;
3123 // uint32_t size;
3124 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003125 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3126 Int8PtrTy,
3127 Int8PtrTy,
3128 IntTy,
3129 IntTy,
3130 NULL);
3131 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3132
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003133 // struct _ivar_list_t {
3134 // uint32 entsize; // sizeof(struct _ivar_t)
3135 // uint32 count;
3136 // struct _iver_t list[count];
3137 // }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003138 IvarListnfABITy = llvm::StructType::get(IntTy,
3139 IntTy,
3140 llvm::ArrayType::get(
3141 IvarnfABITy, 0),
3142 NULL);
3143 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3144
3145 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003146
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003147 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003148 // uint32_t const flags;
3149 // uint32_t const instanceStart;
3150 // uint32_t const instanceSize;
3151 // uint32_t const reserved; // only when building for 64bit targets
3152 // const uint8_t * const ivarLayout;
3153 // const char *const name;
3154 // const struct _method_list_t * const baseMethods;
3155 // const struct _objc_protocol_list *const baseProtocols;
3156 // const struct _ivar_list_t *const ivars;
3157 // const uint8_t * const weakIvarLayout;
3158 // const struct _prop_list_t * const properties;
3159 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003160
3161 // FIXME. Add 'reserved' field in 64bit abi mode!
3162 ClassRonfABITy = llvm::StructType::get(IntTy,
3163 IntTy,
3164 IntTy,
3165 Int8PtrTy,
3166 Int8PtrTy,
3167 MethodListnfABIPtrTy,
3168 ProtocolListnfABIPtrTy,
3169 IvarListnfABIPtrTy,
3170 Int8PtrTy,
3171 PropertyListPtrTy,
3172 NULL);
3173 CGM.getModule().addTypeName("struct._class_ro_t",
3174 ClassRonfABITy);
3175
3176 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3177 std::vector<const llvm::Type*> Params;
3178 Params.push_back(ObjectPtrTy);
3179 Params.push_back(SelectorPtrTy);
3180 ImpnfABITy = llvm::PointerType::getUnqual(
3181 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3182
3183 // struct _class_t {
3184 // struct _class_t *isa;
3185 // struct _class_t * const superclass;
3186 // void *cache;
3187 // IMP *vtable;
3188 // struct class_ro_t *ro;
3189 // }
3190
3191 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3192 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3193 llvm::PointerType::getUnqual(ClassTyHolder),
3194 CachePtrTy,
3195 llvm::PointerType::getUnqual(ImpnfABITy),
3196 llvm::PointerType::getUnqual(
3197 ClassRonfABITy),
3198 NULL);
3199 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3200
3201 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3202 ClassnfABITy);
3203
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003204 // LLVM for struct _class_t *
3205 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3206
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003207 // struct _category_t {
3208 // const char * const name;
3209 // struct _class_t *const cls;
3210 // const struct _method_list_t * const instance_methods;
3211 // const struct _method_list_t * const class_methods;
3212 // const struct _protocol_list_t * const protocols;
3213 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003214 // }
3215 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003216 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003217 MethodListnfABIPtrTy,
3218 MethodListnfABIPtrTy,
3219 ProtocolListnfABIPtrTy,
3220 PropertyListPtrTy,
3221 NULL);
3222 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003223
3224 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003225 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3226 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003227
3228 // MessageRefTy - LLVM for:
3229 // struct _message_ref_t {
3230 // IMP messenger;
3231 // SEL name;
3232 // };
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003233
3234 // First the clang type for struct _message_ref_t
3235 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3236 SourceLocation(),
3237 &Ctx.Idents.get("_message_ref_t"));
3238 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3239 Ctx.VoidPtrTy, 0, false));
3240 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3241 Ctx.getObjCSelType(), 0, false));
3242 RD->completeDefinition(Ctx);
3243
3244 MessageRefCTy = Ctx.getTagDeclType(RD);
3245 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
3246 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003247
3248 // MessageRefPtrTy - LLVM for struct _message_ref_t*
3249 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
3250
3251 // SuperMessageRefTy - LLVM for:
3252 // struct _super_message_ref_t {
3253 // SUPER_IMP messenger;
3254 // SEL name;
3255 // };
3256 SuperMessageRefTy = llvm::StructType::get(ImpnfABITy,
3257 SelectorPtrTy,
3258 NULL);
3259 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
3260
3261 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
3262 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
3263
3264 // id objc_msgSend_fixup (id, struct message_ref_t*, ...)
3265 Params.clear();
3266 Params.push_back(ObjectPtrTy);
3267 Params.push_back(MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00003268 MessengerTy = llvm::FunctionType::get(ObjectPtrTy,
3269 Params,
3270 true);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003271 MessageSendFixupFn =
Fariborz Jahanianef163782009-02-05 01:13:09 +00003272 CGM.CreateRuntimeFunction(MessengerTy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003273 "objc_msgSend_fixup");
3274
3275 // id objc_msgSend_fpret_fixup (id, struct message_ref_t*, ...)
3276 MessageSendFpretFixupFn =
3277 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3278 Params,
3279 true),
3280 "objc_msgSend_fpret_fixup");
3281
3282 // id objc_msgSend_stret_fixup (id, struct message_ref_t*, ...)
3283 MessageSendStretFixupFn =
3284 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3285 Params,
3286 true),
3287 "objc_msgSend_stret_fixup");
3288
3289 // id objc_msgSendId_fixup (id, struct message_ref_t*, ...)
3290 MessageSendIdFixupFn =
3291 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3292 Params,
3293 true),
3294 "objc_msgSendId_fixup");
3295
3296
3297 // id objc_msgSendId_stret_fixup (id, struct message_ref_t*, ...)
3298 MessageSendIdStretFixupFn =
3299 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3300 Params,
3301 true),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003302 "objc_msgSendId_stret_fixup");
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003303
3304 // id objc_msgSendSuper2_fixup (struct objc_super *,
3305 // struct _super_message_ref_t*, ...)
3306 Params.clear();
3307 Params.push_back(SuperPtrTy);
3308 Params.push_back(SuperMessageRefPtrTy);
3309 MessageSendSuper2FixupFn =
3310 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3311 Params,
3312 true),
3313 "objc_msgSendSuper2_fixup");
3314
3315
3316 // id objc_msgSendSuper2_stret_fixup (struct objc_super *,
3317 // struct _super_message_ref_t*, ...)
3318 MessageSendSuper2StretFixupFn =
3319 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3320 Params,
3321 true),
3322 "objc_msgSendSuper2_stret_fixup");
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003323
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003324}
3325
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003326llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
3327 FinishNonFragileABIModule();
3328
3329 return NULL;
3330}
3331
3332void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
3333 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003334
3335 // Build list of all implemented classe addresses in array
3336 // L_OBJC_LABEL_CLASS_$.
3337 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$
3338 // list of 'nonlazy' implementations (defined as those with a +load{}
3339 // method!!).
3340 unsigned NumClasses = DefinedClasses.size();
3341 if (NumClasses) {
3342 std::vector<llvm::Constant*> Symbols(NumClasses);
3343 for (unsigned i=0; i<NumClasses; i++)
3344 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
3345 ObjCTypes.Int8PtrTy);
3346 llvm::Constant* Init =
3347 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3348 NumClasses),
3349 Symbols);
3350
3351 llvm::GlobalVariable *GV =
3352 new llvm::GlobalVariable(Init->getType(), false,
3353 llvm::GlobalValue::InternalLinkage,
3354 Init,
3355 "\01L_OBJC_LABEL_CLASS_$",
3356 &CGM.getModule());
3357 GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip");
3358 UsedGlobals.push_back(GV);
3359 }
3360
3361 // Build list of all implemented category addresses in array
3362 // L_OBJC_LABEL_CATEGORY_$.
3363 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$
3364 // list of 'nonlazy' category implementations (defined as those with a +load{}
3365 // method!!).
3366 unsigned NumCategory = DefinedCategories.size();
3367 if (NumCategory) {
3368 std::vector<llvm::Constant*> Symbols(NumCategory);
3369 for (unsigned i=0; i<NumCategory; i++)
3370 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i],
3371 ObjCTypes.Int8PtrTy);
3372 llvm::Constant* Init =
3373 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3374 NumCategory),
3375 Symbols);
3376
3377 llvm::GlobalVariable *GV =
3378 new llvm::GlobalVariable(Init->getType(), false,
3379 llvm::GlobalValue::InternalLinkage,
3380 Init,
3381 "\01L_OBJC_LABEL_CATEGORY_$",
3382 &CGM.getModule());
3383 GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip");
3384 UsedGlobals.push_back(GV);
3385 }
3386
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00003387 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
3388 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
3389 std::vector<llvm::Constant*> Values(2);
3390 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3391 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3392 llvm::Constant* Init = llvm::ConstantArray::get(
3393 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
3394 Values);
3395 llvm::GlobalVariable *IMGV =
3396 new llvm::GlobalVariable(Init->getType(), false,
3397 llvm::GlobalValue::InternalLinkage,
3398 Init,
3399 "\01L_OBJC_IMAGE_INFO",
3400 &CGM.getModule());
3401 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
3402 UsedGlobals.push_back(IMGV);
3403
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003404 std::vector<llvm::Constant*> Used;
3405 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
3406 e = UsedGlobals.end(); i != e; ++i) {
3407 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
3408 }
3409
3410 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
3411 llvm::GlobalValue *GV =
3412 new llvm::GlobalVariable(AT, false,
3413 llvm::GlobalValue::AppendingLinkage,
3414 llvm::ConstantArray::get(AT, Used),
3415 "llvm.used",
3416 &CGM.getModule());
3417
3418 GV->setSection("llvm.metadata");
3419
3420}
3421
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003422// Metadata flags
3423enum MetaDataDlags {
3424 CLS = 0x0,
3425 CLS_META = 0x1,
3426 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003427 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003428 CLS_EXCEPTION = 0x20
3429};
3430/// BuildClassRoTInitializer - generate meta-data for:
3431/// struct _class_ro_t {
3432/// uint32_t const flags;
3433/// uint32_t const instanceStart;
3434/// uint32_t const instanceSize;
3435/// uint32_t const reserved; // only when building for 64bit targets
3436/// const uint8_t * const ivarLayout;
3437/// const char *const name;
3438/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00003439/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003440/// const struct _ivar_list_t *const ivars;
3441/// const uint8_t * const weakIvarLayout;
3442/// const struct _prop_list_t * const properties;
3443/// }
3444///
3445llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
3446 unsigned flags,
3447 unsigned InstanceStart,
3448 unsigned InstanceSize,
3449 const ObjCImplementationDecl *ID) {
3450 std::string ClassName = ID->getNameAsString();
3451 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
3452 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
3453 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
3454 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
3455 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianda320092009-01-29 19:24:30 +00003456 // FIXME. ivarLayout is currently null!
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003457 Values[ 3] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3458 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003459 // const struct _method_list_t * const baseMethods;
3460 std::vector<llvm::Constant*> Methods;
3461 std::string MethodListName("\01l_OBJC_$_");
3462 if (flags & CLS_META) {
3463 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
3464 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
3465 e = ID->classmeth_end(); i != e; ++i) {
3466 // Class methods should always be defined.
3467 Methods.push_back(GetMethodConstant(*i));
3468 }
3469 } else {
3470 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
3471 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
3472 e = ID->instmeth_end(); i != e; ++i) {
3473 // Instance methods should always be defined.
3474 Methods.push_back(GetMethodConstant(*i));
3475 }
Fariborz Jahanian939abce2009-01-28 22:46:49 +00003476 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
3477 e = ID->propimpl_end(); i != e; ++i) {
3478 ObjCPropertyImplDecl *PID = *i;
3479
3480 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
3481 ObjCPropertyDecl *PD = PID->getPropertyDecl();
3482
3483 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
3484 if (llvm::Constant *C = GetMethodConstant(MD))
3485 Methods.push_back(C);
3486 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
3487 if (llvm::Constant *C = GetMethodConstant(MD))
3488 Methods.push_back(C);
3489 }
3490 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003491 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003492 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003493 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003494
3495 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3496 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
3497 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
3498 + OID->getNameAsString(),
3499 OID->protocol_begin(),
3500 OID->protocol_end());
3501
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003502 if (flags & CLS_META)
3503 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3504 else
3505 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003506 // FIXME. weakIvarLayout is currently null.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003507 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003508 if (flags & CLS_META)
3509 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3510 else
3511 Values[ 9] =
3512 EmitPropertyList(
3513 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
3514 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003515 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
3516 Values);
3517 llvm::GlobalVariable *CLASS_RO_GV =
3518 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
3519 llvm::GlobalValue::InternalLinkage,
3520 Init,
3521 (flags & CLS_META) ?
3522 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
3523 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
3524 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003525 CLASS_RO_GV->setAlignment(
3526 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003527 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003528 UsedGlobals.push_back(CLASS_RO_GV);
3529 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003530
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003531}
3532
3533/// BuildClassMetaData - This routine defines that to-level meta-data
3534/// for the given ClassName for:
3535/// struct _class_t {
3536/// struct _class_t *isa;
3537/// struct _class_t * const superclass;
3538/// void *cache;
3539/// IMP *vtable;
3540/// struct class_ro_t *ro;
3541/// }
3542///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003543llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
3544 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003545 llvm::Constant *IsAGV,
3546 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003547 llvm::Constant *ClassRoGV,
3548 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003549 std::vector<llvm::Constant*> Values(5);
3550 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003551 Values[1] = SuperClassGV
3552 ? SuperClassGV
3553 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003554 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
3555 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
3556 Values[4] = ClassRoGV; // &CLASS_RO_GV
3557 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
3558 Values);
3559 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(ClassName);
3560 if (GV)
3561 GV->setInitializer(Init);
3562 else
3563 GV =
3564 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3565 llvm::GlobalValue::ExternalLinkage,
3566 Init,
3567 ClassName,
3568 &CGM.getModule());
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00003569 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003570 GV->setAlignment(
3571 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003572 if (HiddenVisibility)
3573 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003574 UsedGlobals.push_back(GV);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003575 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003576}
3577
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003578void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
3579 std::string ClassName = ID->getNameAsString();
3580 if (!ObjCEmptyCacheVar) {
3581 ObjCEmptyCacheVar = new llvm::GlobalVariable(
3582 ObjCTypes.CachePtrTy,
3583 false,
3584 llvm::GlobalValue::ExternalLinkage,
3585 0,
Fariborz Jahanian07da3672009-02-03 17:34:34 +00003586 "\01__objc_empty_cache",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003587 &CGM.getModule());
3588 UsedGlobals.push_back(ObjCEmptyCacheVar);
3589
3590 ObjCEmptyVtableVar = new llvm::GlobalVariable(
3591 llvm::PointerType::getUnqual(
3592 ObjCTypes.ImpnfABITy),
3593 false,
3594 llvm::GlobalValue::ExternalLinkage,
3595 0,
Fariborz Jahanian07da3672009-02-03 17:34:34 +00003596 "\01__objc_empty_vtable",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003597 &CGM.getModule());
3598 UsedGlobals.push_back(ObjCEmptyVtableVar);
3599 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003600 assert(ID->getClassInterface() &&
3601 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003602 uint32_t InstanceStart =
3603 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
3604 uint32_t InstanceSize = InstanceStart;
3605 uint32_t flags = CLS_META;
3606 std::string ObjCMetaClassName("\01_OBJC_METACLASS_$_");
3607 std::string ObjCClassName("\01_OBJC_CLASS_$_");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003608
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003609 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003610
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003611 bool classIsHidden = IsClassHidden(ID->getClassInterface());
3612 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003613 flags |= OBJC2_CLS_HIDDEN;
3614 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003615 // class is root
3616 flags |= CLS_ROOT;
3617 std::string SuperClassName = ObjCClassName + ClassName;
3618 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003619 if (!SuperClassGV) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003620 SuperClassGV =
3621 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3622 llvm::GlobalValue::ExternalLinkage,
3623 0,
3624 SuperClassName,
3625 &CGM.getModule());
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003626 UsedGlobals.push_back(SuperClassGV);
3627 }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003628 std::string IsAClassName = ObjCMetaClassName + ClassName;
3629 IsAGV = CGM.getModule().getGlobalVariable(IsAClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003630 if (!IsAGV) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003631 IsAGV =
3632 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3633 llvm::GlobalValue::ExternalLinkage,
3634 0,
3635 IsAClassName,
3636 &CGM.getModule());
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003637 UsedGlobals.push_back(IsAGV);
3638 }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003639 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003640 // Has a root. Current class is not a root.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003641 std::string RootClassName =
3642 ID->getClassInterface()->getSuperClass()->getNameAsString();
3643 std::string SuperClassName = ObjCMetaClassName + RootClassName;
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 IsAGV = SuperClassGV;
3655 }
3656 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
3657 InstanceStart,
3658 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003659 std::string TClassName = ObjCMetaClassName + ClassName;
3660 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003661 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
3662 classIsHidden);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003663
3664 // Metadata for the class
3665 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003666 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003667 flags |= OBJC2_CLS_HIDDEN;
3668 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003669 flags |= CLS_ROOT;
3670 SuperClassGV = 0;
3671 }
3672 else {
3673 // Has a root. Current class is not a root.
3674 std::string RootClassName =
3675 ID->getClassInterface()->getSuperClass()->getNameAsString();
3676 std::string SuperClassName = ObjCClassName + RootClassName;
3677 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003678 if (!SuperClassGV) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003679 SuperClassGV =
3680 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3681 llvm::GlobalValue::ExternalLinkage,
3682 0,
3683 SuperClassName,
3684 &CGM.getModule());
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003685 UsedGlobals.push_back(SuperClassGV);
3686 }
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003687 }
3688
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003689 InstanceStart = InstanceSize = 0;
3690 if (ObjCInterfaceDecl *OID =
3691 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface())) {
3692 // FIXME. Share this with the one in EmitIvarList.
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003693 const llvm::Type *InterfaceTy =
3694 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
3695 const llvm::StructLayout *Layout =
3696 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003697
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003698 RecordDecl::field_iterator firstField, lastField;
3699 const RecordDecl *RD = GetFirstIvarInRecord(OID, firstField, lastField);
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003700
3701 for (RecordDecl::field_iterator e = RD->field_end(),
3702 ifield = firstField; ifield != e; ++ifield)
3703 lastField = ifield;
3704
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003705 if (lastField != RD->field_end()) {
3706 FieldDecl *Field = *lastField;
3707 const llvm::Type *FieldTy =
3708 CGM.getTypes().ConvertTypeForMem(Field->getType());
3709 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3710 InstanceSize = Layout->getElementOffset(
3711 CGM.getTypes().getLLVMFieldNo(Field)) +
3712 Size;
3713 if (firstField == RD->field_end())
3714 InstanceStart = InstanceSize;
3715 else
3716 InstanceStart = Layout->getElementOffset(CGM.getTypes().
3717 getLLVMFieldNo(*firstField));
3718 }
3719 }
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003720 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003721 InstanceStart,
3722 InstanceSize,
3723 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003724
3725 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003726 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003727 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
3728 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003729 DefinedClasses.push_back(ClassMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003730}
3731
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00003732/// GenerateProtocolRef - This routine is called to generate code for
3733/// a protocol reference expression; as in:
3734/// @code
3735/// @protocol(Proto1);
3736/// @endcode
3737/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
3738/// which will hold address of the protocol meta-data.
3739///
3740llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
3741 const ObjCProtocolDecl *PD) {
3742
3743 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
3744 ObjCTypes.ExternalProtocolPtrTy);
3745
3746 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
3747 ProtocolName += PD->getNameAsCString();
3748
3749 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
3750 if (PTGV)
3751 return Builder.CreateLoad(PTGV, false, "tmp");
3752 PTGV = new llvm::GlobalVariable(
3753 Init->getType(), false,
3754 llvm::GlobalValue::WeakLinkage,
3755 Init,
3756 ProtocolName,
3757 &CGM.getModule());
3758 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
3759 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
3760 UsedGlobals.push_back(PTGV);
3761 return Builder.CreateLoad(PTGV, false, "tmp");
3762}
3763
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003764/// GenerateCategory - Build metadata for a category implementation.
3765/// struct _category_t {
3766/// const char * const name;
3767/// struct _class_t *const cls;
3768/// const struct _method_list_t * const instance_methods;
3769/// const struct _method_list_t * const class_methods;
3770/// const struct _protocol_list_t * const protocols;
3771/// const struct _prop_list_t * const properties;
3772/// }
3773///
3774void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD)
3775{
3776 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003777 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
3778 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003779 "_$_" + OCD->getNameAsString());
3780 std::string ExtClassName("\01_OBJC_CLASS_$_" + Interface->getNameAsString());
3781
3782 std::vector<llvm::Constant*> Values(6);
3783 Values[0] = GetClassName(OCD->getIdentifier());
3784 // meta-class entry symbol
3785 llvm::GlobalVariable *ClassGV =
3786 CGM.getModule().getGlobalVariable(ExtClassName);
3787 if (!ClassGV)
3788 ClassGV =
3789 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3790 llvm::GlobalValue::ExternalLinkage,
3791 0,
3792 ExtClassName,
3793 &CGM.getModule());
3794 UsedGlobals.push_back(ClassGV);
3795 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003796 std::vector<llvm::Constant*> Methods;
3797 std::string MethodListName(Prefix);
3798 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
3799 "_$_" + OCD->getNameAsString();
3800
3801 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
3802 e = OCD->instmeth_end(); i != e; ++i) {
3803 // Instance methods should always be defined.
3804 Methods.push_back(GetMethodConstant(*i));
3805 }
3806
3807 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003808 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003809 Methods);
3810
3811 MethodListName = Prefix;
3812 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
3813 OCD->getNameAsString();
3814 Methods.clear();
3815 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
3816 e = OCD->classmeth_end(); i != e; ++i) {
3817 // Class methods should always be defined.
3818 Methods.push_back(GetMethodConstant(*i));
3819 }
3820
3821 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003822 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003823 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003824 const ObjCCategoryDecl *Category =
3825 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanianeb732132009-01-29 23:23:06 +00003826 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
3827 + Interface->getNameAsString() + "_$_"
3828 + Category->getNameAsString(),
3829 Category->protocol_begin(),
3830 Category->protocol_end());
3831
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003832 std::string ExtName(Interface->getNameAsString() + "_$_" +
3833 OCD->getNameAsString());
3834 Values[5] =
3835 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
3836 OCD, Category, ObjCTypes);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003837 llvm::Constant *Init =
3838 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
3839 Values);
3840 llvm::GlobalVariable *GCATV
3841 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
3842 false,
3843 llvm::GlobalValue::InternalLinkage,
3844 Init,
3845 ExtCatName,
3846 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003847 GCATV->setAlignment(
3848 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003849 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003850 UsedGlobals.push_back(GCATV);
3851 DefinedCategories.push_back(GCATV);
3852}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003853
3854/// GetMethodConstant - Return a struct objc_method constant for the
3855/// given method if it has been defined. The result is null if the
3856/// method has not been defined. The return value has type MethodPtrTy.
3857llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
3858 const ObjCMethodDecl *MD) {
3859 // FIXME: Use DenseMap::lookup
3860 llvm::Function *Fn = MethodDefinitions[MD];
3861 if (!Fn)
3862 return 0;
3863
3864 std::vector<llvm::Constant*> Method(3);
3865 Method[0] =
3866 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
3867 ObjCTypes.SelectorPtrTy);
3868 Method[1] = GetMethodVarType(MD);
3869 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
3870 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
3871}
3872
3873/// EmitMethodList - Build meta-data for method declarations
3874/// struct _method_list_t {
3875/// uint32_t entsize; // sizeof(struct _objc_method)
3876/// uint32_t method_count;
3877/// struct _objc_method method_list[method_count];
3878/// }
3879///
3880llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
3881 const std::string &Name,
3882 const char *Section,
3883 const ConstantVector &Methods) {
3884 // Return null for empty list.
3885 if (Methods.empty())
3886 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
3887
3888 std::vector<llvm::Constant*> Values(3);
3889 // sizeof(struct _objc_method)
3890 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy);
3891 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3892 // method_count
3893 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
3894 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
3895 Methods.size());
3896 Values[2] = llvm::ConstantArray::get(AT, Methods);
3897 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
3898
3899 llvm::GlobalVariable *GV =
3900 new llvm::GlobalVariable(Init->getType(), false,
3901 llvm::GlobalValue::InternalLinkage,
3902 Init,
3903 Name,
3904 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003905 GV->setAlignment(
3906 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003907 GV->setSection(Section);
3908 UsedGlobals.push_back(GV);
3909 return llvm::ConstantExpr::getBitCast(GV,
3910 ObjCTypes.MethodListnfABIPtrTy);
3911}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003912
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003913llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
3914 const ObjCImplementationDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003915 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003916 unsigned long int Offset) {
3917
3918 std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.'
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003919 + Ivar->getNameAsString());
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003920 llvm::Constant *Init = llvm::ConstantInt::get(ObjCTypes.LongTy, Offset);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003921
3922 llvm::GlobalVariable *IvarOffsetGV =
3923 CGM.getModule().getGlobalVariable(ExternalName);
3924 if (IvarOffsetGV) {
3925 // ivar offset symbol already built due to user code referencing it.
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003926 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003927 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003928 IvarOffsetGV->setInitializer(Init);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003929 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003930 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003931 return IvarOffsetGV;
3932 }
3933
3934 IvarOffsetGV =
3935 new llvm::GlobalVariable(Init->getType(),
3936 false,
3937 llvm::GlobalValue::ExternalLinkage,
3938 Init,
3939 ExternalName,
3940 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003941 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003942 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003943 // @private and @package have hidden visibility.
3944 bool globalVisibility = (Ivar->getAccessControl() == ObjCIvarDecl::Public ||
3945 Ivar->getAccessControl() == ObjCIvarDecl::Protected);
3946 if (!globalVisibility)
3947 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003948 else
3949 if (const ObjCInterfaceDecl *OID = ID->getClassInterface())
3950 if (IsClassHidden(OID))
3951 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
3952
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003953 IvarOffsetGV->setSection("__DATA, __objc_const");
3954 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003955 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003956}
3957
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003958/// EmitIvarList - Emit the ivar list for the given
3959/// implementation. If ForClass is true the list of class ivars
3960/// (i.e. metaclass ivars) is emitted, otherwise the list of
3961/// interface ivars will be emitted. The return value has type
3962/// IvarListnfABIPtrTy.
3963/// struct _ivar_t {
3964/// unsigned long int *offset; // pointer to ivar offset location
3965/// char *name;
3966/// char *type;
3967/// uint32_t alignment;
3968/// uint32_t size;
3969/// }
3970/// struct _ivar_list_t {
3971/// uint32 entsize; // sizeof(struct _ivar_t)
3972/// uint32 count;
3973/// struct _iver_t list[count];
3974/// }
3975///
3976llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
3977 const ObjCImplementationDecl *ID) {
3978
3979 std::vector<llvm::Constant*> Ivars, Ivar(5);
3980
3981 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3982 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
3983
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003984 // FIXME. Consolidate this with similar code in GenerateClass.
3985 const llvm::Type *InterfaceTy =
3986 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(
3987 const_cast<ObjCInterfaceDecl*>(OID)));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003988 const llvm::StructLayout *Layout =
3989 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003990
3991 RecordDecl::field_iterator i,p;
3992 const RecordDecl *RD = GetFirstIvarInRecord(OID, i,p);
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003993 ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin();
3994
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003995 for (RecordDecl::field_iterator e = RD->field_end(); i != e; ++i) {
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003996 FieldDecl *Field = *i;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003997 unsigned long offset = Layout->getElementOffset(CGM.getTypes().
3998 getLLVMFieldNo(Field));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003999 const ObjCIvarDecl *ivarDecl = *I++;
4000 Ivar[0] = EmitIvarOffsetVar(ID, ivarDecl, offset);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004001 if (Field->getIdentifier())
4002 Ivar[1] = GetMethodVarName(Field->getIdentifier());
4003 else
4004 Ivar[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4005 std::string TypeStr;
4006 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4007 Ivar[2] = GetMethodVarType(TypeStr);
4008 const llvm::Type *FieldTy =
4009 CGM.getTypes().ConvertTypeForMem(Field->getType());
4010 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
4011 unsigned Align = CGM.getContext().getPreferredTypeAlign(
4012 Field->getType().getTypePtr()) >> 3;
4013 Align = llvm::Log2_32(Align);
4014 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Fariborz Jahanian07236ba2009-01-27 22:27:56 +00004015 // NOTE. Size of a bitfield does not match gcc's, because of the way
4016 // bitfields are treated special in each. But I am told that 'size'
4017 // for bitfield ivars is ignored by the runtime so it does not matter.
4018 // (even if it matters, some day, there is enough info. to get the bitfield
4019 // right!
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004020 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4021 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
4022 }
4023 // Return null for empty list.
4024 if (Ivars.empty())
4025 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4026 std::vector<llvm::Constant*> Values(3);
4027 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy);
4028 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4029 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
4030 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
4031 Ivars.size());
4032 Values[2] = llvm::ConstantArray::get(AT, Ivars);
4033 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4034 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
4035 llvm::GlobalVariable *GV =
4036 new llvm::GlobalVariable(Init->getType(), false,
4037 llvm::GlobalValue::InternalLinkage,
4038 Init,
4039 Prefix + OID->getNameAsString(),
4040 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004041 GV->setAlignment(
4042 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004043 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004044
4045 UsedGlobals.push_back(GV);
4046 return llvm::ConstantExpr::getBitCast(GV,
4047 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004048}
4049
4050llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
4051 const ObjCProtocolDecl *PD) {
4052 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4053
4054 if (!Entry) {
4055 // We use the initializer as a marker of whether this is a forward
4056 // reference or not. At module finalization we add the empty
4057 // contents for protocols which were referenced but never defined.
4058 Entry =
4059 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
4060 llvm::GlobalValue::ExternalLinkage,
4061 0,
4062 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(),
4063 &CGM.getModule());
4064 Entry->setSection("__DATA,__datacoal_nt,coalesced");
4065 UsedGlobals.push_back(Entry);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004066 }
4067
4068 return Entry;
4069}
4070
4071/// GetOrEmitProtocol - Generate the protocol meta-data:
4072/// @code
4073/// struct _protocol_t {
4074/// id isa; // NULL
4075/// const char * const protocol_name;
4076/// const struct _protocol_list_t * protocol_list; // super protocols
4077/// const struct method_list_t * const instance_methods;
4078/// const struct method_list_t * const class_methods;
4079/// const struct method_list_t *optionalInstanceMethods;
4080/// const struct method_list_t *optionalClassMethods;
4081/// const struct _prop_list_t * properties;
4082/// const uint32_t size; // sizeof(struct _protocol_t)
4083/// const uint32_t flags; // = 0
4084/// }
4085/// @endcode
4086///
4087
4088llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
4089 const ObjCProtocolDecl *PD) {
4090 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4091
4092 // Early exit if a defining object has already been generated.
4093 if (Entry && Entry->hasInitializer())
4094 return Entry;
4095
4096 const char *ProtocolName = PD->getNameAsCString();
4097
4098 // Construct method lists.
4099 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
4100 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
4101 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
4102 e = PD->instmeth_end(); i != e; ++i) {
4103 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004104 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004105 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4106 OptInstanceMethods.push_back(C);
4107 } else {
4108 InstanceMethods.push_back(C);
4109 }
4110 }
4111
4112 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
4113 e = PD->classmeth_end(); i != e; ++i) {
4114 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004115 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004116 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4117 OptClassMethods.push_back(C);
4118 } else {
4119 ClassMethods.push_back(C);
4120 }
4121 }
4122
4123 std::vector<llvm::Constant*> Values(10);
4124 // isa is NULL
4125 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
4126 Values[1] = GetClassName(PD->getIdentifier());
4127 Values[2] = EmitProtocolList(
4128 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
4129 PD->protocol_begin(),
4130 PD->protocol_end());
4131
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004132 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004133 + PD->getNameAsString(),
4134 "__DATA, __objc_const",
4135 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004136 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004137 + PD->getNameAsString(),
4138 "__DATA, __objc_const",
4139 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004140 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004141 + PD->getNameAsString(),
4142 "__DATA, __objc_const",
4143 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004144 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004145 + PD->getNameAsString(),
4146 "__DATA, __objc_const",
4147 OptClassMethods);
4148 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
4149 0, PD, ObjCTypes);
4150 uint32_t Size =
4151 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy);
4152 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4153 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
4154 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
4155 Values);
4156
4157 if (Entry) {
4158 // Already created, fix the linkage and update the initializer.
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004159 Entry->setLinkage(llvm::GlobalValue::WeakLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004160 Entry->setInitializer(Init);
4161 } else {
4162 Entry =
4163 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004164 llvm::GlobalValue::WeakLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004165 Init,
4166 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName,
4167 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004168 Entry->setAlignment(
4169 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004170 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004171 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004172 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
4173
4174 // Use this protocol meta-data to build protocol list table in section
4175 // __DATA, __objc_protolist
4176 llvm::Type *ptype = llvm::PointerType::getUnqual(ObjCTypes.ProtocolnfABITy);
4177 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
4178 ptype, false,
4179 llvm::GlobalValue::WeakLinkage,
4180 Entry,
4181 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
4182 +ProtocolName,
4183 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004184 PTGV->setAlignment(
4185 CGM.getTargetData().getPrefTypeAlignment(ptype));
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004186 PTGV->setSection("__DATA, __objc_protolist");
4187 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4188 UsedGlobals.push_back(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004189 return Entry;
4190}
4191
4192/// EmitProtocolList - Generate protocol list meta-data:
4193/// @code
4194/// struct _protocol_list_t {
4195/// long protocol_count; // Note, this is 32/64 bit
4196/// struct _protocol_t[protocol_count];
4197/// }
4198/// @endcode
4199///
4200llvm::Constant *
4201CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4202 ObjCProtocolDecl::protocol_iterator begin,
4203 ObjCProtocolDecl::protocol_iterator end) {
4204 std::vector<llvm::Constant*> ProtocolRefs;
4205
4206 for (; begin != end; ++begin)
4207 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4208
4209 // Just return null for empty protocol lists
4210 if (ProtocolRefs.empty())
4211 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4212
4213 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4214 if (GV)
4215 return GV;
4216 // This list is null terminated.
4217 ProtocolRefs.push_back(llvm::Constant::getNullValue(
4218 ObjCTypes.ProtocolListnfABIPtrTy));
4219
4220 std::vector<llvm::Constant*> Values(2);
4221 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
4222 Values[1] =
4223 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolListnfABIPtrTy,
4224 ProtocolRefs.size()),
4225 ProtocolRefs);
4226
4227 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4228 GV = new llvm::GlobalVariable(Init->getType(), false,
4229 llvm::GlobalValue::InternalLinkage,
4230 Init,
4231 Name,
4232 &CGM.getModule());
4233 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004234 GV->setAlignment(
4235 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004236 UsedGlobals.push_back(GV);
4237 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
4238}
4239
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004240/// GetMethodDescriptionConstant - This routine build following meta-data:
4241/// struct _objc_method {
4242/// SEL _cmd;
4243/// char *method_type;
4244/// char *_imp;
4245/// }
4246
4247llvm::Constant *
4248CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4249 std::vector<llvm::Constant*> Desc(3);
4250 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4251 ObjCTypes.SelectorPtrTy);
4252 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004253 // Protocol methods have no implementation. So, this entry is always NULL.
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004254 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4255 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
4256}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004257
4258/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
4259/// This code gen. amounts to generating code for:
4260/// @code
4261/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
4262/// @encode
4263///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004264LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004265 CodeGen::CodeGenFunction &CGF,
4266 QualType ObjectTy,
4267 llvm::Value *BaseValue,
4268 const ObjCIvarDecl *Ivar,
4269 const FieldDecl *Field,
4270 unsigned CVRQualifiers) {
4271 assert(ObjectTy->isObjCInterfaceType() &&
4272 "CGObjCNonFragileABIMac::EmitObjCValueForIvar");
4273 NamedDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
4274 // NOTE. This name must match one in EmitIvarOffsetVar.
4275 // FIXME. Consolidate into one naming routine.
4276 std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.'
4277 + Ivar->getNameAsString());
4278
4279 llvm::GlobalVariable *IvarOffsetGV =
4280 CGM.getModule().getGlobalVariable(ExternalName);
4281 if (!IvarOffsetGV)
4282 IvarOffsetGV =
4283 new llvm::GlobalVariable(ObjCTypes.LongTy,
4284 false,
4285 llvm::GlobalValue::ExternalLinkage,
4286 0,
4287 ExternalName,
4288 &CGM.getModule());
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004289
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004290 // (char *) BaseValue
4291 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue,
4292 ObjCTypes.Int8PtrTy);
4293 llvm::Value *Offset = CGF.Builder.CreateLoad(IvarOffsetGV);
4294 // (char*)BaseValue + Offset_symbol
4295 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
4296 // (type *)((char*)BaseValue + Offset_symbol)
4297 const llvm::Type *IvarTy =
4298 CGM.getTypes().ConvertType(Ivar->getType());
4299 llvm::Type *ptrIvarTy = llvm::PointerType::getUnqual(IvarTy);
4300 V = CGF.Builder.CreateBitCast(V, ptrIvarTy);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004301
4302 if (Ivar->isBitField())
4303 return CGF.EmitLValueForBitfield(V, const_cast<FieldDecl *>(Field),
4304 CVRQualifiers);
4305
4306 LValue LV = LValue::MakeAddr(V,
4307 Ivar->getType().getCVRQualifiers()|CVRQualifiers);
4308 LValue::SetObjCIvar(LV, true);
4309 return LV;
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004310}
4311
Fariborz Jahanian46551122009-02-04 00:22:57 +00004312CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
4313 CodeGen::CodeGenFunction &CGF,
4314 QualType ResultType,
4315 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004316 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00004317 QualType Arg0Ty,
4318 bool IsSuper,
4319 const CallArgList &CallArgs) {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004320 // FIXME. Even though IsSuper is passes. This function doese not
4321 // handle calls to 'super' receivers.
4322 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004323 llvm::Value *Arg0 = Receiver;
4324 if (!IsSuper)
4325 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004326
4327 // Find the message function name.
Fariborz Jahanianef163782009-02-05 01:13:09 +00004328 // FIXME. This is too much work to get the ABI-specific result type
4329 // needed to find the message name.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004330 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType,
4331 llvm::SmallVector<QualType, 16>());
4332 llvm::Constant *Fn;
4333 std::string Name("\01l_");
4334 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004335#if 0
4336 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004337 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
4338 Fn = ObjCTypes.MessageSendIdStretFixupFn;
4339 // FIXME. Is there a better way of getting these names.
4340 // They are available in RuntimeFunctions vector pair.
4341 Name += "objc_msgSendId_stret_fixup";
4342 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004343 else
4344#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004345 if (IsSuper) {
4346 Fn = ObjCTypes.MessageSendSuper2StretFixupFn;
4347 Name += "objc_msgSendSuper2_stret_fixup";
4348 }
4349 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004350 {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004351 Fn = ObjCTypes.MessageSendStretFixupFn;
4352 Name += "objc_msgSend_stret_fixup";
4353 }
4354 }
Fariborz Jahanian1a6b3682009-02-05 19:35:43 +00004355 else if (ResultType->isFloatingType() &&
4356 // Selection of frret API only happens in 32bit nonfragile ABI.
4357 CGM.getTargetData().getTypePaddedSize(ObjCTypes.LongTy) == 4) {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004358 Fn = ObjCTypes.MessageSendFpretFixupFn;
4359 Name += "objc_msgSend_fpret_fixup";
4360 }
4361 else {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004362#if 0
4363// unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004364 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
4365 Fn = ObjCTypes.MessageSendIdFixupFn;
4366 Name += "objc_msgSendId_fixup";
4367 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004368 else
4369#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004370 if (IsSuper) {
4371 Fn = ObjCTypes.MessageSendSuper2FixupFn;
4372 Name += "objc_msgSendSuper2_fixup";
4373 }
4374 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004375 {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004376 Fn = ObjCTypes.MessageSendFixupFn;
4377 Name += "objc_msgSend_fixup";
4378 }
4379 }
4380 Name += '_';
4381 std::string SelName(Sel.getAsString());
4382 // Replace all ':' in selector name with '_' ouch!
4383 for(unsigned i = 0; i < SelName.size(); i++)
4384 if (SelName[i] == ':')
4385 SelName[i] = '_';
4386 Name += SelName;
4387 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
4388 if (!GV) {
4389 // Build messafe ref table entry.
4390 std::vector<llvm::Constant*> Values(2);
4391 Values[0] = Fn;
4392 Values[1] = GetMethodVarName(Sel);
4393 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4394 GV = new llvm::GlobalVariable(Init->getType(), false,
4395 llvm::GlobalValue::WeakLinkage,
4396 Init,
4397 Name,
4398 &CGM.getModule());
4399 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4400 GV->setAlignment(
4401 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.MessageRefTy));
4402 GV->setSection("__DATA, __objc_msgrefs, coalesced");
4403 UsedGlobals.push_back(GV);
4404 }
4405 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00004406
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004407 CallArgList ActualArgs;
4408 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
4409 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
4410 ObjCTypes.MessageRefCPtrTy));
4411 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahanianef163782009-02-05 01:13:09 +00004412 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs);
4413 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
4414 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanianef163782009-02-05 01:13:09 +00004415 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, false);
4416 Callee = CGF.Builder.CreateBitCast(Callee,
4417 llvm::PointerType::getUnqual(FTy));
4418 return CGF.EmitCall(FnInfo1, Callee, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00004419}
4420
4421/// Generate code for a message send expression in the nonfragile abi.
4422CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend(
4423 CodeGen::CodeGenFunction &CGF,
4424 QualType ResultType,
4425 Selector Sel,
4426 llvm::Value *Receiver,
4427 bool IsClassMessage,
4428 const CallArgList &CallArgs) {
Fariborz Jahanian46551122009-02-04 00:22:57 +00004429 return EmitMessageSend(CGF, ResultType, Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004430 Receiver, CGF.getContext().getObjCIdType(),
Fariborz Jahanian46551122009-02-04 00:22:57 +00004431 false, CallArgs);
4432}
4433
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004434llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004435 const ObjCInterfaceDecl *ID,
4436 bool IsSuper) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004437
4438 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
4439
4440 if (!Entry) {
4441 std::string ClassName("\01_OBJC_CLASS_$_" + ID->getNameAsString());
4442 llvm::GlobalVariable *ClassGV =
4443 CGM.getModule().getGlobalVariable(ClassName);
4444 if (!ClassGV) {
4445 ClassGV =
4446 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
4447 llvm::GlobalValue::ExternalLinkage,
4448 0,
4449 ClassName,
4450 &CGM.getModule());
4451 UsedGlobals.push_back(ClassGV);
4452 }
4453 Entry =
4454 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
4455 llvm::GlobalValue::InternalLinkage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004456 ClassGV,
4457 IsSuper ? "\01L_OBJC_CLASSLIST_SUP_REFS_$_"
4458 : "\01L_OBJC_CLASSLIST_REFERENCES_$_",
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004459 &CGM.getModule());
4460 Entry->setAlignment(
4461 CGM.getTargetData().getPrefTypeAlignment(
4462 ObjCTypes.ClassnfABIPtrTy));
4463
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004464 if (IsSuper)
4465 Entry->setSection("__OBJC,__objc_superrefs,regular,no_dead_strip");
4466 else
4467 Entry->setSection("__OBJC,__objc_classrefs,regular,no_dead_strip");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004468 UsedGlobals.push_back(Entry);
4469 }
4470
4471 return Builder.CreateLoad(Entry, false, "tmp");
4472}
4473
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004474/// EmitMetaClassRef - Return a Value * of the address of _class_t
4475/// meta-data
4476///
4477llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
4478 const ObjCInterfaceDecl *ID) {
4479 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
4480 if (Entry)
4481 return Builder.CreateLoad(Entry, false, "tmp");
4482
4483 std::string MetaClassName("\01_OBJC_METACLASS_$_" + ID->getNameAsString());
4484 llvm::GlobalVariable *MetaClassGV =
4485 CGM.getModule().getGlobalVariable(MetaClassName);
4486 if (!MetaClassGV) {
4487 MetaClassGV =
4488 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
4489 llvm::GlobalValue::ExternalLinkage,
4490 0,
4491 MetaClassName,
4492 &CGM.getModule());
4493 UsedGlobals.push_back(MetaClassGV);
4494 }
4495
4496 Entry =
4497 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
4498 llvm::GlobalValue::InternalLinkage,
4499 MetaClassGV,
4500 "\01L_OBJC_CLASSLIST_SUP_REFS_$_",
4501 &CGM.getModule());
4502 Entry->setAlignment(
4503 CGM.getTargetData().getPrefTypeAlignment(
4504 ObjCTypes.ClassnfABIPtrTy));
4505
4506 Entry->setSection("__OBJC,__objc_superrefs,regular,no_dead_strip");
4507 UsedGlobals.push_back(Entry);
4508
4509 return Builder.CreateLoad(Entry, false, "tmp");
4510}
4511
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004512/// GetClass - Return a reference to the class for the given interface
4513/// decl.
4514llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
4515 const ObjCInterfaceDecl *ID) {
4516 return EmitClassRef(Builder, ID);
4517}
4518
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004519/// Generates a message send where the super is the receiver. This is
4520/// a message send to self with special delivery semantics indicating
4521/// which class's method should be called.
4522CodeGen::RValue
4523CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
4524 QualType ResultType,
4525 Selector Sel,
4526 const ObjCInterfaceDecl *Class,
4527 llvm::Value *Receiver,
4528 bool IsClassMessage,
4529 const CodeGen::CallArgList &CallArgs) {
4530 // ...
4531 // Create and init a super structure; this is a (receiver, class)
4532 // pair we will pass to objc_msgSendSuper.
4533 llvm::Value *ObjCSuper =
4534 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
4535
4536 llvm::Value *ReceiverAsObject =
4537 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
4538 CGF.Builder.CreateStore(ReceiverAsObject,
4539 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
4540
4541 // If this is a class message the metaclass is passed as the target.
4542 llvm::Value *Target =
4543 IsClassMessage ? EmitMetaClassRef(CGF.Builder, Class)
4544 : EmitClassRef(CGF.Builder, Class, true);
4545
4546 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
4547 // and ObjCTypes types.
4548 const llvm::Type *ClassTy =
4549 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
4550 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
4551 CGF.Builder.CreateStore(Target,
4552 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
4553
4554 return EmitMessageSend(CGF, ResultType, Sel,
4555 ObjCSuper, ObjCTypes.SuperPtrCTy,
4556 true, CallArgs);
4557}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004558/* *** */
4559
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004560CodeGen::CGObjCRuntime *
4561CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00004562 return new CGObjCMac(CGM);
4563}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004564
4565CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004566CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004567 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004568}