blob: 2a0a4aac91bd7e7852267b4ef280c92b246726c0 [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
Daniel Dunbar948e2582009-02-15 07:36:20 +0000233 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
234 const llvm::Type *ProtocolnfABIPtrTy;
235
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000236 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
237 const llvm::StructType *ProtocolListnfABITy;
238
239 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
240 const llvm::Type *ProtocolListnfABIPtrTy;
241
242 // ClassnfABITy - LLVM for struct _class_t
243 const llvm::StructType *ClassnfABITy;
244
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000245 // ClassnfABIPtrTy - LLVM for struct _class_t*
246 const llvm::Type *ClassnfABIPtrTy;
247
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000248 // IvarnfABITy - LLVM for struct _ivar_t
249 const llvm::StructType *IvarnfABITy;
250
251 // IvarListnfABITy - LLVM for struct _ivar_list_t
252 const llvm::StructType *IvarListnfABITy;
253
254 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
255 const llvm::Type *IvarListnfABIPtrTy;
256
257 // ClassRonfABITy - LLVM for struct _class_ro_t
258 const llvm::StructType *ClassRonfABITy;
259
260 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
261 const llvm::Type *ImpnfABITy;
262
263 // CategorynfABITy - LLVM for struct _category_t
264 const llvm::StructType *CategorynfABITy;
265
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000266 // New types for nonfragile abi messaging.
267
268 // MessageRefTy - LLVM for:
269 // struct _message_ref_t {
270 // IMP messenger;
271 // SEL name;
272 // };
273 const llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000274 // MessageRefCTy - clang type for struct _message_ref_t
275 QualType MessageRefCTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000276
277 // MessageRefPtrTy - LLVM for struct _message_ref_t*
278 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000279 // MessageRefCPtrTy - clang type for struct _message_ref_t*
280 QualType MessageRefCPtrTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000281
Fariborz Jahanianef163782009-02-05 01:13:09 +0000282 // MessengerTy - Type of the messenger (shown as IMP above)
283 const llvm::FunctionType *MessengerTy;
284
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000285 // SuperMessageRefTy - LLVM for:
286 // struct _super_message_ref_t {
287 // SUPER_IMP messenger;
288 // SEL name;
289 // };
290 const llvm::StructType *SuperMessageRefTy;
291
292 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
293 const llvm::Type *SuperMessageRefPtrTy;
294
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000295 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
296 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000297};
298
299class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
300protected:
301 CodeGen::CodeGenModule &CGM;
302 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000303 unsigned ObjCABI;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000304
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000305 /// LazySymbols - Symbols to generate a lazy reference for. See
306 /// DefinedSymbols and FinishModule().
307 std::set<IdentifierInfo*> LazySymbols;
308
309 /// DefinedSymbols - External symbols which are defined by this
310 /// module. The symbols in this list and LazySymbols are used to add
311 /// special linker symbols which ensure that Objective-C modules are
312 /// linked properly.
313 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000314
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000315 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000316 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000317
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000318 /// MethodVarNames - uniqued method variable names.
319 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000320
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000321 /// MethodVarTypes - uniqued method type signatures. We have to use
322 /// a StringMap here because have no other unique reference.
323 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000324
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000325 /// MethodDefinitions - map of methods which have been defined in
326 /// this translation unit.
327 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000328
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000329 /// PropertyNames - uniqued method variable names.
330 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000331
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000332 /// ClassReferences - uniqued class references.
333 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000334
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000335 /// SelectorReferences - uniqued selector references.
336 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000337
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000338 /// Protocols - Protocols for which an objc_protocol structure has
339 /// been emitted. Forward declarations are handled by creating an
340 /// empty structure whose initializer is filled in when/if defined.
341 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000342
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000343 /// DefinedProtocols - Protocols which have actually been
344 /// defined. We should not need this, see FIXME in GenerateProtocol.
345 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000346
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000347 /// DefinedClasses - List of defined classes.
348 std::vector<llvm::GlobalValue*> DefinedClasses;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000349
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000350 /// DefinedCategories - List of defined categories.
351 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000352
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000353 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000354 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000355 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000356
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000357 /// GetNameForMethod - Return a name for the given method.
358 /// \param[out] NameOut - The return value.
359 void GetNameForMethod(const ObjCMethodDecl *OMD,
360 const ObjCContainerDecl *CD,
361 std::string &NameOut);
362
363 /// GetMethodVarName - Return a unique constant for the given
364 /// selector's name. The return value has type char *.
365 llvm::Constant *GetMethodVarName(Selector Sel);
366 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
367 llvm::Constant *GetMethodVarName(const std::string &Name);
368
369 /// GetMethodVarType - Return a unique constant for the given
370 /// selector's name. The return value has type char *.
371
372 // FIXME: This is a horrible name.
373 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
374 llvm::Constant *GetMethodVarType(const std::string &Name);
375
376 /// GetPropertyName - Return a unique constant for the given
377 /// name. The return value has type char *.
378 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
379
380 // FIXME: This can be dropped once string functions are unified.
381 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
382 const Decl *Container);
383
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000384 /// GetClassName - Return a unique constant for the given selector's
385 /// name. The return value has type char *.
386 llvm::Constant *GetClassName(IdentifierInfo *Ident);
387
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000388 const RecordDecl *GetFirstIvarInRecord(const ObjCInterfaceDecl *OID,
389 RecordDecl::field_iterator &FIV,
390 RecordDecl::field_iterator &PIV);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000391 /// EmitPropertyList - Emit the given property list. The return
392 /// value has type PropertyListPtrTy.
393 llvm::Constant *EmitPropertyList(const std::string &Name,
394 const Decl *Container,
395 const ObjCContainerDecl *OCD,
396 const ObjCCommonTypesHelper &ObjCTypes);
397
Fariborz Jahanianda320092009-01-29 19:24:30 +0000398 /// GetProtocolRef - Return a reference to the internal protocol
399 /// description, creating an empty one if it has not been
400 /// defined. The return value has type ProtocolPtrTy.
401 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
402
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000403public:
404 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
405 { }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000406
407 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000408
409 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
410 const ObjCContainerDecl *CD=0);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000411
412 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
413
414 /// GetOrEmitProtocol - Get the protocol object for the given
415 /// declaration, emitting it if necessary. The return value has type
416 /// ProtocolPtrTy.
417 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
418
419 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
420 /// object for the given declaration, emitting it if needed. These
421 /// forward references will be filled in with empty bodies if no
422 /// definition is seen. The return value has type ProtocolPtrTy.
423 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000424};
425
426class CGObjCMac : public CGObjCCommonMac {
427private:
428 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000429 /// EmitImageInfo - Emit the image info marker used to encode some module
430 /// level information.
431 void EmitImageInfo();
432
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000433 /// EmitModuleInfo - Another marker encoding module level
434 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000435 void EmitModuleInfo();
436
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000437 /// EmitModuleSymols - Emit module symbols, the list of defined
438 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000439 llvm::Constant *EmitModuleSymbols();
440
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000441 /// FinishModule - Write out global data structures at the end of
442 /// processing a translation unit.
443 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000444
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000445 /// EmitClassExtension - Generate the class extension structure used
446 /// to store the weak ivar layout and properties. The return value
447 /// has type ClassExtensionPtrTy.
448 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
449
450 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
451 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000452 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000453 const ObjCInterfaceDecl *ID);
454
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000455 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000456 QualType ResultType,
457 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000458 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000459 QualType Arg0Ty,
460 bool IsSuper,
461 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000462
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000463 /// EmitIvarList - Emit the ivar list for the given
464 /// implementation. If ForClass is true the list of class ivars
465 /// (i.e. metaclass ivars) is emitted, otherwise the list of
466 /// interface ivars will be emitted. The return value has type
467 /// IvarListPtrTy.
468 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000469 bool ForClass);
470
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000471 /// EmitMetaClass - Emit a forward reference to the class structure
472 /// for the metaclass of the given interface. The return value has
473 /// type ClassPtrTy.
474 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
475
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000476 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000477 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000478 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
479 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000480 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000481 const ConstantVector &Methods);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000482
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000483 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000484
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000485 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000486
487 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000488 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000489 llvm::Constant *EmitMethodList(const std::string &Name,
490 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000491 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000492
493 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000494 /// method declarations.
495 /// - TypeName: The name for the type containing the methods.
496 /// - IsProtocol: True iff these methods are for a protocol.
497 /// - ClassMethds: True iff these are class methods.
498 /// - Required: When true, only "required" methods are
499 /// listed. Similarly, when false only "optional" methods are
500 /// listed. For classes this should always be true.
501 /// - begin, end: The method list to output.
502 ///
503 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000504 llvm::Constant *EmitMethodDescList(const std::string &Name,
505 const char *Section,
506 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000507
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000508 /// GetOrEmitProtocol - Get the protocol object for the given
509 /// declaration, emitting it if necessary. The return value has type
510 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000511 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000512
513 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
514 /// object for the given declaration, emitting it if needed. These
515 /// forward references will be filled in with empty bodies if no
516 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000517 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000518
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000519 /// EmitProtocolExtension - Generate the protocol extension
520 /// structure used to store optional instance and class methods, and
521 /// protocol properties. The return value has type
522 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000523 llvm::Constant *
524 EmitProtocolExtension(const ObjCProtocolDecl *PD,
525 const ConstantVector &OptInstanceMethods,
526 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000527
528 /// EmitProtocolList - Generate the list of referenced
529 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc933702008-08-21 21:57:41 +0000530 llvm::Constant *EmitProtocolList(const std::string &Name,
531 ObjCProtocolDecl::protocol_iterator begin,
532 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000533
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000534 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
535 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000536 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000537
Fariborz Jahanianda320092009-01-29 19:24:30 +0000538 public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000539 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000540
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000541 virtual llvm::Function *ModuleInitFunction();
542
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000543 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000544 QualType ResultType,
545 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000546 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000547 bool IsClassMessage,
548 const CallArgList &CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000549
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000550 virtual CodeGen::RValue
551 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000552 QualType ResultType,
553 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000554 const ObjCInterfaceDecl *Class,
555 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000556 bool IsClassMessage,
557 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000558
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000559 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000560 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000561
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000562 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000563
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000564 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000565
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000566 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000567
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000568 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000569 const ObjCProtocolDecl *PD);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000570
Daniel Dunbar49f66022008-09-24 03:38:44 +0000571 virtual llvm::Function *GetPropertyGetFunction();
572 virtual llvm::Function *GetPropertySetFunction();
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000573 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000574
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000575 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
576 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000577 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
578 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000579 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000580 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000581 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
582 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000583 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
584 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000585 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
586 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000587 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
588 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +0000589
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000590 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
591 QualType ObjectTy,
592 llvm::Value *BaseValue,
593 const ObjCIvarDecl *Ivar,
594 const FieldDecl *Field,
595 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000596 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
597 ObjCInterfaceDecl *Interface,
598 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000599};
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000600
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000601class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000602private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000603 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000604 llvm::GlobalVariable* ObjCEmptyCacheVar;
605 llvm::GlobalVariable* ObjCEmptyVtableVar;
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +0000606 /// MetaClassReferences - uniqued meta class references.
607 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000608
609 /// FinishNonFragileABIModule - Write out global data structures at the end of
610 /// processing a translation unit.
611 void FinishNonFragileABIModule();
612
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000613 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
614 unsigned InstanceStart,
615 unsigned InstanceSize,
616 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +0000617 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
618 llvm::Constant *IsAGV,
619 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +0000620 llvm::Constant *ClassRoGV,
621 bool HiddenVisibility);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000622
623 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
624
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +0000625 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
626
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000627 /// EmitMethodList - Emit the method list for the given
628 /// implementation. The return value has type MethodListnfABITy.
629 llvm::Constant *EmitMethodList(const std::string &Name,
630 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +0000631 const ConstantVector &Methods);
632 /// EmitIvarList - Emit the ivar list for the given
633 /// implementation. If ForClass is true the list of class ivars
634 /// (i.e. metaclass ivars) is emitted, otherwise the list of
635 /// interface ivars will be emitted. The return value has type
636 /// IvarListnfABIPtrTy.
637 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000638
Fariborz Jahanianed157d32009-02-10 20:21:06 +0000639 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +0000640 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +0000641 unsigned long int offset);
642
Fariborz Jahanianda320092009-01-29 19:24:30 +0000643 /// GetOrEmitProtocol - Get the protocol object for the given
644 /// declaration, emitting it if necessary. The return value has type
645 /// ProtocolPtrTy.
646 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
647
648 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
649 /// object for the given declaration, emitting it if needed. These
650 /// forward references will be filled in with empty bodies if no
651 /// definition is seen. The return value has type ProtocolPtrTy.
652 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
653
654 /// EmitProtocolList - Generate the list of referenced
655 /// protocols. The return value has type ProtocolListPtrTy.
656 llvm::Constant *EmitProtocolList(const std::string &Name,
657 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000658 ObjCProtocolDecl::protocol_iterator end);
659
660 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
661 QualType ResultType,
662 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000663 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000664 QualType Arg0Ty,
665 bool IsSuper,
666 const CallArgList &CallArgs);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000667
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +0000668 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
669 /// for the given class.
670 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +0000671 const ObjCInterfaceDecl *ID,
672 bool IsSuper = false);
673
674 /// EmitMetaClassRef - Return a Value * of the address of _class_t
675 /// meta-data
676 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
677 const ObjCInterfaceDecl *ID);
678
Fariborz Jahanianed157d32009-02-10 20:21:06 +0000679 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
680 /// the given ivar.
681 ///
682 llvm::GlobalVariable * ObjCIvarOffsetVariable(std::string &Name,
Fariborz Jahanian01a0c362009-02-12 18:51:23 +0000683 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +0000684 const ObjCIvarDecl *Ivar);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +0000685
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +0000686 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
687 /// for the given selector.
688 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
689
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000690public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000691 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000692 // FIXME. All stubs for now!
693 virtual llvm::Function *ModuleInitFunction();
694
695 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
696 QualType ResultType,
697 Selector Sel,
698 llvm::Value *Receiver,
699 bool IsClassMessage,
Fariborz Jahanian46551122009-02-04 00:22:57 +0000700 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000701
702 virtual CodeGen::RValue
703 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
704 QualType ResultType,
705 Selector Sel,
706 const ObjCInterfaceDecl *Class,
707 llvm::Value *Receiver,
708 bool IsClassMessage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +0000709 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000710
711 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +0000712 const ObjCInterfaceDecl *ID);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000713
714 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +0000715 { return EmitSelector(Builder, Sel); }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000716
Fariborz Jahanianeb062d92009-01-26 18:32:24 +0000717 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000718
719 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000720 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +0000721 const ObjCProtocolDecl *PD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000722
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000723 virtual llvm::Function *GetPropertyGetFunction(){
724 return ObjCTypes.GetPropertyFn;
725 }
726 virtual llvm::Function *GetPropertySetFunction(){
727 return ObjCTypes.SetPropertyFn;
728 }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000729 virtual llvm::Function *EnumerationMutationFunction()
Fariborz Jahanian3afdfd72009-02-10 22:46:12 +0000730 { return ObjCTypes.EnumerationMutationFn; }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000731
732 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
733 const Stmt &S)
734 { return; }
735 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
736 const ObjCAtThrowStmt &S)
737 { return; }
738 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
739 llvm::Value *AddrWeakObj)
740 { return 0; }
741 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
742 llvm::Value *src, llvm::Value *dst)
743 { return; }
744 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
745 llvm::Value *src, llvm::Value *dest)
746 { return; }
747 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
748 llvm::Value *src, llvm::Value *dest)
749 { return; }
750 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
751 llvm::Value *src, llvm::Value *dest)
752 { return; }
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000753 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
754 QualType ObjectTy,
755 llvm::Value *BaseValue,
756 const ObjCIvarDecl *Ivar,
757 const FieldDecl *Field,
758 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000759 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
760 ObjCInterfaceDecl *Interface,
761 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000762};
763
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000764} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000765
766/* *** Helper Functions *** */
767
768/// getConstantGEP() - Help routine to construct simple GEPs.
769static llvm::Constant *getConstantGEP(llvm::Constant *C,
770 unsigned idx0,
771 unsigned idx1) {
772 llvm::Value *Idxs[] = {
773 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
774 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
775 };
776 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
777}
778
779/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000780
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000781CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
782 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000783{
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000784 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000785 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000786}
787
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000788/// GetClass - Return a reference to the class for the given interface
789/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000790llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000791 const ObjCInterfaceDecl *ID) {
792 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000793}
794
795/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000796llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000797 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000798}
799
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000800/// Generate a constant CFString object.
801/*
802 struct __builtin_CFString {
803 const int *isa; // point to __CFConstantStringClassReference
804 int flags;
805 const char *str;
806 long length;
807 };
808*/
809
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000810llvm::Constant *CGObjCCommonMac::GenerateConstantString(
811 const std::string &String) {
Daniel Dunbar3e9df992008-08-23 18:37:06 +0000812 return CGM.GetAddrOfConstantCFString(String);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000813}
814
815/// Generates a message send where the super is the receiver. This is
816/// a message send to self with special delivery semantics indicating
817/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000818CodeGen::RValue
819CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000820 QualType ResultType,
821 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000822 const ObjCInterfaceDecl *Class,
823 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000824 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000825 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000826 // Create and init a super structure; this is a (receiver, class)
827 // pair we will pass to objc_msgSendSuper.
828 llvm::Value *ObjCSuper =
829 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
830 llvm::Value *ReceiverAsObject =
831 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
832 CGF.Builder.CreateStore(ReceiverAsObject,
833 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000834
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000835 // If this is a class message the metaclass is passed as the target.
836 llvm::Value *Target;
837 if (IsClassMessage) {
838 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
839 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
840 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
841 Target = Super;
842 } else {
843 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
844 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000845 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
846 // and ObjCTypes types.
847 const llvm::Type *ClassTy =
848 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000849 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000850 CGF.Builder.CreateStore(Target,
851 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
852
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000853 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000854 ObjCSuper, ObjCTypes.SuperPtrCTy,
855 true, CallArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000856}
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000857
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000858/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000859CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000860 QualType ResultType,
861 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000862 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000863 bool IsClassMessage,
864 const CallArgList &CallArgs) {
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000865 llvm::Value *Arg0 =
866 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000867 return EmitMessageSend(CGF, ResultType, Sel,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000868 Arg0, CGF.getContext().getObjCIdType(),
869 false, CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000870}
871
872CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000873 QualType ResultType,
874 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000875 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000876 QualType Arg0Ty,
877 bool IsSuper,
878 const CallArgList &CallArgs) {
879 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000880 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
881 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder,
882 Sel)),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000883 CGF.getContext().getObjCSelType()));
884 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000885
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000886 CodeGenTypes &Types = CGM.getTypes();
887 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
888 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false);
Daniel Dunbar5669e572008-10-17 03:24:53 +0000889
890 llvm::Constant *Fn;
Daniel Dunbar88b53962009-02-02 22:03:45 +0000891 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Daniel Dunbar5669e572008-10-17 03:24:53 +0000892 Fn = ObjCTypes.getSendStretFn(IsSuper);
893 } else if (ResultType->isFloatingType()) {
894 // FIXME: Sadly, this is wrong. This actually depends on the
895 // architecture. This happens to be right for x86-32 though.
896 Fn = ObjCTypes.getSendFpretFn(IsSuper);
897 } else {
898 Fn = ObjCTypes.getSendFn(IsSuper);
899 }
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +0000900 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar88b53962009-02-02 22:03:45 +0000901 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000902}
903
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000904llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000905 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +0000906 // FIXME: I don't understand why gcc generates this, or where it is
907 // resolved. Investigate. Its also wasteful to look this up over and
908 // over.
909 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
910
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000911 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
912 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000913}
914
Fariborz Jahanianda320092009-01-29 19:24:30 +0000915void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000916 // FIXME: We shouldn't need this, the protocol decl should contain
917 // enough information to tell us whether this was a declaration or a
918 // definition.
919 DefinedProtocols.insert(PD->getIdentifier());
920
921 // If we have generated a forward reference to this protocol, emit
922 // it now. Otherwise do nothing, the protocol objects are lazily
923 // emitted.
924 if (Protocols.count(PD->getIdentifier()))
925 GetOrEmitProtocol(PD);
926}
927
Fariborz Jahanianda320092009-01-29 19:24:30 +0000928llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000929 if (DefinedProtocols.count(PD->getIdentifier()))
930 return GetOrEmitProtocol(PD);
931 return GetOrEmitProtocolRef(PD);
932}
933
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000934/*
935 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
936 struct _objc_protocol {
937 struct _objc_protocol_extension *isa;
938 char *protocol_name;
939 struct _objc_protocol_list *protocol_list;
940 struct _objc__method_prototype_list *instance_methods;
941 struct _objc__method_prototype_list *class_methods
942 };
943
944 See EmitProtocolExtension().
945*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000946llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
947 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
948
949 // Early exit if a defining object has already been generated.
950 if (Entry && Entry->hasInitializer())
951 return Entry;
952
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000953 // FIXME: I don't understand why gcc generates this, or where it is
954 // resolved. Investigate. Its also wasteful to look this up over and
955 // over.
956 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
957
Chris Lattner8ec03f52008-11-24 03:54:41 +0000958 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000959
960 // Construct method lists.
961 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
962 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
963 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
964 e = PD->instmeth_end(); i != e; ++i) {
965 ObjCMethodDecl *MD = *i;
966 llvm::Constant *C = GetMethodDescriptionConstant(MD);
967 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
968 OptInstanceMethods.push_back(C);
969 } else {
970 InstanceMethods.push_back(C);
971 }
972 }
973
974 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
975 e = PD->classmeth_end(); i != e; ++i) {
976 ObjCMethodDecl *MD = *i;
977 llvm::Constant *C = GetMethodDescriptionConstant(MD);
978 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
979 OptClassMethods.push_back(C);
980 } else {
981 ClassMethods.push_back(C);
982 }
983 }
984
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000985 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000986 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000987 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +0000988 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000989 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +0000990 PD->protocol_begin(),
991 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000992 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000993 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
994 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000995 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
996 InstanceMethods);
997 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000998 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
999 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001000 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1001 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001002 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
1003 Values);
1004
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001005 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001006 // Already created, fix the linkage and update the initializer.
1007 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001008 Entry->setInitializer(Init);
1009 } else {
1010 Entry =
1011 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
1012 llvm::GlobalValue::InternalLinkage,
1013 Init,
1014 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
1015 &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 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001021
1022 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001023}
1024
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001025llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001026 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1027
1028 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001029 // We use the initializer as a marker of whether this is a forward
1030 // reference or not. At module finalization we add the empty
1031 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001032 Entry =
1033 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001034 llvm::GlobalValue::ExternalLinkage,
1035 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001036 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001037 &CGM.getModule());
1038 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
1039 UsedGlobals.push_back(Entry);
1040 // FIXME: Is this necessary? Why only for protocol?
1041 Entry->setAlignment(4);
1042 }
1043
1044 return Entry;
1045}
1046
1047/*
1048 struct _objc_protocol_extension {
1049 uint32_t size;
1050 struct objc_method_description_list *optional_instance_methods;
1051 struct objc_method_description_list *optional_class_methods;
1052 struct objc_property_list *instance_properties;
1053 };
1054*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001055llvm::Constant *
1056CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1057 const ConstantVector &OptInstanceMethods,
1058 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001059 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001060 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001061 std::vector<llvm::Constant*> Values(4);
1062 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001063 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001064 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1065 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001066 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1067 OptInstanceMethods);
1068 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001069 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1070 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001071 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1072 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001073 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1074 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001075 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001076
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001077 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001078 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1079 Values[3]->isNullValue())
1080 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
1081
1082 llvm::Constant *Init =
1083 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
1084 llvm::GlobalVariable *GV =
1085 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false,
1086 llvm::GlobalValue::InternalLinkage,
1087 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001088 "\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001089 &CGM.getModule());
1090 // No special section, but goes in llvm.used
1091 UsedGlobals.push_back(GV);
1092
1093 return GV;
1094}
1095
1096/*
1097 struct objc_protocol_list {
1098 struct objc_protocol_list *next;
1099 long count;
1100 Protocol *list[];
1101 };
1102*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001103llvm::Constant *
1104CGObjCMac::EmitProtocolList(const std::string &Name,
1105 ObjCProtocolDecl::protocol_iterator begin,
1106 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001107 std::vector<llvm::Constant*> ProtocolRefs;
1108
Daniel Dunbardbc933702008-08-21 21:57:41 +00001109 for (; begin != end; ++begin)
1110 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001111
1112 // Just return null for empty protocol lists
1113 if (ProtocolRefs.empty())
1114 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1115
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001116 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001117 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1118
1119 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001120 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001121 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1122 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1123 Values[2] =
1124 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1125 ProtocolRefs.size()),
1126 ProtocolRefs);
1127
1128 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1129 llvm::GlobalVariable *GV =
1130 new llvm::GlobalVariable(Init->getType(), false,
1131 llvm::GlobalValue::InternalLinkage,
1132 Init,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001133 Name,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001134 &CGM.getModule());
1135 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip");
1136 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1137}
1138
1139/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001140 struct _objc_property {
1141 const char * const name;
1142 const char * const attributes;
1143 };
1144
1145 struct _objc_property_list {
1146 uint32_t entsize; // sizeof (struct _objc_property)
1147 uint32_t prop_count;
1148 struct _objc_property[prop_count];
1149 };
1150*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001151llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1152 const Decl *Container,
1153 const ObjCContainerDecl *OCD,
1154 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001155 std::vector<llvm::Constant*> Properties, Prop(2);
Steve Naroff93983f82009-01-11 12:47:58 +00001156 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1157 E = OCD->prop_end(); I != E; ++I) {
1158 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001159 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001160 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001161 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1162 Prop));
1163 }
1164
1165 // Return null for empty list.
1166 if (Properties.empty())
1167 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1168
1169 unsigned PropertySize =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001170 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001171 std::vector<llvm::Constant*> Values(3);
1172 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1173 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1174 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1175 Properties.size());
1176 Values[2] = llvm::ConstantArray::get(AT, Properties);
1177 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1178
1179 llvm::GlobalVariable *GV =
1180 new llvm::GlobalVariable(Init->getType(), false,
1181 llvm::GlobalValue::InternalLinkage,
1182 Init,
1183 Name,
1184 &CGM.getModule());
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001185 if (ObjCABI == 2)
1186 GV->setSection("__DATA, __objc_const");
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001187 // No special section on property lists?
1188 UsedGlobals.push_back(GV);
1189 return llvm::ConstantExpr::getBitCast(GV,
1190 ObjCTypes.PropertyListPtrTy);
1191
1192}
1193
1194/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001195 struct objc_method_description_list {
1196 int count;
1197 struct objc_method_description list[];
1198 };
1199*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001200llvm::Constant *
1201CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1202 std::vector<llvm::Constant*> Desc(2);
1203 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1204 ObjCTypes.SelectorPtrTy);
1205 Desc[1] = GetMethodVarType(MD);
1206 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1207 Desc);
1208}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001209
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001210llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1211 const char *Section,
1212 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001213 // Return null for empty list.
1214 if (Methods.empty())
1215 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1216
1217 std::vector<llvm::Constant*> Values(2);
1218 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1219 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1220 Methods.size());
1221 Values[1] = llvm::ConstantArray::get(AT, Methods);
1222 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1223
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001224 llvm::GlobalVariable *GV =
1225 new llvm::GlobalVariable(Init->getType(), false,
1226 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001227 Init, Name, &CGM.getModule());
1228 GV->setSection(Section);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001229 UsedGlobals.push_back(GV);
1230 return llvm::ConstantExpr::getBitCast(GV,
1231 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001232}
1233
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001234/*
1235 struct _objc_category {
1236 char *category_name;
1237 char *class_name;
1238 struct _objc_method_list *instance_methods;
1239 struct _objc_method_list *class_methods;
1240 struct _objc_protocol_list *protocols;
1241 uint32_t size; // <rdar://4585769>
1242 struct _objc_property_list *instance_properties;
1243 };
1244 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001245void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001246 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001247
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001248 // FIXME: This is poor design, the OCD should have a pointer to the
1249 // category decl. Additionally, note that Category can be null for
1250 // the @implementation w/o an @interface case. Sema should just
1251 // create one for us as it does for @implementation so everyone else
1252 // can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001253 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001254 const ObjCCategoryDecl *Category =
1255 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001256 std::string ExtName(Interface->getNameAsString() + "_" +
1257 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001258
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001259 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1260 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
1261 e = OCD->instmeth_end(); i != e; ++i) {
1262 // Instance methods should always be defined.
1263 InstanceMethods.push_back(GetMethodConstant(*i));
1264 }
1265 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
1266 e = OCD->classmeth_end(); i != e; ++i) {
1267 // Class methods should always be defined.
1268 ClassMethods.push_back(GetMethodConstant(*i));
1269 }
1270
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001271 std::vector<llvm::Constant*> Values(7);
1272 Values[0] = GetClassName(OCD->getIdentifier());
1273 Values[1] = GetClassName(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001274 Values[2] =
1275 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1276 ExtName,
1277 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001278 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001279 Values[3] =
1280 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
1281 "__OBJC,__cat_class_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001282 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001283 if (Category) {
1284 Values[4] =
1285 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1286 Category->protocol_begin(),
1287 Category->protocol_end());
1288 } else {
1289 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1290 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001291 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001292
1293 // If there is no category @interface then there can be no properties.
1294 if (Category) {
1295 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001296 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001297 } else {
1298 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1299 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001300
1301 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1302 Values);
1303
1304 llvm::GlobalVariable *GV =
1305 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false,
1306 llvm::GlobalValue::InternalLinkage,
1307 Init,
1308 std::string("\01L_OBJC_CATEGORY_")+ExtName,
1309 &CGM.getModule());
1310 GV->setSection("__OBJC,__category,regular,no_dead_strip");
1311 UsedGlobals.push_back(GV);
1312 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001313}
1314
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001315// FIXME: Get from somewhere?
1316enum ClassFlags {
1317 eClassFlags_Factory = 0x00001,
1318 eClassFlags_Meta = 0x00002,
1319 // <rdr://5142207>
1320 eClassFlags_HasCXXStructors = 0x02000,
1321 eClassFlags_Hidden = 0x20000,
1322 eClassFlags_ABI2_Hidden = 0x00010,
1323 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1324};
1325
1326// <rdr://5142207&4705298&4843145>
1327static bool IsClassHidden(const ObjCInterfaceDecl *ID) {
1328 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) {
1329 // FIXME: Support -fvisibility
1330 switch (attr->getVisibility()) {
1331 default:
1332 assert(0 && "Unknown visibility");
1333 return false;
1334 case VisibilityAttr::DefaultVisibility:
1335 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here?
1336 return false;
1337 case VisibilityAttr::HiddenVisibility:
1338 return true;
1339 }
1340 } else {
1341 return false; // FIXME: Support -fvisibility
1342 }
1343}
1344
1345/*
1346 struct _objc_class {
1347 Class isa;
1348 Class super_class;
1349 const char *name;
1350 long version;
1351 long info;
1352 long instance_size;
1353 struct _objc_ivar_list *ivars;
1354 struct _objc_method_list *methods;
1355 struct _objc_cache *cache;
1356 struct _objc_protocol_list *protocols;
1357 // Objective-C 1.0 extensions (<rdr://4585769>)
1358 const char *ivar_layout;
1359 struct _objc_class_ext *ext;
1360 };
1361
1362 See EmitClassExtension();
1363 */
1364void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001365 DefinedSymbols.insert(ID->getIdentifier());
1366
Chris Lattner8ec03f52008-11-24 03:54:41 +00001367 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001368 // FIXME: Gross
1369 ObjCInterfaceDecl *Interface =
1370 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001371 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001372 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001373 Interface->protocol_begin(),
1374 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001375 const llvm::Type *InterfaceTy =
Fariborz Jahanianf3710ba2009-02-14 20:13:28 +00001376 CGM.getTypes().ConvertType(CGM.getContext().buildObjCInterfaceType(Interface));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001377 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001378 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001379
1380 // FIXME: Set CXX-structors flag.
1381 if (IsClassHidden(ID->getClassInterface()))
1382 Flags |= eClassFlags_Hidden;
1383
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001384 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1385 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
1386 e = ID->instmeth_end(); i != e; ++i) {
1387 // Instance methods should always be defined.
1388 InstanceMethods.push_back(GetMethodConstant(*i));
1389 }
1390 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
1391 e = ID->classmeth_end(); i != e; ++i) {
1392 // Class methods should always be defined.
1393 ClassMethods.push_back(GetMethodConstant(*i));
1394 }
1395
1396 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
1397 e = ID->propimpl_end(); i != e; ++i) {
1398 ObjCPropertyImplDecl *PID = *i;
1399
1400 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1401 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1402
1403 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1404 if (llvm::Constant *C = GetMethodConstant(MD))
1405 InstanceMethods.push_back(C);
1406 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1407 if (llvm::Constant *C = GetMethodConstant(MD))
1408 InstanceMethods.push_back(C);
1409 }
1410 }
1411
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001412 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001413 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001414 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001415 // Record a reference to the super class.
1416 LazySymbols.insert(Super->getIdentifier());
1417
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001418 Values[ 1] =
1419 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1420 ObjCTypes.ClassPtrTy);
1421 } else {
1422 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1423 }
1424 Values[ 2] = GetClassName(ID->getIdentifier());
1425 // Version is always 0.
1426 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1427 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1428 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001429 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001430 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001431 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001432 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001433 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001434 // cache is always NULL.
1435 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1436 Values[ 9] = Protocols;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001437 // FIXME: Set ivar_layout
1438 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001439 Values[11] = EmitClassExtension(ID);
1440 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1441 Values);
1442
1443 llvm::GlobalVariable *GV =
1444 new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1445 llvm::GlobalValue::InternalLinkage,
1446 Init,
1447 std::string("\01L_OBJC_CLASS_")+ClassName,
1448 &CGM.getModule());
1449 GV->setSection("__OBJC,__class,regular,no_dead_strip");
1450 UsedGlobals.push_back(GV);
1451 // FIXME: Why?
1452 GV->setAlignment(32);
1453 DefinedClasses.push_back(GV);
1454}
1455
1456llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
1457 llvm::Constant *Protocols,
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001458 const llvm::Type *InterfaceTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001459 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001460 unsigned Flags = eClassFlags_Meta;
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001461 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001462
1463 if (IsClassHidden(ID->getClassInterface()))
1464 Flags |= eClassFlags_Hidden;
1465
1466 std::vector<llvm::Constant*> Values(12);
1467 // The isa for the metaclass is the root of the hierarchy.
1468 const ObjCInterfaceDecl *Root = ID->getClassInterface();
1469 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
1470 Root = Super;
1471 Values[ 0] =
1472 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
1473 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001474 // The super class for the metaclass is emitted as the name of the
1475 // super class. The runtime fixes this up to point to the
1476 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001477 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
1478 Values[ 1] =
1479 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
1480 ObjCTypes.ClassPtrTy);
1481 } else {
1482 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
1483 }
1484 Values[ 2] = GetClassName(ID->getIdentifier());
1485 // Version is always 0.
1486 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
1487 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
1488 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001489 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001490 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001491 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001492 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001493 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001494 // cache is always NULL.
1495 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
1496 Values[ 9] = Protocols;
1497 // ivar_layout for metaclass is always NULL.
1498 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1499 // The class extension is always unused for metaclasses.
1500 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1501 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
1502 Values);
1503
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001504 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00001505 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001506
1507 // Check for a forward reference.
1508 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
1509 if (GV) {
1510 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1511 "Forward metaclass reference has incorrect type.");
1512 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
1513 GV->setInitializer(Init);
1514 } else {
1515 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1516 llvm::GlobalValue::InternalLinkage,
1517 Init, Name,
1518 &CGM.getModule());
1519 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001520 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
1521 UsedGlobals.push_back(GV);
1522 // FIXME: Why?
1523 GV->setAlignment(32);
1524
1525 return GV;
1526}
1527
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001528llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001529 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001530
1531 // FIXME: Should we look these up somewhere other than the
1532 // module. Its a bit silly since we only generate these while
1533 // processing an implementation, so exactly one pointer would work
1534 // if know when we entered/exitted an implementation block.
1535
1536 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00001537 // Previously, metaclass with internal linkage may have been defined.
1538 // pass 'true' as 2nd argument so it is returned.
1539 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001540 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
1541 "Forward metaclass reference has incorrect type.");
1542 return GV;
1543 } else {
1544 // Generate as an external reference to keep a consistent
1545 // module. This will be patched up when we emit the metaclass.
1546 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
1547 llvm::GlobalValue::ExternalLinkage,
1548 0,
1549 Name,
1550 &CGM.getModule());
1551 }
1552}
1553
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001554/*
1555 struct objc_class_ext {
1556 uint32_t size;
1557 const char *weak_ivar_layout;
1558 struct _objc_property_list *properties;
1559 };
1560*/
1561llvm::Constant *
1562CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
1563 uint64_t Size =
Daniel Dunbar491c7b72009-01-12 21:08:18 +00001564 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001565
1566 std::vector<llvm::Constant*> Values(3);
1567 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001568 // FIXME: Output weak_ivar_layout string.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001569 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001570 Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001571 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001572
1573 // Return null if no extension bits are used.
1574 if (Values[1]->isNullValue() && Values[2]->isNullValue())
1575 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
1576
1577 llvm::Constant *Init =
1578 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
1579 llvm::GlobalVariable *GV =
1580 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false,
1581 llvm::GlobalValue::InternalLinkage,
1582 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001583 "\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001584 &CGM.getModule());
1585 // No special section, but goes in llvm.used
1586 UsedGlobals.push_back(GV);
1587
1588 return GV;
1589}
1590
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001591/// countInheritedIvars - count number of ivars in class and its super class(s)
1592///
1593static int countInheritedIvars(const ObjCInterfaceDecl *OI) {
1594 int count = 0;
1595 if (!OI)
1596 return 0;
1597 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
1598 if (SuperClass)
1599 count += countInheritedIvars(SuperClass);
1600 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1601 E = OI->ivar_end(); I != E; ++I)
1602 ++count;
1603 return count;
1604}
1605
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00001606/// getInterfaceDeclForIvar - Get the interface declaration node where
1607/// this ivar is declared in.
1608/// FIXME. Ideally, this info should be in the ivar node. But currently
1609/// it is not and prevailing wisdom is that ASTs should not have more
1610/// info than is absolutely needed, even though this info reflects the
1611/// source language.
1612///
1613static const ObjCInterfaceDecl *getInterfaceDeclForIvar(
1614 const ObjCInterfaceDecl *OI,
1615 const ObjCIvarDecl *IVD) {
1616 if (!OI)
1617 return 0;
1618 assert(isa<ObjCInterfaceDecl>(OI) && "OI is not an interface");
1619 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
1620 E = OI->ivar_end(); I != E; ++I)
1621 if ((*I)->getIdentifier() == IVD->getIdentifier())
1622 return OI;
1623 return getInterfaceDeclForIvar(OI->getSuperClass(), IVD);
1624}
1625
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001626/*
1627 struct objc_ivar {
1628 char *ivar_name;
1629 char *ivar_type;
1630 int ivar_offset;
1631 };
1632
1633 struct objc_ivar_list {
1634 int ivar_count;
1635 struct objc_ivar list[count];
1636 };
1637 */
1638llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001639 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001640 std::vector<llvm::Constant*> Ivars, Ivar(3);
1641
1642 // When emitting the root class GCC emits ivar entries for the
1643 // actual class structure. It is not clear if we need to follow this
1644 // behavior; for now lets try and get away with not doing it. If so,
1645 // the cleanest solution would be to make up an ObjCInterfaceDecl
1646 // for the class.
1647 if (ForClass)
1648 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001649
1650 ObjCInterfaceDecl *OID =
1651 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
1652 const llvm::Type *InterfaceTy =
1653 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001654 const llvm::StructLayout *Layout =
1655 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001656
1657 RecordDecl::field_iterator ifield, pfield;
1658 const RecordDecl *RD = GetFirstIvarInRecord(OID, ifield, pfield);
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001659 for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) {
1660 FieldDecl *Field = *ifield;
1661 unsigned Offset = Layout->getElementOffset(CGM.getTypes().
1662 getLLVMFieldNo(Field));
1663 if (Field->getIdentifier())
1664 Ivar[0] = GetMethodVarName(Field->getIdentifier());
1665 else
1666 Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001667 std::string TypeStr;
Fariborz Jahanianf54b1942009-01-17 19:36:33 +00001668 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001669 Ivar[1] = GetMethodVarType(TypeStr);
1670 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001671 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001672 }
1673
1674 // Return null for empty list.
1675 if (Ivars.empty())
1676 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
1677
1678 std::vector<llvm::Constant*> Values(2);
1679 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
1680 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
1681 Ivars.size());
1682 Values[1] = llvm::ConstantArray::get(AT, Ivars);
1683 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1684
1685 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" :
1686 "\01L_OBJC_INSTANCE_VARIABLES_");
1687 llvm::GlobalVariable *GV =
1688 new llvm::GlobalVariable(Init->getType(), false,
1689 llvm::GlobalValue::InternalLinkage,
1690 Init,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001691 Prefix + ID->getNameAsString(),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001692 &CGM.getModule());
1693 if (ForClass) {
1694 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip");
1695 // FIXME: Why is this only here?
1696 GV->setAlignment(32);
1697 } else {
1698 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip");
1699 }
1700 UsedGlobals.push_back(GV);
1701 return llvm::ConstantExpr::getBitCast(GV,
1702 ObjCTypes.IvarListPtrTy);
1703}
1704
1705/*
1706 struct objc_method {
1707 SEL method_name;
1708 char *method_types;
1709 void *method;
1710 };
1711
1712 struct objc_method_list {
1713 struct objc_method_list *obsolete;
1714 int count;
1715 struct objc_method methods_list[count];
1716 };
1717*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001718
1719/// GetMethodConstant - Return a struct objc_method constant for the
1720/// given method if it has been defined. The result is null if the
1721/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001722llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001723 // FIXME: Use DenseMap::lookup
1724 llvm::Function *Fn = MethodDefinitions[MD];
1725 if (!Fn)
1726 return 0;
1727
1728 std::vector<llvm::Constant*> Method(3);
1729 Method[0] =
1730 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1731 ObjCTypes.SelectorPtrTy);
1732 Method[1] = GetMethodVarType(MD);
1733 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
1734 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
1735}
1736
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001737llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
1738 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001739 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001740 // Return null for empty list.
1741 if (Methods.empty())
1742 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
1743
1744 std::vector<llvm::Constant*> Values(3);
1745 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
1746 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1747 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
1748 Methods.size());
1749 Values[2] = llvm::ConstantArray::get(AT, Methods);
1750 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1751
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001752 llvm::GlobalVariable *GV =
1753 new llvm::GlobalVariable(Init->getType(), false,
1754 llvm::GlobalValue::InternalLinkage,
1755 Init,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001756 Name,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001757 &CGM.getModule());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001758 GV->setSection(Section);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001759 UsedGlobals.push_back(GV);
1760 return llvm::ConstantExpr::getBitCast(GV,
1761 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001762}
1763
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001764llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbarbb36d332009-02-02 21:43:58 +00001765 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001766 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001767 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001768
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001769 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001770 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001771 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001772 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001773 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001774 llvm::GlobalValue::InternalLinkage,
1775 Name,
1776 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001777 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001778
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001779 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001780}
1781
1782llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001783 // Abuse this interface function as a place to finalize.
1784 FinishModule();
1785
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001786 return NULL;
1787}
1788
Daniel Dunbar49f66022008-09-24 03:38:44 +00001789llvm::Function *CGObjCMac::GetPropertyGetFunction() {
1790 return ObjCTypes.GetPropertyFn;
1791}
1792
1793llvm::Function *CGObjCMac::GetPropertySetFunction() {
1794 return ObjCTypes.SetPropertyFn;
1795}
1796
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001797llvm::Function *CGObjCMac::EnumerationMutationFunction()
1798{
1799 return ObjCTypes.EnumerationMutationFn;
1800}
1801
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001802/*
1803
1804Objective-C setjmp-longjmp (sjlj) Exception Handling
1805--
1806
1807The basic framework for a @try-catch-finally is as follows:
1808{
1809 objc_exception_data d;
1810 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00001811 bool _call_try_exit = true;
1812
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001813 objc_exception_try_enter(&d);
1814 if (!setjmp(d.jmp_buf)) {
1815 ... try body ...
1816 } else {
1817 // exception path
1818 id _caught = objc_exception_extract(&d);
1819
1820 // enter new try scope for handlers
1821 if (!setjmp(d.jmp_buf)) {
1822 ... match exception and execute catch blocks ...
1823
1824 // fell off end, rethrow.
1825 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00001826 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001827 } else {
1828 // exception in catch block
1829 _rethrow = objc_exception_extract(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00001830 _call_try_exit = false;
1831 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001832 }
1833 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00001834 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001835
1836finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00001837 if (_call_try_exit)
1838 objc_exception_try_exit(&d);
1839
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001840 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00001841 ... dispatch to finally destination ...
1842
1843finally_rethrow:
1844 objc_exception_throw(_rethrow);
1845
1846finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001847}
1848
1849This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00001850uses _rethrow to determine if objc_exception_try_exit should be called
1851and if the object should be rethrown. This breaks in the face of
1852throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001853
1854We specialize this framework for a few particular circumstances:
1855
1856 - If there are no catch blocks, then we avoid emitting the second
1857 exception handling context.
1858
1859 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
1860 e)) we avoid emitting the code to rethrow an uncaught exception.
1861
1862 - FIXME: If there is no @finally block we can do a few more
1863 simplifications.
1864
1865Rethrows and Jumps-Through-Finally
1866--
1867
1868Support for implicit rethrows and jumping through the finally block is
1869handled by storing the current exception-handling context in
1870ObjCEHStack.
1871
Daniel Dunbar898d5082008-09-30 01:06:03 +00001872In order to implement proper @finally semantics, we support one basic
1873mechanism for jumping through the finally block to an arbitrary
1874destination. Constructs which generate exits from a @try or @catch
1875block use this mechanism to implement the proper semantics by chaining
1876jumps, as necessary.
1877
1878This mechanism works like the one used for indirect goto: we
1879arbitrarily assign an ID to each destination and store the ID for the
1880destination in a variable prior to entering the finally block. At the
1881end of the finally block we simply create a switch to the proper
1882destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001883
1884Code gen for @synchronized(expr) stmt;
1885Effectively generating code for:
1886objc_sync_enter(expr);
1887@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001888*/
1889
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001890void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1891 const Stmt &S) {
1892 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00001893 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00001894 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Anders Carlsson190d00e2009-02-07 21:26:04 +00001895 llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit");
Daniel Dunbar55e87422008-11-11 02:29:29 +00001896 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
1897 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1898 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001899
1900 // Push an EH context entry, used for handling rethrows and jumps
1901 // through finally.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00001902 CGF.PushCleanupBlock(FinallyBlock);
1903
Anders Carlsson273558f2009-02-07 21:37:21 +00001904 CGF.ObjCEHValueStack.push_back(0);
1905
Daniel Dunbar898d5082008-09-30 01:06:03 +00001906 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00001907 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
1908 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001909 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
1910 "_rethrow");
Anders Carlsson190d00e2009-02-07 21:26:04 +00001911 llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty,
1912 "_call_try_exit");
1913 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr);
1914
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001915 if (!isTry) {
1916 // For @synchronized, call objc_sync_enter(sync.expr)
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00001917 llvm::Value *Arg = CGF.EmitScalarExpr(
1918 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1919 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
1920 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, Arg);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001921 }
Anders Carlsson80f25672008-09-09 17:59:25 +00001922
1923 // Enter a new try block and call setjmp.
1924 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
1925 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
1926 "jmpbufarray");
1927 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
1928 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1929 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00001930
Daniel Dunbar55e87422008-11-11 02:29:29 +00001931 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1932 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001933 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001934 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001935
1936 // Emit the @try block.
1937 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001938 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
1939 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00001940 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00001941
1942 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001943 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001944
1945 // Retrieve the exception object. We may emit multiple blocks but
1946 // nothing can cross this so the value is already in SSA form.
1947 llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
1948 ExceptionData,
1949 "caught");
Anders Carlsson273558f2009-02-07 21:37:21 +00001950 CGF.ObjCEHValueStack.back() = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001951 if (!isTry)
1952 {
1953 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00001954 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00001955 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001956 }
1957 else if (const ObjCAtCatchStmt* CatchStmt =
1958 cast<ObjCAtTryStmt>(S).getCatchStmts())
1959 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00001960 // Enter a new exception try block (in case a @catch block throws
1961 // an exception).
Anders Carlsson80f25672008-09-09 17:59:25 +00001962 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00001963
Anders Carlsson80f25672008-09-09 17:59:25 +00001964 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn,
1965 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00001966 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00001967
Daniel Dunbar55e87422008-11-11 02:29:29 +00001968 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
1969 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00001970 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00001971
1972 CGF.EmitBlock(CatchBlock);
1973
Daniel Dunbar55e40722008-09-27 07:03:52 +00001974 // Handle catch list. As a special case we check if everything is
1975 // matched and avoid generating code for falling off the end if
1976 // so.
1977 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00001978 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00001979 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00001980
Anders Carlssondde0a942008-09-11 09:15:33 +00001981 const DeclStmt *CatchParam =
1982 cast_or_null<DeclStmt>(CatchStmt->getCatchParamStmt());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001983 const VarDecl *VD = 0;
1984 const PointerType *PT = 0;
1985
Anders Carlsson80f25672008-09-09 17:59:25 +00001986 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00001987 if (!CatchParam) {
1988 AllMatched = true;
1989 } else {
Ted Kremenekde3b8fb2008-10-06 20:58:56 +00001990 VD = cast<VarDecl>(CatchParam->getSolitaryDecl());
Daniel Dunbar129271a2008-09-27 07:36:24 +00001991 PT = VD->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00001992
Daniel Dunbar97f61d12008-09-27 22:21:14 +00001993 // catch(id e) always matches.
1994 // FIXME: For the time being we also match id<X>; this should
1995 // be rejected by Sema instead.
Steve Naroff389bf462009-02-12 17:52:19 +00001996 if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) ||
Daniel Dunbar97f61d12008-09-27 22:21:14 +00001997 VD->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00001998 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00001999 }
2000
Daniel Dunbar55e40722008-09-27 07:03:52 +00002001 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00002002 if (CatchParam) {
2003 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002004 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar129271a2008-09-27 07:36:24 +00002005 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00002006 }
Anders Carlsson1452f552008-09-11 08:21:54 +00002007
Anders Carlssondde0a942008-09-11 09:15:33 +00002008 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002009 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002010 break;
2011 }
2012
Daniel Dunbar129271a2008-09-27 07:36:24 +00002013 assert(PT && "Unexpected non-pointer type in @catch");
2014 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00002015 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002016 assert(ObjCType && "Catch parameter must have Objective-C type!");
2017
2018 // Check if the @catch block matches the exception object.
2019 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
2020
Anders Carlsson80f25672008-09-09 17:59:25 +00002021 llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn,
2022 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00002023
Daniel Dunbar55e87422008-11-11 02:29:29 +00002024 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00002025
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002026 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002027 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002028
2029 // Emit the @catch block.
2030 CGF.EmitBlock(MatchedBlock);
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002031 CGF.EmitStmt(CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002032 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002033
2034 llvm::Value *Tmp =
2035 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(VD->getType()),
2036 "tmp");
2037 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(VD));
Anders Carlssondde0a942008-09-11 09:15:33 +00002038
2039 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002040 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002041
2042 CGF.EmitBlock(NextCatchBlock);
2043 }
2044
Daniel Dunbar55e40722008-09-27 07:03:52 +00002045 if (!AllMatched) {
2046 // None of the handlers caught the exception, so store it to be
2047 // rethrown at the end of the @finally block.
2048 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002049 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002050 }
2051
2052 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002053 CGF.EmitBlock(CatchHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002054 CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn,
2055 ExceptionData),
2056 RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002057 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002058 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002059 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00002060 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002061 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002062 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlsson80f25672008-09-09 17:59:25 +00002063 }
2064
Daniel Dunbar898d5082008-09-30 01:06:03 +00002065 // Pop the exception-handling stack entry. It is important to do
2066 // this now, because the code in the @finally block is not in this
2067 // context.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002068 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
2069
Anders Carlsson273558f2009-02-07 21:37:21 +00002070 CGF.ObjCEHValueStack.pop_back();
2071
Anders Carlsson80f25672008-09-09 17:59:25 +00002072 // Emit the @finally block.
2073 CGF.EmitBlock(FinallyBlock);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002074 llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp");
2075
2076 CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit);
2077
2078 CGF.EmitBlock(FinallyExit);
Anders Carlsson80f25672008-09-09 17:59:25 +00002079 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00002080
2081 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002082 if (isTry) {
2083 if (const ObjCAtFinallyStmt* FinallyStmt =
2084 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2085 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2086 }
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002087 else {
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002088 // For @synchronized objc_sync_exit(expr); As finally's sole statement.
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002089 // For @synchronized, call objc_sync_enter(sync.expr)
2090 llvm::Value *Arg = CGF.EmitScalarExpr(
2091 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2092 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy);
2093 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, Arg);
2094 }
Anders Carlsson80f25672008-09-09 17:59:25 +00002095
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002096 // Emit the switch block
2097 if (Info.SwitchBlock)
2098 CGF.EmitBlock(Info.SwitchBlock);
2099 if (Info.EndBlock)
2100 CGF.EmitBlock(Info.EndBlock);
2101
Daniel Dunbar898d5082008-09-30 01:06:03 +00002102 CGF.EmitBlock(FinallyRethrow);
2103 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn,
2104 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002105 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00002106
2107 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002108}
2109
2110void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002111 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002112 llvm::Value *ExceptionAsObject;
2113
2114 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2115 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2116 ExceptionAsObject =
2117 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2118 } else {
Anders Carlsson273558f2009-02-07 21:37:21 +00002119 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002120 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00002121 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002122 }
2123
2124 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002125 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002126
2127 // Clear the insertion point to indicate we are in unreachable code.
2128 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002129}
2130
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002131/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002132/// object: objc_read_weak (id *src)
2133///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002134llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002135 llvm::Value *AddrWeakObj)
2136{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002137 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002138 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn,
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002139 AddrWeakObj, "weakread");
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002140 return read_weak;
2141}
2142
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002143/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2144/// objc_assign_weak (id src, id *dst)
2145///
2146void CGObjCMac::EmitObjCWeakAssign(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 Jahanian3e283e32008-11-18 22:37:34 +00002151 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn,
2152 src, dst, "weakassign");
2153 return;
2154}
2155
Fariborz Jahanian58626502008-11-19 00:59:10 +00002156/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2157/// objc_assign_global (id src, id *dst)
2158///
2159void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2160 llvm::Value *src, llvm::Value *dst)
2161{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002162 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2163 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002164 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn,
2165 src, dst, "globalassign");
2166 return;
2167}
2168
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002169/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2170/// objc_assign_ivar (id src, id *dst)
2171///
2172void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2173 llvm::Value *src, llvm::Value *dst)
2174{
2175 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2176 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
2177 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn,
2178 src, dst, "assignivar");
2179 return;
2180}
2181
Fariborz Jahanian58626502008-11-19 00:59:10 +00002182/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2183/// objc_assign_strongCast (id src, id *dst)
2184///
2185void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2186 llvm::Value *src, llvm::Value *dst)
2187{
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002188 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2189 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002190 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn,
2191 src, dst, "weakassign");
2192 return;
2193}
2194
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002195/// EmitObjCValueForIvar - Code Gen for ivar reference.
2196///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002197LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2198 QualType ObjectTy,
2199 llvm::Value *BaseValue,
2200 const ObjCIvarDecl *Ivar,
2201 const FieldDecl *Field,
2202 unsigned CVRQualifiers) {
2203 if (Ivar->isBitField())
2204 return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field),
2205 CVRQualifiers);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002206 // TODO: Add a special case for isa (index 0)
2207 unsigned Index = CGM.getTypes().getLLVMFieldNo(Field);
2208 llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp");
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002209 LValue LV = LValue::MakeAddr(V,
2210 Ivar->getType().getCVRQualifiers()|CVRQualifiers);
2211 LValue::SetObjCIvar(LV, true);
2212 return LV;
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002213}
2214
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002215llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
2216 ObjCInterfaceDecl *Interface,
2217 const ObjCIvarDecl *Ivar) {
2218 const llvm::Type *InterfaceLTy =
2219 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface));
2220 const llvm::StructLayout *Layout =
2221 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceLTy));
2222 FieldDecl *Field = Interface->lookupFieldDeclForIvar(CGM.getContext(), Ivar);
2223 uint64_t Offset =
2224 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
2225
2226 return llvm::ConstantInt::get(
2227 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
2228 Offset);
2229}
2230
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002231/* *** Private Interface *** */
2232
2233/// EmitImageInfo - Emit the image info marker used to encode some module
2234/// level information.
2235///
2236/// See: <rdr://4810609&4810587&4810587>
2237/// struct IMAGE_INFO {
2238/// unsigned version;
2239/// unsigned flags;
2240/// };
2241enum ImageInfoFlags {
2242 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
2243 eImageInfo_GarbageCollected = (1 << 1),
2244 eImageInfo_GCOnly = (1 << 2)
2245};
2246
2247void CGObjCMac::EmitImageInfo() {
2248 unsigned version = 0; // Version is unused?
2249 unsigned flags = 0;
2250
2251 // FIXME: Fix and continue?
2252 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2253 flags |= eImageInfo_GarbageCollected;
2254 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2255 flags |= eImageInfo_GCOnly;
2256
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002257 // Emitted as int[2];
2258 llvm::Constant *values[2] = {
2259 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2260 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2261 };
2262 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002263 llvm::GlobalVariable *GV =
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002264 new llvm::GlobalVariable(AT, true,
2265 llvm::GlobalValue::InternalLinkage,
2266 llvm::ConstantArray::get(AT, values, 2),
2267 "\01L_OBJC_IMAGE_INFO",
2268 &CGM.getModule());
2269
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002270 if (ObjCABI == 1) {
2271 GV->setSection("__OBJC, __image_info,regular");
2272 } else {
2273 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
2274 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002275
2276 UsedGlobals.push_back(GV);
2277}
2278
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002279
2280// struct objc_module {
2281// unsigned long version;
2282// unsigned long size;
2283// const char *name;
2284// Symtab symtab;
2285// };
2286
2287// FIXME: Get from somewhere
2288static const int ModuleVersion = 7;
2289
2290void CGObjCMac::EmitModuleInfo() {
Daniel Dunbar491c7b72009-01-12 21:08:18 +00002291 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002292
2293 std::vector<llvm::Constant*> Values(4);
2294 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2295 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002296 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002297 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002298 Values[3] = EmitModuleSymbols();
2299
2300 llvm::GlobalVariable *GV =
2301 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false,
2302 llvm::GlobalValue::InternalLinkage,
2303 llvm::ConstantStruct::get(ObjCTypes.ModuleTy,
2304 Values),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002305 "\01L_OBJC_MODULES",
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002306 &CGM.getModule());
2307 GV->setSection("__OBJC,__module_info,regular,no_dead_strip");
2308 UsedGlobals.push_back(GV);
2309}
2310
2311llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002312 unsigned NumClasses = DefinedClasses.size();
2313 unsigned NumCategories = DefinedCategories.size();
2314
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002315 // Return null if no symbols were defined.
2316 if (!NumClasses && !NumCategories)
2317 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2318
2319 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002320 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2321 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2322 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2323 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2324
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002325 // The runtime expects exactly the list of defined classes followed
2326 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002327 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002328 for (unsigned i=0; i<NumClasses; i++)
2329 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2330 ObjCTypes.Int8PtrTy);
2331 for (unsigned i=0; i<NumCategories; i++)
2332 Symbols[NumClasses + i] =
2333 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2334 ObjCTypes.Int8PtrTy);
2335
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002336 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002337 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002338 NumClasses + NumCategories),
2339 Symbols);
2340
2341 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2342
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002343 llvm::GlobalVariable *GV =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002344 new llvm::GlobalVariable(Init->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002345 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002346 Init,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002347 "\01L_OBJC_SYMBOLS",
2348 &CGM.getModule());
2349 GV->setSection("__OBJC,__symbols,regular,no_dead_strip");
2350 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002351 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2352}
2353
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002354llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002355 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002356 LazySymbols.insert(ID->getIdentifier());
2357
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002358 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2359
2360 if (!Entry) {
2361 llvm::Constant *Casted =
2362 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2363 ObjCTypes.ClassPtrTy);
2364 Entry =
2365 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false,
2366 llvm::GlobalValue::InternalLinkage,
2367 Casted, "\01L_OBJC_CLASS_REFERENCES_",
2368 &CGM.getModule());
2369 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip");
2370 UsedGlobals.push_back(Entry);
2371 }
2372
2373 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002374}
2375
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002376llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002377 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2378
2379 if (!Entry) {
2380 llvm::Constant *Casted =
2381 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2382 ObjCTypes.SelectorPtrTy);
2383 Entry =
2384 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
2385 llvm::GlobalValue::InternalLinkage,
2386 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
2387 &CGM.getModule());
2388 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip");
2389 UsedGlobals.push_back(Entry);
2390 }
2391
2392 return Builder.CreateLoad(Entry, false, "tmp");
2393}
2394
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002395llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002396 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002397
2398 if (!Entry) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002399 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002400 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002401 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002402 llvm::GlobalValue::InternalLinkage,
2403 C, "\01L_OBJC_CLASS_NAME_",
2404 &CGM.getModule());
2405 Entry->setSection("__TEXT,__cstring,cstring_literals");
2406 UsedGlobals.push_back(Entry);
2407 }
2408
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002409 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002410}
2411
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002412llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002413 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
2414
2415 if (!Entry) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00002416 // FIXME: Avoid std::string copying.
2417 llvm::Constant *C = llvm::ConstantArray::get(Sel.getAsString());
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002418 Entry =
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002419 new llvm::GlobalVariable(C->getType(), false,
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002420 llvm::GlobalValue::InternalLinkage,
2421 C, "\01L_OBJC_METH_VAR_NAME_",
2422 &CGM.getModule());
2423 Entry->setSection("__TEXT,__cstring,cstring_literals");
2424 UsedGlobals.push_back(Entry);
2425 }
2426
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002427 return getConstantGEP(Entry, 0, 0);
2428}
2429
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002430// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002431llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002432 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
2433}
2434
2435// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002436llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002437 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
2438}
2439
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002440llvm::Constant *CGObjCCommonMac::GetMethodVarType(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002441 llvm::GlobalVariable *&Entry = MethodVarTypes[Name];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002442
2443 if (!Entry) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002444 llvm::Constant *C = llvm::ConstantArray::get(Name);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002445 Entry =
2446 new llvm::GlobalVariable(C->getType(), false,
2447 llvm::GlobalValue::InternalLinkage,
2448 C, "\01L_OBJC_METH_VAR_TYPE_",
2449 &CGM.getModule());
2450 Entry->setSection("__TEXT,__cstring,cstring_literals");
2451 UsedGlobals.push_back(Entry);
2452 }
2453
2454 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002455}
2456
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002457// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002458llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002459 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002460 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
2461 TypeStr);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002462 return GetMethodVarType(TypeStr);
2463}
2464
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002465// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002466llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002467 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
2468
2469 if (!Entry) {
2470 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName());
2471 Entry =
2472 new llvm::GlobalVariable(C->getType(), false,
2473 llvm::GlobalValue::InternalLinkage,
2474 C, "\01L_OBJC_PROP_NAME_ATTR_",
2475 &CGM.getModule());
2476 Entry->setSection("__TEXT,__cstring,cstring_literals");
2477 UsedGlobals.push_back(Entry);
2478 }
2479
2480 return getConstantGEP(Entry, 0, 0);
2481}
2482
2483// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002484// FIXME: This Decl should be more precise.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002485llvm::Constant *CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002486 const Decl *Container) {
2487 std::string TypeStr;
2488 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002489 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
2490}
2491
Fariborz Jahanian56210f72009-01-21 23:34:32 +00002492void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
2493 const ObjCContainerDecl *CD,
2494 std::string &NameOut) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002495 // FIXME: Find the mangling GCC uses.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002496 NameOut = (D->isInstanceMethod() ? "-" : "+");
Chris Lattner077bf5e2008-11-24 03:33:13 +00002497 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002498 assert (CD && "Missing container decl in GetNameForMethod");
2499 NameOut += CD->getNameAsString();
Fariborz Jahanian52847332009-01-26 23:49:05 +00002500 // FIXME. For a method in a category, (CAT_NAME) is inserted here.
2501 // Right now! there is not enough info. to do this.
Chris Lattner077bf5e2008-11-24 03:33:13 +00002502 NameOut += ' ';
2503 NameOut += D->getSelector().getAsString();
2504 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002505}
2506
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002507/// GetFirstIvarInRecord - This routine returns the record for the
2508/// implementation of the fiven class OID. It also returns field
2509/// corresponding to the first ivar in the class in FIV. It also
2510/// returns the one before the first ivar.
2511///
2512const RecordDecl *CGObjCCommonMac::GetFirstIvarInRecord(
2513 const ObjCInterfaceDecl *OID,
2514 RecordDecl::field_iterator &FIV,
2515 RecordDecl::field_iterator &PIV) {
2516 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass());
2517 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID);
2518 RecordDecl::field_iterator ifield = RD->field_begin();
2519 RecordDecl::field_iterator pfield = RD->field_end();
2520 while (countSuperClassIvars-- > 0) {
2521 pfield = ifield;
2522 ++ifield;
2523 }
2524 FIV = ifield;
2525 PIV = pfield;
2526 return RD;
2527}
2528
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002529void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002530 EmitModuleInfo();
2531
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002532 // Emit the dummy bodies for any protocols which were referenced but
2533 // never defined.
2534 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
2535 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
2536 if (i->second->hasInitializer())
2537 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002538
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002539 std::vector<llvm::Constant*> Values(5);
2540 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
2541 Values[1] = GetClassName(i->first);
2542 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
2543 Values[3] = Values[4] =
2544 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
2545 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
2546 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
2547 Values));
2548 }
2549
2550 std::vector<llvm::Constant*> Used;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002551 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002552 e = UsedGlobals.end(); i != e; ++i) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002553 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002554 }
2555
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002556 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002557 llvm::GlobalValue *GV =
2558 new llvm::GlobalVariable(AT, false,
2559 llvm::GlobalValue::AppendingLinkage,
2560 llvm::ConstantArray::get(AT, Used),
2561 "llvm.used",
2562 &CGM.getModule());
2563
2564 GV->setSection("llvm.metadata");
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002565
2566 // Add assembler directives to add lazy undefined symbol references
2567 // for classes which are referenced but not defined. This is
2568 // important for correct linker interaction.
2569
2570 // FIXME: Uh, this isn't particularly portable.
2571 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00002572
2573 if (!CGM.getModule().getModuleInlineAsm().empty())
2574 s << "\n";
2575
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002576 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
2577 e = LazySymbols.end(); i != e; ++i) {
2578 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
2579 }
2580 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
2581 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002582 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002583 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
2584 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00002585
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002586 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002587}
2588
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002589CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002590 : CGObjCCommonMac(cgm),
2591 ObjCTypes(cgm)
2592{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00002593 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002594 ObjCABI = 2;
2595}
2596
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002597/* *** */
2598
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002599ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
2600: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00002601{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002602 CodeGen::CodeGenTypes &Types = CGM.getTypes();
2603 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002604
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002605 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002606 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002607 LongTy = Types.ConvertType(Ctx.LongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002608 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
2609
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002610 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00002611 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002612 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002613
2614 // FIXME: It would be nice to unify this with the opaque type, so
2615 // that the IR comes out a bit cleaner.
2616 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
2617 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002618
2619 // I'm not sure I like this. The implicit coordination is a bit
2620 // gross. We should solve this in a reasonable fashion because this
2621 // is a pretty common task (match some runtime data structure with
2622 // an LLVM data structure).
2623
2624 // FIXME: This is leaked.
2625 // FIXME: Merge with rewriter code?
2626
2627 // struct _objc_super {
2628 // id self;
2629 // Class cls;
2630 // }
2631 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
2632 SourceLocation(),
2633 &Ctx.Idents.get("_objc_super"));
2634 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2635 Ctx.getObjCIdType(), 0, false));
2636 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
2637 Ctx.getObjCClassType(), 0, false));
2638 RD->completeDefinition(Ctx);
2639
2640 SuperCTy = Ctx.getTagDeclType(RD);
2641 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
2642
2643 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002644 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
2645
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002646 // struct _prop_t {
2647 // char *name;
2648 // char *attributes;
2649 // }
2650 PropertyTy = llvm::StructType::get(Int8PtrTy,
2651 Int8PtrTy,
2652 NULL);
2653 CGM.getModule().addTypeName("struct._prop_t",
2654 PropertyTy);
2655
2656 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002657 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002658 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002659 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002660 // }
2661 PropertyListTy = llvm::StructType::get(IntTy,
2662 IntTy,
2663 llvm::ArrayType::get(PropertyTy, 0),
2664 NULL);
2665 CGM.getModule().addTypeName("struct._prop_list_t",
2666 PropertyListTy);
2667 // struct _prop_list_t *
2668 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
2669
2670 // struct _objc_method {
2671 // SEL _cmd;
2672 // char *method_type;
2673 // char *_imp;
2674 // }
2675 MethodTy = llvm::StructType::get(SelectorPtrTy,
2676 Int8PtrTy,
2677 Int8PtrTy,
2678 NULL);
2679 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00002680
2681 // struct _objc_cache *
2682 CacheTy = llvm::OpaqueType::get();
2683 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
2684 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00002685
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002686 // Property manipulation functions.
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002687
2688 QualType IdType = Ctx.getObjCIdType();
2689 QualType SelType = Ctx.getObjCSelType();
2690 llvm::SmallVector<QualType,16> Params;
2691 const llvm::FunctionType *FTy;
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002692
2693 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002694 Params.push_back(IdType);
2695 Params.push_back(SelType);
2696 Params.push_back(Ctx.LongTy);
2697 Params.push_back(Ctx.BoolTy);
2698 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params),
2699 false);
2700 GetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002701
2702 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
2703 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002704 Params.push_back(IdType);
2705 Params.push_back(SelType);
2706 Params.push_back(Ctx.LongTy);
2707 Params.push_back(IdType);
2708 Params.push_back(Ctx.BoolTy);
2709 Params.push_back(Ctx.BoolTy);
2710 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
2711 SetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
2712
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002713 // Enumeration mutation.
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002714
2715 // void objc_enumerationMutation (id)
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002716 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002717 Params.push_back(IdType);
2718 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
2719 EnumerationMutationFn = CGM.CreateRuntimeFunction(FTy,
2720 "objc_enumerationMutation");
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002721
2722 // gc's API
2723 // id objc_read_weak (id *)
2724 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002725 Params.push_back(Ctx.getPointerType(IdType));
2726 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
2727 GcReadWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
2728
2729 // id objc_assign_weak (id, id *)
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002730 Params.clear();
Daniel Dunbar34c94a22009-02-04 00:44:42 +00002731 Params.push_back(IdType);
2732 Params.push_back(Ctx.getPointerType(IdType));
2733
2734 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
2735 GcAssignWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
2736 GcAssignGlobalFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
2737 GcAssignIvarFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
2738 GcAssignStrongCastFn =
2739 CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002740}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002741
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002742ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
2743 : ObjCCommonTypesHelper(cgm)
2744{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002745 // struct _objc_method_description {
2746 // SEL name;
2747 // char *types;
2748 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002749 MethodDescriptionTy =
2750 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002751 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002752 NULL);
2753 CGM.getModule().addTypeName("struct._objc_method_description",
2754 MethodDescriptionTy);
2755
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002756 // struct _objc_method_description_list {
2757 // int count;
2758 // struct _objc_method_description[1];
2759 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002760 MethodDescriptionListTy =
2761 llvm::StructType::get(IntTy,
2762 llvm::ArrayType::get(MethodDescriptionTy, 0),
2763 NULL);
2764 CGM.getModule().addTypeName("struct._objc_method_description_list",
2765 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002766
2767 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002768 MethodDescriptionListPtrTy =
2769 llvm::PointerType::getUnqual(MethodDescriptionListTy);
2770
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002771 // Protocol description structures
2772
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002773 // struct _objc_protocol_extension {
2774 // uint32_t size; // sizeof(struct _objc_protocol_extension)
2775 // struct _objc_method_description_list *optional_instance_methods;
2776 // struct _objc_method_description_list *optional_class_methods;
2777 // struct _objc_property_list *instance_properties;
2778 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002779 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002780 llvm::StructType::get(IntTy,
2781 MethodDescriptionListPtrTy,
2782 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002783 PropertyListPtrTy,
2784 NULL);
2785 CGM.getModule().addTypeName("struct._objc_protocol_extension",
2786 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002787
2788 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002789 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
2790
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002791 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002792
2793 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
2794 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
2795
Fariborz Jahanianee0af742009-01-21 22:04:16 +00002796 const llvm::Type *T =
2797 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
2798 LongTy,
2799 llvm::ArrayType::get(ProtocolTyHolder, 0),
2800 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002801 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
2802
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002803 // struct _objc_protocol {
2804 // struct _objc_protocol_extension *isa;
2805 // char *protocol_name;
2806 // struct _objc_protocol **_objc_protocol_list;
2807 // struct _objc_method_description_list *instance_methods;
2808 // struct _objc_method_description_list *class_methods;
2809 // }
2810 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002811 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002812 llvm::PointerType::getUnqual(ProtocolListTyHolder),
2813 MethodDescriptionListPtrTy,
2814 MethodDescriptionListPtrTy,
2815 NULL);
2816 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
2817
2818 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
2819 CGM.getModule().addTypeName("struct._objc_protocol_list",
2820 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002821 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002822 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
2823
2824 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002825 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002826 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002827
2828 // Class description structures
2829
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002830 // struct _objc_ivar {
2831 // char *ivar_name;
2832 // char *ivar_type;
2833 // int ivar_offset;
2834 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002835 IvarTy = llvm::StructType::get(Int8PtrTy,
2836 Int8PtrTy,
2837 IntTy,
2838 NULL);
2839 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
2840
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002841 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002842 IvarListTy = llvm::OpaqueType::get();
2843 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
2844 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
2845
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002846 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002847 MethodListTy = llvm::OpaqueType::get();
2848 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
2849 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
2850
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002851 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002852 ClassExtensionTy =
2853 llvm::StructType::get(IntTy,
2854 Int8PtrTy,
2855 PropertyListPtrTy,
2856 NULL);
2857 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
2858 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
2859
2860 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
2861
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002862 // struct _objc_class {
2863 // Class isa;
2864 // Class super_class;
2865 // char *name;
2866 // long version;
2867 // long info;
2868 // long instance_size;
2869 // struct _objc_ivar_list *ivars;
2870 // struct _objc_method_list *methods;
2871 // struct _objc_cache *cache;
2872 // struct _objc_protocol_list *protocols;
2873 // char *ivar_layout;
2874 // struct _objc_class_ext *ext;
2875 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002876 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
2877 llvm::PointerType::getUnqual(ClassTyHolder),
2878 Int8PtrTy,
2879 LongTy,
2880 LongTy,
2881 LongTy,
2882 IvarListPtrTy,
2883 MethodListPtrTy,
2884 CachePtrTy,
2885 ProtocolListPtrTy,
2886 Int8PtrTy,
2887 ClassExtensionPtrTy,
2888 NULL);
2889 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
2890
2891 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
2892 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
2893 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
2894
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002895 // struct _objc_category {
2896 // char *category_name;
2897 // char *class_name;
2898 // struct _objc_method_list *instance_method;
2899 // struct _objc_method_list *class_method;
2900 // uint32_t size; // sizeof(struct _objc_category)
2901 // struct _objc_property_list *instance_properties;// category's @property
2902 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002903 CategoryTy = llvm::StructType::get(Int8PtrTy,
2904 Int8PtrTy,
2905 MethodListPtrTy,
2906 MethodListPtrTy,
2907 ProtocolListPtrTy,
2908 IntTy,
2909 PropertyListPtrTy,
2910 NULL);
2911 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
2912
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002913 // Global metadata structures
2914
Fariborz Jahanian10a42312009-01-21 00:39:53 +00002915 // struct _objc_symtab {
2916 // long sel_ref_cnt;
2917 // SEL *refs;
2918 // short cls_def_cnt;
2919 // short cat_def_cnt;
2920 // char *defs[cls_def_cnt + cat_def_cnt];
2921 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002922 SymtabTy = llvm::StructType::get(LongTy,
2923 SelectorPtrTy,
2924 ShortTy,
2925 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002926 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002927 NULL);
2928 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
2929 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
2930
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002931 // struct _objc_module {
2932 // long version;
2933 // long size; // sizeof(struct _objc_module)
2934 // char *name;
2935 // struct _objc_symtab* symtab;
2936 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002937 ModuleTy =
2938 llvm::StructType::get(LongTy,
2939 LongTy,
2940 Int8PtrTy,
2941 SymtabPtrTy,
2942 NULL);
2943 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002944
Daniel Dunbar49f66022008-09-24 03:38:44 +00002945 // Message send functions.
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002946
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002947 // id objc_msgSend (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002948 std::vector<const llvm::Type*> Params;
2949 Params.push_back(ObjectPtrTy);
2950 Params.push_back(SelectorPtrTy);
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002951 MessageSendFn =
2952 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2953 Params,
2954 true),
2955 "objc_msgSend");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002956
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002957 // id objc_msgSend_stret (id, SEL, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002958 Params.clear();
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002959 Params.push_back(ObjectPtrTy);
2960 Params.push_back(SelectorPtrTy);
2961 MessageSendStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002962 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2963 Params,
2964 true),
2965 "objc_msgSend_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00002966
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002967 //
Daniel Dunbar5669e572008-10-17 03:24:53 +00002968 Params.clear();
2969 Params.push_back(ObjectPtrTy);
2970 Params.push_back(SelectorPtrTy);
2971 // FIXME: This should be long double on x86_64?
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002972 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
Daniel Dunbar5669e572008-10-17 03:24:53 +00002973 MessageSendFpretFn =
2974 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
2975 Params,
2976 true),
2977 "objc_msgSend_fpret");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002978
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002979 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002980 Params.clear();
2981 Params.push_back(SuperPtrTy);
2982 Params.push_back(SelectorPtrTy);
2983 MessageSendSuperFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002984 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
2985 Params,
2986 true),
2987 "objc_msgSendSuper");
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002988
Fariborz Jahaniandb286862009-01-22 00:37:21 +00002989 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
2990 // SEL op, ...)
Daniel Dunbar14c80b72008-08-23 09:25:55 +00002991 Params.clear();
2992 Params.push_back(Int8PtrTy);
2993 Params.push_back(SuperPtrTy);
2994 Params.push_back(SelectorPtrTy);
2995 MessageSendSuperStretFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00002996 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
2997 Params,
2998 true),
2999 "objc_msgSendSuper_stret");
Daniel Dunbar5669e572008-10-17 03:24:53 +00003000
3001 // There is no objc_msgSendSuper_fpret? How can that work?
3002 MessageSendSuperFpretFn = MessageSendSuperFn;
Anders Carlsson2abd89c2008-08-31 04:05:03 +00003003
Anders Carlsson124526b2008-09-09 10:10:21 +00003004 // FIXME: This is the size of the setjmp buffer and should be
3005 // target specific. 18 is what's used on 32-bit X86.
3006 uint64_t SetJmpBufferSize = 18;
3007
3008 // Exceptions
3009 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00003010 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00003011
3012 ExceptionDataTy =
3013 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
3014 SetJmpBufferSize),
3015 StackPtrTy, NULL);
3016 CGM.getModule().addTypeName("struct._objc_exception_data",
3017 ExceptionDataTy);
3018
3019 Params.clear();
3020 Params.push_back(ObjectPtrTy);
3021 ExceptionThrowFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003022 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3023 Params,
3024 false),
3025 "objc_exception_throw");
Anders Carlsson124526b2008-09-09 10:10:21 +00003026
3027 Params.clear();
3028 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
3029 ExceptionTryEnterFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003030 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3031 Params,
3032 false),
3033 "objc_exception_try_enter");
Anders Carlsson124526b2008-09-09 10:10:21 +00003034 ExceptionTryExitFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003035 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3036 Params,
3037 false),
3038 "objc_exception_try_exit");
Anders Carlsson124526b2008-09-09 10:10:21 +00003039 ExceptionExtractFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003040 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3041 Params,
3042 false),
3043 "objc_exception_extract");
Anders Carlsson124526b2008-09-09 10:10:21 +00003044
3045 Params.clear();
3046 Params.push_back(ClassPtrTy);
3047 Params.push_back(ObjectPtrTy);
3048 ExceptionMatchFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003049 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3050 Params,
3051 false),
3052 "objc_exception_match");
Chris Lattner10cac6f2008-11-15 21:26:17 +00003053
3054 // synchronized APIs
3055 // void objc_sync_enter (id)
3056 Params.clear();
3057 Params.push_back(ObjectPtrTy);
3058 SyncEnterFn =
3059 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3060 Params,
3061 false),
3062 "objc_sync_enter");
3063 // void objc_sync_exit (id)
3064 SyncExitFn =
3065 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
3066 Params,
3067 false),
3068 "objc_sync_exit");
3069
Anders Carlsson124526b2008-09-09 10:10:21 +00003070
3071 Params.clear();
3072 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
3073 SetJmpFn =
Daniel Dunbarad2dc712008-10-01 01:06:06 +00003074 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
3075 Params,
3076 false),
3077 "_setjmp");
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00003078
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003079}
3080
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003081ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003082: ObjCCommonTypesHelper(cgm)
3083{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003084 // struct _method_list_t {
3085 // uint32_t entsize; // sizeof(struct _objc_method)
3086 // uint32_t method_count;
3087 // struct _objc_method method_list[method_count];
3088 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003089 MethodListnfABITy = llvm::StructType::get(IntTy,
3090 IntTy,
3091 llvm::ArrayType::get(MethodTy, 0),
3092 NULL);
3093 CGM.getModule().addTypeName("struct.__method_list_t",
3094 MethodListnfABITy);
3095 // struct method_list_t *
3096 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003097
3098 // struct _protocol_t {
3099 // id isa; // NULL
3100 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003101 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003102 // const struct method_list_t * const instance_methods;
3103 // const struct method_list_t * const class_methods;
3104 // const struct method_list_t *optionalInstanceMethods;
3105 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003106 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003107 // const uint32_t size; // sizeof(struct _protocol_t)
3108 // const uint32_t flags; // = 0
3109 // }
3110
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003111 // Holder for struct _protocol_list_t *
3112 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3113
3114 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
3115 Int8PtrTy,
3116 llvm::PointerType::getUnqual(
3117 ProtocolListTyHolder),
3118 MethodListnfABIPtrTy,
3119 MethodListnfABIPtrTy,
3120 MethodListnfABIPtrTy,
3121 MethodListnfABIPtrTy,
3122 PropertyListPtrTy,
3123 IntTy,
3124 IntTy,
3125 NULL);
3126 CGM.getModule().addTypeName("struct._protocol_t",
3127 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003128
3129 // struct _protocol_t*
3130 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003131
Fariborz Jahanianda320092009-01-29 19:24:30 +00003132 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003133 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00003134 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003135 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003136 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3137 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003138 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003139 NULL);
3140 CGM.getModule().addTypeName("struct._objc_protocol_list",
3141 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003142 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3143 ProtocolListnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003144
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003145 // struct _objc_protocol_list*
3146 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003147
3148 // struct _ivar_t {
3149 // unsigned long int *offset; // pointer to ivar offset location
3150 // char *name;
3151 // char *type;
3152 // uint32_t alignment;
3153 // uint32_t size;
3154 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003155 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3156 Int8PtrTy,
3157 Int8PtrTy,
3158 IntTy,
3159 IntTy,
3160 NULL);
3161 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3162
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003163 // struct _ivar_list_t {
3164 // uint32 entsize; // sizeof(struct _ivar_t)
3165 // uint32 count;
3166 // struct _iver_t list[count];
3167 // }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003168 IvarListnfABITy = llvm::StructType::get(IntTy,
3169 IntTy,
3170 llvm::ArrayType::get(
3171 IvarnfABITy, 0),
3172 NULL);
3173 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3174
3175 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003176
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003177 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003178 // uint32_t const flags;
3179 // uint32_t const instanceStart;
3180 // uint32_t const instanceSize;
3181 // uint32_t const reserved; // only when building for 64bit targets
3182 // const uint8_t * const ivarLayout;
3183 // const char *const name;
3184 // const struct _method_list_t * const baseMethods;
3185 // const struct _objc_protocol_list *const baseProtocols;
3186 // const struct _ivar_list_t *const ivars;
3187 // const uint8_t * const weakIvarLayout;
3188 // const struct _prop_list_t * const properties;
3189 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003190
3191 // FIXME. Add 'reserved' field in 64bit abi mode!
3192 ClassRonfABITy = llvm::StructType::get(IntTy,
3193 IntTy,
3194 IntTy,
3195 Int8PtrTy,
3196 Int8PtrTy,
3197 MethodListnfABIPtrTy,
3198 ProtocolListnfABIPtrTy,
3199 IvarListnfABIPtrTy,
3200 Int8PtrTy,
3201 PropertyListPtrTy,
3202 NULL);
3203 CGM.getModule().addTypeName("struct._class_ro_t",
3204 ClassRonfABITy);
3205
3206 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3207 std::vector<const llvm::Type*> Params;
3208 Params.push_back(ObjectPtrTy);
3209 Params.push_back(SelectorPtrTy);
3210 ImpnfABITy = llvm::PointerType::getUnqual(
3211 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3212
3213 // struct _class_t {
3214 // struct _class_t *isa;
3215 // struct _class_t * const superclass;
3216 // void *cache;
3217 // IMP *vtable;
3218 // struct class_ro_t *ro;
3219 // }
3220
3221 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3222 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3223 llvm::PointerType::getUnqual(ClassTyHolder),
3224 CachePtrTy,
3225 llvm::PointerType::getUnqual(ImpnfABITy),
3226 llvm::PointerType::getUnqual(
3227 ClassRonfABITy),
3228 NULL);
3229 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3230
3231 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3232 ClassnfABITy);
3233
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003234 // LLVM for struct _class_t *
3235 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3236
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003237 // struct _category_t {
3238 // const char * const name;
3239 // struct _class_t *const cls;
3240 // const struct _method_list_t * const instance_methods;
3241 // const struct _method_list_t * const class_methods;
3242 // const struct _protocol_list_t * const protocols;
3243 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003244 // }
3245 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003246 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003247 MethodListnfABIPtrTy,
3248 MethodListnfABIPtrTy,
3249 ProtocolListnfABIPtrTy,
3250 PropertyListPtrTy,
3251 NULL);
3252 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003253
3254 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003255 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3256 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003257
3258 // MessageRefTy - LLVM for:
3259 // struct _message_ref_t {
3260 // IMP messenger;
3261 // SEL name;
3262 // };
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003263
3264 // First the clang type for struct _message_ref_t
3265 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3266 SourceLocation(),
3267 &Ctx.Idents.get("_message_ref_t"));
3268 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3269 Ctx.VoidPtrTy, 0, false));
3270 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3271 Ctx.getObjCSelType(), 0, false));
3272 RD->completeDefinition(Ctx);
3273
3274 MessageRefCTy = Ctx.getTagDeclType(RD);
3275 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
3276 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003277
3278 // MessageRefPtrTy - LLVM for struct _message_ref_t*
3279 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
3280
3281 // SuperMessageRefTy - LLVM for:
3282 // struct _super_message_ref_t {
3283 // SUPER_IMP messenger;
3284 // SEL name;
3285 // };
3286 SuperMessageRefTy = llvm::StructType::get(ImpnfABITy,
3287 SelectorPtrTy,
3288 NULL);
3289 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
3290
3291 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
3292 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
3293
3294 // id objc_msgSend_fixup (id, struct message_ref_t*, ...)
3295 Params.clear();
3296 Params.push_back(ObjectPtrTy);
3297 Params.push_back(MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00003298 MessengerTy = llvm::FunctionType::get(ObjectPtrTy,
3299 Params,
3300 true);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003301 MessageSendFixupFn =
Fariborz Jahanianef163782009-02-05 01:13:09 +00003302 CGM.CreateRuntimeFunction(MessengerTy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003303 "objc_msgSend_fixup");
3304
3305 // id objc_msgSend_fpret_fixup (id, struct message_ref_t*, ...)
3306 MessageSendFpretFixupFn =
3307 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3308 Params,
3309 true),
3310 "objc_msgSend_fpret_fixup");
3311
3312 // id objc_msgSend_stret_fixup (id, struct message_ref_t*, ...)
3313 MessageSendStretFixupFn =
3314 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3315 Params,
3316 true),
3317 "objc_msgSend_stret_fixup");
3318
3319 // id objc_msgSendId_fixup (id, struct message_ref_t*, ...)
3320 MessageSendIdFixupFn =
3321 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3322 Params,
3323 true),
3324 "objc_msgSendId_fixup");
3325
3326
3327 // id objc_msgSendId_stret_fixup (id, struct message_ref_t*, ...)
3328 MessageSendIdStretFixupFn =
3329 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3330 Params,
3331 true),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003332 "objc_msgSendId_stret_fixup");
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003333
3334 // id objc_msgSendSuper2_fixup (struct objc_super *,
3335 // struct _super_message_ref_t*, ...)
3336 Params.clear();
3337 Params.push_back(SuperPtrTy);
3338 Params.push_back(SuperMessageRefPtrTy);
3339 MessageSendSuper2FixupFn =
3340 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3341 Params,
3342 true),
3343 "objc_msgSendSuper2_fixup");
3344
3345
3346 // id objc_msgSendSuper2_stret_fixup (struct objc_super *,
3347 // struct _super_message_ref_t*, ...)
3348 MessageSendSuper2StretFixupFn =
3349 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
3350 Params,
3351 true),
3352 "objc_msgSendSuper2_stret_fixup");
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003353
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003354}
3355
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003356llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
3357 FinishNonFragileABIModule();
3358
3359 return NULL;
3360}
3361
3362void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
3363 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003364
3365 // Build list of all implemented classe addresses in array
3366 // L_OBJC_LABEL_CLASS_$.
3367 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$
3368 // list of 'nonlazy' implementations (defined as those with a +load{}
3369 // method!!).
3370 unsigned NumClasses = DefinedClasses.size();
3371 if (NumClasses) {
3372 std::vector<llvm::Constant*> Symbols(NumClasses);
3373 for (unsigned i=0; i<NumClasses; i++)
3374 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
3375 ObjCTypes.Int8PtrTy);
3376 llvm::Constant* Init =
3377 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3378 NumClasses),
3379 Symbols);
3380
3381 llvm::GlobalVariable *GV =
3382 new llvm::GlobalVariable(Init->getType(), false,
3383 llvm::GlobalValue::InternalLinkage,
3384 Init,
3385 "\01L_OBJC_LABEL_CLASS_$",
3386 &CGM.getModule());
3387 GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip");
3388 UsedGlobals.push_back(GV);
3389 }
3390
3391 // Build list of all implemented category addresses in array
3392 // L_OBJC_LABEL_CATEGORY_$.
3393 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$
3394 // list of 'nonlazy' category implementations (defined as those with a +load{}
3395 // method!!).
3396 unsigned NumCategory = DefinedCategories.size();
3397 if (NumCategory) {
3398 std::vector<llvm::Constant*> Symbols(NumCategory);
3399 for (unsigned i=0; i<NumCategory; i++)
3400 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i],
3401 ObjCTypes.Int8PtrTy);
3402 llvm::Constant* Init =
3403 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3404 NumCategory),
3405 Symbols);
3406
3407 llvm::GlobalVariable *GV =
3408 new llvm::GlobalVariable(Init->getType(), false,
3409 llvm::GlobalValue::InternalLinkage,
3410 Init,
3411 "\01L_OBJC_LABEL_CATEGORY_$",
3412 &CGM.getModule());
3413 GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip");
3414 UsedGlobals.push_back(GV);
3415 }
3416
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00003417 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
3418 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
3419 std::vector<llvm::Constant*> Values(2);
3420 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3421 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
3422 llvm::Constant* Init = llvm::ConstantArray::get(
3423 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
3424 Values);
3425 llvm::GlobalVariable *IMGV =
3426 new llvm::GlobalVariable(Init->getType(), false,
3427 llvm::GlobalValue::InternalLinkage,
3428 Init,
3429 "\01L_OBJC_IMAGE_INFO",
3430 &CGM.getModule());
3431 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
3432 UsedGlobals.push_back(IMGV);
3433
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003434 std::vector<llvm::Constant*> Used;
3435 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
3436 e = UsedGlobals.end(); i != e; ++i) {
3437 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy));
3438 }
3439
3440 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size());
3441 llvm::GlobalValue *GV =
3442 new llvm::GlobalVariable(AT, false,
3443 llvm::GlobalValue::AppendingLinkage,
3444 llvm::ConstantArray::get(AT, Used),
3445 "llvm.used",
3446 &CGM.getModule());
3447
3448 GV->setSection("llvm.metadata");
3449
3450}
3451
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003452// Metadata flags
3453enum MetaDataDlags {
3454 CLS = 0x0,
3455 CLS_META = 0x1,
3456 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003457 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003458 CLS_EXCEPTION = 0x20
3459};
3460/// BuildClassRoTInitializer - generate meta-data for:
3461/// struct _class_ro_t {
3462/// uint32_t const flags;
3463/// uint32_t const instanceStart;
3464/// uint32_t const instanceSize;
3465/// uint32_t const reserved; // only when building for 64bit targets
3466/// const uint8_t * const ivarLayout;
3467/// const char *const name;
3468/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00003469/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003470/// const struct _ivar_list_t *const ivars;
3471/// const uint8_t * const weakIvarLayout;
3472/// const struct _prop_list_t * const properties;
3473/// }
3474///
3475llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
3476 unsigned flags,
3477 unsigned InstanceStart,
3478 unsigned InstanceSize,
3479 const ObjCImplementationDecl *ID) {
3480 std::string ClassName = ID->getNameAsString();
3481 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
3482 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
3483 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
3484 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
3485 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianda320092009-01-29 19:24:30 +00003486 // FIXME. ivarLayout is currently null!
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003487 Values[ 3] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3488 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003489 // const struct _method_list_t * const baseMethods;
3490 std::vector<llvm::Constant*> Methods;
3491 std::string MethodListName("\01l_OBJC_$_");
3492 if (flags & CLS_META) {
3493 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
3494 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(),
3495 e = ID->classmeth_end(); i != e; ++i) {
3496 // Class methods should always be defined.
3497 Methods.push_back(GetMethodConstant(*i));
3498 }
3499 } else {
3500 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
3501 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(),
3502 e = ID->instmeth_end(); i != e; ++i) {
3503 // Instance methods should always be defined.
3504 Methods.push_back(GetMethodConstant(*i));
3505 }
Fariborz Jahanian939abce2009-01-28 22:46:49 +00003506 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(),
3507 e = ID->propimpl_end(); i != e; ++i) {
3508 ObjCPropertyImplDecl *PID = *i;
3509
3510 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
3511 ObjCPropertyDecl *PD = PID->getPropertyDecl();
3512
3513 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
3514 if (llvm::Constant *C = GetMethodConstant(MD))
3515 Methods.push_back(C);
3516 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
3517 if (llvm::Constant *C = GetMethodConstant(MD))
3518 Methods.push_back(C);
3519 }
3520 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003521 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003522 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003523 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003524
3525 const ObjCInterfaceDecl *OID = ID->getClassInterface();
3526 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
3527 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
3528 + OID->getNameAsString(),
3529 OID->protocol_begin(),
3530 OID->protocol_end());
3531
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003532 if (flags & CLS_META)
3533 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
3534 else
3535 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianda320092009-01-29 19:24:30 +00003536 // FIXME. weakIvarLayout is currently null.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003537 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003538 if (flags & CLS_META)
3539 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3540 else
3541 Values[ 9] =
3542 EmitPropertyList(
3543 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
3544 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003545 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
3546 Values);
3547 llvm::GlobalVariable *CLASS_RO_GV =
3548 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
3549 llvm::GlobalValue::InternalLinkage,
3550 Init,
3551 (flags & CLS_META) ?
3552 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
3553 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
3554 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003555 CLASS_RO_GV->setAlignment(
3556 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003557 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003558 UsedGlobals.push_back(CLASS_RO_GV);
3559 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003560
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003561}
3562
3563/// BuildClassMetaData - This routine defines that to-level meta-data
3564/// for the given ClassName for:
3565/// struct _class_t {
3566/// struct _class_t *isa;
3567/// struct _class_t * const superclass;
3568/// void *cache;
3569/// IMP *vtable;
3570/// struct class_ro_t *ro;
3571/// }
3572///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003573llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
3574 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003575 llvm::Constant *IsAGV,
3576 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003577 llvm::Constant *ClassRoGV,
3578 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003579 std::vector<llvm::Constant*> Values(5);
3580 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003581 Values[1] = SuperClassGV
3582 ? SuperClassGV
3583 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003584 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
3585 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
3586 Values[4] = ClassRoGV; // &CLASS_RO_GV
3587 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
3588 Values);
3589 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(ClassName);
3590 if (GV)
3591 GV->setInitializer(Init);
3592 else
3593 GV =
3594 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3595 llvm::GlobalValue::ExternalLinkage,
3596 Init,
3597 ClassName,
3598 &CGM.getModule());
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00003599 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003600 GV->setAlignment(
3601 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003602 if (HiddenVisibility)
3603 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003604 UsedGlobals.push_back(GV);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003605 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003606}
3607
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003608void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
3609 std::string ClassName = ID->getNameAsString();
3610 if (!ObjCEmptyCacheVar) {
3611 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003612 ObjCTypes.CacheTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003613 false,
3614 llvm::GlobalValue::ExternalLinkage,
3615 0,
Fariborz Jahanian07da3672009-02-03 17:34:34 +00003616 "\01__objc_empty_cache",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003617 &CGM.getModule());
3618 UsedGlobals.push_back(ObjCEmptyCacheVar);
3619
3620 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003621 ObjCTypes.ImpnfABITy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003622 false,
3623 llvm::GlobalValue::ExternalLinkage,
3624 0,
Fariborz Jahanian07da3672009-02-03 17:34:34 +00003625 "\01__objc_empty_vtable",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003626 &CGM.getModule());
3627 UsedGlobals.push_back(ObjCEmptyVtableVar);
3628 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003629 assert(ID->getClassInterface() &&
3630 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003631 uint32_t InstanceStart =
3632 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy);
3633 uint32_t InstanceSize = InstanceStart;
3634 uint32_t flags = CLS_META;
3635 std::string ObjCMetaClassName("\01_OBJC_METACLASS_$_");
3636 std::string ObjCClassName("\01_OBJC_CLASS_$_");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003637
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003638 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003639
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003640 bool classIsHidden = IsClassHidden(ID->getClassInterface());
3641 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003642 flags |= OBJC2_CLS_HIDDEN;
3643 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003644 // class is root
3645 flags |= CLS_ROOT;
3646 std::string SuperClassName = ObjCClassName + ClassName;
3647 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003648 if (!SuperClassGV) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003649 SuperClassGV =
3650 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3651 llvm::GlobalValue::ExternalLinkage,
3652 0,
3653 SuperClassName,
3654 &CGM.getModule());
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003655 UsedGlobals.push_back(SuperClassGV);
3656 }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003657 std::string IsAClassName = ObjCMetaClassName + ClassName;
3658 IsAGV = CGM.getModule().getGlobalVariable(IsAClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003659 if (!IsAGV) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003660 IsAGV =
3661 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3662 llvm::GlobalValue::ExternalLinkage,
3663 0,
3664 IsAClassName,
3665 &CGM.getModule());
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003666 UsedGlobals.push_back(IsAGV);
3667 }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003668 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003669 // Has a root. Current class is not a root.
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003670 std::string RootClassName =
3671 ID->getClassInterface()->getSuperClass()->getNameAsString();
3672 std::string SuperClassName = ObjCMetaClassName + RootClassName;
3673 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003674 if (!SuperClassGV) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003675 SuperClassGV =
3676 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3677 llvm::GlobalValue::ExternalLinkage,
3678 0,
3679 SuperClassName,
3680 &CGM.getModule());
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003681 UsedGlobals.push_back(SuperClassGV);
3682 }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003683 IsAGV = SuperClassGV;
3684 }
3685 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
3686 InstanceStart,
3687 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003688 std::string TClassName = ObjCMetaClassName + ClassName;
3689 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003690 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
3691 classIsHidden);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003692
3693 // Metadata for the class
3694 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003695 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003696 flags |= OBJC2_CLS_HIDDEN;
3697 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003698 flags |= CLS_ROOT;
3699 SuperClassGV = 0;
3700 }
3701 else {
3702 // Has a root. Current class is not a root.
3703 std::string RootClassName =
3704 ID->getClassInterface()->getSuperClass()->getNameAsString();
3705 std::string SuperClassName = ObjCClassName + RootClassName;
3706 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003707 if (!SuperClassGV) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003708 SuperClassGV =
3709 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3710 llvm::GlobalValue::ExternalLinkage,
3711 0,
3712 SuperClassName,
3713 &CGM.getModule());
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00003714 UsedGlobals.push_back(SuperClassGV);
3715 }
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003716 }
3717
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003718 InstanceStart = InstanceSize = 0;
3719 if (ObjCInterfaceDecl *OID =
3720 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface())) {
3721 // FIXME. Share this with the one in EmitIvarList.
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003722 const llvm::Type *InterfaceTy =
Fariborz Jahanianf3710ba2009-02-14 20:13:28 +00003723 CGM.getTypes().ConvertType(CGM.getContext().buildObjCInterfaceType(OID));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003724 const llvm::StructLayout *Layout =
3725 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003726
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00003727 RecordDecl::field_iterator firstField, lastField;
3728 const RecordDecl *RD = GetFirstIvarInRecord(OID, firstField, lastField);
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003729
3730 for (RecordDecl::field_iterator e = RD->field_end(),
3731 ifield = firstField; ifield != e; ++ifield)
3732 lastField = ifield;
3733
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003734 if (lastField != RD->field_end()) {
3735 FieldDecl *Field = *lastField;
3736 const llvm::Type *FieldTy =
3737 CGM.getTypes().ConvertTypeForMem(Field->getType());
3738 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
3739 InstanceSize = Layout->getElementOffset(
3740 CGM.getTypes().getLLVMFieldNo(Field)) +
3741 Size;
3742 if (firstField == RD->field_end())
3743 InstanceStart = InstanceSize;
3744 else
3745 InstanceStart = Layout->getElementOffset(CGM.getTypes().
3746 getLLVMFieldNo(*firstField));
3747 }
3748 }
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003749 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00003750 InstanceStart,
3751 InstanceSize,
3752 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00003753
3754 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003755 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00003756 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
3757 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00003758 DefinedClasses.push_back(ClassMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003759}
3760
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00003761/// GenerateProtocolRef - This routine is called to generate code for
3762/// a protocol reference expression; as in:
3763/// @code
3764/// @protocol(Proto1);
3765/// @endcode
3766/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
3767/// which will hold address of the protocol meta-data.
3768///
3769llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
3770 const ObjCProtocolDecl *PD) {
3771
3772 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
3773 ObjCTypes.ExternalProtocolPtrTy);
3774
3775 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
3776 ProtocolName += PD->getNameAsCString();
3777
3778 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
3779 if (PTGV)
3780 return Builder.CreateLoad(PTGV, false, "tmp");
3781 PTGV = new llvm::GlobalVariable(
3782 Init->getType(), false,
3783 llvm::GlobalValue::WeakLinkage,
3784 Init,
3785 ProtocolName,
3786 &CGM.getModule());
3787 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
3788 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
3789 UsedGlobals.push_back(PTGV);
3790 return Builder.CreateLoad(PTGV, false, "tmp");
3791}
3792
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003793/// GenerateCategory - Build metadata for a category implementation.
3794/// struct _category_t {
3795/// const char * const name;
3796/// struct _class_t *const cls;
3797/// const struct _method_list_t * const instance_methods;
3798/// const struct _method_list_t * const class_methods;
3799/// const struct _protocol_list_t * const protocols;
3800/// const struct _prop_list_t * const properties;
3801/// }
3802///
3803void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD)
3804{
3805 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003806 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
3807 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003808 "_$_" + OCD->getNameAsString());
3809 std::string ExtClassName("\01_OBJC_CLASS_$_" + Interface->getNameAsString());
3810
3811 std::vector<llvm::Constant*> Values(6);
3812 Values[0] = GetClassName(OCD->getIdentifier());
3813 // meta-class entry symbol
3814 llvm::GlobalVariable *ClassGV =
3815 CGM.getModule().getGlobalVariable(ExtClassName);
3816 if (!ClassGV)
3817 ClassGV =
3818 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
3819 llvm::GlobalValue::ExternalLinkage,
3820 0,
3821 ExtClassName,
3822 &CGM.getModule());
3823 UsedGlobals.push_back(ClassGV);
3824 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003825 std::vector<llvm::Constant*> Methods;
3826 std::string MethodListName(Prefix);
3827 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
3828 "_$_" + OCD->getNameAsString();
3829
3830 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(),
3831 e = OCD->instmeth_end(); i != e; ++i) {
3832 // Instance methods should always be defined.
3833 Methods.push_back(GetMethodConstant(*i));
3834 }
3835
3836 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003837 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003838 Methods);
3839
3840 MethodListName = Prefix;
3841 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
3842 OCD->getNameAsString();
3843 Methods.clear();
3844 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(),
3845 e = OCD->classmeth_end(); i != e; ++i) {
3846 // Class methods should always be defined.
3847 Methods.push_back(GetMethodConstant(*i));
3848 }
3849
3850 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003851 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00003852 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00003853 const ObjCCategoryDecl *Category =
3854 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00003855 if (Category) {
3856 std::string ExtName(Interface->getNameAsString() + "_$_" +
3857 OCD->getNameAsString());
3858 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
3859 + Interface->getNameAsString() + "_$_"
3860 + Category->getNameAsString(),
3861 Category->protocol_begin(),
3862 Category->protocol_end());
3863 Values[5] =
3864 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
3865 OCD, Category, ObjCTypes);
3866 }
3867 else {
3868 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
3869 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
3870 }
3871
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003872 llvm::Constant *Init =
3873 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
3874 Values);
3875 llvm::GlobalVariable *GCATV
3876 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
3877 false,
3878 llvm::GlobalValue::InternalLinkage,
3879 Init,
3880 ExtCatName,
3881 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003882 GCATV->setAlignment(
3883 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003884 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00003885 UsedGlobals.push_back(GCATV);
3886 DefinedCategories.push_back(GCATV);
3887}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003888
3889/// GetMethodConstant - Return a struct objc_method constant for the
3890/// given method if it has been defined. The result is null if the
3891/// method has not been defined. The return value has type MethodPtrTy.
3892llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
3893 const ObjCMethodDecl *MD) {
3894 // FIXME: Use DenseMap::lookup
3895 llvm::Function *Fn = MethodDefinitions[MD];
3896 if (!Fn)
3897 return 0;
3898
3899 std::vector<llvm::Constant*> Method(3);
3900 Method[0] =
3901 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
3902 ObjCTypes.SelectorPtrTy);
3903 Method[1] = GetMethodVarType(MD);
3904 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
3905 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
3906}
3907
3908/// EmitMethodList - Build meta-data for method declarations
3909/// struct _method_list_t {
3910/// uint32_t entsize; // sizeof(struct _objc_method)
3911/// uint32_t method_count;
3912/// struct _objc_method method_list[method_count];
3913/// }
3914///
3915llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
3916 const std::string &Name,
3917 const char *Section,
3918 const ConstantVector &Methods) {
3919 // Return null for empty list.
3920 if (Methods.empty())
3921 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
3922
3923 std::vector<llvm::Constant*> Values(3);
3924 // sizeof(struct _objc_method)
3925 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy);
3926 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
3927 // method_count
3928 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
3929 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
3930 Methods.size());
3931 Values[2] = llvm::ConstantArray::get(AT, Methods);
3932 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
3933
3934 llvm::GlobalVariable *GV =
3935 new llvm::GlobalVariable(Init->getType(), false,
3936 llvm::GlobalValue::InternalLinkage,
3937 Init,
3938 Name,
3939 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003940 GV->setAlignment(
3941 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00003942 GV->setSection(Section);
3943 UsedGlobals.push_back(GV);
3944 return llvm::ConstantExpr::getBitCast(GV,
3945 ObjCTypes.MethodListnfABIPtrTy);
3946}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00003947
Fariborz Jahanianed157d32009-02-10 20:21:06 +00003948/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
3949/// the given ivar.
3950///
3951llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(
3952 std::string &Name,
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00003953 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00003954 const ObjCIvarDecl *Ivar) {
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00003955 Name += "\01_OBJC_IVAR_$_" +
3956 getInterfaceDeclForIvar(ID, Ivar)->getNameAsString() + '.'
Fariborz Jahanianed157d32009-02-10 20:21:06 +00003957 + Ivar->getNameAsString();
3958 llvm::GlobalVariable *IvarOffsetGV =
3959 CGM.getModule().getGlobalVariable(Name);
3960 if (!IvarOffsetGV)
3961 IvarOffsetGV =
3962 new llvm::GlobalVariable(ObjCTypes.LongTy,
3963 false,
3964 llvm::GlobalValue::ExternalLinkage,
3965 0,
3966 Name,
3967 &CGM.getModule());
3968 return IvarOffsetGV;
3969}
3970
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003971llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
Fariborz Jahanianed157d32009-02-10 20:21:06 +00003972 const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003973 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003974 unsigned long int Offset) {
3975
Fariborz Jahanianed157d32009-02-10 20:21:06 +00003976 assert(ID && "EmitIvarOffsetVar - null interface decl.");
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003977 std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.'
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003978 + Ivar->getNameAsString());
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003979 llvm::Constant *Init = llvm::ConstantInt::get(ObjCTypes.LongTy, Offset);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003980
3981 llvm::GlobalVariable *IvarOffsetGV =
3982 CGM.getModule().getGlobalVariable(ExternalName);
3983 if (IvarOffsetGV) {
3984 // ivar offset symbol already built due to user code referencing it.
Fariborz Jahanian09796d62009-01-31 02:43:27 +00003985 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003986 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003987 IvarOffsetGV->setInitializer(Init);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00003988 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00003989 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00003990 return IvarOffsetGV;
3991 }
3992
3993 IvarOffsetGV =
3994 new llvm::GlobalVariable(Init->getType(),
3995 false,
3996 llvm::GlobalValue::ExternalLinkage,
3997 Init,
3998 ExternalName,
3999 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004000 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004001 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004002 // @private and @package have hidden visibility.
4003 bool globalVisibility = (Ivar->getAccessControl() == ObjCIvarDecl::Public ||
4004 Ivar->getAccessControl() == ObjCIvarDecl::Protected);
4005 if (!globalVisibility)
4006 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004007 else
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004008 if (IsClassHidden(ID))
4009 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004010
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004011 IvarOffsetGV->setSection("__DATA, __objc_const");
4012 UsedGlobals.push_back(IvarOffsetGV);
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004013 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004014}
4015
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004016/// EmitIvarList - Emit the ivar list for the given
4017/// implementation. If ForClass is true the list of class ivars
4018/// (i.e. metaclass ivars) is emitted, otherwise the list of
4019/// interface ivars will be emitted. The return value has type
4020/// IvarListnfABIPtrTy.
4021/// struct _ivar_t {
4022/// unsigned long int *offset; // pointer to ivar offset location
4023/// char *name;
4024/// char *type;
4025/// uint32_t alignment;
4026/// uint32_t size;
4027/// }
4028/// struct _ivar_list_t {
4029/// uint32 entsize; // sizeof(struct _ivar_t)
4030/// uint32 count;
4031/// struct _iver_t list[count];
4032/// }
4033///
4034llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
4035 const ObjCImplementationDecl *ID) {
4036
4037 std::vector<llvm::Constant*> Ivars, Ivar(5);
4038
4039 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4040 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
4041
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004042 // FIXME. Consolidate this with similar code in GenerateClass.
4043 const llvm::Type *InterfaceTy =
4044 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(
4045 const_cast<ObjCInterfaceDecl*>(OID)));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004046 const llvm::StructLayout *Layout =
4047 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy));
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00004048
4049 RecordDecl::field_iterator i,p;
4050 const RecordDecl *RD = GetFirstIvarInRecord(OID, i,p);
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004051 ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin();
4052
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004053 for (RecordDecl::field_iterator e = RD->field_end(); i != e; ++i) {
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004054 FieldDecl *Field = *i;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004055 unsigned long offset = Layout->getElementOffset(CGM.getTypes().
4056 getLLVMFieldNo(Field));
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004057 const ObjCIvarDecl *ivarDecl = *I++;
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004058 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), ivarDecl, offset);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004059 if (Field->getIdentifier())
4060 Ivar[1] = GetMethodVarName(Field->getIdentifier());
4061 else
4062 Ivar[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4063 std::string TypeStr;
4064 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4065 Ivar[2] = GetMethodVarType(TypeStr);
4066 const llvm::Type *FieldTy =
4067 CGM.getTypes().ConvertTypeForMem(Field->getType());
4068 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy);
4069 unsigned Align = CGM.getContext().getPreferredTypeAlign(
4070 Field->getType().getTypePtr()) >> 3;
4071 Align = llvm::Log2_32(Align);
4072 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Fariborz Jahanian07236ba2009-01-27 22:27:56 +00004073 // NOTE. Size of a bitfield does not match gcc's, because of the way
4074 // bitfields are treated special in each. But I am told that 'size'
4075 // for bitfield ivars is ignored by the runtime so it does not matter.
4076 // (even if it matters, some day, there is enough info. to get the bitfield
4077 // right!
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004078 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4079 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
4080 }
4081 // Return null for empty list.
4082 if (Ivars.empty())
4083 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4084 std::vector<llvm::Constant*> Values(3);
4085 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy);
4086 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4087 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
4088 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
4089 Ivars.size());
4090 Values[2] = llvm::ConstantArray::get(AT, Ivars);
4091 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4092 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
4093 llvm::GlobalVariable *GV =
4094 new llvm::GlobalVariable(Init->getType(), false,
4095 llvm::GlobalValue::InternalLinkage,
4096 Init,
4097 Prefix + OID->getNameAsString(),
4098 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004099 GV->setAlignment(
4100 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004101 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004102
4103 UsedGlobals.push_back(GV);
4104 return llvm::ConstantExpr::getBitCast(GV,
4105 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004106}
4107
4108llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
4109 const ObjCProtocolDecl *PD) {
4110 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4111
4112 if (!Entry) {
4113 // We use the initializer as a marker of whether this is a forward
4114 // reference or not. At module finalization we add the empty
4115 // contents for protocols which were referenced but never defined.
4116 Entry =
4117 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
4118 llvm::GlobalValue::ExternalLinkage,
4119 0,
4120 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(),
4121 &CGM.getModule());
4122 Entry->setSection("__DATA,__datacoal_nt,coalesced");
4123 UsedGlobals.push_back(Entry);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004124 }
4125
4126 return Entry;
4127}
4128
4129/// GetOrEmitProtocol - Generate the protocol meta-data:
4130/// @code
4131/// struct _protocol_t {
4132/// id isa; // NULL
4133/// const char * const protocol_name;
4134/// const struct _protocol_list_t * protocol_list; // super protocols
4135/// const struct method_list_t * const instance_methods;
4136/// const struct method_list_t * const class_methods;
4137/// const struct method_list_t *optionalInstanceMethods;
4138/// const struct method_list_t *optionalClassMethods;
4139/// const struct _prop_list_t * properties;
4140/// const uint32_t size; // sizeof(struct _protocol_t)
4141/// const uint32_t flags; // = 0
4142/// }
4143/// @endcode
4144///
4145
4146llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
4147 const ObjCProtocolDecl *PD) {
4148 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4149
4150 // Early exit if a defining object has already been generated.
4151 if (Entry && Entry->hasInitializer())
4152 return Entry;
4153
4154 const char *ProtocolName = PD->getNameAsCString();
4155
4156 // Construct method lists.
4157 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
4158 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
4159 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(),
4160 e = PD->instmeth_end(); i != e; ++i) {
4161 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004162 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004163 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4164 OptInstanceMethods.push_back(C);
4165 } else {
4166 InstanceMethods.push_back(C);
4167 }
4168 }
4169
4170 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(),
4171 e = PD->classmeth_end(); i != e; ++i) {
4172 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004173 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004174 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4175 OptClassMethods.push_back(C);
4176 } else {
4177 ClassMethods.push_back(C);
4178 }
4179 }
4180
4181 std::vector<llvm::Constant*> Values(10);
4182 // isa is NULL
4183 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
4184 Values[1] = GetClassName(PD->getIdentifier());
4185 Values[2] = EmitProtocolList(
4186 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
4187 PD->protocol_begin(),
4188 PD->protocol_end());
4189
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004190 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004191 + PD->getNameAsString(),
4192 "__DATA, __objc_const",
4193 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004194 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004195 + PD->getNameAsString(),
4196 "__DATA, __objc_const",
4197 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004198 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004199 + PD->getNameAsString(),
4200 "__DATA, __objc_const",
4201 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004202 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004203 + PD->getNameAsString(),
4204 "__DATA, __objc_const",
4205 OptClassMethods);
4206 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
4207 0, PD, ObjCTypes);
4208 uint32_t Size =
4209 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy);
4210 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4211 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
4212 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
4213 Values);
4214
4215 if (Entry) {
4216 // Already created, fix the linkage and update the initializer.
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004217 Entry->setLinkage(llvm::GlobalValue::WeakLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004218 Entry->setInitializer(Init);
4219 } else {
4220 Entry =
4221 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004222 llvm::GlobalValue::WeakLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004223 Init,
4224 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName,
4225 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004226 Entry->setAlignment(
4227 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004228 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004229 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004230 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
4231
4232 // Use this protocol meta-data to build protocol list table in section
4233 // __DATA, __objc_protolist
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004234 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004235 ObjCTypes.ProtocolnfABIPtrTy, false,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004236 llvm::GlobalValue::WeakLinkage,
4237 Entry,
4238 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
4239 +ProtocolName,
4240 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004241 PTGV->setAlignment(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004242 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004243 PTGV->setSection("__DATA, __objc_protolist");
4244 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4245 UsedGlobals.push_back(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004246 return Entry;
4247}
4248
4249/// EmitProtocolList - Generate protocol list meta-data:
4250/// @code
4251/// struct _protocol_list_t {
4252/// long protocol_count; // Note, this is 32/64 bit
4253/// struct _protocol_t[protocol_count];
4254/// }
4255/// @endcode
4256///
4257llvm::Constant *
4258CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4259 ObjCProtocolDecl::protocol_iterator begin,
4260 ObjCProtocolDecl::protocol_iterator end) {
4261 std::vector<llvm::Constant*> ProtocolRefs;
4262
Fariborz Jahanianda320092009-01-29 19:24:30 +00004263 // Just return null for empty protocol lists
Daniel Dunbar948e2582009-02-15 07:36:20 +00004264 if (begin == end)
Fariborz Jahanianda320092009-01-29 19:24:30 +00004265 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4266
Daniel Dunbar948e2582009-02-15 07:36:20 +00004267 // FIXME: We shouldn't need to do this lookup here, should we?
Fariborz Jahanianda320092009-01-29 19:24:30 +00004268 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4269 if (GV)
Daniel Dunbar948e2582009-02-15 07:36:20 +00004270 return llvm::ConstantExpr::getBitCast(GV,
4271 ObjCTypes.ProtocolListnfABIPtrTy);
4272
4273 for (; begin != end; ++begin)
4274 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4275
Fariborz Jahanianda320092009-01-29 19:24:30 +00004276 // This list is null terminated.
4277 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004278 ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004279
4280 std::vector<llvm::Constant*> Values(2);
4281 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
4282 Values[1] =
Daniel Dunbar948e2582009-02-15 07:36:20 +00004283 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004284 ProtocolRefs.size()),
4285 ProtocolRefs);
4286
4287 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4288 GV = new llvm::GlobalVariable(Init->getType(), false,
4289 llvm::GlobalValue::InternalLinkage,
4290 Init,
4291 Name,
4292 &CGM.getModule());
4293 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004294 GV->setAlignment(
4295 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004296 UsedGlobals.push_back(GV);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004297 return llvm::ConstantExpr::getBitCast(GV,
4298 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004299}
4300
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004301/// GetMethodDescriptionConstant - This routine build following meta-data:
4302/// struct _objc_method {
4303/// SEL _cmd;
4304/// char *method_type;
4305/// char *_imp;
4306/// }
4307
4308llvm::Constant *
4309CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4310 std::vector<llvm::Constant*> Desc(3);
4311 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4312 ObjCTypes.SelectorPtrTy);
4313 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004314 // Protocol methods have no implementation. So, this entry is always NULL.
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004315 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4316 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
4317}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004318
4319/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
4320/// This code gen. amounts to generating code for:
4321/// @code
4322/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
4323/// @encode
4324///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004325LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004326 CodeGen::CodeGenFunction &CGF,
4327 QualType ObjectTy,
4328 llvm::Value *BaseValue,
4329 const ObjCIvarDecl *Ivar,
4330 const FieldDecl *Field,
4331 unsigned CVRQualifiers) {
4332 assert(ObjectTy->isObjCInterfaceType() &&
4333 "CGObjCNonFragileABIMac::EmitObjCValueForIvar");
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00004334 ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004335 std::string ExternalName;
4336 llvm::GlobalVariable *IvarOffsetGV =
4337 ObjCIvarOffsetVariable(ExternalName, ID, Ivar);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004338
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004339 // (char *) BaseValue
4340 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue,
4341 ObjCTypes.Int8PtrTy);
4342 llvm::Value *Offset = CGF.Builder.CreateLoad(IvarOffsetGV);
4343 // (char*)BaseValue + Offset_symbol
4344 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
4345 // (type *)((char*)BaseValue + Offset_symbol)
4346 const llvm::Type *IvarTy =
4347 CGM.getTypes().ConvertType(Ivar->getType());
4348 llvm::Type *ptrIvarTy = llvm::PointerType::getUnqual(IvarTy);
4349 V = CGF.Builder.CreateBitCast(V, ptrIvarTy);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004350
4351 if (Ivar->isBitField())
4352 return CGF.EmitLValueForBitfield(V, const_cast<FieldDecl *>(Field),
4353 CVRQualifiers);
4354
4355 LValue LV = LValue::MakeAddr(V,
4356 Ivar->getType().getCVRQualifiers()|CVRQualifiers);
4357 LValue::SetObjCIvar(LV, true);
4358 return LV;
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004359}
4360
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004361llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
4362 CodeGen::CodeGenFunction &CGF,
4363 ObjCInterfaceDecl *Interface,
4364 const ObjCIvarDecl *Ivar) {
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004365 std::string ExternalName;
4366 llvm::GlobalVariable *IvarOffsetGV =
4367 ObjCIvarOffsetVariable(ExternalName, Interface, Ivar);
4368
4369 return CGF.Builder.CreateLoad(IvarOffsetGV, false, "ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004370}
4371
Fariborz Jahanian46551122009-02-04 00:22:57 +00004372CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
4373 CodeGen::CodeGenFunction &CGF,
4374 QualType ResultType,
4375 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004376 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00004377 QualType Arg0Ty,
4378 bool IsSuper,
4379 const CallArgList &CallArgs) {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004380 // FIXME. Even though IsSuper is passes. This function doese not
4381 // handle calls to 'super' receivers.
4382 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004383 llvm::Value *Arg0 = Receiver;
4384 if (!IsSuper)
4385 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004386
4387 // Find the message function name.
Fariborz Jahanianef163782009-02-05 01:13:09 +00004388 // FIXME. This is too much work to get the ABI-specific result type
4389 // needed to find the message name.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004390 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType,
4391 llvm::SmallVector<QualType, 16>());
4392 llvm::Constant *Fn;
4393 std::string Name("\01l_");
4394 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004395#if 0
4396 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004397 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
4398 Fn = ObjCTypes.MessageSendIdStretFixupFn;
4399 // FIXME. Is there a better way of getting these names.
4400 // They are available in RuntimeFunctions vector pair.
4401 Name += "objc_msgSendId_stret_fixup";
4402 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004403 else
4404#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004405 if (IsSuper) {
4406 Fn = ObjCTypes.MessageSendSuper2StretFixupFn;
4407 Name += "objc_msgSendSuper2_stret_fixup";
4408 }
4409 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004410 {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004411 Fn = ObjCTypes.MessageSendStretFixupFn;
4412 Name += "objc_msgSend_stret_fixup";
4413 }
4414 }
Fariborz Jahanian1a6b3682009-02-05 19:35:43 +00004415 else if (ResultType->isFloatingType() &&
4416 // Selection of frret API only happens in 32bit nonfragile ABI.
4417 CGM.getTargetData().getTypePaddedSize(ObjCTypes.LongTy) == 4) {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004418 Fn = ObjCTypes.MessageSendFpretFixupFn;
4419 Name += "objc_msgSend_fpret_fixup";
4420 }
4421 else {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004422#if 0
4423// unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004424 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
4425 Fn = ObjCTypes.MessageSendIdFixupFn;
4426 Name += "objc_msgSendId_fixup";
4427 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004428 else
4429#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004430 if (IsSuper) {
4431 Fn = ObjCTypes.MessageSendSuper2FixupFn;
4432 Name += "objc_msgSendSuper2_fixup";
4433 }
4434 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004435 {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004436 Fn = ObjCTypes.MessageSendFixupFn;
4437 Name += "objc_msgSend_fixup";
4438 }
4439 }
4440 Name += '_';
4441 std::string SelName(Sel.getAsString());
4442 // Replace all ':' in selector name with '_' ouch!
4443 for(unsigned i = 0; i < SelName.size(); i++)
4444 if (SelName[i] == ':')
4445 SelName[i] = '_';
4446 Name += SelName;
4447 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
4448 if (!GV) {
4449 // Build messafe ref table entry.
4450 std::vector<llvm::Constant*> Values(2);
4451 Values[0] = Fn;
4452 Values[1] = GetMethodVarName(Sel);
4453 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4454 GV = new llvm::GlobalVariable(Init->getType(), false,
4455 llvm::GlobalValue::WeakLinkage,
4456 Init,
4457 Name,
4458 &CGM.getModule());
4459 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4460 GV->setAlignment(
4461 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.MessageRefTy));
4462 GV->setSection("__DATA, __objc_msgrefs, coalesced");
4463 UsedGlobals.push_back(GV);
4464 }
4465 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00004466
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004467 CallArgList ActualArgs;
4468 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
4469 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
4470 ObjCTypes.MessageRefCPtrTy));
4471 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahanianef163782009-02-05 01:13:09 +00004472 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs);
4473 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
4474 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00004475 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00004476 Callee = CGF.Builder.CreateBitCast(Callee,
4477 llvm::PointerType::getUnqual(FTy));
4478 return CGF.EmitCall(FnInfo1, Callee, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00004479}
4480
4481/// Generate code for a message send expression in the nonfragile abi.
4482CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend(
4483 CodeGen::CodeGenFunction &CGF,
4484 QualType ResultType,
4485 Selector Sel,
4486 llvm::Value *Receiver,
4487 bool IsClassMessage,
4488 const CallArgList &CallArgs) {
Fariborz Jahanian46551122009-02-04 00:22:57 +00004489 return EmitMessageSend(CGF, ResultType, Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004490 Receiver, CGF.getContext().getObjCIdType(),
Fariborz Jahanian46551122009-02-04 00:22:57 +00004491 false, CallArgs);
4492}
4493
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004494llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004495 const ObjCInterfaceDecl *ID,
4496 bool IsSuper) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004497
4498 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
4499
4500 if (!Entry) {
4501 std::string ClassName("\01_OBJC_CLASS_$_" + ID->getNameAsString());
4502 llvm::GlobalVariable *ClassGV =
4503 CGM.getModule().getGlobalVariable(ClassName);
4504 if (!ClassGV) {
4505 ClassGV =
4506 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
4507 llvm::GlobalValue::ExternalLinkage,
4508 0,
4509 ClassName,
4510 &CGM.getModule());
4511 UsedGlobals.push_back(ClassGV);
4512 }
4513 Entry =
4514 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
4515 llvm::GlobalValue::InternalLinkage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004516 ClassGV,
4517 IsSuper ? "\01L_OBJC_CLASSLIST_SUP_REFS_$_"
4518 : "\01L_OBJC_CLASSLIST_REFERENCES_$_",
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004519 &CGM.getModule());
4520 Entry->setAlignment(
4521 CGM.getTargetData().getPrefTypeAlignment(
4522 ObjCTypes.ClassnfABIPtrTy));
4523
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004524 if (IsSuper)
4525 Entry->setSection("__OBJC,__objc_superrefs,regular,no_dead_strip");
4526 else
4527 Entry->setSection("__OBJC,__objc_classrefs,regular,no_dead_strip");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004528 UsedGlobals.push_back(Entry);
4529 }
4530
4531 return Builder.CreateLoad(Entry, false, "tmp");
4532}
4533
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004534/// EmitMetaClassRef - Return a Value * of the address of _class_t
4535/// meta-data
4536///
4537llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
4538 const ObjCInterfaceDecl *ID) {
4539 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
4540 if (Entry)
4541 return Builder.CreateLoad(Entry, false, "tmp");
4542
4543 std::string MetaClassName("\01_OBJC_METACLASS_$_" + ID->getNameAsString());
4544 llvm::GlobalVariable *MetaClassGV =
4545 CGM.getModule().getGlobalVariable(MetaClassName);
4546 if (!MetaClassGV) {
4547 MetaClassGV =
4548 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
4549 llvm::GlobalValue::ExternalLinkage,
4550 0,
4551 MetaClassName,
4552 &CGM.getModule());
4553 UsedGlobals.push_back(MetaClassGV);
4554 }
4555
4556 Entry =
4557 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
4558 llvm::GlobalValue::InternalLinkage,
4559 MetaClassGV,
4560 "\01L_OBJC_CLASSLIST_SUP_REFS_$_",
4561 &CGM.getModule());
4562 Entry->setAlignment(
4563 CGM.getTargetData().getPrefTypeAlignment(
4564 ObjCTypes.ClassnfABIPtrTy));
4565
4566 Entry->setSection("__OBJC,__objc_superrefs,regular,no_dead_strip");
4567 UsedGlobals.push_back(Entry);
4568
4569 return Builder.CreateLoad(Entry, false, "tmp");
4570}
4571
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00004572/// GetClass - Return a reference to the class for the given interface
4573/// decl.
4574llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
4575 const ObjCInterfaceDecl *ID) {
4576 return EmitClassRef(Builder, ID);
4577}
4578
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004579/// Generates a message send where the super is the receiver. This is
4580/// a message send to self with special delivery semantics indicating
4581/// which class's method should be called.
4582CodeGen::RValue
4583CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
4584 QualType ResultType,
4585 Selector Sel,
4586 const ObjCInterfaceDecl *Class,
4587 llvm::Value *Receiver,
4588 bool IsClassMessage,
4589 const CodeGen::CallArgList &CallArgs) {
4590 // ...
4591 // Create and init a super structure; this is a (receiver, class)
4592 // pair we will pass to objc_msgSendSuper.
4593 llvm::Value *ObjCSuper =
4594 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
4595
4596 llvm::Value *ReceiverAsObject =
4597 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
4598 CGF.Builder.CreateStore(ReceiverAsObject,
4599 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
4600
4601 // If this is a class message the metaclass is passed as the target.
4602 llvm::Value *Target =
4603 IsClassMessage ? EmitMetaClassRef(CGF.Builder, Class)
4604 : EmitClassRef(CGF.Builder, Class, true);
4605
4606 // FIXME: We shouldn't need to do this cast, rectify the ASTContext
4607 // and ObjCTypes types.
4608 const llvm::Type *ClassTy =
4609 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
4610 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
4611 CGF.Builder.CreateStore(Target,
4612 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
4613
4614 return EmitMessageSend(CGF, ResultType, Sel,
4615 ObjCSuper, ObjCTypes.SuperPtrCTy,
4616 true, CallArgs);
4617}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00004618
4619llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
4620 Selector Sel) {
4621 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
4622
4623 if (!Entry) {
4624 llvm::Constant *Casted =
4625 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
4626 ObjCTypes.SelectorPtrTy);
4627 Entry =
4628 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
4629 llvm::GlobalValue::InternalLinkage,
4630 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
4631 &CGM.getModule());
4632 Entry->setSection("__DATA,__objc_selrefs,literal_pointers,no_dead_strip");
4633 UsedGlobals.push_back(Entry);
4634 }
4635
4636 return Builder.CreateLoad(Entry, false, "tmp");
4637}
4638
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004639/* *** */
4640
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004641CodeGen::CGObjCRuntime *
4642CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00004643 return new CGObjCMac(CGM);
4644}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004645
4646CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004647CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004648 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004649}